diff --git a/src/Core/Console.php b/src/Core/Console.php index a1143ae1d..82c485179 100644 --- a/src/Core/Console.php +++ b/src/Core/Console.php @@ -21,6 +21,7 @@ class Console extends \Asika\SimpleConsole\Console 'extract' => __NAMESPACE__ . '\Console\Extract', 'globalcommunityblock' => __NAMESPACE__ . '\Console\GlobalCommunityBlock', 'globalcommunitysilence' => __NAMESPACE__ . '\Console\GlobalCommunitySilence', + 'autoinstall' => __NAMESPACE__ . '\Console\AutomaticInstallation', 'maintenance' => __NAMESPACE__ . '\Console\Maintenance', 'newpassword' => __NAMESPACE__ . '\Console\NewPassword', 'php2po' => __NAMESPACE__ . '\Console\PhpToPo', @@ -42,6 +43,7 @@ Commands: globalcommunityblock Block remote profile from interacting with this node globalcommunitysilence Silence remote profile from global community page help Show help about a command, e.g (bin/console help config) + autoinstall Starts automatic installation of friendica based on values from htconfig.php maintenance Set maintenance mode for this node newpassword Set a new password for a given user php2po Generate a messages.po file from a strings.php file diff --git a/src/Core/Console/AutomaticInstallation.php b/src/Core/Console/AutomaticInstallation.php new file mode 100644 index 000000000..6318b19a8 --- /dev/null +++ b/src/Core/Console/AutomaticInstallation.php @@ -0,0 +1,164 @@ +out("Initializing setup...\n"); + + $a = get_app(); + $db_host = ''; + $db_user = ''; + $db_pass = ''; + $db_data = ''; + require_once 'htconfig.php'; + + $this->out(" Complete!\n\n"); + + // Check basic setup + $this->out("Checking basic setup...\n"); + + $checkResults = []; + $checkResults['basic'] = $this->runBasicChecks($a); + $errorMessage = $this->extractErrors($checkResults['basic']); + + if ($errorMessage !== '') { + throw new \RuntimeException($errorMessage); + } + + $this->out(" Complete!\n\n"); + + // Check database connection + $this->out("Checking database...\n"); + + $checkResults['db'] = array(); + $checkResults['db'][] = $this->runDatabaseCheck($db_host, $db_user, $db_pass, $db_data); + $errorMessage = $this->extractErrors($checkResults['db']); + + if ($errorMessage !== '') { + throw new \RuntimeException($errorMessage); + } + + $this->out(" Complete!\n\n"); + + // Install database + $this->out("Inserting data into database...\n"); + + $checkResults['data'] = load_database(); + + if ($checkResults['data'] !== '') { + throw new \RuntimeException("ERROR: DB Database creation error. Is the DB empty?\n"); + } + + $this->out(" Complete!\n\n"); + + // Copy config file + $this->out("Saving config file...\n"); + if (!copy('htconfig.php', '.htconfig.php')) { + throw new \RuntimeException("ERROR: Saving config file failed. Please copy .htautoinstall.php to .htconfig.php manually.\n"); + } + $this->out(" Complete!\n\n"); + $this->out("\nInstallation is finished\n"); + + return 0; + } + + /** + * @param App $app + * @return array + */ + private function runBasicChecks($app) + { + $checks = []; + + check_funcs($checks); + check_imagik($checks); + check_htconfig($checks); + check_smarty3($checks); + check_keys($checks); + + if (!empty($app->config['php_path'])) { + check_php($app->config['php_path'], $checks); + } else { + throw new \RuntimeException(" ERROR: The php_path is not set in the config. Please check the file .htconfig.php.\n"); + } + + $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n"); + + return $checks; + } + + /** + * @param $db_host + * @param $db_user + * @param $db_pass + * @param $db_data + * @return array + */ + private function runDatabaseCheck($db_host, $db_user, $db_pass, $db_data) + { + $result = array( + 'title' => 'MySQL Connection', + 'required' => true, + 'status' => true, + 'help' => '', + ); + + + if (!dba::connect($db_host, $db_user, $db_pass, $db_data, true)) { + $result['status'] = false; + $result['help'] = 'Failed, please check your MySQL settings and credentials.'; + } + + return $result; + } + + /** + * @param array $results + * @return string + */ + private function extractErrors($results) + { + $errorMessage = ''; + $allChecksRequired = $this->getOption('a') !== null; + + foreach ($results as $result) { + if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) { + $errorMessage .= "--------\n"; + $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n"; + } + } + + return $errorMessage; + } +}