Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
4.3 KiB

  1. <?php
  2. namespace Friendica\Core\Console;
  3. use Asika\SimpleConsole\Console;
  4. use dba;
  5. use Friendica\App;
  6. use Friendica\Core\Install;
  7. use Friendica\Core\Theme;
  8. require_once 'mod/install.php';
  9. require_once 'include/dba.php';
  10. class AutomaticInstallation extends Console
  11. {
  12. protected function getHelp()
  13. {
  14. return <<<HELP
  15. Installation - Install Friendica automatically
  16. Synopsis
  17. bin/console autoinstall [-h|--help|-?] [-v] [-a]
  18. Description
  19. Installs Friendica with data based on the htconfig.php file
  20. Notes:
  21. Not checking .htaccess/URL-Rewrite during CLI installation.
  22. Options
  23. -h|--help|-? Show help information
  24. -v Show more debug information.
  25. -a All setup checks are required (except .htaccess)
  26. -f prepared config file (e.g. ".htconfig.php" itself)
  27. HELP;
  28. }
  29. protected function doExecute()
  30. {
  31. // Initialise the app
  32. $this->out("Initializing setup...\n");
  33. $a = get_app();
  34. $db_host = '';
  35. $db_user = '';
  36. $db_pass = '';
  37. $db_data = '';
  38. $config_file = $this->getOption('f', 'htconfig.php');
  39. $this->out("Using config $config_file...\n");
  40. require_once $config_file;
  41. Install::setInstallMode();
  42. $this->out(" Complete!\n\n");
  43. // Check basic setup
  44. $this->out("Checking basic setup...\n");
  45. $checkResults = [];
  46. $checkResults['basic'] = $this->runBasicChecks($a);
  47. $errorMessage = $this->extractErrors($checkResults['basic']);
  48. if ($errorMessage !== '') {
  49. throw new \RuntimeException($errorMessage);
  50. }
  51. $this->out(" Complete!\n\n");
  52. // Check database connection
  53. $this->out("Checking database...\n");
  54. $checkResults['db'] = array();
  55. $checkResults['db'][] = $this->runDatabaseCheck($db_host, $db_user, $db_pass, $db_data);
  56. $errorMessage = $this->extractErrors($checkResults['db']);
  57. if ($errorMessage !== '') {
  58. throw new \RuntimeException($errorMessage);
  59. }
  60. $this->out(" Complete!\n\n");
  61. // Install database
  62. $this->out("Inserting data into database...\n");
  63. $checkResults['data'] = Install::installDatabaseStructure();
  64. if ($checkResults['data'] !== '') {
  65. throw new \RuntimeException("ERROR: DB Database creation error. Is the DB empty?\n");
  66. }
  67. $this->out(" Complete!\n\n");
  68. // Install theme
  69. $this->out("Installing theme\n");
  70. if (!empty($a->config['system']['theme'])) {
  71. Theme::install($a->config['system']['theme']);
  72. $this->out(" Complete\n\n");
  73. } else {
  74. $this->out(" Theme setting is empty. Please check the file htconfig.php\n\n");
  75. }
  76. // Copy config file
  77. $this->out("Saving config file...\n");
  78. if ($config_file != '.htconfig.php' && !copy($config_file, '.htconfig.php')) {
  79. throw new \RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '.htconfig.php' manually.\n");
  80. }
  81. $this->out(" Complete!\n\n");
  82. $this->out("\nInstallation is finished\n");
  83. return 0;
  84. }
  85. /**
  86. * @param App $app
  87. * @return array
  88. */
  89. private function runBasicChecks($app)
  90. {
  91. $checks = [];
  92. Install::checkFunctions($checks);
  93. Install::checkImagick($checks);
  94. Install::checkLocalIni($checks);
  95. Install::checkSmarty3($checks);
  96. Install::checkKeys($checks);
  97. if (!empty($app->config['php_path'])) {
  98. Install::checkPHP($app->config['php_path'], $checks);
  99. } else {
  100. throw new \RuntimeException(" ERROR: The php_path is not set in the config. Please check the file .htconfig.php.\n");
  101. }
  102. $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
  103. return $checks;
  104. }
  105. /**
  106. * @param $db_host
  107. * @param $db_user
  108. * @param $db_pass
  109. * @param $db_data
  110. * @return array
  111. */
  112. private function runDatabaseCheck($db_host, $db_user, $db_pass, $db_data)
  113. {
  114. $result = array(
  115. 'title' => 'MySQL Connection',
  116. 'required' => true,
  117. 'status' => true,
  118. 'help' => '',
  119. );
  120. if (!dba::connect($db_host, $db_user, $db_pass, $db_data)) {
  121. $result['status'] = false;
  122. $result['help'] = 'Failed, please check your MySQL settings and credentials.';
  123. }
  124. return $result;
  125. }
  126. /**
  127. * @param array $results
  128. * @return string
  129. */
  130. private function extractErrors($results)
  131. {
  132. $errorMessage = '';
  133. $allChecksRequired = $this->getOption('a') !== null;
  134. foreach ($results as $result) {
  135. if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
  136. $errorMessage .= "--------\n";
  137. $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
  138. }
  139. }
  140. return $errorMessage;
  141. }
  142. }