do-install.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. $parameters = [
  9. 'long' => [
  10. 'environment' => ':',
  11. 'base-url' => ':',
  12. 'language' => ':',
  13. 'title' => ':',
  14. 'default-user' => ':',
  15. 'allow-anonymous' => '',
  16. 'allow-anonymous-refresh' => '',
  17. 'auth-type' => ':',
  18. 'api-enabled' => '',
  19. 'allow-robots' => '',
  20. 'disable-update' => '',
  21. 'db-type' => ':',
  22. 'db-host' => ':',
  23. 'db-user' => ':',
  24. 'db-password' => ':',
  25. 'db-base' => ':',
  26. 'db-prefix' => '::',
  27. ],
  28. 'short' => [],
  29. 'deprecated' => [
  30. 'base-url' => 'base_url',
  31. 'default-user' => 'default_user',
  32. 'allow-anonymous' => 'allow_anonymous',
  33. 'allow-anonymous-refresh' => 'allow_anonymous_refresh',
  34. 'auth-type' => 'auth_type',
  35. 'api-enabled' => 'api_enabled',
  36. 'allow-robots' => 'allow_robots',
  37. 'disable-update' => 'disable_update',
  38. ],
  39. ];
  40. $configParams = [
  41. 'environment' => 'environment',
  42. 'base-url' => 'base_url',
  43. 'language' => 'language',
  44. 'title' => 'title',
  45. 'default-user' => 'default_user',
  46. 'allow-anonymous' => 'allow_anonymous',
  47. 'allow-anonymous-refresh' => 'allow_anonymous_refresh',
  48. 'auth-type' => 'auth_type',
  49. 'api-enabled' => 'api_enabled',
  50. 'allow-robots' => 'allow_robots',
  51. 'disable-update' => 'disable_update',
  52. ];
  53. $dBconfigParams = [
  54. 'db-type' => 'type',
  55. 'db-host' => 'host',
  56. 'db-user' => 'user',
  57. 'db-password' => 'password',
  58. 'db-base' => 'base',
  59. 'db-prefix' => 'prefix',
  60. ];
  61. $options = parseCliParams($parameters);
  62. if (!empty($options['invalid']) || empty($options['valid']['default-user']) || !is_string($options['valid']['default-user'])) {
  63. fail('Usage: ' . basename(__FILE__) . " --default-user admin ( --auth-type form" .
  64. " --environment production --base-url https://rss.example.net --allow-robots" .
  65. " --language en --title FreshRSS --allow-anonymous --allow-anonymous-refresh --api-enabled" .
  66. " --db-type mysql --db-host localhost:3306 --db-user freshrss --db-password dbPassword123" .
  67. " --db-base freshrss --db-prefix freshrss_ --disable-update )");
  68. }
  69. fwrite(STDERR, 'FreshRSS install…' . "\n");
  70. $config = array(
  71. 'salt' => generateSalt(),
  72. 'db' => FreshRSS_Context::systemConf()->db,
  73. );
  74. $customConfigPath = DATA_PATH . '/config.custom.php';
  75. if (file_exists($customConfigPath)) {
  76. $customConfig = include($customConfigPath);
  77. if (is_array($customConfig)) {
  78. $config = array_merge($customConfig, $config);
  79. }
  80. }
  81. foreach ($configParams as $param => $configParam) {
  82. if (isset($options['valid'][$param])) {
  83. $isFlag = $parameters['long'][$param] === '';
  84. $config[$configParam] = $isFlag ? true : $options['valid'][$param];
  85. }
  86. }
  87. if ((!empty($config['base_url'])) && is_string($config['base_url']) && Minz_Request::serverIsPublic($config['base_url'])) {
  88. $config['pubsubhubbub_enabled'] = true;
  89. }
  90. foreach ($dBconfigParams as $dBparam => $configDbParam) {
  91. if (isset($options['valid'][$dBparam])) {
  92. $config['db'][$configDbParam] = $options['valid'][$dBparam];
  93. }
  94. }
  95. performRequirementCheck($config['db']['type']);
  96. if (!FreshRSS_user_Controller::checkUsername($options['valid']['default-user'])) {
  97. fail('FreshRSS error: invalid default username “' . $options['valid']['default-user']
  98. . '”! Must be matching ' . FreshRSS_user_Controller::USERNAME_PATTERN);
  99. }
  100. if (isset($options['valid']['auth-type']) && !in_array($options['valid']['auth-type'], ['form', 'http_auth', 'none'], true)) {
  101. fail('FreshRSS invalid authentication method (auth-type must be one of { form, http_auth, none })');
  102. }
  103. if (file_put_contents(join_path(DATA_PATH, 'config.php'),
  104. "<?php\n return " . var_export($config, true) . ";\n") === false) {
  105. fail('FreshRSS could not write configuration file!: ' . join_path(DATA_PATH, 'config.php'));
  106. }
  107. if (function_exists('opcache_reset')) {
  108. opcache_reset();
  109. }
  110. FreshRSS_Context::initSystem(true);
  111. Minz_User::change(Minz_User::INTERNAL_USER);
  112. $ok = false;
  113. try {
  114. $error = initDb();
  115. if ($error != '') {
  116. $_SESSION['bd_error'] = $error;
  117. } else {
  118. $ok = true;
  119. }
  120. } catch (Exception $ex) {
  121. $_SESSION['bd_error'] = $ex->getMessage();
  122. }
  123. if (!$ok) {
  124. @unlink(join_path(DATA_PATH, 'config.php'));
  125. fail('FreshRSS database error: ' . (empty($_SESSION['bd_error']) ? 'Unknown error' : $_SESSION['bd_error']));
  126. }
  127. echo 'ℹ️ Remember to create the default user: ', $config['default_user'],
  128. "\t", './cli/create-user.php --user ', $config['default_user'], " --password 'password' --more-options\n";
  129. accessRights();
  130. if (!setupMigrations()) {
  131. fail('FreshRSS access right problem while creating migrations version file!');
  132. }
  133. done();