lib_install.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. * GMP is needed to convert entry identifiers to the Google Reader format on 32-bit PHP.
  7. *
  8. * @return 'ok'|'ko'|null
  9. */
  10. function gmpRequirementStatus(int $integerSize = PHP_INT_SIZE, ?bool $gmpLoaded = null): ?string {
  11. if ($integerSize >= 8) {
  12. return null;
  13. }
  14. return ($gmpLoaded ?? extension_loaded('gmp')) ? 'ok' : 'ko';
  15. }
  16. /**
  17. * Server-side check of the database parameters received by the Web installer
  18. */
  19. function checkDbParameters(string $host, string $user, string $base, string $prefix): bool {
  20. return preg_match('/^[0-9A-Z\/a-z_.\-]{1,64}(:[0-9]{2,5})?$/D', $host) === 1 &&
  21. preg_match('/^[0-9A-Za-z@_.\-]{1,64}$/D', $user) === 1 &&
  22. preg_match('/^[0-9A-Za-z_\-]{1,64}$/D', $base) === 1 &&
  23. preg_match('/^[0-9A-Za-z_]{0,16}$/D', $prefix) === 1;
  24. }
  25. /**
  26. * @param 'mysql'|'pgsql'|'sqlite'|'' $dbType
  27. * @return array<string,'ok'|'ko'|'warn'>
  28. */
  29. function checkRequirements(string $dbType = '', bool $checkPhp = true, bool $checkFiles = true): array {
  30. $php = version_compare(PHP_VERSION, FRESHRSS_MIN_PHP_VERSION) >= 0;
  31. $curl = extension_loaded('curl'); // TODO: We actually require cURL >= 7.52 for CURLPROXY_HTTPS
  32. $pdo_mysql = extension_loaded('pdo_mysql');
  33. $pdo_sqlite = extension_loaded('pdo_sqlite');
  34. $pdo_pgsql = extension_loaded('pdo_pgsql');
  35. switch ($dbType) {
  36. case 'mysql':
  37. $pdo_sqlite = $pdo_pgsql = true;
  38. $pdo = $pdo_mysql;
  39. break;
  40. case 'sqlite':
  41. $pdo_mysql = $pdo_pgsql = true;
  42. $pdo = $pdo_sqlite;
  43. break;
  44. case 'pgsql':
  45. $pdo_mysql = $pdo_sqlite = true;
  46. $pdo = $pdo_pgsql;
  47. break;
  48. case '':
  49. $pdo = $pdo_mysql || $pdo_sqlite || $pdo_pgsql;
  50. break;
  51. default:
  52. throw new InvalidArgumentException('Invalid database type!');
  53. }
  54. $pdo &= class_exists('PDO');
  55. $pcre = extension_loaded('pcre');
  56. $ctype = extension_loaded('ctype');
  57. $fileinfo = extension_loaded('fileinfo');
  58. $dom = class_exists('DOMDocument');
  59. $xml = function_exists('xml_parser_create');
  60. $json = function_exists('json_encode');
  61. $intl = extension_loaded('intl');
  62. $mbstring = extension_loaded('mbstring');
  63. $zip = extension_loaded('zip');
  64. $gmp = gmpRequirementStatus();
  65. $data = is_dir(DATA_PATH) && touch(DATA_PATH . '/index.html'); // is_writable() is not reliable for a folder on NFS
  66. $cache = is_dir(CACHE_PATH) && touch(CACHE_PATH . '/index.html');
  67. $tmp = is_dir(TMP_PATH) && is_writable(TMP_PATH);
  68. $users = is_dir(USERS_PATH) && touch(USERS_PATH . '/index.html');
  69. $favicons = is_dir(DATA_PATH) && touch(DATA_PATH . '/favicons/index.html');
  70. $tokens = is_dir(DATA_PATH) && touch(DATA_PATH . '/tokens/index.html');
  71. // An unknown or incorrect document root might web-expose folders such as `./data`.
  72. // An empty `DOCUMENT_ROOT` outside of a Web server context (e.g. CLI, cron) is ignored because `realpath('')` resolves to the current directory.
  73. $documentRoot = $_SERVER['DOCUMENT_ROOT'] ?? null;
  74. $docRootPath = is_string($documentRoot) && $documentRoot !== '' ? realpath($documentRoot) : false;
  75. $docRootOk = $documentRoot === '' || ($docRootPath !== false && $docRootPath === realpath(PUBLIC_PATH));
  76. $result = [];
  77. if ($checkPhp) {
  78. $result += [
  79. 'php' => $php ? 'ok' : 'ko',
  80. 'pdo' => $pdo ? 'ok' : 'ko',
  81. 'pdo-sqlite' => $pdo_sqlite ? 'ok' : ($dbType === 'sqlite' ? 'ko' : 'warn'),
  82. 'pdo-pgsql' => ($dbType === 'pgsql' && !$pdo_pgsql) ? 'ko' : null,
  83. 'pdo-mysql' => ($dbType === 'mysql' && !$pdo_mysql) ? 'ko' : null,
  84. 'dom' => $dom ? 'ok' : 'ko',
  85. 'xml' => $xml ? 'ok' : 'ko',
  86. 'curl' => $curl ? 'ok' : 'ko',
  87. 'pcre' => $pcre ? 'ok' : 'ko',
  88. 'ctype' => $ctype ? 'ok' : 'ko',
  89. 'json' => $json ? 'ok' : 'ko',
  90. 'mbstring' => $mbstring ? 'ok' : 'warn',
  91. 'intl' => $intl ? 'ok' : 'warn',
  92. 'zip' => $zip ? 'ok' : 'warn',
  93. 'fileinfo' => $fileinfo ? 'ok' : 'warn',
  94. 'gmp' => $gmp,
  95. ];
  96. }
  97. if ($checkFiles) {
  98. $result += [
  99. 'data' => $data ? 'ok' : 'ko',
  100. 'cache' => $cache ? 'ok' : 'ko',
  101. 'tmp' => $tmp ? 'ok' : 'ko',
  102. 'users' => $users ? 'ok' : 'ko',
  103. 'favicons' => $favicons ? 'ok' : 'ko',
  104. 'tokens' => $tokens ? 'ok' : 'ko',
  105. 'docroot' => $docRootOk ? 'ok' : 'warn',
  106. ];
  107. }
  108. if ($checkPhp && $checkFiles) {
  109. $result['all'] = $php && $curl && $json && $pdo && $pcre && $ctype && $dom && $xml && $gmp !== 'ko' &&
  110. $data && $cache && $tmp && $users && $favicons && $tokens ? 'ok' : 'ko';
  111. }
  112. $result = array_filter($result, static fn($v) => $v !== null);
  113. return $result;
  114. }
  115. function generateSalt(): string {
  116. return hash('sha256', uniqid(more_entropy: true) . implode('', stat(__FILE__) ?: []) . random_bytes(32));
  117. }
  118. /**
  119. * @throws FreshRSS_Context_Exception
  120. */
  121. function initDb(): string {
  122. $db = FreshRSS_Context::systemConf()->db;
  123. if (empty($db['pdo_options'])) {
  124. $db['pdo_options'] = [];
  125. }
  126. $db['pdo_options'][PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
  127. FreshRSS_Context::systemConf()->db = $db; //TODO: Remove this Minz limitation "Indirect modification of overloaded property"
  128. if (empty($db['type'])) {
  129. $db['type'] = 'sqlite';
  130. }
  131. //Attempt to auto-create database if it does not already exist
  132. if ($db['type'] !== 'sqlite') {
  133. Minz_ModelPdo::$usesSharedPdo = false;
  134. $dbBase = $db['base'] ?? '';
  135. //For first connection, use default database for PostgreSQL, empty database for MySQL / MariaDB:
  136. $db['base'] = $db['type'] === 'pgsql' ? 'postgres' : '';
  137. FreshRSS_Context::systemConf()->db = $db;
  138. try {
  139. //First connection without database name to create the database
  140. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  141. } catch (Exception $ex) {
  142. $databaseDAO = null;
  143. }
  144. //Restore final database parameters for auto-creation and for future connections
  145. $db['base'] = $dbBase;
  146. FreshRSS_Context::systemConf()->db = $db;
  147. if ($databaseDAO != null) {
  148. //Perform database auto-creation
  149. $databaseDAO->create();
  150. }
  151. }
  152. //New connection with the database name
  153. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  154. Minz_ModelPdo::$usesSharedPdo = true;
  155. return $databaseDAO->testConnection();
  156. }
  157. function setupMigrations(): bool {
  158. $migrations_path = APP_PATH . '/migrations';
  159. $migrations_version_path = DATA_PATH . '/applied_migrations.txt';
  160. $migrator = new Minz_Migrator($migrations_path);
  161. $versions = implode("\n", $migrator->versions());
  162. return @file_put_contents($migrations_version_path, $versions) !== false;
  163. }