do-install.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env php
  2. <?php
  3. require(__DIR__ . '/_cli.php');
  4. if (file_exists(DATA_PATH . '/applied_migrations.txt')) {
  5. fail('FreshRSS seems to be already installed!' . "\n" . 'Please use `./cli/reconfigure.php` instead.', EXIT_CODE_ALREADY_EXISTS);
  6. }
  7. $params = array(
  8. 'environment:',
  9. 'base_url:',
  10. 'language:',
  11. 'title:',
  12. 'default_user:',
  13. 'allow_anonymous',
  14. 'allow_anonymous_refresh',
  15. 'auth_type:',
  16. 'api_enabled',
  17. 'allow_robots',
  18. 'disable_update',
  19. );
  20. $dBparams = array(
  21. 'db-type:',
  22. 'db-host:',
  23. 'db-user:',
  24. 'db-password:',
  25. 'db-base:',
  26. 'db-prefix::',
  27. );
  28. $options = getopt('', array_merge($params, $dBparams));
  29. if (!validateOptions($argv, array_merge($params, $dBparams)) || empty($options['default_user'])) {
  30. fail('Usage: ' . basename(__FILE__) . " --default_user admin ( --auth_type form" .
  31. " --environment production --base_url https://rss.example.net --allow_robots" .
  32. " --language en --title FreshRSS --allow_anonymous --allow_anonymous_refresh --api_enabled" .
  33. " --db-type mysql --db-host localhost:3306 --db-user freshrss --db-password dbPassword123" .
  34. " --db-base freshrss --db-prefix freshrss_ --disable_update )");
  35. }
  36. fwrite(STDERR, 'FreshRSS install…' . "\n");
  37. $config = array(
  38. 'salt' => generateSalt(),
  39. 'db' => FreshRSS_Context::$system_conf->db,
  40. );
  41. $customConfigPath = DATA_PATH . '/config.custom.php';
  42. if (file_exists($customConfigPath)) {
  43. $customConfig = include($customConfigPath);
  44. if (is_array($customConfig)) {
  45. $config = array_merge($customConfig, $config);
  46. }
  47. }
  48. foreach ($params as $param) {
  49. $param = rtrim($param, ':');
  50. if (isset($options[$param])) {
  51. $config[$param] = $options[$param] === false ? true : $options[$param];
  52. }
  53. }
  54. if ((!empty($config['base_url'])) && Minz_Request::serverIsPublic($config['base_url'])) {
  55. $config['pubsubhubbub_enabled'] = true;
  56. }
  57. foreach ($dBparams as $dBparam) {
  58. $dBparam = rtrim($dBparam, ':');
  59. if (isset($options[$dBparam])) {
  60. $param = substr($dBparam, strlen('db-'));
  61. $config['db'][$param] = $options[$dBparam];
  62. }
  63. }
  64. performRequirementCheck($config['db']['type']);
  65. if (!FreshRSS_user_Controller::checkUsername($options['default_user'])) {
  66. fail('FreshRSS error: invalid default username “' . $options['default_user']
  67. . '”! Must be matching ' . FreshRSS_user_Controller::USERNAME_PATTERN);
  68. }
  69. if (isset($options['auth_type']) && !in_array($options['auth_type'], array('form', 'http_auth', 'none'))) {
  70. fail('FreshRSS invalid authentication method (auth_type must be one of { form, http_auth, none }): '
  71. . $options['auth_type']);
  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();