install.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. ),
  175. 'db' => array(
  176. 'type' => $_SESSION['bd_type'],
  177. 'host' => $_SESSION['bd_host'],
  178. 'user' => $_SESSION['bd_user'],
  179. 'password' => $_SESSION['bd_password'],
  180. 'base' => $_SESSION['bd_base'],
  181. 'prefix' => $_SESSION['bd_prefix'],
  182. ),
  183. );
  184. @unlink(DATA_PATH . '/config.php'); //To avoid access-rights problems
  185. file_put_contents(DATA_PATH . '/config.php', "<?php\n return " . var_export($ini_array, true) . ';');
  186. if (file_exists(DATA_PATH . '/config.php') && file_exists(DATA_PATH . '/application.ini')) {
  187. @unlink(DATA_PATH . '/application.ini'); //v0.6
  188. }
  189. $res = checkBD ();
  190. if ($res) {
  191. $_SESSION['bd_error'] = '';
  192. header ('Location: index.php?step=4');
  193. } elseif (empty($_SESSION['bd_error'])) {
  194. $_SESSION['bd_error'] = 'Unknown error!';
  195. }
  196. }
  197. invalidateHttpCache();
  198. }
  199. function updateDatabase($perform = false) {
  200. $needs = array('bd_type', 'bd_host', 'bd_base', 'bd_user', 'bd_password', 'bd_prefix', 'bd_prefix_user');
  201. foreach ($needs as $need) {
  202. if (!isset($_SESSION[$need])) {
  203. return false;
  204. }
  205. }
  206. try {
  207. $str = '';
  208. switch ($_SESSION['bd_type']) {
  209. case 'mysql':
  210. $str = 'mysql:host=' . $_SESSION['bd_host'] . ';dbname=' . $_SESSION['bd_base'];
  211. $driver_options = array(
  212. PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
  213. );
  214. break;
  215. case 'sqlite':
  216. $str = 'sqlite:' . DATA_PATH . $_SESSION['bd_base'] . '.sqlite';
  217. $driver_options = null;
  218. break;
  219. default:
  220. return false;
  221. }
  222. $c = new PDO($str, $_SESSION['bd_user'], $_SESSION['bd_password'], $driver_options);
  223. $stm = $c->prepare(SQL_SHOW_TABLES);
  224. $stm->execute();
  225. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  226. if (!in_array($_SESSION['bd_prefix'] . 'entry006', $res)) {
  227. return false;
  228. }
  229. $sql = sprintf(SQL_SHOW_COLUMNS_UPDATEv006, $_SESSION['bd_prefix']);
  230. $stm = $c->prepare($sql);
  231. $stm->execute();
  232. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  233. if (!in_array('id2', $res)) {
  234. if (!$perform) {
  235. return true;
  236. }
  237. $sql = sprintf(SQL_UPDATEv006, $_SESSION['bd_prefix'], $_SESSION['bd_prefix_user']);
  238. $stm = $c->prepare($sql, array(PDO::ATTR_EMULATE_PREPARES => true));
  239. $stm->execute();
  240. }
  241. $sql = sprintf(SQL_UPDATE_HISTORYv007b, $_SESSION['bd_prefix_user']);
  242. $stm = $c->prepare($sql);
  243. $stm->execute();
  244. $sql = sprintf(SQL_UPDATE_CACHED_VALUES, $_SESSION['bd_prefix_user']);
  245. $stm = $c->prepare($sql);
  246. $stm->execute();
  247. $sql = sprintf(SQL_CONVERT_SELECTv006, $_SESSION['bd_prefix'], $_SESSION['bd_prefix_user']);
  248. if (!$perform) {
  249. $sql .= ' LIMIT 1';
  250. }
  251. $stm = $c->prepare($sql);
  252. $stm->execute();
  253. if (!$perform) {
  254. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  255. return count($res) > 0;
  256. } else {
  257. @set_time_limit(300);
  258. }
  259. $c2 = new PDO($str, $_SESSION['bd_user'], $_SESSION['bd_password'], $driver_options);
  260. $sql = sprintf(SQL_CONVERT_UPDATEv006, $_SESSION['bd_prefix_user']);
  261. $stm2 = $c2->prepare($sql);
  262. while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
  263. $id = $row['id2'];
  264. $content = unserialize(gzinflate(base64_decode($row['content'])));
  265. $stm2->execute(array($content, $id));
  266. }
  267. return true;
  268. } catch (PDOException $e) {
  269. return false;
  270. }
  271. return false;
  272. }
  273. function deleteInstall () {
  274. $res = unlink (INDEX_PATH . '/install.php');
  275. if ($res) {
  276. header ('Location: index.php');
  277. }
  278. $needs = array('bd_type', 'bd_host', 'bd_base', 'bd_user', 'bd_password', 'bd_prefix');
  279. foreach ($needs as $need) {
  280. if (!isset($_SESSION[$need])) {
  281. return false;
  282. }
  283. }
  284. try {
  285. switch ($_SESSION['bd_type']) {
  286. case 'mysql':
  287. $str = 'mysql:host=' . $_SESSION['bd_host'] . ';dbname=' . $_SESSION['bd_base'];
  288. $driver_options = array(
  289. PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
  290. );
  291. break;
  292. case 'sqlite':
  293. $str = 'sqlite:' . DATA_PATH . $_SESSION['bd_base'] . '.sqlite';
  294. $driver_options = null;
  295. break;
  296. default:
  297. return false;
  298. }
  299. $c = new PDO($str, $_SESSION['bd_user'], $_SESSION['bd_password'], $driver_options);
  300. $sql = sprintf(SQL_DROP_BACKUPv006, $_SESSION['bd_prefix']);
  301. $stm = $c->prepare($sql);
  302. $stm->execute();
  303. return true;
  304. } catch (PDOException $e) {
  305. return false;
  306. }
  307. return false;
  308. }
  309. function moveOldFiles() {
  310. $mvs = array(
  311. '/app/configuration/application.ini' => '/data/application.ini', //v0.6
  312. '/public/data/Configuration.array.php' => '/data/Configuration.array.php', //v0.6
  313. );
  314. $ok = true;
  315. foreach ($mvs as $fFrom => $fTo) {
  316. if (file_exists(FRESHRSS_PATH . $fFrom)) {
  317. if (copy(FRESHRSS_PATH . $fFrom, FRESHRSS_PATH . $fTo)) {
  318. @unlink(FRESHRSS_PATH . $fFrom);
  319. } else {
  320. $ok = false;
  321. }
  322. }
  323. }
  324. return $ok;
  325. }
  326. function delTree($dir) { //http://php.net/rmdir#110489
  327. if (!is_dir($dir)) {
  328. return true;
  329. }
  330. $files = array_diff(scandir($dir), array('.', '..'));
  331. foreach ($files as $file) {
  332. $f = $dir . '/' . $file;
  333. if (is_dir($f)) {
  334. @chmod($f, 0777);
  335. delTree($f);
  336. }
  337. else unlink($f);
  338. }
  339. return rmdir($dir);
  340. }
  341. /*** VÉRIFICATIONS ***/
  342. function checkStep () {
  343. $s0 = checkStep0 ();
  344. $s1 = checkStep1 ();
  345. $s2 = checkStep2 ();
  346. $s3 = checkStep3 ();
  347. if (STEP > 0 && $s0['all'] != 'ok') {
  348. header ('Location: index.php?step=0');
  349. } elseif (STEP > 1 && $s1['all'] != 'ok') {
  350. header ('Location: index.php?step=1');
  351. } elseif (STEP > 2 && $s2['all'] != 'ok') {
  352. header ('Location: index.php?step=2');
  353. } elseif (STEP > 3 && $s3['all'] != 'ok') {
  354. header ('Location: index.php?step=3');
  355. }
  356. $_SESSION['actualize_feeds'] = true;
  357. }
  358. function checkStep0 () {
  359. moveOldFiles();
  360. if (file_exists(DATA_PATH . '/config.php')) {
  361. $ini_array = include(DATA_PATH . '/config.php');
  362. } elseif (file_exists(DATA_PATH . '/application.ini')) {
  363. $ini_array = parse_ini_file(DATA_PATH . '/application.ini', true);
  364. } else {
  365. $ini_array = null;
  366. }
  367. if ($ini_array) {
  368. $ini_general = isset($ini_array['general']) ? $ini_array['general'] : null;
  369. if ($ini_general) {
  370. $keys = array('environment', 'salt', 'title', 'default_user');
  371. foreach ($keys as $key) {
  372. if ((empty($_SESSION[$key])) && isset($ini_general[$key])) {
  373. $_SESSION[$key] = $ini_general[$key];
  374. }
  375. }
  376. }
  377. $ini_db = isset($ini_array['db']) ? $ini_array['db'] : null;
  378. if ($ini_db) {
  379. $keys = array('type', 'host', 'user', 'password', 'base', 'prefix');
  380. foreach ($keys as $key) {
  381. if ((!isset($_SESSION['bd_' . $key])) && isset($ini_db[$key])) {
  382. $_SESSION['bd_' . $key] = $ini_db[$key];
  383. }
  384. }
  385. }
  386. }
  387. if (isset($_SESSION['default_user']) && file_exists(DATA_PATH . '/' . $_SESSION['default_user'] . '_user.php')) {
  388. $userConfig = include(DATA_PATH . '/' . $_SESSION['default_user'] . '_user.php');
  389. } elseif (file_exists(DATA_PATH . '/Configuration.array.php')) {
  390. $userConfig = include(DATA_PATH . '/Configuration.array.php'); //v0.6
  391. } else {
  392. $userConfig = array();
  393. }
  394. $keys = array('language', 'old_entries', 'mail_login');
  395. foreach ($keys as $key) {
  396. if ((!isset($_SESSION[$key])) && isset($userConfig[$key])) {
  397. $_SESSION[$key] = $userConfig[$key];
  398. }
  399. }
  400. $languages = availableLanguages ();
  401. $language = isset ($_SESSION['language']) &&
  402. isset ($languages[$_SESSION['language']]);
  403. return array (
  404. 'language' => $language ? 'ok' : 'ko',
  405. 'all' => $language ? 'ok' : 'ko'
  406. );
  407. }
  408. function checkStep1 () {
  409. $php = version_compare (PHP_VERSION, '5.2.0') >= 0;
  410. $minz = file_exists (LIB_PATH . '/Minz');
  411. $curl = extension_loaded ('curl');
  412. $pdo = extension_loaded ('pdo_mysql');
  413. $pcre = extension_loaded ('pcre');
  414. $ctype = extension_loaded ('ctype');
  415. $dom = class_exists('DOMDocument');
  416. $data = DATA_PATH && is_writable (DATA_PATH);
  417. $cache = CACHE_PATH && is_writable (CACHE_PATH);
  418. $log = LOG_PATH && is_writable (LOG_PATH);
  419. $favicons = is_writable (DATA_PATH . '/favicons');
  420. return array (
  421. 'php' => $php ? 'ok' : 'ko',
  422. 'minz' => $minz ? 'ok' : 'ko',
  423. 'curl' => $curl ? 'ok' : 'ko',
  424. 'pdo-mysql' => $pdo ? 'ok' : 'ko',
  425. 'pcre' => $pcre ? 'ok' : 'ko',
  426. 'ctype' => $ctype ? 'ok' : 'ko',
  427. 'dom' => $dom ? 'ok' : 'ko',
  428. 'data' => $data ? 'ok' : 'ko',
  429. 'cache' => $cache ? 'ok' : 'ko',
  430. 'log' => $log ? 'ok' : 'ko',
  431. 'favicons' => $favicons ? 'ok' : 'ko',
  432. 'all' => $php && $minz && $curl && $pdo && $pcre && $ctype && $dom && $data && $cache && $log && $favicons ? 'ok' : 'ko'
  433. );
  434. }
  435. function checkStep2 () {
  436. $conf = !empty($_SESSION['salt']) &&
  437. !empty($_SESSION['title']) &&
  438. !empty($_SESSION['old_entries']) &&
  439. isset($_SESSION['mail_login']) &&
  440. !empty($_SESSION['default_user']);
  441. $defaultUser = empty($_POST['default_user']) ? null : $_POST['default_user'];
  442. if ($defaultUser === null) {
  443. $defaultUser = empty($_SESSION['default_user']) ? '' : $_SESSION['default_user'];
  444. }
  445. $data = is_writable(DATA_PATH . '/' . $defaultUser . '_user.php');
  446. if ($data) {
  447. @unlink(DATA_PATH . '/Configuration.array.php'); //v0.6
  448. }
  449. return array (
  450. 'conf' => $conf ? 'ok' : 'ko',
  451. 'data' => $data ? 'ok' : 'ko',
  452. 'all' => $conf && $data ? 'ok' : 'ko'
  453. );
  454. }
  455. function checkStep3 () {
  456. $conf = is_writable(DATA_PATH . '/config.php');
  457. $bd = isset ($_SESSION['bd_type']) &&
  458. isset ($_SESSION['bd_host']) &&
  459. isset ($_SESSION['bd_user']) &&
  460. isset ($_SESSION['bd_password']) &&
  461. isset ($_SESSION['bd_base']) &&
  462. isset ($_SESSION['bd_prefix']) &&
  463. isset ($_SESSION['bd_error']);
  464. $conn = empty($_SESSION['bd_error']);
  465. return array (
  466. 'bd' => $bd ? 'ok' : 'ko',
  467. 'conn' => $conn ? 'ok' : 'ko',
  468. 'conf' => $conf ? 'ok' : 'ko',
  469. 'all' => $bd && $conn && $conf ? 'ok' : 'ko'
  470. );
  471. }
  472. function checkBD () {
  473. $ok = false;
  474. try {
  475. $str = '';
  476. $driver_options = null;
  477. switch ($_SESSION['bd_type']) {
  478. case 'mysql':
  479. $driver_options = array(
  480. PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
  481. );
  482. try { // on ouvre une connexion juste pour créer la base si elle n'existe pas
  483. $str = 'mysql:host=' . $_SESSION['bd_host'] . ';';
  484. $c = new PDO ($str, $_SESSION['bd_user'], $_SESSION['bd_password'], $driver_options);
  485. $sql = sprintf (SQL_CREATE_DB, $_SESSION['bd_base']);
  486. $res = $c->query ($sql);
  487. } catch (PDOException $e) {
  488. }
  489. // on écrase la précédente connexion en sélectionnant la nouvelle BDD
  490. $str = 'mysql:host=' . $_SESSION['bd_host'] . ';dbname=' . $_SESSION['bd_base'];
  491. break;
  492. case 'sqlite':
  493. $str = 'sqlite:' . DATA_PATH . $_SESSION['bd_base'] . '.sqlite';
  494. break;
  495. default:
  496. return false;
  497. }
  498. $c = new PDO ($str, $_SESSION['bd_user'], $_SESSION['bd_password'], $driver_options);
  499. $stm = $c->prepare(SQL_SHOW_TABLES);
  500. $stm->execute();
  501. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  502. if (in_array($_SESSION['bd_prefix'] . 'entry', $res) && !in_array($_SESSION['bd_prefix'] . 'entry006', $res)) {
  503. $sql = sprintf(SQL_BACKUP006, $_SESSION['bd_prefix']); //v0.6
  504. $res = $c->query($sql); //Backup tables
  505. }
  506. $sql = sprintf(SQL_CREATE_TABLES, $_SESSION['bd_prefix_user']);
  507. $stm = $c->prepare($sql, array(PDO::ATTR_EMULATE_PREPARES => true));
  508. $values = array(
  509. 'catName' => _t('default_category'),
  510. );
  511. $ok = $stm->execute($values);
  512. } catch (PDOException $e) {
  513. $ok = false;
  514. $_SESSION['bd_error'] = $e->getMessage();
  515. }
  516. if (!$ok) {
  517. @unlink(DATA_PATH . '/config.php');
  518. }
  519. return $ok;
  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. <div class="form-group">
  628. <label class="group-name" for="title"><?php echo _t ('title'); ?></label>
  629. <div class="group-controls">
  630. <input type="text" id="title" name="title" value="<?php echo isset ($_SESSION['title']) ? $_SESSION['title'] : _t ('freshrss'); ?>" />
  631. </div>
  632. </div>
  633. <div class="form-group">
  634. <label class="group-name" for="old_entries"><?php echo _t ('delete_articles_every'); ?></label>
  635. <div class="group-controls">
  636. <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'); ?>
  637. </div>
  638. </div>
  639. <div class="form-group">
  640. <label class="group-name" for="default_user"><?php echo _t ('default_user'); ?></label>
  641. <div class="group-controls">
  642. <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(); ?>" />
  643. </div>
  644. </div>
  645. <div class="form-group">
  646. <label class="group-name" for="mail_login"><?php echo _t ('persona_connection_email'); ?></label>
  647. <div class="group-controls">
  648. <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'); ?>" />
  649. <noscript><b><?php echo _t ('javascript_should_be_activated'); ?></b></noscript>
  650. </div>
  651. </div>
  652. <div class="form-group form-actions">
  653. <div class="group-controls">
  654. <button type="submit" class="btn btn-important"><?php echo _t ('save'); ?></button>
  655. <button type="reset" class="btn"><?php echo _t ('cancel'); ?></button>
  656. <?php if ($s2['all'] == 'ok') { ?>
  657. <a class="btn btn-important next-step" href="?step=3"><?php echo _t ('next_step'); ?></a>
  658. <?php } ?>
  659. </div>
  660. </div>
  661. </form>
  662. <?php
  663. }
  664. function printStep3 () {
  665. ?>
  666. <?php $s3 = checkStep3 (); if ($s3['all'] == 'ok') { ?>
  667. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('ok'); ?></span> <?php echo _t ('bdd_conf_is_ok'); ?></p>
  668. <?php } elseif ($s3['conn'] == 'ko') { ?>
  669. <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>
  670. <?php } ?>
  671. <form action="index.php?step=3" method="post">
  672. <legend><?php echo _t ('bdd_configuration'); ?></legend>
  673. <div class="form-group">
  674. <label class="group-name" for="type"><?php echo _t ('bdd_type'); ?></label>
  675. <div class="group-controls">
  676. <select name="type" id="type">
  677. <option value="mysql"
  678. <?php echo (isset($_SESSION['bd_type']) && $_SESSION['bd_type'] === 'mysql') ? 'selected="selected"' : ''; ?>>
  679. MySQL
  680. </option>
  681. <!-- TODO : l'utilisation de SQLite n'est pas encore possible. Pour tester tout de même, décommentez ce bloc
  682. <option value="sqlite"
  683. <?php echo (isset($_SESSION['bd_type']) && $_SESSION['bd_type'] === 'sqlite') ? 'selected="selected"' : ''; ?>>
  684. SQLite
  685. </option>-->
  686. </select>
  687. </div>
  688. </div>
  689. <div class="form-group">
  690. <label class="group-name" for="host"><?php echo _t ('host'); ?></label>
  691. <div class="group-controls">
  692. <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'; ?>" />
  693. </div>
  694. </div>
  695. <div class="form-group">
  696. <label class="group-name" for="user"><?php echo _t ('username'); ?></label>
  697. <div class="group-controls">
  698. <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'] : ''; ?>" />
  699. </div>
  700. </div>
  701. <div class="form-group">
  702. <label class="group-name" for="pass"><?php echo _t ('password'); ?></label>
  703. <div class="group-controls">
  704. <input type="password" id="pass" name="pass" value="<?php echo isset ($_SESSION['bd_password']) ? $_SESSION['bd_password'] : ''; ?>" />
  705. </div>
  706. </div>
  707. <div class="form-group">
  708. <label class="group-name" for="base"><?php echo _t ('bdd'); ?></label>
  709. <div class="group-controls">
  710. <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" />
  711. </div>
  712. </div>
  713. <div class="form-group">
  714. <label class="group-name" for="prefix"><?php echo _t ('prefix'); ?></label>
  715. <div class="group-controls">
  716. <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_'; ?>" />
  717. </div>
  718. </div>
  719. <div class="form-group form-actions">
  720. <div class="group-controls">
  721. <button type="submit" class="btn btn-important"><?php echo _t ('save'); ?></button>
  722. <button type="reset" class="btn"><?php echo _t ('cancel'); ?></button>
  723. <?php if ($s3['all'] == 'ok') { ?>
  724. <a class="btn btn-important next-step" href="?step=4"><?php echo _t ('next_step'); ?></a>
  725. <?php } ?>
  726. </div>
  727. </div>
  728. </form>
  729. <?php
  730. }
  731. function printStep4 () {
  732. ?>
  733. <form action="index.php?step=4" method="post">
  734. <legend><?php echo _t ('version_update'); ?></legend>
  735. <div class="form-group form-actions">
  736. <div class="group-controls">
  737. <?php if (updateDatabase(false)) { ?>
  738. <input type="hidden" name="updateDatabase" value="1" />
  739. <button type="submit" class="btn btn-important"><?php echo _t ('update_start'); ?></button>
  740. <p><?php echo _t ('update_long'); ?></p>
  741. <?php } else { ?>
  742. <a class="btn btn-important next-step" href="?step=5"><?php echo _t ('next_step'); ?></a>
  743. <?php } ?>
  744. </div>
  745. </div>
  746. </form>
  747. <?php
  748. }
  749. function printStep5 () {
  750. ?>
  751. <p class="alert alert-success"><span class="alert-head"><?php echo _t ('congratulations'); ?></span> <?php echo _t ('installation_is_ok'); ?></p>
  752. <a class="btn btn-important next-step" href="?step=6"><?php echo _t ('finish_installation'); ?></a>
  753. <?php
  754. }
  755. function printStep6 () {
  756. ?>
  757. <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>
  758. <?php
  759. }
  760. checkStep ();
  761. initTranslate ();
  762. switch (STEP) {
  763. case 0:
  764. default:
  765. saveLanguage ();
  766. break;
  767. case 1:
  768. break;
  769. case 2:
  770. saveStep2 ();
  771. break;
  772. case 3:
  773. saveStep3 ();
  774. break;
  775. case 4:
  776. if (!empty($_POST['updateDatabase'])) {
  777. updateDatabase(true);
  778. }
  779. break;
  780. case 5:
  781. break;
  782. case 6:
  783. deleteInstall ();
  784. break;
  785. }
  786. ?>
  787. <!DOCTYPE html>
  788. <html lang="fr">
  789. <head>
  790. <meta charset="utf-8">
  791. <meta name="viewport" content="initial-scale=1.0">
  792. <title><?php echo _t ('freshrss_installation'); ?></title>
  793. <link rel="stylesheet" type="text/css" media="all" href="../themes/default/global.css" />
  794. <link rel="stylesheet" type="text/css" media="all" href="../themes/default/freshrss.css" />
  795. </head>
  796. <body>
  797. <div class="header">
  798. <div class="item title">
  799. <h1><a href="index.php"><?php echo _t ('freshrss'); ?></a></h1>
  800. <h2><?php echo _t ('installation_step', STEP); ?></h2>
  801. </div>
  802. </div>
  803. <div id="global">
  804. <ul class="nav nav-list aside">
  805. <li class="nav-header"><?php echo _t ('steps'); ?></li>
  806. <li class="item<?php echo STEP == 0 ? ' active' : ''; ?>"><a href="?step=0"><?php echo _t ('language'); ?></a></li>
  807. <li class="item<?php echo STEP == 1 ? ' active' : ''; ?>"><a href="?step=1"><?php echo _t ('checks'); ?></a></li>
  808. <li class="item<?php echo STEP == 2 ? ' active' : ''; ?>"><a href="?step=2"><?php echo _t ('general_configuration'); ?></a></li>
  809. <li class="item<?php echo STEP == 3 ? ' active' : ''; ?>"><a href="?step=3"><?php echo _t ('bdd_configuration'); ?></a></li>
  810. <li class="item<?php echo STEP == 4 ? ' active' : ''; ?>"><a href="?step=4"><?php echo _t ('version_update'); ?></a></li>
  811. <li class="item<?php echo STEP == 5 ? ' active' : ''; ?>"><a href="?step=5"><?php echo _t ('this_is_the_end'); ?></a></li>
  812. </ul>
  813. <div class="post">
  814. <?php
  815. switch (STEP) {
  816. case 0:
  817. default:
  818. printStep0 ();
  819. break;
  820. case 1:
  821. printStep1 ();
  822. break;
  823. case 2:
  824. printStep2 ();
  825. break;
  826. case 3:
  827. printStep3 ();
  828. break;
  829. case 4:
  830. printStep4 ();
  831. break;
  832. case 5:
  833. printStep5 ();
  834. break;
  835. case 6:
  836. printStep6 ();
  837. break;
  838. }
  839. ?>
  840. </div>
  841. </div>
  842. </body>
  843. </html>