lib_install.php 4.4 KB

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