lib_install.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. FreshRSS_SystemConfiguration::register('default_system', join_path(FRESHRSS_PATH, 'config.default.php'));
  3. FreshRSS_UserConfiguration::register('default_user', join_path(FRESHRSS_PATH, 'config-user.default.php'));
  4. /** @return array<string,string> */
  5. function checkRequirements(string $dbType = ''): array {
  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 != '' && touch(DATA_PATH . '/index.html'); // is_writable() is not reliable for a folder on NFS
  43. // @phpstan-ignore-next-line
  44. $cache = CACHE_PATH != '' && touch(CACHE_PATH . '/index.html');
  45. $tmp = TMP_PATH != '' && is_writable(TMP_PATH);
  46. // @phpstan-ignore-next-line
  47. $users = USERS_PATH != '' && touch(USERS_PATH . '/index.html');
  48. // @phpstan-ignore-next-line
  49. $favicons = DATA_PATH != '' && touch(DATA_PATH . '/favicons/index.html');
  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(): string {
  75. return sha1(uniqid('' . mt_rand(), true).implode('', stat(__FILE__) ?: []));
  76. }
  77. function initDb(): string {
  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. if (empty($db['type'])) {
  86. $db['type'] = 'sqlite';
  87. }
  88. //Attempt to auto-create database if it does not already exist
  89. if ($db['type'] !== 'sqlite') {
  90. Minz_ModelPdo::$usesSharedPdo = false;
  91. $dbBase = $db['base'] ?? '';
  92. //For first connection, use default database for PostgreSQL, empty database for MySQL / MariaDB:
  93. $db['base'] = $db['type'] === 'pgsql' ? 'postgres' : '';
  94. $conf->db = $db;
  95. try {
  96. //First connection without database name to create the database
  97. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  98. } catch (Exception $ex) {
  99. $databaseDAO = null;
  100. }
  101. //Restore final database parameters for auto-creation and for future connections
  102. $db['base'] = $dbBase;
  103. $conf->db = $db;
  104. if ($databaseDAO != null) {
  105. //Perform database auto-creation
  106. $databaseDAO->create();
  107. }
  108. }
  109. //New connection with the database name
  110. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  111. Minz_ModelPdo::$usesSharedPdo = true;
  112. return $databaseDAO->testConnection();
  113. }
  114. function setupMigrations(): bool {
  115. $migrations_path = APP_PATH . '/migrations';
  116. $migrations_version_path = DATA_PATH . '/applied_migrations.txt';
  117. $migrator = new Minz_Migrator($migrations_path);
  118. $versions = implode("\n", $migrator->versions());
  119. return @file_put_contents($migrations_version_path, $versions) !== false;
  120. }