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.

243 lines
8.3KB

  1. <?php
  2. namespace Friendica\Core\Console;
  3. use Asika\SimpleConsole\Console;
  4. use Friendica\BaseObject;
  5. use Friendica\Core\Config;
  6. use Friendica\Core\Installer;
  7. use Friendica\Core\Theme;
  8. use Friendica\Database\DBA;
  9. use Friendica\Database\DBStructure;
  10. use RuntimeException;
  11. class AutomaticInstallation extends Console
  12. {
  13. protected function getHelp()
  14. {
  15. return <<<HELP
  16. Installation - Install Friendica automatically
  17. Synopsis
  18. bin/console autoinstall [-h|--help|-?] [-v] [-a] [-f]
  19. Description
  20. Installs Friendica with data based on the local.config.php file or environment variables
  21. Notes
  22. Not checking .htaccess/URL-Rewrite during CLI installation.
  23. Options
  24. -h|--help|-? Show help information
  25. -v Show more debug information.
  26. -a All setup checks are required (except .htaccess)
  27. -f|--file <config> prepared config file (e.g. "config/local.config.php" itself) which will override every other config option - except the environment variables)
  28. -s|--savedb Save the DB credentials to the file (if environment variables is used)
  29. -H|--dbhost <host> The host of the mysql/mariadb database (env MYSQL_HOST)
  30. -p|--dbport <port> The port of the mysql/mariadb database (env MYSQL_PORT)
  31. -d|--dbdata <database> The name of the mysql/mariadb database (env MYSQL_DATABASE)
  32. -U|--dbuser <username> The username of the mysql/mariadb database login (env MYSQL_USER or MYSQL_USERNAME)
  33. -P|--dbpass <password> The password of the mysql/mariadb database login (env MYSQL_PASSWORD)
  34. -u|--urlpath <url_path> The URL path of Friendica - f.e. '/friendica' (env FRIENDICA_URL_PATH)
  35. -b|--phppath <php_path> The path of the PHP binary (env FRIENDICA_PHP_PATH)
  36. -A|--admin <mail> The admin email address of Friendica (env FRIENDICA_ADMIN_MAIL)
  37. -T|--tz <timezone> The timezone of Friendica (env FRIENDICA_TZ)
  38. -L|--lang <language> The language of Friendica (env FRIENDICA_LANG)
  39. Environment variables
  40. MYSQL_HOST The host of the mysql/mariadb database (mandatory if mysql and environment is used)
  41. MYSQL_PORT The port of the mysql/mariadb database
  42. MYSQL_USERNAME|MYSQL_USER The username of the mysql/mariadb database login (MYSQL_USERNAME is for mysql, MYSQL_USER for mariadb)
  43. MYSQL_PASSWORD The password of the mysql/mariadb database login
  44. MYSQL_DATABASE The name of the mysql/mariadb database
  45. FRIENDICA_URL_PATH The URL path of Friendica (f.e. '/friendica')
  46. FRIENDICA_PHP_PATH The path of the PHP binary
  47. FRIENDICA_ADMIN_MAIL The admin email address of Friendica (this email will be used for admin access)
  48. FRIENDICA_TZ The timezone of Friendica
  49. FRIENDICA_LANG The langauge of Friendica
  50. Examples
  51. bin/console autoinstall -f 'input.config.php
  52. Installs Friendica with the prepared 'input.config.php' file
  53. bin/console autoinstall --savedb
  54. Installs Friendica with environment variables and saves them to the 'config/local.config.php' file
  55. bin/console autoinstall -h localhost -p 3365 -U user -P passwort1234 -d friendica
  56. Installs Friendica with a local mysql database with credentials
  57. HELP;
  58. }
  59. protected function doExecute()
  60. {
  61. // Initialise the app
  62. $this->out("Initializing setup...\n");
  63. $a = BaseObject::getApp();
  64. $installer = new Installer();
  65. $this->out(" Complete!\n\n");
  66. // Check Environment
  67. $this->out("Checking environment...\n");
  68. $installer->resetChecks();
  69. if (!$this->runBasicChecks($installer)) {
  70. $errorMessage = $this->extractErrors($installer->getChecks());
  71. throw new RuntimeException($errorMessage);
  72. }
  73. $this->out(" Complete!\n\n");
  74. // if a config file is set,
  75. $config_file = $this->getOption(['f', 'file']);
  76. if (!empty($config_file)) {
  77. if ($config_file != 'config' . DIRECTORY_SEPARATOR . 'local.config.php') {
  78. // Copy config file
  79. $this->out("Copying config file...\n");
  80. if (!copy($a->getBasePath() . DIRECTORY_SEPARATOR . $config_file, $a->getBasePath() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.config.php')) {
  81. throw new RuntimeException("ERROR: Saving config file failed. Please copy '$config_file' to '" . $a->getBasePath() . "'" . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "local.config.php' manually.\n");
  82. }
  83. }
  84. $db_host = $a->getConfigValue('database', 'hostname');
  85. $db_user = $a->getConfigValue('database', 'username');
  86. $db_pass = $a->getConfigValue('database', 'password');
  87. $db_data = $a->getConfigValue('database', 'database');
  88. } else {
  89. // Creating config file
  90. $this->out("Creating config file...\n");
  91. $save_db = $this->getOption(['s', 'savedb'], false);
  92. $db_host = $this->getOption(['H', 'dbhost'], ($save_db) ? getenv('MYSQL_HOST') : '');
  93. $db_port = $this->getOption(['p', 'dbport'], ($save_db) ? getenv('MYSQL_PORT') : null);
  94. $db_data = $this->getOption(['d', 'dbdata'], ($save_db) ? getenv('MYSQL_DATABASE') : '');
  95. $db_user = $this->getOption(['U', 'dbuser'], ($save_db) ? getenv('MYSQL_USER') . getenv('MYSQL_USERNAME') : '');
  96. $db_pass = $this->getOption(['P', 'dbpass'], ($save_db) ? getenv('MYSQL_PASSWORD') : '');
  97. $url_path = $this->getOption(['u', 'urlpath'], !empty('FRIENDICA_URL_PATH') ? getenv('FRIENDICA_URL_PATH') : null);
  98. $php_path = $this->getOption(['b', 'phppath'], !empty('FRIENDICA_PHP_PATH') ? getenv('FRIENDICA_PHP_PATH') : null);
  99. $admin_mail = $this->getOption(['A', 'admin'], !empty('FRIENDICA_ADMIN_MAIL') ? getenv('FRIENDICA_ADMIN_MAIL') : '');
  100. $tz = $this->getOption(['T', 'tz'], !empty('FRIENDICA_TZ') ? getenv('FRIENDICA_TZ') : '');
  101. $lang = $this->getOption(['L', 'lang'], !empty('FRIENDICA_LANG') ? getenv('FRIENDICA_LANG') : '');
  102. if (empty($php_path)) {
  103. $php_path = $installer->getPHPPath();
  104. }
  105. $installer->createConfig(
  106. $php_path,
  107. $url_path,
  108. (!empty($db_port) ? $db_host . ':' . $db_port : $db_host),
  109. $db_user,
  110. $db_pass,
  111. $db_data,
  112. $tz,
  113. $lang,
  114. $admin_mail,
  115. $a->getBasePath()
  116. );
  117. }
  118. $this->out(" Complete!\n\n");
  119. // Check database connection
  120. $this->out("Checking database...\n");
  121. $installer->resetChecks();
  122. if (!$installer->checkDB($db_host, $db_user, $db_pass, $db_data)) {
  123. $errorMessage = $this->extractErrors($installer->getChecks());
  124. throw new RuntimeException($errorMessage);
  125. }
  126. $this->out(" Complete!\n\n");
  127. // Install database
  128. $this->out("Inserting data into database...\n");
  129. $installer->resetChecks();
  130. if (!$installer->installDatabase()) {
  131. $errorMessage = $this->extractErrors($installer->getChecks());
  132. throw new RuntimeException($errorMessage);
  133. }
  134. $this->out(" Complete!\n\n");
  135. // Install theme
  136. $this->out("Installing theme\n");
  137. if (!empty(Config::get('system', 'theme'))) {
  138. Theme::install(Config::get('system', 'theme'));
  139. $this->out(" Complete\n\n");
  140. } else {
  141. $this->out(" Theme setting is empty. Please check the file 'config/local.config.php'\n\n");
  142. }
  143. $this->out("\nInstallation is finished\n");
  144. return 0;
  145. }
  146. /**
  147. * @param Installer $installer the Installer instance
  148. *
  149. * @return bool true if checks were successfully, otherwise false
  150. */
  151. private function runBasicChecks(Installer $installer)
  152. {
  153. $checked = true;
  154. $installer->resetChecks();
  155. if (!$installer->checkFunctions()) {
  156. $checked = false;
  157. }
  158. if (!$installer->checkImagick()) {
  159. $checked = false;
  160. }
  161. if (!$installer->checkLocalIni()) {
  162. $checked = false;
  163. }
  164. if (!$installer->checkSmarty3()) {
  165. $checked = false;
  166. }
  167. if (!$installer->checkKeys()) {
  168. $checked = false;
  169. }
  170. $php_path = null;
  171. if (!empty(Config::get('config', 'php_path'))) {
  172. $php_path = Config::get('config', 'php_path');
  173. }
  174. if (!$installer->checkPHP($php_path, true)) {
  175. $checked = false;
  176. }
  177. $this->out(" NOTICE: Not checking .htaccess/URL-Rewrite during CLI installation.\n");
  178. return $checked;
  179. }
  180. /**
  181. * @param array $results
  182. * @return string
  183. */
  184. private function extractErrors($results)
  185. {
  186. $errorMessage = '';
  187. $allChecksRequired = $this->getOption('a') !== null;
  188. foreach ($results as $result) {
  189. if (($allChecksRequired || $result['required'] === true) && $result['status'] === false) {
  190. $errorMessage .= "--------\n";
  191. $errorMessage .= $result['title'] . ': ' . $result['help'] . "\n";
  192. }
  193. }
  194. return $errorMessage;
  195. }
  196. }