install.php 31 KB

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