do-install.php 4.6 KB

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