do-install.php 3.6 KB

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