4
0

install.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. <?php
  2. declare(strict_types=1);
  3. if (isset($_SESSION) || basename(is_string($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : '') !== 'index.php') {
  4. header('HTTP/1.1 403 Forbidden');
  5. exit('Forbidden');
  6. }
  7. if (function_exists('opcache_reset')) {
  8. opcache_reset();
  9. }
  10. header("Content-Security-Policy: default-src 'self'; frame-ancestors 'none'");
  11. header('Referrer-Policy: same-origin');
  12. header('Cross-Origin-Opener-Policy: same-origin');
  13. require LIB_PATH . '/lib_install.php';
  14. Minz_Session::init('FreshRSS');
  15. if (isset($_GET['step']) && is_numeric($_GET['step'])) {
  16. define('STEP', (int)$_GET['step']);
  17. } else {
  18. define('STEP', 0);
  19. }
  20. if (STEP === 2 && isset($_POST['type'])) {
  21. Minz_Session::_param('db_type', $_POST['type']);
  22. }
  23. function param(string $key, string $default = ''): string {
  24. return isset($_POST[$key]) && is_string($_POST[$key]) ? trim($_POST[$key]) : $default;
  25. }
  26. // Internationalization
  27. function initTranslate(): void {
  28. Minz_Translate::init();
  29. $available_languages = Minz_Translate::availableLanguages();
  30. if (Minz_Session::paramString('language') == '') {
  31. Minz_Session::_param('language', get_best_language());
  32. }
  33. if (!in_array(Minz_Session::paramString('language'), $available_languages, true)) {
  34. Minz_Session::_param('language', Minz_Translate::DEFAULT_LANGUAGE);
  35. }
  36. Minz_Translate::reset(Minz_Session::paramString('language'));
  37. }
  38. function get_best_language(): string {
  39. $accept = empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) || !is_string($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? '' : $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  40. return strtolower(substr($accept, 0, 2));
  41. }
  42. /*** SAVE OPERATIONS ***/
  43. function saveLanguage(): bool {
  44. if (!empty($_POST)) {
  45. if (!isset($_POST['language'])) {
  46. return false;
  47. }
  48. Minz_Session::_param('language', $_POST['language']);
  49. Minz_Session::_param('sessionWorking', 'ok');
  50. header('Location: index.php?step=1');
  51. }
  52. return true;
  53. }
  54. function saveStep1(): void {
  55. if (isset($_POST['freshrss-keep-install']) &&
  56. $_POST['freshrss-keep-install'] === '1') {
  57. // We want to keep our previous installation of FreshRSS
  58. // so we need to make next steps valid by setting $_SESSION vars
  59. // with values from the previous installation
  60. // First, we try to get previous configurations
  61. FreshRSS_Context::initSystem();
  62. FreshRSS_Context::initUser(FreshRSS_Context::systemConf()->default_user, false);
  63. // Then, we set $_SESSION vars
  64. Minz_Session::_params([
  65. 'title' => FreshRSS_Context::systemConf()->title,
  66. 'auth_type' => FreshRSS_Context::systemConf()->auth_type,
  67. 'default_user' => Minz_User::name() ?? '',
  68. 'passwordHash' => FreshRSS_Context::userConf()->passwordHash,
  69. 'db_type' => FreshRSS_Context::systemConf()->db['type'] ?? '',
  70. 'db_host' => FreshRSS_Context::systemConf()->db['host'] ?? '',
  71. 'db_user' => FreshRSS_Context::systemConf()->db['user'] ?? '',
  72. 'db_password' => FreshRSS_Context::systemConf()->db['password'] ?? '',
  73. 'db_base' => FreshRSS_Context::systemConf()->db['base'] ?? '',
  74. 'db_prefix' => FreshRSS_Context::systemConf()->db['prefix'] ?? '',
  75. 'db_error' => false,
  76. ]);
  77. header('Location: index.php?step=4');
  78. }
  79. }
  80. function saveStep2(): void {
  81. if (!empty($_POST)) {
  82. if (Minz_Session::paramString('db_type') === 'sqlite') {
  83. Minz_Session::_params([
  84. 'db_base' => false,
  85. 'db_host' => false,
  86. 'db_user' => false,
  87. 'db_password' => false,
  88. 'db_prefix' => false,
  89. ]);
  90. } else {
  91. if (empty($_POST['type']) || !is_string($_POST['type']) ||
  92. empty($_POST['host']) || !is_string($_POST['host']) ||
  93. empty($_POST['user']) || !is_string($_POST['user']) ||
  94. empty($_POST['base']) || !is_string($_POST['base']) ||
  95. !is_string($_POST['pass'] ?? null) || !is_string($_POST['prefix'] ?? null)
  96. ) {
  97. Minz_Session::_param('db_error', 'Missing parameters!');
  98. } else {
  99. Minz_Session::_params([
  100. 'db_base' => substr($_POST['base'], 0, 64),
  101. 'db_host' => $_POST['host'],
  102. 'db_user' => $_POST['user'],
  103. 'db_password' => $_POST['pass'],
  104. 'db_prefix' => substr($_POST['prefix'], 0, 16),
  105. ]);
  106. }
  107. }
  108. // We use dirname to remove the /i part
  109. $base_url = dirname(Minz_Request::guessBaseUrl());
  110. $config_array = [
  111. 'salt' => generateSalt(),
  112. 'base_url' => $base_url,
  113. 'default_user' => '_',
  114. 'db' => [
  115. 'type' => Minz_Session::paramString('db_type'),
  116. 'host' => Minz_Session::paramString('db_host'),
  117. 'user' => Minz_Session::paramString('db_user'),
  118. 'password' => Minz_Session::paramString('db_password'),
  119. 'base' => Minz_Session::paramString('db_base'),
  120. 'prefix' => Minz_Session::paramString('db_prefix'),
  121. 'pdo_options' => [],
  122. ],
  123. 'pubsubhubbub_enabled' => Minz_Request::serverIsPublic($base_url),
  124. ];
  125. if (Minz_Session::paramString('title') != '') {
  126. $config_array['title'] = Minz_Session::paramString('title');
  127. }
  128. $customConfigPath = DATA_PATH . '/config.custom.php';
  129. if (file_exists($customConfigPath)) {
  130. $customConfig = include $customConfigPath;
  131. if (is_array($customConfig)) {
  132. $config_array = array_merge($customConfig, $config_array);
  133. if (!is_string($config_array['default_user'] ?? null)) {
  134. $config_array['default_user'] = '_';
  135. }
  136. }
  137. }
  138. @unlink(DATA_PATH . '/config.php'); //To avoid access-rights problems
  139. file_put_contents(DATA_PATH . '/config.php', "<?php\n return " . var_export($config_array, true) . ";\n");
  140. if (function_exists('opcache_reset')) {
  141. opcache_reset();
  142. }
  143. FreshRSS_Context::initSystem(true);
  144. $ok = false;
  145. try {
  146. if (!is_string($config_array['default_user'])) {
  147. throw new Exception('Invalid default user name');
  148. }
  149. Minz_User::change($config_array['default_user']);
  150. $error = initDb();
  151. Minz_User::change();
  152. if ($error != '') {
  153. Minz_Session::_param('db_error', $error);
  154. } else {
  155. $ok = true;
  156. }
  157. } catch (Exception $ex) {
  158. Minz_Session::_param('db_error', $ex->getMessage());
  159. $ok = false;
  160. }
  161. if (!$ok) {
  162. @unlink(join_path(DATA_PATH, 'config.php'));
  163. }
  164. if ($ok) {
  165. Minz_Session::_param('db_error');
  166. header('Location: index.php?step=3');
  167. } elseif (Minz_Session::paramString('db_error') == '') {
  168. Minz_Session::_param('db_error', 'Unknown error!');
  169. }
  170. }
  171. invalidateHttpCache();
  172. }
  173. function saveStep3(): bool {
  174. FreshRSS_Context::initSystem();
  175. Minz_Translate::init(Minz_Session::paramString('language'));
  176. if (!empty($_POST)) {
  177. $auth_type = param('auth_type', 'form');
  178. if (in_array($auth_type, ['form', 'http_auth', 'none'], true)) {
  179. FreshRSS_Context::systemConf()->auth_type = $auth_type;
  180. Minz_Session::_param('auth_type', FreshRSS_Context::systemConf()->auth_type);
  181. } else {
  182. return false;
  183. }
  184. $password_plain = param('passwordPlain', '');
  185. if (FreshRSS_Context::systemConf()->auth_type === 'form' && !FreshRSS_FormAuth::passwordRequirementsMet($password_plain)) {
  186. return false;
  187. }
  188. if (FreshRSS_user_Controller::checkUsername(param('default_user', ''))) {
  189. FreshRSS_Context::systemConf()->default_user = param('default_user', '');
  190. Minz_Session::_param('default_user', FreshRSS_Context::systemConf()->default_user);
  191. } else {
  192. return false;
  193. }
  194. if (FreshRSS_Context::systemConf()->auth_type === 'http_auth' &&
  195. Minz_Request::connectionRemoteAddress() !== '' &&
  196. empty($_SERVER['REMOTE_USER']) && empty($_SERVER['REDIRECT_REMOTE_USER']) && // No safe authentication HTTP headers
  197. (!empty($_SERVER['HTTP_REMOTE_USER']) || !empty($_SERVER['HTTP_X_WEBAUTH_USER'])) // but has unsafe authentication HTTP headers
  198. ) {
  199. // Trust by default the remote IP address (e.g. last proxy) used during install to provide remote user name via unsafe HTTP header
  200. FreshRSS_Context::systemConf()->trusted_sources[] = Minz_Request::connectionRemoteAddress();
  201. FreshRSS_Context::systemConf()->trusted_sources = array_unique(FreshRSS_Context::systemConf()->trusted_sources);
  202. }
  203. // Create default user files but first, we delete previous data to
  204. // avoid access right problems.
  205. recursive_unlink(USERS_PATH . '/' . Minz_Session::paramString('default_user'));
  206. $ok = false;
  207. try {
  208. Minz_ModelPdo::$usesSharedPdo = false;
  209. $databaseDAO = FreshRSS_Factory::createDatabaseDAO(Minz_User::INTERNAL_USER);
  210. if (!$databaseDAO->testTyping()) {
  211. $message = 'Invalid PDO driver behaviour for selected database type!';
  212. if (Minz_Session::paramString('db_type') === 'mysql') {
  213. $message .= ' MySQL requires mysqlnd.';
  214. }
  215. throw new Exception($message);
  216. }
  217. Minz_ModelPdo::$usesSharedPdo = true;
  218. $ok = FreshRSS_user_Controller::createUser(
  219. Minz_Session::paramString('default_user'),
  220. '', //TODO: Add e-mail
  221. $password_plain,
  222. [
  223. 'language' => Minz_Session::paramString('language'),
  224. 'is_admin' => true,
  225. 'enabled' => true,
  226. ]
  227. );
  228. } catch (Exception $e) {
  229. Minz_Session::_param('db_error', $e->getMessage());
  230. $ok = false;
  231. }
  232. if (!$ok) {
  233. checkStep();
  234. return false;
  235. }
  236. FreshRSS_Context::systemConf()->save();
  237. header('Location: index.php?step=4');
  238. }
  239. return true;
  240. }
  241. /*** CHECKS ***/
  242. function checkStep(): void {
  243. $s0 = checkStep0();
  244. $s1 = checkRequirements();
  245. $s2 = checkStep2();
  246. $s3 = checkStep3();
  247. if (STEP > 0 && $s0['all'] !== 'ok') {
  248. header('Location: index.php?step=0');
  249. } elseif (STEP > 1 && $s1['all'] !== 'ok') {
  250. header('Location: index.php?step=1');
  251. } elseif (STEP > 2 && $s2['all'] !== 'ok') {
  252. header('Location: index.php?step=2');
  253. } elseif (STEP > 3 && $s3['all'] !== 'ok') {
  254. header('Location: index.php?step=3');
  255. }
  256. Minz_Session::_param('actualize_feeds', true);
  257. }
  258. /** @return array<string,string> */
  259. function checkStep0(): array {
  260. $languages = Minz_Translate::availableLanguages();
  261. $language = Minz_Session::paramString('language') != '' && in_array(Minz_Session::paramString('language'), $languages, true);
  262. $sessionWorking = Minz_Session::paramString('sessionWorking') === 'ok';
  263. return [
  264. 'language' => $language ? 'ok' : 'ko',
  265. 'sessionWorking' => $sessionWorking ? 'ok' : 'ko',
  266. 'all' => $language && $sessionWorking ? 'ok' : 'ko',
  267. ];
  268. }
  269. function freshrss_already_installed(): bool {
  270. $conf_path = join_path(DATA_PATH, 'config.php');
  271. if (!file_exists($conf_path)) {
  272. return false;
  273. }
  274. // A configuration file already exists, we try to load it.
  275. $system_conf = null;
  276. try {
  277. $system_conf = FreshRSS_SystemConfiguration::init($conf_path);
  278. } catch (Minz_FileNotExistException $e) {
  279. return false;
  280. }
  281. // ok, the global conf exists… but what about default user conf?
  282. $current_user = $system_conf->default_user;
  283. try {
  284. FreshRSS_UserConfiguration::init(USERS_PATH . '/' . $current_user . '/config.php');
  285. } catch (Minz_FileNotExistException $e) {
  286. return false;
  287. }
  288. // ok, ok, default user exists too!
  289. return true;
  290. }
  291. /** @return array<string,string> */
  292. function checkStep2(): array {
  293. $conf = is_writable(join_path(DATA_PATH, 'config.php'));
  294. $db = Minz_Session::paramString('db_type') != '';
  295. $conn = Minz_Session::paramString('db_error') == '';
  296. return [
  297. 'db' => $db ? 'ok' : 'ko',
  298. 'conn' => $conn ? 'ok' : 'ko',
  299. 'conf' => $conf ? 'ok' : 'ko',
  300. 'all' => $db && $conn && $conf ? 'ok' : 'ko',
  301. ];
  302. }
  303. /** @return array<string,string> */
  304. function checkStep3(): array {
  305. $conf = Minz_Session::paramString('default_user') != '';
  306. $form = Minz_Session::paramString('auth_type') != '';
  307. $defaultUser = is_string($_POST['default_user'] ?? null) ? trim($_POST['default_user']) : '';
  308. if ($defaultUser === '') {
  309. $defaultUser = Minz_Session::paramString('default_user') == '' ? '' : Minz_Session::paramString('default_user');
  310. }
  311. $data = is_writable(USERS_PATH . '/' . $defaultUser . '/config.php');
  312. return [
  313. 'conf' => $conf ? 'ok' : 'ko',
  314. 'form' => $form ? 'ok' : 'ko',
  315. 'data' => $data ? 'ok' : 'ko',
  316. 'all' => $conf && $form && $data ? 'ok' : 'ko',
  317. ];
  318. }
  319. /* select language */
  320. function printStep0(): void {
  321. $actual = Minz_Translate::language();
  322. $languages = Minz_Translate::availableLanguages();
  323. $s0 = checkStep0();
  324. ?>
  325. <?php if ($s0['all'] === 'ok') { ?>
  326. <p class="alert alert-success"><span class="alert-head"><?= _t('gen.short.ok') ?></span> <?= _t('install.language.defined') ?></p>
  327. <?php } elseif (!empty($_POST) && $s0['sessionWorking'] !== 'ok') { ?>
  328. <p class="alert alert-error"><span class="alert-head"><?= _t('gen.short.damn') ?></span> <?= _t('install.session.nok') ?></p>
  329. <?php } ?>
  330. <div class="form-group">
  331. <label class="group-name"><?= _t('index.about') ?></label>
  332. <div class="group-controls">
  333. <?= _t('index.about.freshrss_description') ?>
  334. </div>
  335. </div>
  336. <div class="form-group">
  337. <label class="group-name"><?= _t('index.about.project_website') ?></label>
  338. <div class="group-controls">
  339. <a href="<?= FRESHRSS_WEBSITE ?>" target="_blank"><?= FRESHRSS_WEBSITE ?></a>
  340. </div>
  341. </div>
  342. <div class="form-group">
  343. <label class="group-name"><?= _t('index.about.documentation') ?></label>
  344. <div class="group-controls">
  345. <a href="<?= FRESHRSS_WIKI ?>" target="_blank"><?= FRESHRSS_WIKI ?></a>
  346. </div>
  347. </div>
  348. <div class="form-group">
  349. <label class="group-name"><?= _t('index.about.version') ?></label>
  350. <div class="group-controls">
  351. <?= FRESHRSS_VERSION ?>
  352. </div>
  353. </div>
  354. <h2><?= _t('install.language.choose') ?></h2>
  355. <form action="index.php?step=0" method="post">
  356. <div class="form-group">
  357. <label class="group-name" for="language"><?= _t('install.language') ?></label>
  358. <div class="group-controls">
  359. <select name="language" id="language" tabindex="1" >
  360. <?php foreach ($languages as $lang) { ?>
  361. <option value="<?= $lang ?>"<?= $actual == $lang ? ' selected="selected"' : '' ?>>
  362. <?= _t('gen.lang.' . $lang) ?>
  363. </option>
  364. <?php } ?>
  365. </select>
  366. </div>
  367. </div>
  368. <div class="form-group form-actions">
  369. <div class="group-controls">
  370. <button type="submit" class="btn btn-important" tabindex="2" ><?= _t('gen.action.submit') ?></button>
  371. <?php if ($s0['all'] == 'ok') { ?>
  372. <a class="next-step" href="?step=1" tabindex="4" ><?= _t('install.action.next_step') ?></a>
  373. <?php } ?>
  374. </div>
  375. </div>
  376. </form>
  377. <?php
  378. }
  379. /**
  380. * Alert box template
  381. * @param array<string> $messageParams
  382. * */
  383. function printStep1Template(string $key, string $value, array $messageParams = []): void {
  384. if ('ok' === $value) {
  385. $message = _t("install.check.{$key}.ok", ...$messageParams);
  386. ?><p class="alert alert-success"><span class="alert-head"><?= _t('gen.short.ok') ?></span> <?= $message ?></p><?php
  387. } elseif ($value === 'warn') {
  388. $message = _t("install.check.{$key}.nok", ...$messageParams);
  389. ?><p class="alert alert-warn"><span class="alert-head"><?= _t('gen.short.attention') ?></span> <?= $message ?></p><?php
  390. } else {
  391. $message = _t("install.check.{$key}.nok", ...$messageParams);
  392. ?><p class="alert alert-error"><span class="alert-head"><?= _t('gen.short.damn') ?></span> <?= $message ?></p><?php
  393. }
  394. }
  395. function getProcessUsername(): string {
  396. if (function_exists('posix_getpwuid') && function_exists('posix_geteuid')) {
  397. $processUser = posix_getpwuid(posix_geteuid()) ?: [];
  398. if (!empty($processUser['name'])) {
  399. return $processUser['name'];
  400. }
  401. }
  402. if (function_exists('exec')) {
  403. exec('whoami', $output);
  404. if (!empty($output[0])) {
  405. return $output[0];
  406. }
  407. }
  408. return _t('install.check.unknown_process_username');
  409. }
  410. // @todo refactor this view with the check_install action
  411. /* check system environment */
  412. function printStep1(): void {
  413. $res = checkRequirements();
  414. ?>
  415. <h2><?= _t('install.check.php') ?></h2>
  416. <noscript><p class="alert alert-warn"><span class="alert-head"><?= _t('gen.short.attention') ?></span> <?= _t('install.javascript_is_better') ?></p></noscript>
  417. <?php
  418. printStep1Template('php', $res['php'], [PHP_VERSION, FRESHRSS_MIN_PHP_VERSION]);
  419. printStep1Template('pdo', $res['pdo']);
  420. $curlVersion = function_exists('curl_version') ? curl_version() : [];
  421. $curlVersion = is_string($curlVersion['version'] ?? null) ? $curlVersion['version'] : '';
  422. printStep1Template('curl', $res['curl'], [$curlVersion]); // TODO: We actually require cURL >= 7.52 for CURLPROXY_HTTPS
  423. printStep1Template('json', $res['json']);
  424. printStep1Template('pcre', $res['pcre']);
  425. printStep1Template('ctype', $res['ctype']);
  426. printStep1Template('dom', $res['dom']);
  427. printStep1Template('xml', $res['xml']);
  428. printStep1Template('mbstring', $res['mbstring']);
  429. printStep1Template('intl', $res['intl']);
  430. printStep1Template('fileinfo', $res['fileinfo']);
  431. printStep1Template('zip', $res['zip']);
  432. if (isset($res['gmp'])) {
  433. printStep1Template('gmp', $res['gmp']);
  434. }
  435. ?>
  436. <h2><?= _t('install.check.files') ?></h2>
  437. <?php
  438. $processUsername = getProcessUsername();
  439. printStep1Template('data', $res['data'], [DATA_PATH, $processUsername]);
  440. printStep1Template('cache', $res['cache'], [CACHE_PATH, $processUsername]);
  441. printStep1Template('tmp', $res['tmp'], [TMP_PATH, $processUsername]);
  442. printStep1Template('users', $res['users'], [USERS_PATH, $processUsername]);
  443. printStep1Template('favicons', $res['favicons'], [DATA_PATH . '/favicons', $processUsername]);
  444. printStep1Template('docroot', $res['docroot']);
  445. ?>
  446. <?php if (freshrss_already_installed() && $res['all'] == 'ok') { ?>
  447. <p class="alert alert-warn"><span class="alert-head"><?= _t('gen.short.attention') ?></span> <?= _t('install.check.already_installed') ?></p>
  448. <div class="form-group form-actions">
  449. <div class="group-controls">
  450. <form action="index.php?step=1" method="post">
  451. <input type="hidden" name="freshrss-keep-install" value="1" />
  452. <button type="submit" class="btn btn-important" tabindex="1"><?= _t('install.action.keep_install') ?></button>
  453. <a class="btn btn-attention confirm" data-str-confirm="<?= _t('install.js.confirm_reinstall') ?>"
  454. href="?step=2" tabindex="2"><?= _t('install.action.reinstall') ?></a>
  455. </form>
  456. </div>
  457. </div>
  458. <?php } elseif ($res['all'] == 'ok') { ?>
  459. <div class="form-group form-actions">
  460. <div class="group-controls">
  461. <a class="btn btn-important" href="?step=2" tabindex="1"><?= _t('install.action.next_step') ?></a>
  462. </div>
  463. </div>
  464. <?php } else { ?>
  465. <p class="alert alert-error"><?= _t('install.action.fix_errors_before') ?></p>
  466. <div class="form-group form-actions">
  467. <div class="group-controls">
  468. <a id="actualize" class="btn" href="./index.php?step=1" title="<?= _t('install.check.reload') ?>" tabindex="1">
  469. <?= _i('refresh') ?>
  470. </a>
  471. </div>
  472. </div>
  473. <?php } ?>
  474. <?php
  475. }
  476. /**
  477. * Select database & configuration
  478. * @throws Minz_ConfigurationNamespaceException
  479. */
  480. function printStep2(): void {
  481. $system_default_config = FreshRSS_SystemConfiguration::get('default_system');
  482. $s2 = checkStep2();
  483. if ($s2['all'] == 'ok') { ?>
  484. <p class="alert alert-success"><span class="alert-head"><?= _t('gen.short.ok') ?></span> <?= _t('install.bdd.conf.ok') ?></p>
  485. <?php } elseif ($s2['conn'] == 'ko') { ?>
  486. <p class="alert alert-error"><span class="alert-head"><?= _t('gen.short.damn') ?></span> <?= _t('install.bdd.conf.ko'),
  487. (empty($_SESSION['db_error']) || !is_string($_SESSION['db_error']) ? '' : ' ' . $_SESSION['db_error']) ?></p>
  488. <?php } ?>
  489. <h2><?= _t('install.bdd.conf') ?></h2>
  490. <form action="index.php?step=2" method="post" autocomplete="off">
  491. <div class="form-group">
  492. <label class="group-name" for="type"><?= _t('install.bdd.type') ?></label>
  493. <div class="group-controls">
  494. <select name="type" id="type" tabindex="1">
  495. <?php if (extension_loaded('pdo_sqlite')) {?>
  496. <option value="sqlite"
  497. <?= ($_SESSION['db_type'] ?? null) === 'sqlite' ? 'selected="selected"' : '' ?>>
  498. SQLite
  499. </option>
  500. <?php }?>
  501. <?php if (extension_loaded('pdo_mysql')) {?>
  502. <option value="mysql"
  503. <?= ($_SESSION['db_type'] ?? null) === 'mysql' ? 'selected="selected"' : '' ?>>
  504. MySQL / MariaDB
  505. </option>
  506. <?php }?>
  507. <?php if (extension_loaded('pdo_pgsql')) {?>
  508. <option value="pgsql"
  509. <?= ($_SESSION['db_type'] ?? null) === 'pgsql' ? 'selected="selected"' : '' ?>>
  510. PostgreSQL
  511. </option>
  512. <?php }?>
  513. </select>
  514. </div>
  515. </div>
  516. <div id="mysql">
  517. <?php
  518. $db_base = is_string($_SESSION['db_base'] ?? null) ? $_SESSION['db_base'] : null;
  519. $db_host = is_string($_SESSION['db_host'] ?? null) ? $_SESSION['db_host'] : null;
  520. $db_password = is_string($_SESSION['db_password'] ?? null) ? $_SESSION['db_password'] : null;
  521. $db_prefix = is_string($_SESSION['db_prefix'] ?? null) ? $_SESSION['db_prefix'] : null;
  522. $db_user = is_string($_SESSION['db_user'] ?? null) ? $_SESSION['db_user'] : null;
  523. ?>
  524. <div class="form-group">
  525. <label class="group-name" for="host"><?= _t('install.bdd.host') ?></label>
  526. <div class="group-controls">
  527. <input type="text" id="host" name="host" pattern="[0-9A-Z\/a-z_.\-]{1,64}(:[0-9]{2,5})?" value="<?=
  528. $db_host ?? $system_default_config->db['host'] ?? '' ?>" tabindex="2" />
  529. </div>
  530. </div>
  531. <div class="form-group">
  532. <label class="group-name" for="user"><?= _t('install.bdd.username') ?></label>
  533. <div class="group-controls">
  534. <input type="text" id="user" name="user" maxlength="64" pattern="[0-9A-Za-z@_.\-]{1,64}" value="<?=
  535. $db_user ?? '' ?>" tabindex="3" />
  536. </div>
  537. </div>
  538. <div class="form-group">
  539. <label class="group-name" for="pass"><?= _t('install.bdd.password') ?></label>
  540. <div class="group-controls">
  541. <div class="stick">
  542. <input type="password" id="pass" name="pass" value="<?=
  543. $db_password ?? '' ?>" tabindex="4" autocomplete="off" />
  544. <a class="btn toggle-password" tabindex="5"><?= FreshRSS_Themes::icon('key') ?></a>
  545. </div>
  546. </div>
  547. </div>
  548. <div class="form-group">
  549. <label class="group-name" for="base"><?= _t('install.bdd') ?></label>
  550. <div class="group-controls">
  551. <input type="text" id="base" name="base" maxlength="64" pattern="[0-9A-Za-z_\-]{1,64}" value="<?=
  552. $db_base ?? '' ?>" tabindex="6" />
  553. </div>
  554. </div>
  555. <div class="form-group">
  556. <label class="group-name" for="prefix"><?= _t('install.bdd.prefix') ?></label>
  557. <div class="group-controls">
  558. <input type="text" id="prefix" name="prefix" maxlength="16" pattern="[0-9A-Za-z_]{1,16}" value="<?=
  559. $db_prefix ?? $system_default_config->db['prefix'] ?? '' ?>" tabindex="7" />
  560. </div>
  561. </div>
  562. </div>
  563. <div class="form-group form-actions">
  564. <div class="group-controls">
  565. <button type="submit" class="btn btn-important" tabindex="8" ><?= _t('gen.action.submit') ?></button>
  566. <button type="reset" class="btn" tabindex="9" ><?= _t('gen.action.cancel') ?></button>
  567. <?php if ($s2['all'] == 'ok') { ?>
  568. <a class="next-step" href="?step=3" tabindex="10" ><?= _t('install.action.next_step') ?></a>
  569. <?php } ?>
  570. </div>
  571. </div>
  572. </form>
  573. <?php
  574. }
  575. function no_auth(string $auth_type): bool {
  576. return !in_array($auth_type, ['form', 'http_auth', 'none'], true);
  577. }
  578. /* Create default user */
  579. function printStep3(): void {
  580. $auth_type = is_string($_SESSION['auth_type'] ?? null) ? $_SESSION['auth_type'] : '';
  581. $default_user = is_string($_SESSION['default_user'] ?? null) ? $_SESSION['default_user'] : '';
  582. $s3 = checkStep3();
  583. if ($s3['all'] == 'ok') { ?>
  584. <p class="alert alert-success"><span class="alert-head"><?= _t('gen.short.ok') ?></span> <?= _t('install.conf.ok') ?></p>
  585. <?php } elseif (!empty($_POST)) { ?>
  586. <p class="alert alert-error"><?= _t('install.fix_errors_before') ?></p>
  587. <?php } ?>
  588. <h2><?= _t('install.conf') ?></h2>
  589. <form action="index.php?step=3" method="post">
  590. <div class="form-group">
  591. <label class="group-name" for="default_user"><?= _t('install.default_user') ?></label>
  592. <div class="group-controls">
  593. <input type="text" id="default_user" name="default_user" autocomplete="username" required="required" size="16"
  594. pattern="<?= FreshRSS_user_Controller::USERNAME_PATTERN ?>" value="<?= $default_user ?>"
  595. placeholder="<?= FreshRSS_http_Util::httpAuthUser(false) == '' ? 'alice' : FreshRSS_http_Util::httpAuthUser(false) ?>" tabindex="1" />
  596. <p class="help"><?= _i('help') ?> <?= _t('install.default_user.max_char') ?></p>
  597. </div>
  598. </div>
  599. <div class="form-group">
  600. <label class="group-name" for="auth_type"><?= _t('admin.auth.type') ?></label>
  601. <div class="group-controls">
  602. <select id="auth_type" name="auth_type" required="required" tabindex="2">
  603. <option value="form"<?= $auth_type === 'form' || (no_auth($auth_type) && FreshRSS_password_Util::cryptAvailable()) ? ' selected="selected"' : '',
  604. FreshRSS_password_Util::cryptAvailable() ? '' : ' disabled="disabled"' ?>><?= _t('admin.auth.form') ?></option>
  605. <option value="http_auth"<?= $auth_type === 'http_auth' ? ' selected="selected"' : '',
  606. FreshRSS_http_Util::httpAuthUser(false) == '' ? ' disabled="disabled"' : '' ?>>
  607. <?= _t('admin.auth.http') ?> (REMOTE_USER = '<?= FreshRSS_http_Util::httpAuthUser(false) ?>')</option>
  608. <option value="none"<?= $auth_type === 'none' || (no_auth($auth_type) && !FreshRSS_password_Util::cryptAvailable()) ? ' selected="selected"' : ''
  609. ?>><?= _t('admin.auth.none') ?></option>
  610. </select>
  611. </div>
  612. </div>
  613. <div class="form-group">
  614. <label class="group-name" for="passwordPlain"><?= _t('admin.user.password_form') ?></label>
  615. <div class="group-controls">
  616. <div class="stick">
  617. <input
  618. type="password" id="passwordPlain" name="passwordPlain"
  619. autocomplete="off" <?= $auth_type === 'form' ? ' required="required"' : '' ?> tabindex="3"
  620. minlength="<?= FreshRSS_FormAuth::MIN_PASSWORD_LENGTH ?>"
  621. pattern=".{<?= FreshRSS_FormAuth::MIN_PASSWORD_LENGTH ?>,<?= FreshRSS_FormAuth::MAX_PASSWORD_LENGTH ?>}"
  622. data-max-bytes="<?= FreshRSS_FormAuth::MAX_PASSWORD_LENGTH ?>" />
  623. <button type="button" class="btn toggle-password" tabindex="4"><?= FreshRSS_Themes::icon('key') ?></button>
  624. </div>
  625. <p class="help"><?= _i('help') ?> <?= _t('admin.user.password_format') ?></p>
  626. </div>
  627. </div>
  628. <div class="form-group form-actions">
  629. <div class="group-controls">
  630. <button type="submit" class="btn btn-important" tabindex="5" ><?= _t('gen.action.submit') ?></button>
  631. <button type="reset" class="btn" tabindex="6" ><?= _t('gen.action.cancel') ?></button>
  632. <?php if ($s3['all'] == 'ok') { ?>
  633. <a class="next-step" href="?step=4" tabindex="7" ><?= _t('install.action.next_step') ?></a>
  634. <?php } ?>
  635. </div>
  636. </div>
  637. </form>
  638. <?php
  639. }
  640. /* congrats. Installation successful completed */
  641. function printStep4(): void {
  642. ?>
  643. <p class="alert alert-success"><span class="alert-head"><?= _t('install.congratulations') ?></span> <?= _t('install.ok') ?></p>
  644. <div class="form-group form-actions">
  645. <div class="group-controls">
  646. <a class="btn btn-important" href="?step=5" tabindex="1"><?= _t('install.action.finish') ?></a>
  647. </div>
  648. </div>
  649. <?php
  650. }
  651. /* failed */
  652. function printStep5(): void {
  653. ?>
  654. <p class="alert alert-error">
  655. <span class="alert-head"><?= _t('gen.short.damn') ?></span>
  656. <?= _t('install.missing_applied_migrations', DATA_PATH . '/applied_migrations.txt') ?>
  657. </p>
  658. <?php
  659. }
  660. initTranslate();
  661. checkStep();
  662. switch (STEP) {
  663. case 0:
  664. default:
  665. saveLanguage();
  666. break;
  667. case 1:
  668. saveStep1();
  669. break;
  670. case 2:
  671. saveStep2();
  672. break;
  673. case 3:
  674. saveStep3();
  675. break;
  676. case 4:
  677. break;
  678. case 5:
  679. if (setupMigrations()) {
  680. header('Location: index.php');
  681. }
  682. break;
  683. }
  684. ?>
  685. <!DOCTYPE html>
  686. <html <?php
  687. if (_t('gen.dir') === 'rtl') {
  688. echo ' dir="rtl" class="rtl"';
  689. }
  690. ?>>
  691. <head>
  692. <meta charset="UTF-8" />
  693. <meta name="viewport" content="initial-scale=1.0" />
  694. <script id="jsonVars" type="application/json">
  695. <?= json_encode([
  696. 'i18n' => [
  697. 'provided_password_too_long' => _t('user.password.provided_password_too_long'),
  698. ]
  699. ], JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP) ?>
  700. </script>
  701. <title><?= _t('install.title') ?>: <?= _t('install.step', STEP + 1) ?></title>
  702. <link rel="stylesheet" href="../themes/base-theme/frss.css?<?= @filemtime(PUBLIC_PATH . '/themes/base-theme/frss.css') ?>" />
  703. <link rel="stylesheet" href="../themes/Origine/origine.css?<?= @filemtime(PUBLIC_PATH . '/themes/Origine/origine.css') ?>" />
  704. <link rel="shortcut icon" id="favicon" type="image/x-icon" sizes="16x16 64x64" href="../favicon.ico" />
  705. <link rel="icon msapplication-TileImage apple-touch-icon" type="image/png" sizes="256x256" href="../themes/icons/favicon-256.png" />
  706. <link rel="apple-touch-icon" href="../themes/icons/apple-touch-icon.png" />
  707. <meta name="apple-mobile-web-app-capable" content="yes" />
  708. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  709. <meta name="apple-mobile-web-app-title" content="FreshRSS">
  710. <meta name="robots" content="noindex,nofollow" />
  711. </head>
  712. <body>
  713. <header class="header">
  714. <div class="item title">
  715. <div id="logo-wrapper">
  716. <a href="./">
  717. <img class="logo" src="../themes/icons/FreshRSS-logo.svg" alt="" loading="lazy">
  718. </a>
  719. </div>
  720. </div>
  721. <div class="item"></div>
  722. <div class="item configure">
  723. <a class="btn only-mobile" href="#aside"><?= _i('view-normal') ?></a>
  724. </div>
  725. </header>
  726. <div id="global">
  727. <nav class="nav nav-list aside" id="aside">
  728. <a class="toggle_aside" href="#close"><?= _i('close') ?></a>
  729. <ul>
  730. <li class="item nav-section">
  731. <div class="nav-header"><?= _t('install.steps') ?></div>
  732. <ol>
  733. <li class="item<?= STEP == 0 ? ' active' : '' ?>">
  734. <a href="?step=0" title="<?= _t('install.step', 0) ?>: <?= _t('install.language') ?>"><?= _t('install.language') ?></a>
  735. </li>
  736. <li class="item<?= STEP == 1 ? ' active' : '' ?>">
  737. <?php if (STEP > 0) {?>
  738. <a href="?step=1" title="<?= _t('install.step', 1) ?>: <?= _t('install.check') ?>"><?= _t('install.check') ?></a>
  739. <?php } else { ?>
  740. <span><?= _t('install.check') ?></span>
  741. <?php } ?>
  742. </li>
  743. <li class="item<?= STEP == 2 ? ' active' : '' ?>">
  744. <?php if (STEP > 1) {?>
  745. <a href="?step=2" title="<?= _t('install.step', 2) ?>: <?= _t('install.bdd.conf') ?>"><?= _t('install.bdd.conf') ?></a>
  746. <?php } else { ?>
  747. <span><?= _t('install.bdd.conf') ?></span>
  748. <?php } ?>
  749. </li>
  750. <li class="item<?= STEP == 3 ? ' active' : '' ?>">
  751. <?php if (STEP > 2) {?>
  752. <a href="?step=3" title="<?= _t('install.step', 3) ?>: <?= _t('install.conf') ?>"><?= _t('install.conf') ?></a>
  753. <?php } else { ?>
  754. <span><?= _t('install.conf') ?></span>
  755. <?php } ?>
  756. </li>
  757. <li class="item<?= STEP == 4 ? ' active' : '' ?>">
  758. <?php if (STEP > 3) {?>
  759. <a href="?step=4" title="<?= _t('install.step', 4) ?>: <?= _t('install.this_is_the_end') ?>"><?= _t('install.this_is_the_end') ?></a>
  760. <?php } else { ?>
  761. <span><?= _t('install.this_is_the_end') ?></span>
  762. <?php } ?>
  763. </li>
  764. </ol>
  765. </li>
  766. </ul>
  767. </nav>
  768. <a class="close-aside" href="#close">❌</a>
  769. <main class="post">
  770. <h1><?= _t('install.title') ?>: <?= _t('install.step', STEP + 1) ?></h1>
  771. <?php
  772. switch (STEP) {
  773. case 0:
  774. default:
  775. printStep0();
  776. break;
  777. case 1:
  778. printStep1();
  779. break;
  780. case 2:
  781. printStep2();
  782. break;
  783. case 3:
  784. printStep3();
  785. break;
  786. case 4:
  787. printStep4();
  788. break;
  789. case 5:
  790. printStep5();
  791. break;
  792. }
  793. ?>
  794. </main>
  795. </div>
  796. <script src="../scripts/install.js?<?= @filemtime(PUBLIC_PATH . '/scripts/install.js') ?>"></script>
  797. <script src="../scripts/extra.js?<?= @filemtime(PUBLIC_PATH . '/scripts/extra.js') ?>"></script>
  798. </body>
  799. </html>