4
0

do-install.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env php
  2. <?php
  3. declare(strict_types=1);
  4. require(__DIR__ . '/_cli.php');
  5. if (file_exists(DATA_PATH . '/applied_migrations.txt')) {
  6. fail('FreshRSS seems to be already installed!' . "\n" . 'Please use `./cli/reconfigure.php` instead.', EXIT_CODE_ALREADY_EXISTS);
  7. }
  8. $cliOptions = new class extends CliOptionsParser {
  9. public string $defaultUser;
  10. public string $environment;
  11. public string $baseUrl;
  12. public string $language;
  13. public string $title;
  14. public bool $allowAnonymous;
  15. public bool $allowAnonymousRefresh;
  16. public string $authType;
  17. public bool $apiEnabled;
  18. public bool $allowRobots;
  19. public bool $disableUpdate;
  20. public string $dbType;
  21. public string $dbHost;
  22. public string $dbUser;
  23. public string $dbPassword;
  24. public string $dbBase;
  25. public string $dbPrefix;
  26. public function __construct() {
  27. $this->addRequiredOption('defaultUser', (new CliOption('default-user'))->deprecatedAs('default_user'));
  28. $this->addOption('environment', (new CliOption('environment')));
  29. $this->addOption('baseUrl', (new CliOption('base-url'))->deprecatedAs('base_url'));
  30. $this->addOption('language', (new CliOption('language')));
  31. $this->addOption('title', (new CliOption('title')));
  32. $this->addOption(
  33. 'allowAnonymous',
  34. (new CliOption('allow-anonymous'))->withValueOptional('true')->deprecatedAs('allow_anonymous')->typeOfBool()
  35. );
  36. $this->addOption(
  37. 'allowAnonymousRefresh',
  38. (new CliOption('allow-anonymous-refresh'))->withValueOptional('true')->deprecatedAs('allow_anonymous_refresh')->typeOfBool()
  39. );
  40. $this->addOption('authType', (new CliOption('auth-type'))->deprecatedAs('auth_type'));
  41. $this->addOption(
  42. 'apiEnabled',
  43. (new CliOption('api-enabled'))->withValueOptional('true')->deprecatedAs('api_enabled')->typeOfBool()
  44. );
  45. $this->addOption(
  46. 'allowRobots',
  47. (new CliOption('allow-robots'))->withValueOptional('true')->deprecatedAs('allow_robots')->typeOfBool()
  48. );
  49. $this->addOption(
  50. 'disableUpdate',
  51. (new CliOption('disable-update'))->withValueOptional('true')->deprecatedAs('disable_update')->typeOfBool()
  52. );
  53. $this->addOption('dbType', (new CliOption('db-type')));
  54. $this->addOption('dbHost', (new CliOption('db-host')));
  55. $this->addOption('dbUser', (new CliOption('db-user')));
  56. $this->addOption('dbPassword', (new CliOption('db-password')));
  57. $this->addOption('dbBase', (new CliOption('db-base')));
  58. $this->addOption('dbPrefix', (new CliOption('db-prefix'))->withValueOptional());
  59. parent::__construct();
  60. }
  61. };
  62. if (!empty($cliOptions->errors)) {
  63. fail('FreshRSS error: ' . array_shift($cliOptions->errors) . "\n" . $cliOptions->usage);
  64. }
  65. fwrite(STDERR, 'FreshRSS install…' . "\n");
  66. $values = [
  67. 'default_user' => $cliOptions->defaultUser ?? null,
  68. 'environment' => $cliOptions->environment ?? null,
  69. 'base_url' => $cliOptions->baseUrl ?? null,
  70. 'language' => $cliOptions->language ?? null,
  71. 'title' => $cliOptions->title ?? null,
  72. 'allow_anonymous' => $cliOptions->allowAnonymous ?? null,
  73. 'allow_anonymous_refresh' => $cliOptions->allowAnonymousRefresh ?? null,
  74. 'auth_type' => $cliOptions->authType ?? null,
  75. 'api_enabled' => $cliOptions->apiEnabled ?? null,
  76. 'allow_robots' => $cliOptions->allowRobots ?? null,
  77. 'disable_update' => $cliOptions->disableUpdate ?? null,
  78. ];
  79. $dbValues = [
  80. 'type' => $cliOptions->dbType ?? null,
  81. 'host' => $cliOptions->dbHost ?? null,
  82. 'user' => $cliOptions->dbUser ?? null,
  83. 'password' => $cliOptions->dbPassword ?? null,
  84. 'base' => $cliOptions->dbBase ?? null,
  85. 'prefix' => $cliOptions->dbPrefix ?? null,
  86. ];
  87. $config = [
  88. 'salt' => generateSalt(),
  89. 'db' => FreshRSS_Context::systemConf()->db,
  90. ];
  91. $customConfigPath = DATA_PATH . '/config.custom.php';
  92. if (file_exists($customConfigPath)) {
  93. $customConfig = include($customConfigPath);
  94. if (is_array($customConfig)) {
  95. $config = array_merge($customConfig, $config);
  96. }
  97. }
  98. foreach ($values as $name => $value) {
  99. if ($value !== null) {
  100. switch ($name) {
  101. case 'default_user':
  102. if (!FreshRSS_user_Controller::checkUsername($value)) {
  103. fail('FreshRSS invalid default username! default_user must be ASCII alphanumeric');
  104. }
  105. break;
  106. case 'environment':
  107. if (!in_array($value, ['development', 'production', 'silent'], true)) {
  108. fail('FreshRSS invalid environment! environment must be one of { development, production, silent }');
  109. }
  110. break;
  111. case 'auth_type':
  112. if (!in_array($value, ['form', 'http_auth', 'none'], true)) {
  113. fail('FreshRSS invalid authentication method! auth_type must be one of { form, http_auth, none }');
  114. }
  115. break;
  116. }
  117. $config[$name] = $value;
  118. }
  119. }
  120. if ((!empty($config['base_url'])) && is_string($config['base_url']) && Minz_Request::serverIsPublic($config['base_url'])) {
  121. $config['pubsubhubbub_enabled'] = true;
  122. }
  123. $config['db'] = array_merge($config['db'], array_filter($dbValues, static fn($value) => $value !== null));
  124. performRequirementCheck($config['db']['type']);
  125. if (file_put_contents(join_path(DATA_PATH, 'config.php'),
  126. "<?php\n return " . var_export($config, true) . ";\n") === false) {
  127. fail('FreshRSS could not write configuration file!: ' . join_path(DATA_PATH, 'config.php'));
  128. }
  129. if (function_exists('opcache_reset')) {
  130. opcache_reset();
  131. }
  132. FreshRSS_Context::initSystem(true);
  133. Minz_User::change(Minz_User::INTERNAL_USER);
  134. $ok = false;
  135. try {
  136. $error = initDb();
  137. if ($error != '') {
  138. $_SESSION['bd_error'] = $error;
  139. } else {
  140. $ok = true;
  141. }
  142. } catch (Exception $ex) {
  143. $_SESSION['bd_error'] = $ex->getMessage();
  144. }
  145. if (!$ok) {
  146. @unlink(join_path(DATA_PATH, 'config.php'));
  147. fail('FreshRSS database error: ' . (empty($_SESSION['bd_error']) ? 'Unknown error' : $_SESSION['bd_error']));
  148. }
  149. echo 'ℹ️ Remember to create the default user: ', $config['default_user'],
  150. "\t", './cli/create-user.php --user ', $config['default_user'], " --password 'password' --more-options\n";
  151. accessRights();
  152. if (!setupMigrations()) {
  153. fail('FreshRSS access right problem while creating migrations version file!');
  154. }
  155. done();