4
0

lib_install.php 5.0 KB

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