Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
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.

AutomaticInstallation.php 8.4KB

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