4
0

lib_install.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // An unknown or incorrect document root might web-expose folders such as `./data`.
  51. // An empty `DOCUMENT_ROOT` outside of a Web server context (e.g. CLI, cron) is ignored because `realpath('')` resolves to the current directory.
  52. $documentRoot = $_SERVER['DOCUMENT_ROOT'] ?? null;
  53. $docRootPath = is_string($documentRoot) && $documentRoot !== '' ? realpath($documentRoot) : false;
  54. $docRootOk = $documentRoot === '' || ($docRootPath !== false && $docRootPath === realpath(PUBLIC_PATH));
  55. $result = [];
  56. if ($checkPhp) {
  57. $result += [
  58. 'php' => $php ? 'ok' : 'ko',
  59. 'pdo' => $pdo ? 'ok' : 'ko',
  60. 'pdo-sqlite' => $pdo_sqlite ? 'ok' : ($dbType === 'sqlite' ? 'ko' : 'warn'),
  61. 'pdo-pgsql' => ($dbType === 'pgsql' && !$pdo_pgsql) ? 'ko' : null,
  62. 'pdo-mysql' => ($dbType === 'mysql' && !$pdo_mysql) ? 'ko' : null,
  63. 'dom' => $dom ? 'ok' : 'ko',
  64. 'xml' => $xml ? 'ok' : 'ko',
  65. 'curl' => $curl ? 'ok' : 'ko',
  66. 'pcre' => $pcre ? 'ok' : 'ko',
  67. 'ctype' => $ctype ? 'ok' : 'ko',
  68. 'json' => $json ? 'ok' : 'ko',
  69. 'mbstring' => $mbstring ? 'ok' : 'warn',
  70. 'intl' => $intl ? 'ok' : 'warn',
  71. 'zip' => $zip ? 'ok' : 'warn',
  72. 'fileinfo' => $fileinfo ? 'ok' : 'warn',
  73. ];
  74. }
  75. if ($checkFiles) {
  76. $result += [
  77. 'data' => $data ? 'ok' : 'ko',
  78. 'cache' => $cache ? 'ok' : 'ko',
  79. 'tmp' => $tmp ? 'ok' : 'ko',
  80. 'users' => $users ? 'ok' : 'ko',
  81. 'favicons' => $favicons ? 'ok' : 'ko',
  82. 'tokens' => $tokens ? 'ok' : 'ko',
  83. 'docroot' => $docRootOk ? 'ok' : 'warn',
  84. ];
  85. }
  86. if ($checkPhp && $checkFiles) {
  87. $result['all'] = $php && $curl && $json && $pdo && $pcre && $ctype && $dom && $xml &&
  88. $data && $cache && $tmp && $users && $favicons && $tokens ? 'ok' : 'ko';
  89. }
  90. $result = array_filter($result, static fn($v) => $v !== null);
  91. return $result;
  92. }
  93. function generateSalt(): string {
  94. return hash('sha256', uniqid(more_entropy: true) . implode('', stat(__FILE__) ?: []) . random_bytes(32));
  95. }
  96. /**
  97. * @throws FreshRSS_Context_Exception
  98. */
  99. function initDb(): string {
  100. $db = FreshRSS_Context::systemConf()->db;
  101. if (empty($db['pdo_options'])) {
  102. $db['pdo_options'] = [];
  103. }
  104. $db['pdo_options'][PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
  105. FreshRSS_Context::systemConf()->db = $db; //TODO: Remove this Minz limitation "Indirect modification of overloaded property"
  106. if (empty($db['type'])) {
  107. $db['type'] = 'sqlite';
  108. }
  109. //Attempt to auto-create database if it does not already exist
  110. if ($db['type'] !== 'sqlite') {
  111. Minz_ModelPdo::$usesSharedPdo = false;
  112. $dbBase = $db['base'] ?? '';
  113. //For first connection, use default database for PostgreSQL, empty database for MySQL / MariaDB:
  114. $db['base'] = $db['type'] === 'pgsql' ? 'postgres' : '';
  115. FreshRSS_Context::systemConf()->db = $db;
  116. try {
  117. //First connection without database name to create the database
  118. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  119. } catch (Exception $ex) {
  120. $databaseDAO = null;
  121. }
  122. //Restore final database parameters for auto-creation and for future connections
  123. $db['base'] = $dbBase;
  124. FreshRSS_Context::systemConf()->db = $db;
  125. if ($databaseDAO != null) {
  126. //Perform database auto-creation
  127. $databaseDAO->create();
  128. }
  129. }
  130. //New connection with the database name
  131. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  132. Minz_ModelPdo::$usesSharedPdo = true;
  133. return $databaseDAO->testConnection();
  134. }
  135. function setupMigrations(): bool {
  136. $migrations_path = APP_PATH . '/migrations';
  137. $migrations_version_path = DATA_PATH . '/applied_migrations.txt';
  138. $migrator = new Minz_Migrator($migrations_path);
  139. $versions = implode("\n", $migrator->versions());
  140. return @file_put_contents($migrations_version_path, $versions) !== false;
  141. }