install.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. <?php
  2. require('../../constants.php');
  3. include(LIB_PATH . '/lib_rss.php');
  4. session_name('FreshRSS');
  5. session_set_cookie_params(0, dirname(empty($_SERVER['REQUEST_URI']) ? '/' : dirname($_SERVER['REQUEST_URI'])), null, false, true);
  6. session_start();
  7. if (isset ($_GET['step'])) {
  8. define ('STEP', $_GET['step']);
  9. } else {
  10. define ('STEP', 1);
  11. }
  12. define('SQL_CREATE_DB', 'CREATE DATABASE IF NOT EXISTS %1$s DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;');
  13. include(APP_PATH . '/sql.php');
  14. //<updates>
  15. define('SQL_SHOW_TABLES', 'SHOW tables;');
  16. define('SQL_BACKUP006', 'RENAME TABLE `%1$scategory` TO `%1$scategory006`, `%1$sfeed` TO `%1$sfeed006`, `%1$sentry` TO `%1$sentry006`;');
  17. define('SQL_SHOW_COLUMNS_UPDATEv006', 'SHOW columns FROM `%1$sentry006` LIKE "id2";');
  18. define('SQL_UPDATEv006', '
  19. ALTER TABLE `%1$scategory006` ADD id2 SMALLINT;
  20. SET @i = 0;
  21. UPDATE `%1$scategory006` SET id2=(@i:=@i+1) ORDER BY id;
  22. ALTER TABLE `%1$sfeed006` ADD id2 SMALLINT, ADD category2 SMALLINT;
  23. SET @i = 0;
  24. UPDATE `%1$sfeed006` SET id2=(@i:=@i+1) ORDER BY name;
  25. UPDATE `%1$sfeed006` f
  26. INNER JOIN `%1$scategory006` c ON f.category = c.id
  27. SET f.category2 = c.id2;
  28. INSERT IGNORE INTO `%2$scategory` (name, color)
  29. SELECT name, color
  30. FROM `%1$scategory006`
  31. ORDER BY id2;
  32. INSERT IGNORE INTO `%2$sfeed` (url, category, name, website, description, priority, pathEntries, httpAuth, keep_history)
  33. SELECT url, category2, name, website, description, priority, pathEntries, httpAuth, IF(keep_history = 1, -1, -2)
  34. FROM `%1$sfeed006`
  35. ORDER BY id2;
  36. ALTER TABLE `%1$sentry006` ADD id2 bigint;
  37. UPDATE `%1$sentry006` SET id2 = ((date * 1000000) + (rand() * 100000000));
  38. INSERT IGNORE INTO `%2$sentry` (id, guid, title, author, link, date, is_read, is_favorite, id_feed, tags)
  39. SELECT e0.id2, e0.guid, e0.title, e0.author, e0.link, e0.date, e0.is_read, e0.is_favorite, f0.id2, e0.tags
  40. FROM `%1$sentry006` e0
  41. INNER JOIN `%1$sfeed006` f0 ON e0.id_feed = f0.id;
  42. ');
  43. define('SQL_CONVERT_SELECTv006', '
  44. SELECT e0.id2, e0.content
  45. FROM `%1$sentry006` e0
  46. INNER JOIN `%2$sentry` e1 ON e0.id2 = e1.id
  47. WHERE e1.content_bin IS NULL');
  48. define('SQL_CONVERT_UPDATEv006', 'UPDATE `%1$sentry` SET content_bin=COMPRESS(?) WHERE id=?;');
  49. define('SQL_DROP_BACKUPv006', 'DROP TABLE IF EXISTS `%1$sentry006`, `%1$sfeed006`, `%1$scategory006`;');
  50. define('SQL_UPDATE_CACHED_VALUES', '
  51. UPDATE `%1$sfeed` f
  52. INNER JOIN (
  53. SELECT e.id_feed,
  54. COUNT(CASE WHEN e.is_read = 0 THEN 1 END) AS nbUnreads,
  55. COUNT(e.id) AS nbEntries
  56. FROM `%1$sentry` e
  57. GROUP BY e.id_feed
  58. ) x ON x.id_feed=f.id
  59. SET f.cache_nbEntries=x.nbEntries, f.cache_nbUnreads=x.nbUnreads
  60. ');
  61. define('SQL_UPDATE_HISTORYv007b', 'UPDATE `%1$sfeed` SET keep_history = CASE WHEN keep_history = 0 THEN -2 WHEN keep_history = 1 THEN -1 ELSE keep_history END;');
  62. //</updates>
  63. // gestion internationalisation
  64. $translates = array ();
  65. $actual = 'en';
  66. function initTranslate () {
  67. global $translates;
  68. global $actual;
  69. $actual = isset($_SESSION['language']) ? $_SESSION['language'] : getBetterLanguage('en');
  70. $file = APP_PATH . '/i18n/' . $actual . '.php';
  71. if (file_exists($file)) {
  72. $translates = array_merge($translates, include($file));
  73. }
  74. $file = APP_PATH . '/i18n/install.' . $actual . '.php';
  75. if (file_exists($file)) {
  76. $translates = array_merge($translates, include($file));
  77. }
  78. }
  79. function getBetterLanguage ($fallback) {
  80. $available = availableLanguages ();
  81. $accept = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  82. $language = strtolower (substr ($accept, 0, 2));
  83. if (isset ($available[$language])) {
  84. return $language;
  85. } else {
  86. return $fallback;
  87. }
  88. }
  89. function availableLanguages () {
  90. return array (
  91. 'en' => 'English',
  92. 'fr' => 'Français'
  93. );
  94. }
  95. function _t ($key) {
  96. global $translates;
  97. $translate = $key;
  98. if (isset ($translates[$key])) {
  99. $translate = $translates[$key];
  100. }
  101. $args = func_get_args ();
  102. unset($args[0]);
  103. return vsprintf ($translate, $args);
  104. }
  105. /*** SAUVEGARDES ***/
  106. function saveLanguage () {
  107. if (!empty ($_POST)) {
  108. if (!isset ($_POST['language'])) {
  109. return false;
  110. }
  111. $_SESSION['language'] = $_POST['language'];
  112. header ('Location: index.php?step=1');
  113. }
  114. }
  115. function saveStep2 () {
  116. if (!empty ($_POST)) {
  117. if (empty ($_POST['title']) ||
  118. empty ($_POST['old_entries']) ||
  119. empty ($_POST['default_user'])) {
  120. return false;
  121. }
  122. $_SESSION['salt'] = sha1(uniqid(mt_rand(), true).implode('', stat(__FILE__)));
  123. $_SESSION['title'] = substr(trim($_POST['title']), 0, 25);
  124. $_SESSION['old_entries'] = $_POST['old_entries'];
  125. if ((!ctype_digit($_SESSION['old_entries'])) || ($_SESSION['old_entries'] < 1)) {
  126. $_SESSION['old_entries'] = 3;
  127. }
  128. $_SESSION['mail_login'] = filter_var($_POST['mail_login'], FILTER_VALIDATE_EMAIL);
  129. $_SESSION['default_user'] = substr(preg_replace('/[^a-zA-Z0-9]/', '', $_POST['default_user']), 0, 16);
  130. $token = '';
  131. if ($_SESSION['mail_login']) {
  132. $token = sha1($_SESSION['salt'] . $_SESSION['mail_login']);
  133. }
  134. $config_array = array (
  135. 'language' => $_SESSION['language'],
  136. 'old_entries' => $_SESSION['old_entries'],
  137. 'mail_login' => $_SESSION['mail_login'],
  138. 'token' => $token,
  139. );
  140. $configPath = DATA_PATH . '/' . $_SESSION['default_user'] . '_user.php';
  141. @unlink($configPath); //To avoid access-rights problems
  142. file_put_contents($configPath, "<?php\n return " . var_export($config_array, true) . ';');
  143. if ($_SESSION['mail_login'] != '') {
  144. $personaFile = DATA_PATH . '/persona/' . $_SESSION['mail_login'] . '.txt';
  145. @unlink($personaFile);
  146. file_put_contents($personaFile, $_SESSION['default_user']);
  147. }
  148. header ('Location: index.php?step=3');
  149. }
  150. }
  151. function saveStep3 () {
  152. if (!empty ($_POST)) {
  153. if (empty ($_POST['type']) ||
  154. empty ($_POST['host']) ||
  155. empty ($_POST['user']) ||
  156. empty ($_POST['base'])) {
  157. $_SESSION['bd_error'] = 'Missing parameters!';
  158. }
  159. $_SESSION['bd_type'] = isset ($_POST['type']) ? $_POST['type'] : 'mysql';
  160. $_SESSION['bd_host'] = $_POST['host'];
  161. $_SESSION['bd_user'] = $_POST['user'];
  162. $_SESSION['bd_password'] = $_POST['pass'];
  163. $_SESSION['bd_base'] = substr($_POST['base'], 0, 64);
  164. $_SESSION['bd_prefix'] = substr($_POST['prefix'], 0, 16);
  165. $_SESSION['bd_prefix_user'] = $_SESSION['bd_prefix'] . (empty($_SESSION['default_user']) ? '' : ($_SESSION['default_user'] . '_'));
  166. $ini_array = array(
  167. 'general' => array(
  168. 'environment' => empty($_SESSION['environment']) ? 'production' : $_SESSION['environment'],
  169. 'use_url_rewriting' => false,
  170. 'salt' => $_SESSION['salt'],
  171. 'base_url' => '',
  172. 'title' => $_SESSION['title'],
  173. 'default_user' => $_SESSION['default_user'],
  174. 'auth_type' => $_SESSION['auth_type'],
  175. 'allow_anonymous' => $_SESSION['allow_anonymous'],
  176. ),
  177. 'db' => array(
  178. 'type' => $_SESSION['bd_type'],
  179. 'host' => $_SESSION['bd_host'],
  180. 'user' => $_SESSION['bd_user'],
  181. 'password' => $_SESSION['bd_password'],
  182. 'base' => $_SESSION['bd_base'],
  183. 'prefix' => $_SESSION['bd_prefix'],
  184. ),
  185. );
  186. @unlink(DATA_PATH . '/config.php'); //To avoid access-rights problems
  187. file_put_contents(DATA_PATH . '/config.php', "<?php\n return " . var_export($ini_array, true) . ';');
  188. if (file_exists(DATA_PATH . '/config.php') && file_exists(DATA_PATH . '/application.ini')) {
  189. @unlink(DATA_PATH . '/application.ini'); //v0.6
  190. }
  191. $res = checkBD ();
  192. if ($res) {
  193. $_SESSION['bd_error'] = '';
  194. header ('Location: index.php?step=4');
  195. } elseif (empty($_SESSION['bd_error'])) {
  196. $_SESSION['bd_error'] = 'Unknown error!';
  197. }
  198. }
  199. invalidateHttpCache();
  200. }
  201. function updateDatabase($perform = false) {
  202. $needs = array('bd_type', 'bd_host', 'bd_base', 'bd_user', 'bd_password', 'bd_prefix', 'bd_prefix_user');
  203. foreach ($needs as $need) {
  204. if (!isset($_SESSION[$need])) {
  205. return false;
  206. }
  207. }
  208. try {
  209. $str = '';
  210. switch ($_SESSION['bd_type']) {
  211. case 'mysql':
  212. $str = 'mysql:host=' . $_SESSION['bd_host'] . ';dbname=' . $_SESSION['bd_base'];
  213. $driver_options = array(
  214. PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
  215. );
  216. break;
  217. case 'sqlite':
  218. $str = 'sqlite:' . DATA_PATH . $_SESSION['bd_base'] . '.sqlite';
  219. $driver_options = null;
  220. break;
  221. default:
  222. return false;
  223. }
  224. $c = new PDO($str, $_SESSION['bd_user'], $_SESSION['bd_password'], $driver_options);
  225. $stm = $c->prepare(SQL_SHOW_TABLES);
  226. $stm->execute();
  227. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  228. if (!in_array($_SESSION['bd_prefix'] . 'entry006', $res)) {
  229. return false;
  230. }
  231. $sql = sprintf(SQL_SHOW_COLUMNS_UPDATEv006, $_SESSION['bd_prefix']);
  232. $stm = $c->prepare($sql);
  233. $stm->execute();
  234. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  235. if (!in_array('id2', $res)) {
  236. if (!$perform) {
  237. return true;
  238. }
  239. $sql = sprintf(SQL_UPDATEv006, $_SESSION['bd_prefix'], $_SESSION['bd_prefix_user']);
  240. $stm = $c->prepare($sql, array(PDO::ATTR_EMULATE_PREPARES => true));
  241. $stm->execute();
  242. }
  243. $sql = sprintf(SQL_UPDATE_HISTORYv007b, $_SESSION['bd_prefix_user']);
  244. $stm = $c->prepare($sql);
  245. $stm->execute();
  246. $sql = sprintf(SQL_UPDATE_CACHED_VALUES, $_SESSION['bd_prefix_user']);
  247. $stm = $c->prepare($sql);
  248. $stm->execute();
  249. $sql = sprintf(SQL_CONVERT_SELECTv006, $_SESSION['bd_prefix'], $_SESSION['bd_prefix_user']);
  250. if (!$perform) {
  251. $sql .= ' LIMIT 1';
  252. }
  253. $stm = $c->prepare($sql);
  254. $stm->execute();
  255. if (!$perform) {
  256. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  257. return count($res) > 0;
  258. } else {
  259. @set_time_limit(300);
  260. }
  261. $c2 = new PDO($str, $_SESSION['bd_user'], $_SESSION['bd_password'], $driver_options);
  262. $sql = sprintf(SQL_CONVERT_UPDATEv006, $_SESSION['bd_prefix_user']);
  263. $stm2 = $c2->prepare($sql);
  264. while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
  265. $id = $row['id2'];
  266. $content = unserialize(gzinflate(base64_decode($row['content'])));
  267. $stm2->execute(array($content, $id));
  268. }
  269. return true;
  270. } catch (PDOException $e) {
  271. return false;
  272. }
  273. return false;
  274. }
  275. function deleteInstall () {
  276. $res = unlink (INDEX_PATH . '/install.php');
  277. if ($res) {
  278. header ('Location: index.php');
  279. }
  280. $needs = array('bd_type', 'bd_host', 'bd_base', 'bd_user', 'bd_password', 'bd_prefix');
  281. foreach ($needs as $need) {
  282. if (!isset($_SESSION[$need])) {
  283. return false;
  284. }
  285. }
  286. try {
  287. switch ($_SESSION['bd_type']) {
  288. case 'mysql':
  289. $str = 'mysql:host=' . $_SESSION['bd_host'] . ';dbname=' . $_SESSION['bd_base'];
  290. $driver_options = array(
  291. PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
  292. );
  293. break;
  294. case 'sqlite':
  295. $str = 'sqlite:' . DATA_PATH . $_SESSION['bd_base'] . '.sqlite';
  296. $driver_options = null;
  297. break;
  298. default:
  299. return false;
  300. }
  301. $c = new PDO($str, $_SESSION['bd_user'], $_SESSION['bd_password'], $driver_options);
  302. $sql = sprintf(SQL_DROP_BACKUPv006, $_SESSION['bd_prefix']);
  303. $stm = $c->prepare($sql);
  304. $stm->execute();
  305. return true;
  306. } catch (PDOException $e) {
  307. return false;
  308. }
  309. return false;
  310. }
  311. function moveOldFiles() {
  312. $mvs = array(
  313. '/app/configuration/application.ini' => '/data/application.ini', //v0.6
  314. '/public/data/Configuration.array.php' => '/data/Configuration.array.php', //v0.6
  315. );
  316. $ok = true;
  317. foreach ($mvs as $fFrom => $fTo) {
  318. if (file_exists(FRESHRSS_PATH . $fFrom)) {
  319. if (copy(FRESHRSS_PATH . $fFrom, FRESHRSS_PATH . $fTo)) {
  320. @unlink(FRESHRSS_PATH . $fFrom);
  321. } else {
  322. $ok = false;
  323. }
  324. }
  325. }
  326. return $ok;
  327. }
  328. function delTree($dir) { //http://php.net/rmdir#110489
  329. if (!is_dir($dir)) {
  330. return true;
  331. }
  332. $files = array_diff(scandir($dir), array('.', '..'));
  333. foreach ($files as $file) {
  334. $f = $dir . '/' . $file;
  335. if (is_dir($f)) {
  336. @chmod($f, 0777);
  337. delTree($f);
  338. }
  339. else unlink($f);
  340. }
  341. return rmdir($dir);
  342. }
  343. /*** VÉRIFICATIONS ***/
  344. function checkStep () {
  345. $s0 = checkStep0 ();
  346. $s1 = checkStep1 ();
  347. $s2 = checkStep2 ();
  348. $s3 = checkStep3 ();
  349. if (STEP > 0 && $s0['all'] != 'ok') {
  350. header ('Location: index.php?step=0');
  351. } elseif (STEP > 1 && $s1['all'] != 'ok') {
  352. header ('Location: index.php?step=1');
  353. } elseif (STEP > 2 && $s2['all'] != 'ok') {
  354. header ('Location: index.php?step=2');
  355. } elseif (STEP > 3 && $s3['all'] != 'ok') {
  356. header ('Location: index.php?step=3');
  357. }
  358. $_SESSION['actualize_feeds'] = true;
  359. }
  360. function checkStep0 () {
  361. moveOldFiles();
  362. if (file_exists(DATA_PATH . '/config.php')) {
  363. $ini_array = include(DATA_PATH . '/config.php');
  364. } elseif (file_exists(DATA_PATH . '/application.ini')) { //v0.6
  365. $ini_array = parse_ini_file(DATA_PATH . '/application.ini', true);
  366. $ini_array['general']['title'] = empty($ini_array['general']['title']) ? '' : stripslashes($ini_array['general']['title']);
  367. } else {
  368. $ini_array = null;
  369. }
  370. if ($ini_array) {
  371. $ini_general = isset($ini_array['general']) ? $ini_array['general'] : null;
  372. if ($ini_general) {
  373. $keys = array('environment', 'salt', 'title', 'default_user', 'allow_anonymous', 'auth_type');
  374. foreach ($keys as $key) {
  375. if ((empty($_SESSION[$key])) && isset($ini_general[$key])) {
  376. $_SESSION[$key] = $ini_general[$key];
  377. }
  378. }
  379. }
  380. $ini_db = isset($ini_array['db']) ? $ini_array['db'] : null;
  381. if ($ini_db) {
  382. $keys = array('type', 'host', 'user', 'password', 'base', 'prefix');
  383. foreach ($keys as $key) {
  384. if ((!isset($_SESSION['bd_' . $key])) && isset($ini_db[$key])) {
  385. $_SESSION['bd_' . $key] = $ini_db[$key];
  386. }
  387. }
  388. }
  389. }
  390. if (isset($_SESSION['default_user']) && file_exists(DATA_PATH . '/' . $_SESSION['default_user'] . '_user.php')) {
  391. $userConfig = include(DATA_PATH . '/' . $_SESSION['default_user'] . '_user.php');
  392. } elseif (file_exists(DATA_PATH . '/Configuration.array.php')) {
  393. $userConfig = include(DATA_PATH . '/Configuration.array.php'); //v0.6
  394. if (empty($_SESSION['auth_type'])) {
  395. $_SESSION['auth_type'] = empty($userConfig['mail_login']) ? 'none' : 'persona';
  396. }
  397. if (!isset($_SESSION['allow_anonymous'])) {
  398. $_SESSION['allow_anonymous'] = empty($userConfig['anon_access']) ? false : ($userConfig['anon_access'] === 'yes');
  399. }
  400. } else {
  401. $userConfig = array();
  402. }
  403. $keys = array('language', 'old_entries', 'mail_login');
  404. foreach ($keys as $key) {
  405. if ((!isset($_SESSION[$key])) && isset($userConfig[$key])) {
  406. $_SESSION[$key] = $userConfig[$key];
  407. }
  408. }
  409. $languages = availableLanguages ();
  410. $language = isset ($_SESSION['language']) &&
  411. isset ($languages[$_SESSION['language']]);
  412. return array (
  413. 'language' => $language ? 'ok' : 'ko',
  414. 'all' => $language ? 'ok' : 'ko'
  415. );
  416. }
  417. function checkStep1 () {
  418. $php = version_compare (PHP_VERSION, '5.2.0') >= 0;
  419. $minz = file_exists (LIB_PATH . '/Minz');
  420. $curl = extension_loaded ('curl');
  421. $pdo = extension_loaded ('pdo_mysql');
  422. $pcre = extension_loaded ('pcre');
  423. $ctype = extension_loaded ('ctype');
  424. $dom = class_exists('DOMDocument');
  425. $data = DATA_PATH && is_writable (DATA_PATH);
  426. $cache = CACHE_PATH && is_writable (CACHE_PATH);
  427. $log = LOG_PATH && is_writable (LOG_PATH);
  428. $favicons = is_writable (DATA_PATH . '/favicons');
  429. $persona = is_writable (DATA_PATH . '/persona');
  430. return array (
  431. 'php' => $php ? 'ok' : 'ko',
  432. 'minz' => $minz ? 'ok' : 'ko',
  433. 'curl' => $curl ? 'ok' : 'ko',
  434. 'pdo-mysql' => $pdo ? 'ok' : 'ko',
  435. 'pcre' => $pcre ? 'ok' : 'ko',
  436. 'ctype' => $ctype ? 'ok' : 'ko',
  437. 'dom' => $dom ? 'ok' : 'ko',
  438. 'data' => $data ? 'ok' : 'ko',
  439. 'cache' => $cache ? 'ok' : 'ko',
  440. 'log' => $log ? 'ok' : 'ko',
  441. 'favicons' => $favicons ? 'ok' : 'ko',
  442. 'persona' => $persona ? 'ok' : 'ko',
  443. 'all' => $php && $minz && $curl && $pdo && $pcre && $ctype && $dom && $data && $cache && $log && $favicons && $persona ? 'ok' : 'ko'
  444. );
  445. }
  446. function checkStep2 () {
  447. $conf = !empty($_SESSION['salt']) &&
  448. !empty($_SESSION['title']) &&
  449. !empty($_SESSION['old_entries']) &&
  450. isset($_SESSION['mail_login']) &&
  451. !empty($_SESSION['default_user']);
  452. $defaultUser = empty($_POST['default_user']) ? null : $_POST['default_user'];
  453. if ($defaultUser === null) {
  454. $defaultUser = empty($_SESSION['default_user']) ? '' : $_SESSION['default_user'];
  455. }
  456. $data = is_writable(DATA_PATH . '/' . $defaultUser . '_user.php');
  457. if ($data) {
  458. @unlink(DATA_PATH . '/Configuration.array.php'); //v0.6
  459. }
  460. return array (
  461. 'conf' => $conf ? 'ok' : 'ko',
  462. 'data' => $data ? 'ok' : 'ko',
  463. 'all' => $conf && $data ? 'ok' : 'ko'
  464. );
  465. }
  466. function checkStep3 () {
  467. $conf = is_writable(DATA_PATH . '/config.php');
  468. $bd = isset ($_SESSION['bd_type']) &&
  469. isset ($_SESSION['bd_host']) &&
  470. isset ($_SESSION['bd_user']) &&
  471. isset ($_SESSION['bd_password']) &&
  472. isset ($_SESSION['bd_base']) &&
  473. isset ($_SESSION['bd_prefix']) &&
  474. isset ($_SESSION['bd_error']);
  475. $conn = empty($_SESSION['bd_error']);
  476. return array (
  477. 'bd' => $bd ? 'ok' : 'ko',
  478. 'conn' => $conn ? 'ok' : 'ko',
  479. 'conf' => $conf ? 'ok' : 'ko',
  480. 'all' => $bd && $conn && $conf ? 'ok' : 'ko'
  481. );
  482. }
  483. function checkBD () {
  484. $ok = false;
  485. try {
  486. $str = '';
  487. $driver_options = null;
  488. switch ($_SESSION['bd_type']) {
  489. case 'mysql':
  490. $driver_options = array(
  491. PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
  492. );
  493. try { // on ouvre une connexion juste pour créer la base si elle n'existe pas
  494. $str = 'mysql:host=' . $_SESSION['bd_host'] . ';';
  495. $c = new PDO ($str, $_SESSION['bd_user'], $_SESSION['bd_password'], $driver_options);
  496. $sql = sprintf (SQL_CREATE_DB, $_SESSION['bd_base']);
  497. $res = $c->query ($sql);
  498. } catch (PDOException $e) {
  499. }
  500. // on écrase la précédente connexion en sélectionnant la nouvelle BDD
  501. $str = 'mysql:host=' . $_SESSION['bd_host'] . ';dbname=' . $_SESSION['bd_base'];
  502. break;
  503. case 'sqlite':
  504. $str = 'sqlite:' . DATA_PATH . $_SESSION['bd_base'] . '.sqlite';
  505. break;
  506. default:
  507. return false;
  508. }
  509. $c = new PDO ($str, $_SESSION['bd_user'], $_SESSION['bd_password'], $driver_options);
  510. $stm = $c->prepare(SQL_SHOW_TABLES);
  511. $stm->execute();
  512. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  513. if (in_array($_SESSION['bd_prefix'] . 'entry', $res) && !in_array($_SESSION['bd_prefix'] . 'entry006', $res)) {
  514. $sql = sprintf(SQL_BACKUP006, $_SESSION['bd_prefix']); //v0.6
  515. $res = $c->query($sql); //Backup tables
  516. }
  517. $sql = sprintf(SQL_CREATE_TABLES, $_SESSION['bd_prefix_user']);
  518. $stm = $c->prepare($sql, array(PDO::ATTR_EMULATE_PREPARES => true));
  519. $values = array(
  520. 'catName' => _t('default_category'),
  521. );
  522. $ok = $stm->execute($values);
  523. } catch (PDOException $e) {
  524. $ok = false;
  525. $_SESSION['bd_error'] = $e->getMessage();
  526. }
  527. if (!$ok) {
  528. @unlink(DATA_PATH . '/config.php');
  529. }
  530. return $ok;
  531. }
  532. /*** AFFICHAGE ***/
  533. function printStep0 () {
  534. global $actual;
  535. ?>
  536. <?php $s0 = checkStep0 (); if ($s0['all'] == 'ok') { ?>
  537. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('ok'); ?></span> <?php echo _t ('language_defined'); ?></p>
  538. <?php } ?>
  539. <form action="index.php?step=0" method="post">
  540. <legend><?php echo _t ('choose_language'); ?></legend>
  541. <div class="form-group">
  542. <label class="group-name" for="language"><?php echo _t ('language'); ?></label>
  543. <div class="group-controls">
  544. <select name="language" id="language">
  545. <?php $languages = availableLanguages (); ?>
  546. <?php foreach ($languages as $short => $lib) { ?>
  547. <option value="<?php echo $short; ?>"<?php echo $actual == $short ? ' selected="selected"' : ''; ?>><?php echo $lib; ?></option>
  548. <?php } ?>
  549. </select>
  550. </div>
  551. </div>
  552. <div class="form-group form-actions">
  553. <div class="group-controls">
  554. <button type="submit" class="btn btn-important"><?php echo _t ('save'); ?></button>
  555. <button type="reset" class="btn"><?php echo _t ('cancel'); ?></button>
  556. <?php if ($s0['all'] == 'ok') { ?>
  557. <a class="btn btn-important next-step" href="?step=1"><?php echo _t ('next_step'); ?></a>
  558. <?php } ?>
  559. </div>
  560. </div>
  561. </form>
  562. <?php
  563. }
  564. function printStep1 () {
  565. $res = checkStep1 ();
  566. ?>
  567. <noscript><p class="alert alert-warn"><span class="alert-head"><?php echo _t ('attention'); ?></span> <?php echo _t ('javascript_is_better'); ?></p></noscript>
  568. <?php if ($res['php'] == 'ok') { ?>
  569. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('ok'); ?></span> <?php echo _t ('php_is_ok', PHP_VERSION); ?></p>
  570. <?php } else { ?>
  571. <p class="alert alert-error"><span class="alert-head"><?php echo _t ('damn'); ?></span> <?php echo _t ('php_is_nok', PHP_VERSION, '5.1.0'); ?></p>
  572. <?php } ?>
  573. <?php if ($res['minz'] == 'ok') { ?>
  574. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('ok'); ?></span> <?php echo _t ('minz_is_ok'); ?></p>
  575. <?php } else { ?>
  576. <p class="alert alert-error"><span class="alert-head"><?php echo _t ('damn'); ?></span> <?php echo _t ('minz_is_nok', LIB_PATH . '/Minz'); ?></p>
  577. <?php } ?>
  578. <?php if ($res['pdo-mysql'] == 'ok') { ?>
  579. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('ok'); ?></span> <?php echo _t ('pdomysql_is_ok'); ?></p>
  580. <?php } else { ?>
  581. <p class="alert alert-error"><span class="alert-head"><?php echo _t ('damn'); ?></span> <?php echo _t ('pdomysql_is_nok'); ?></p>
  582. <?php } ?>
  583. <?php if ($res['curl'] == 'ok') { ?>
  584. <?php $version = curl_version(); ?>
  585. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('ok'); ?></span> <?php echo _t ('curl_is_ok', $version['version']); ?></p>
  586. <?php } else { ?>
  587. <p class="alert alert-error"><span class="alert-head"><?php echo _t ('damn'); ?></span> <?php echo _t ('curl_is_nok'); ?></p>
  588. <?php } ?>
  589. <?php if ($res['pcre'] == 'ok') { ?>
  590. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('ok'); ?></span> <?php echo _t ('pcre_is_ok'); ?></p>
  591. <?php } else { ?>
  592. <p class="alert alert-error"><span class="alert-head"><?php echo _t ('damn'); ?></span> <?php echo _t ('pcre_is_nok'); ?></p>
  593. <?php } ?>
  594. <?php if ($res['ctype'] == 'ok') { ?>
  595. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('ok'); ?></span> <?php echo _t ('ctype_is_ok'); ?></p>
  596. <?php } else { ?>
  597. <p class="alert alert-error"><span class="alert-head"><?php echo _t ('damn'); ?></span> <?php echo _t ('ctype_is_nok'); ?></p>
  598. <?php } ?>
  599. <?php if ($res['dom'] == 'ok') { ?>
  600. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('ok'); ?></span> <?php echo _t ('dom_is_ok'); ?></p>
  601. <?php } else { ?>
  602. <p class="alert alert-error"><span class="alert-head"><?php echo _t ('damn'); ?></span> <?php echo _t ('dom_is_nok'); ?></p>
  603. <?php } ?>
  604. <?php if ($res['data'] == 'ok') { ?>
  605. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('ok'); ?></span> <?php echo _t ('data_is_ok'); ?></p>
  606. <?php } else { ?>
  607. <p class="alert alert-error"><span class="alert-head"><?php echo _t ('damn'); ?></span> <?php echo _t ('file_is_nok', DATA_PATH); ?></p>
  608. <?php } ?>
  609. <?php if ($res['cache'] == 'ok') { ?>
  610. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('ok'); ?></span> <?php echo _t ('cache_is_ok'); ?></p>
  611. <?php } else { ?>
  612. <p class="alert alert-error"><span class="alert-head"><?php echo _t ('damn'); ?></span> <?php echo _t ('file_is_nok', CACHE_PATH); ?></p>
  613. <?php } ?>
  614. <?php if ($res['log'] == 'ok') { ?>
  615. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('ok'); ?></span> <?php echo _t ('log_is_ok'); ?></p>
  616. <?php } else { ?>
  617. <p class="alert alert-error"><span class="alert-head"><?php echo _t ('damn'); ?></span> <?php echo _t ('file_is_nok', LOG_PATH); ?></p>
  618. <?php } ?>
  619. <?php if ($res['favicons'] == 'ok') { ?>
  620. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('ok'); ?></span> <?php echo _t ('favicons_is_ok'); ?></p>
  621. <?php } else { ?>
  622. <p class="alert alert-error"><span class="alert-head"><?php echo _t ('damn'); ?></span> <?php echo _t ('file_is_nok', DATA_PATH . '/favicons'); ?></p>
  623. <?php } ?>
  624. <?php if ($res['persona'] == 'ok') { ?>
  625. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('ok'); ?></span> <?php echo _t ('persona_is_ok'); ?></p>
  626. <?php } else { ?>
  627. <p class="alert alert-error"><span class="alert-head"><?php echo _t ('damn'); ?></span> <?php echo _t ('file_is_nok', DATA_PATH . '/persona'); ?></p>
  628. <?php } ?>
  629. <?php if ($res['all'] == 'ok') { ?>
  630. <a class="btn btn-important next-step" href="?step=2"><?php echo _t ('next_step'); ?></a>
  631. <?php } else { ?>
  632. <p class="alert alert-error"><?php echo _t ('fix_errors_before'); ?></p>
  633. <?php } ?>
  634. <?php
  635. }
  636. function printStep2 () {
  637. ?>
  638. <?php $s2 = checkStep2 (); if ($s2['all'] == 'ok') { ?>
  639. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('ok'); ?></span> <?php echo _t ('general_conf_is_ok'); ?></p>
  640. <?php } ?>
  641. <form action="index.php?step=2" method="post">
  642. <legend><?php echo _t ('general_configuration'); ?></legend>
  643. <div class="form-group">
  644. <label class="group-name" for="title"><?php echo _t ('title'); ?></label>
  645. <div class="group-controls">
  646. <input type="text" id="title" name="title" value="<?php echo isset ($_SESSION['title']) ? $_SESSION['title'] : _t ('freshrss'); ?>" />
  647. </div>
  648. </div>
  649. <div class="form-group">
  650. <label class="group-name" for="old_entries"><?php echo _t ('delete_articles_every'); ?></label>
  651. <div class="group-controls">
  652. <input type="number" id="old_entries" name="old_entries" required="required" min="1" max="1200" value="<?php echo isset ($_SESSION['old_entries']) ? $_SESSION['old_entries'] : '3'; ?>" /> <?php echo _t ('month'); ?>
  653. </div>
  654. </div>
  655. <div class="form-group">
  656. <label class="group-name" for="default_user"><?php echo _t ('default_user'); ?></label>
  657. <div class="group-controls">
  658. <input type="text" id="default_user" name="default_user" required="required" size="16" maxlength="16" pattern="[0-9a-zA-Z]{1,16}" value="<?php echo isset ($_SESSION['default_user']) ? $_SESSION['default_user'] : ''; ?>" placeholder="<?php echo httpAuthUser() == '' ? 'user1' : httpAuthUser(); ?>" />
  659. </div>
  660. </div>
  661. <div class="form-group">
  662. <label class="group-name" for="mail_login"><?php echo _t ('persona_connection_email'); ?></label>
  663. <div class="group-controls">
  664. <input type="email" id="mail_login" name="mail_login" value="<?php echo isset ($_SESSION['mail_login']) ? $_SESSION['mail_login'] : ''; ?>" placeholder="<?php echo _t ('blank_to_disable'); ?>" />
  665. <noscript><b><?php echo _t ('javascript_should_be_activated'); ?></b></noscript>
  666. </div>
  667. </div>
  668. <div class="form-group form-actions">
  669. <div class="group-controls">
  670. <button type="submit" class="btn btn-important"><?php echo _t ('save'); ?></button>
  671. <button type="reset" class="btn"><?php echo _t ('cancel'); ?></button>
  672. <?php if ($s2['all'] == 'ok') { ?>
  673. <a class="btn btn-important next-step" href="?step=3"><?php echo _t ('next_step'); ?></a>
  674. <?php } ?>
  675. </div>
  676. </div>
  677. </form>
  678. <?php
  679. }
  680. function printStep3 () {
  681. ?>
  682. <?php $s3 = checkStep3 (); if ($s3['all'] == 'ok') { ?>
  683. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('ok'); ?></span> <?php echo _t ('bdd_conf_is_ok'); ?></p>
  684. <?php } elseif ($s3['conn'] == 'ko') { ?>
  685. <p class="alert alert-error"><span class="alert-head"><?php echo _t ('damn'); ?></span> <?php echo _t ('bdd_conf_is_ko'), (empty($_SESSION['bd_error']) ? '' : ' : ' . $_SESSION['bd_error']); ?></p>
  686. <?php } ?>
  687. <form action="index.php?step=3" method="post">
  688. <legend><?php echo _t ('bdd_configuration'); ?></legend>
  689. <div class="form-group">
  690. <label class="group-name" for="type"><?php echo _t ('bdd_type'); ?></label>
  691. <div class="group-controls">
  692. <select name="type" id="type">
  693. <option value="mysql"
  694. <?php echo (isset($_SESSION['bd_type']) && $_SESSION['bd_type'] === 'mysql') ? 'selected="selected"' : ''; ?>>
  695. MySQL
  696. </option>
  697. <!-- TODO : l'utilisation de SQLite n'est pas encore possible. Pour tester tout de même, décommentez ce bloc
  698. <option value="sqlite"
  699. <?php echo (isset($_SESSION['bd_type']) && $_SESSION['bd_type'] === 'sqlite') ? 'selected="selected"' : ''; ?>>
  700. SQLite
  701. </option>-->
  702. </select>
  703. </div>
  704. </div>
  705. <div class="form-group">
  706. <label class="group-name" for="host"><?php echo _t ('host'); ?></label>
  707. <div class="group-controls">
  708. <input type="text" id="host" name="host" pattern="[0-9A-Za-z_.-]{1,64}" value="<?php echo isset ($_SESSION['bd_host']) ? $_SESSION['bd_host'] : 'localhost'; ?>" />
  709. </div>
  710. </div>
  711. <div class="form-group">
  712. <label class="group-name" for="user"><?php echo _t ('username'); ?></label>
  713. <div class="group-controls">
  714. <input type="text" id="user" name="user" maxlength="16" pattern="[0-9A-Za-z_]{1,16}" value="<?php echo isset ($_SESSION['bd_user']) ? $_SESSION['bd_user'] : ''; ?>" />
  715. </div>
  716. </div>
  717. <div class="form-group">
  718. <label class="group-name" for="pass"><?php echo _t ('password'); ?></label>
  719. <div class="group-controls">
  720. <input type="password" id="pass" name="pass" value="<?php echo isset ($_SESSION['bd_password']) ? $_SESSION['bd_password'] : ''; ?>" />
  721. </div>
  722. </div>
  723. <div class="form-group">
  724. <label class="group-name" for="base"><?php echo _t ('bdd'); ?></label>
  725. <div class="group-controls">
  726. <input type="text" id="base" name="base" maxlength="64" pattern="[0-9A-Za-z_]{1,64}" value="<?php echo isset ($_SESSION['bd_base']) ? $_SESSION['bd_base'] : ''; ?>" placeholder="freshrss" />
  727. </div>
  728. </div>
  729. <div class="form-group">
  730. <label class="group-name" for="prefix"><?php echo _t ('prefix'); ?></label>
  731. <div class="group-controls">
  732. <input type="text" id="prefix" name="prefix" maxlength="16" pattern="[0-9A-Za-z_]{1,16}" value="<?php echo isset ($_SESSION['bd_prefix']) ? $_SESSION['bd_prefix'] : 'freshrss_'; ?>" />
  733. </div>
  734. </div>
  735. <div class="form-group form-actions">
  736. <div class="group-controls">
  737. <button type="submit" class="btn btn-important"><?php echo _t ('save'); ?></button>
  738. <button type="reset" class="btn"><?php echo _t ('cancel'); ?></button>
  739. <?php if ($s3['all'] == 'ok') { ?>
  740. <a class="btn btn-important next-step" href="?step=4"><?php echo _t ('next_step'); ?></a>
  741. <?php } ?>
  742. </div>
  743. </div>
  744. </form>
  745. <?php
  746. }
  747. function printStep4 () {
  748. ?>
  749. <form action="index.php?step=4" method="post">
  750. <legend><?php echo _t ('version_update'); ?></legend>
  751. <div class="form-group form-actions">
  752. <div class="group-controls">
  753. <?php if (updateDatabase(false)) { ?>
  754. <input type="hidden" name="updateDatabase" value="1" />
  755. <button type="submit" class="btn btn-important"><?php echo _t ('update_start'); ?></button>
  756. <p><?php echo _t ('update_long'); ?></p>
  757. <?php } else { ?>
  758. <a class="btn btn-important next-step" href="?step=5"><?php echo _t ('next_step'); ?></a>
  759. <?php } ?>
  760. </div>
  761. </div>
  762. </form>
  763. <?php
  764. }
  765. function printStep5 () {
  766. ?>
  767. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('congratulations'); ?></span> <?php echo _t ('installation_is_ok'); ?></p>
  768. <a class="btn btn-important next-step" href="?step=6"><?php echo _t ('finish_installation'); ?></a>
  769. <?php
  770. }
  771. function printStep6 () {
  772. ?>
  773. <p class="alert alert-error"><span class="alert-head"><?php echo _t ('oops'); ?></span> <?php echo _t ('install_not_deleted', INDEX_PATH . '/install.php'); ?></p>
  774. <?php
  775. }
  776. checkStep ();
  777. initTranslate ();
  778. switch (STEP) {
  779. case 0:
  780. default:
  781. saveLanguage ();
  782. break;
  783. case 1:
  784. break;
  785. case 2:
  786. saveStep2 ();
  787. break;
  788. case 3:
  789. saveStep3 ();
  790. break;
  791. case 4:
  792. if (!empty($_POST['updateDatabase'])) {
  793. updateDatabase(true);
  794. }
  795. break;
  796. case 5:
  797. break;
  798. case 6:
  799. deleteInstall ();
  800. break;
  801. }
  802. ?>
  803. <!DOCTYPE html>
  804. <html lang="fr">
  805. <head>
  806. <meta charset="utf-8">
  807. <meta name="viewport" content="initial-scale=1.0">
  808. <title><?php echo _t ('freshrss_installation'); ?></title>
  809. <link rel="stylesheet" type="text/css" media="all" href="../themes/default/global.css" />
  810. <link rel="stylesheet" type="text/css" media="all" href="../themes/default/freshrss.css" />
  811. </head>
  812. <body>
  813. <div class="header">
  814. <div class="item title">
  815. <h1><a href="index.php"><?php echo _t ('freshrss'); ?></a></h1>
  816. <h2><?php echo _t ('installation_step', STEP); ?></h2>
  817. </div>
  818. </div>
  819. <div id="global">
  820. <ul class="nav nav-list aside">
  821. <li class="nav-header"><?php echo _t ('steps'); ?></li>
  822. <li class="item<?php echo STEP == 0 ? ' active' : ''; ?>"><a href="?step=0"><?php echo _t ('language'); ?></a></li>
  823. <li class="item<?php echo STEP == 1 ? ' active' : ''; ?>"><a href="?step=1"><?php echo _t ('checks'); ?></a></li>
  824. <li class="item<?php echo STEP == 2 ? ' active' : ''; ?>"><a href="?step=2"><?php echo _t ('general_configuration'); ?></a></li>
  825. <li class="item<?php echo STEP == 3 ? ' active' : ''; ?>"><a href="?step=3"><?php echo _t ('bdd_configuration'); ?></a></li>
  826. <li class="item<?php echo STEP == 4 ? ' active' : ''; ?>"><a href="?step=4"><?php echo _t ('version_update'); ?></a></li>
  827. <li class="item<?php echo STEP == 5 ? ' active' : ''; ?>"><a href="?step=5"><?php echo _t ('this_is_the_end'); ?></a></li>
  828. </ul>
  829. <div class="post">
  830. <?php
  831. switch (STEP) {
  832. case 0:
  833. default:
  834. printStep0 ();
  835. break;
  836. case 1:
  837. printStep1 ();
  838. break;
  839. case 2:
  840. printStep2 ();
  841. break;
  842. case 3:
  843. printStep3 ();
  844. break;
  845. case 4:
  846. printStep4 ();
  847. break;
  848. case 5:
  849. printStep5 ();
  850. break;
  851. case 6:
  852. printStep6 ();
  853. break;
  854. }
  855. ?>
  856. </div>
  857. </div>
  858. </body>
  859. </html>