From 12b6a9b0c28c55f3dc440a0cecf9e62452023ae2 Mon Sep 17 00:00:00 2001 From: mrflos Date: Tue, 16 Aug 2022 09:46:38 +0300 Subject: [PATCH] feat(yeswiki-installer): first cli script and utils WIP --- composer.json | 5 + composer.lock | 189 ++++++++++++++++++++++++++++++++ yeswiki-installer.php | 44 ++++++++ yeswiki-installer.utils.inc.php | 134 ++++++++++++++++++++++ 4 files changed, 372 insertions(+) create mode 100644 composer.json create mode 100644 composer.lock create mode 100755 yeswiki-installer.php create mode 100644 yeswiki-installer.utils.inc.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..46fac1f --- /dev/null +++ b/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "league/climate": "^3.8" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..78c264a --- /dev/null +++ b/composer.lock @@ -0,0 +1,189 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "7804f603c1e45b579e09b65c72451f75", + "packages": [ + { + "name": "league/climate", + "version": "3.8.2", + "source": { + "type": "git", + "url": "https://github.com/thephpleague/climate.git", + "reference": "a785a3ac8f584eed4abd45e4e16fe64c46659a28" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/thephpleague/climate/zipball/a785a3ac8f584eed4abd45e4e16fe64c46659a28", + "reference": "a785a3ac8f584eed4abd45e4e16fe64c46659a28", + "shasum": "" + }, + "require": { + "php": "^7.3 || ^8.0", + "psr/log": "^1.0 || ^2.0 || ^3.0", + "seld/cli-prompt": "^1.0" + }, + "require-dev": { + "mikey179/vfsstream": "^1.6.10", + "mockery/mockery": "^1.4.2", + "phpunit/phpunit": "^9.5.10" + }, + "suggest": { + "ext-mbstring": "If ext-mbstring is not available you MUST install symfony/polyfill-mbstring" + }, + "type": "library", + "autoload": { + "psr-4": { + "League\\CLImate\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Joe Tannenbaum", + "email": "hey@joe.codes", + "homepage": "http://joe.codes/", + "role": "Developer" + }, + { + "name": "Craig Duncan", + "email": "git@duncanc.co.uk", + "homepage": "https://github.com/duncan3dc", + "role": "Developer" + } + ], + "description": "PHP's best friend for the terminal. CLImate allows you to easily output colored text, special formats, and more.", + "keywords": [ + "cli", + "colors", + "command", + "php", + "terminal" + ], + "support": { + "issues": "https://github.com/thephpleague/climate/issues", + "source": "https://github.com/thephpleague/climate/tree/3.8.2" + }, + "time": "2022-06-18T14:42:08+00:00" + }, + { + "name": "psr/log", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/fe5ea303b0887d5caefd3d431c3e61ad47037001", + "reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001", + "shasum": "" + }, + "require": { + "php": ">=8.0.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Log\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "homepage": "https://github.com/php-fig/log", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "support": { + "source": "https://github.com/php-fig/log/tree/3.0.0" + }, + "time": "2021-07-14T16:46:02+00:00" + }, + { + "name": "seld/cli-prompt", + "version": "1.0.4", + "source": { + "type": "git", + "url": "https://github.com/Seldaek/cli-prompt.git", + "reference": "b8dfcf02094b8c03b40322c229493bb2884423c5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Seldaek/cli-prompt/zipball/b8dfcf02094b8c03b40322c229493bb2884423c5", + "reference": "b8dfcf02094b8c03b40322c229493bb2884423c5", + "shasum": "" + }, + "require": { + "php": ">=5.3" + }, + "require-dev": { + "phpstan/phpstan": "^0.12.63" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Seld\\CliPrompt\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be" + } + ], + "description": "Allows you to prompt for user input on the command line, and optionally hide the characters they type", + "keywords": [ + "cli", + "console", + "hidden", + "input", + "prompt" + ], + "support": { + "issues": "https://github.com/Seldaek/cli-prompt/issues", + "source": "https://github.com/Seldaek/cli-prompt/tree/1.0.4" + }, + "time": "2020-12-15T21:32:01+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.3.0" +} diff --git a/yeswiki-installer.php b/yeswiki-installer.php new file mode 100755 index 0000000..e0f1dd2 --- /dev/null +++ b/yeswiki-installer.php @@ -0,0 +1,44 @@ +#!/usr/bin/php +description('yeswiki-installer, install YesWiki like a professionnal 🌈🦄'); +$climate->arguments->add([ + 'domain' => [ + 'prefix' => 'd', + 'longPrefix' => 'domain', + 'description' => 'Domain name used for installation', + 'required' => true, + 'defaultValue' => 'example.com' + ], + 'type' => [ + 'prefix' => 't', + 'longPrefix' => 'type', + 'description' => 'Type of installation, can be "solo" or "ferme"', + 'required' => true, + 'defaultValue' => 'solo' + ], + 'quota' => [ + 'prefix' => 'q', + 'longPrefix' => 'quota', + 'description' => 'User quota for hard drive space, in bytes', + 'required' => true, + 'defaultValue' => 2000000, + 'castTo' => 'int' + ], +]); +$climate->arguments->parse(); +$domain = $climate->arguments->get('domain'); +if (!empty($domain) && $domain !== 'example.com') { + $climate->bold()->underline()->out('Installation of YesWiki on '.$domain); + $res = installYesWiki($domain, $climate->arguments->get('quota'), $climate->arguments->get('type')); + if (!empty($res['error'])) { + $climate->error('ERROR : '.$res['error']); + } else { + $climate->dump($res); + } +} else { + $climate->usage(); +} diff --git a/yeswiki-installer.utils.inc.php b/yeswiki-installer.utils.inc.php new file mode 100644 index 0000000..a2ad763 --- /dev/null +++ b/yeswiki-installer.utils.inc.php @@ -0,0 +1,134 @@ + 'Not valid domain.']; + } + $currentip = dns_get_record($domain, DNS_A + DNS_AAAA); + if ($currentip[0]['ip'] !== $ip4) { + return ['error' => 'The current ip v4 address of '.$domain.' is '.$currentip[0]['ip'].'. it should be '.$ip4]; + } + if ($currentip[1]['ipv6'] !== $ip6) { + return ['error' => 'The current ip v6 address of '.$domain.' is '.$currentip[1]['ipv6'].'. it should be '.$ip6]; + } + return true; +} + +function checkIfInstalled($domain) { + return true; +} + +function checkIfUserExist($user) { + return true; +} + +function generateUserFromDomain($domain, $recursive = null) { + $user = 'toto'; + return $user; +} + +function generatePassword($length = 32, $add_dashes = false, $available_sets = 'luds') { + $sets = array(); + if(strpos($available_sets, 'l') !== false) + $sets[] = 'abcdefghjkmnpqrstuvwxyz'; + if(strpos($available_sets, 'u') !== false) + $sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ'; + if(strpos($available_sets, 'd') !== false) + $sets[] = '23456789'; + if(strpos($available_sets, 's') !== false) + $sets[] = '!@#$%&*?'; + + $all = ''; + $password = ''; + foreach($sets as $set) + { + $password .= $set[array_rand(str_split($set))]; + $all .= $set; + } + + $all = str_split($all); + for($i = 0; $i < $length - count($sets); $i++) + $password .= $all[array_rand($all)]; + + $password = str_shuffle($password); + + if(!$add_dashes) + return $password; + + $dash_len = floor(sqrt($length)); + $dash_str = ''; + while(strlen($password) > $dash_len) + { + $dash_str .= substr($password, 0, $dash_len) . '-'; + $password = substr($password, $dash_len); + } + $dash_str .= $password; + return $dash_str; +} + +function createSQLUserAndDatabase($user) { + $pass = generatePassword(); + return ['database' => 'dbtata', 'user' => 'dbtata', 'password' => $pass]; +} + +function createUnixUserWithQuota($user, $quota) { + $pass = generatePassword(); + return ['user' => 'toto', 'password' => $pass, 'quota' => $quota]; +} + +function createNginxConfig($domain, $user) { + +} + +function createPhpFpmConfig($user) { + +} + +function copyYesWikiFiles($domain, $user) { + +} + +function copyYesWikiDatabase($databaseModel) { + +} + +function addHerse($id, $pass) { + return ['user' => $id, 'password' => $pass]; +} + +function installYesWiki($domain, $quota, $type) { + $res = checkDNS($domain); + if (!empty($res['error'])) { + return $res; + } + $res = checkIfInstalled($domain); + if (!empty($res['error'])) { + return $res; + } + $user = generateUserFromDomain($domain); + $unixUser = createUnixUserWithQuota($user, $quota); + $dbUser = createSQLUserAndDatabase($user); + createNginxConfig($domain, $user); + createPhpFpmConfig($user); + copyYesWikiFiles($domain, $user); + $databaseModel = ($type === 'solo') ? 'modelesolo' : 'modeleferme' ; + copyYesWikiDatabase($databaseModel); + + $herseUser = []; + if (!empty($herseId) && !empty($hersePass)) { + $herseUser = addHerse($herseId, $hersePass); + } + return [ + 'domain'=> $domain, + 'type' => $type, + 'user' => $unixUser, + 'db' => $dbUser, + 'herse' => $herseUser + ]; +} + +function removeYesWiki($domain, $user) { + // enlever la db et le user sql + // enlever la config nginx et la conf php-fpm + // enlever le user unix et son home +}