lib_install.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. define('BCRYPT_COST', 9);
  3. Minz_Configuration::register('default_system', join_path(FRESHRSS_PATH, 'config.default.php'));
  4. Minz_Configuration::register('default_user', join_path(FRESHRSS_PATH, 'config-user.default.php'));
  5. function checkRequirements($dbType = '') {
  6. $php = version_compare(PHP_VERSION, FRESHRSS_MIN_PHP_VERSION) >= 0;
  7. $curl = extension_loaded('curl');
  8. $pdo_mysql = extension_loaded('pdo_mysql');
  9. $pdo_sqlite = extension_loaded('pdo_sqlite');
  10. $pdo_pgsql = extension_loaded('pdo_pgsql');
  11. $message = '';
  12. switch ($dbType) {
  13. case 'mysql':
  14. $pdo_sqlite = $pdo_pgsql = true;
  15. $pdo = $pdo_mysql;
  16. break;
  17. case 'sqlite':
  18. $pdo_mysql = $pdo_pgsql = true;
  19. $pdo = $pdo_sqlite;
  20. break;
  21. case 'pgsql':
  22. $pdo_mysql = $pdo_sqlite = true;
  23. $pdo = $pdo_pgsql;
  24. break;
  25. case '':
  26. $pdo = $pdo_mysql || $pdo_sqlite || $pdo_pgsql;
  27. break;
  28. default:
  29. $pdo_mysql = $pdo_sqlite = $pdo_pgsql = true;
  30. $pdo = false;
  31. $message = 'Invalid database type!';
  32. break;
  33. }
  34. $pcre = extension_loaded('pcre');
  35. $ctype = extension_loaded('ctype');
  36. $fileinfo = extension_loaded('fileinfo');
  37. $dom = class_exists('DOMDocument');
  38. $xml = function_exists('xml_parser_create');
  39. $json = function_exists('json_encode');
  40. $mbstring = extension_loaded('mbstring');
  41. // @phpstan-ignore-next-line
  42. $data = DATA_PATH && is_writable(DATA_PATH);
  43. // @phpstan-ignore-next-line
  44. $cache = CACHE_PATH && is_writable(CACHE_PATH);
  45. // @phpstan-ignore-next-line
  46. $tmp = TMP_PATH && is_writable(TMP_PATH);
  47. // @phpstan-ignore-next-line
  48. $users = USERS_PATH && is_writable(USERS_PATH);
  49. $favicons = is_writable(join_path(DATA_PATH, 'favicons'));
  50. return array(
  51. 'php' => $php ? 'ok' : 'ko',
  52. 'curl' => $curl ? 'ok' : 'ko',
  53. 'pdo-mysql' => $pdo_mysql ? 'ok' : 'ko',
  54. 'pdo-sqlite' => $pdo_sqlite ? 'ok' : 'ko',
  55. 'pdo-pgsql' => $pdo_pgsql ? 'ok' : 'ko',
  56. 'pdo' => $pdo ? 'ok' : 'ko',
  57. 'pcre' => $pcre ? 'ok' : 'ko',
  58. 'ctype' => $ctype ? 'ok' : 'ko',
  59. 'fileinfo' => $fileinfo ? 'ok' : 'ko',
  60. 'dom' => $dom ? 'ok' : 'ko',
  61. 'xml' => $xml ? 'ok' : 'ko',
  62. 'json' => $json ? 'ok' : 'ko',
  63. 'mbstring' => $mbstring ? 'ok' : 'ko',
  64. 'data' => $data ? 'ok' : 'ko',
  65. 'cache' => $cache ? 'ok' : 'ko',
  66. 'tmp' => $tmp ? 'ok' : 'ko',
  67. 'users' => $users ? 'ok' : 'ko',
  68. 'favicons' => $favicons ? 'ok' : 'ko',
  69. 'message' => $message ?: '',
  70. 'all' => $php && $curl && $pdo && $pcre && $ctype && $dom && $xml &&
  71. $data && $cache && $tmp && $users && $favicons && $message == '' ? 'ok' : 'ko'
  72. );
  73. }
  74. function generateSalt() {
  75. return sha1(uniqid('' . mt_rand(), true).implode('', stat(__FILE__)));
  76. }
  77. function initDb() {
  78. $conf = FreshRSS_Context::$system_conf;
  79. $db = $conf->db;
  80. if (empty($db['pdo_options'])) {
  81. $db['pdo_options'] = [];
  82. }
  83. $db['pdo_options'][PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
  84. $conf->db = $db; //TODO: Remove this Minz limitation "Indirect modification of overloaded property"
  85. //Attempt to auto-create database if it does not already exist
  86. if ($db['type'] !== 'sqlite') {
  87. Minz_ModelPdo::$usesSharedPdo = false;
  88. $dbBase = isset($db['base']) ? $db['base'] : '';
  89. //For first connection, use default database for PostgreSQL, empty database for MySQL / MariaDB:
  90. $db['base'] = $db['type'] === 'pgsql' ? 'postgres' : '';
  91. $conf->db = $db;
  92. try {
  93. //First connection without database name to create the database
  94. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  95. } catch (Exception $ex) {
  96. $databaseDAO = null;
  97. }
  98. //Restore final database parameters for auto-creation and for future connections
  99. $db['base'] = $dbBase;
  100. $conf->db = $db;
  101. if ($databaseDAO != null) {
  102. //Perform database auto-creation
  103. $databaseDAO->create();
  104. }
  105. }
  106. //New connection with the database name
  107. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  108. Minz_ModelPdo::$usesSharedPdo = true;
  109. return $databaseDAO->testConnection();
  110. }
  111. function setupMigrations() {
  112. $migrations_path = APP_PATH . '/migrations';
  113. $migrations_version_path = DATA_PATH . '/applied_migrations.txt';
  114. $migrator = new Minz_Migrator($migrations_path);
  115. $versions = implode("\n", $migrator->versions());
  116. return @file_put_contents($migrations_version_path, $versions) !== false;
  117. }