index.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <html>
  2. <head>
  3. <title>Tiny Tiny RSS - Installer</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <link rel="stylesheet" type="text/css" href="../css/default.css">
  6. <style type="text/css">
  7. textarea { font-size : 12px; }
  8. </style>
  9. </head>
  10. <body class="claro ttrss_utility">
  11. <?php
  12. // could be needed because of existing config.php
  13. function define_default($param, $value) {
  14. //
  15. }
  16. function make_password($length = 8) {
  17. $password = "";
  18. $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ*%+^";
  19. $i = 0;
  20. while ($i < $length) {
  21. $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
  22. if (!strstr($password, $char)) {
  23. $password .= $char;
  24. $i++;
  25. }
  26. }
  27. return $password;
  28. }
  29. function sanity_check($db_type) {
  30. $errors = array();
  31. if (version_compare(PHP_VERSION, '5.4.0', '<')) {
  32. array_push($errors, "PHP version 5.4.0 or newer required.");
  33. }
  34. if (!function_exists("curl_init") && !ini_get("allow_url_fopen")) {
  35. array_push($errors, "PHP configuration option allow_url_fopen is disabled, and CURL functions are not present. Either enable allow_url_fopen or install PHP extension for CURL.");
  36. }
  37. if (!function_exists("json_encode")) {
  38. array_push($errors, "PHP support for JSON is required, but was not found.");
  39. }
  40. if (!class_exists("PDO")) {
  41. array_push($errors, "PHP support for PDO is required but was not found.");
  42. }
  43. if (!function_exists("mb_strlen")) {
  44. array_push($errors, "PHP support for mbstring functions is required but was not found.");
  45. }
  46. if (!function_exists("hash")) {
  47. array_push($errors, "PHP support for hash() function is required but was not found.");
  48. }
  49. if (!function_exists("iconv")) {
  50. array_push($errors, "PHP support for iconv is required to handle multiple charsets.");
  51. }
  52. if (ini_get("safe_mode")) {
  53. array_push($errors, "PHP safe mode setting is obsolete and not supported by tt-rss.");
  54. }
  55. if (!class_exists("DOMDocument")) {
  56. array_push($errors, "PHP support for DOMDocument is required, but was not found.");
  57. }
  58. return $errors;
  59. }
  60. function print_error($msg) {
  61. print "<div class='alert alert-error'>$msg</div>";
  62. }
  63. function print_notice($msg) {
  64. print "<div class=\"alert alert-info\">$msg</div>";
  65. }
  66. function pdo_connect($host, $user, $pass, $db, $type, $port = false) {
  67. $db_port = $port ? ';port=' . $port : '';
  68. $db_host = $host ? ';host=' . $host : '';
  69. try {
  70. $pdo = new PDO($type . ':dbname=' . $db . $db_host . $db_port,
  71. $user,
  72. $pass);
  73. return $pdo;
  74. } catch (Exception $e) {
  75. print "<div class='alert alert-danger'>" . $e->getMessage() . "</div>";
  76. return null;
  77. }
  78. }
  79. function make_config($DB_TYPE, $DB_HOST, $DB_USER, $DB_NAME, $DB_PASS,
  80. $DB_PORT, $SELF_URL_PATH) {
  81. $data = explode("\n", file_get_contents("../config.php-dist"));
  82. $rv = "";
  83. $finished = false;
  84. foreach ($data as $line) {
  85. if (preg_match("/define\('DB_TYPE'/", $line)) {
  86. $rv .= "\tdefine('DB_TYPE', '$DB_TYPE');\n";
  87. } else if (preg_match("/define\('DB_HOST'/", $line)) {
  88. $rv .= "\tdefine('DB_HOST', '$DB_HOST');\n";
  89. } else if (preg_match("/define\('DB_USER'/", $line)) {
  90. $rv .= "\tdefine('DB_USER', '$DB_USER');\n";
  91. } else if (preg_match("/define\('DB_NAME'/", $line)) {
  92. $rv .= "\tdefine('DB_NAME', '$DB_NAME');\n";
  93. } else if (preg_match("/define\('DB_PASS'/", $line)) {
  94. $rv .= "\tdefine('DB_PASS', '$DB_PASS');\n";
  95. } else if (preg_match("/define\('DB_PORT'/", $line)) {
  96. $rv .= "\tdefine('DB_PORT', '$DB_PORT');\n";
  97. } else if (preg_match("/define\('SELF_URL_PATH'/", $line)) {
  98. $rv .= "\tdefine('SELF_URL_PATH', '$SELF_URL_PATH');\n";
  99. } else if (!$finished) {
  100. $rv .= "$line\n";
  101. }
  102. if (preg_match("/\?\>/", $line)) {
  103. $finished = true;
  104. }
  105. }
  106. return $rv;
  107. }
  108. function is_server_https() {
  109. return (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] != 'off')) || $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https';
  110. }
  111. function make_self_url_path() {
  112. $url_path = (is_server_https() ? 'https://' : 'http://') . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
  113. return $url_path;
  114. }
  115. ?>
  116. <div class="floatingLogo"><img src="../images/logo_small.png"></div>
  117. <h1>Tiny Tiny RSS Installer</h1>
  118. <div class='content'>
  119. <?php
  120. if (file_exists("../config.php")) {
  121. require "../config.php";
  122. if (!defined('_INSTALLER_IGNORE_CONFIG_CHECK')) {
  123. print_error("Error: config.php already exists in tt-rss directory; aborting.");
  124. exit;
  125. }
  126. }
  127. @$op = $_REQUEST['op'];
  128. @$DB_HOST = strip_tags($_POST['DB_HOST']);
  129. @$DB_TYPE = strip_tags($_POST['DB_TYPE']);
  130. @$DB_USER = strip_tags($_POST['DB_USER']);
  131. @$DB_NAME = strip_tags($_POST['DB_NAME']);
  132. @$DB_PASS = strip_tags($_POST['DB_PASS']);
  133. @$DB_PORT = strip_tags($_POST['DB_PORT']);
  134. @$SELF_URL_PATH = strip_tags($_POST['SELF_URL_PATH']);
  135. if (!$SELF_URL_PATH) {
  136. $SELF_URL_PATH = preg_replace("/\/install\/$/", "/", make_self_url_path());
  137. }
  138. ?>
  139. <form action="" method="post">
  140. <input type="hidden" name="op" value="testconfig">
  141. <h2>Database settings</h2>
  142. <?php
  143. $issel_pgsql = $DB_TYPE == "pgsql" ? "selected" : "";
  144. $issel_mysql = $DB_TYPE == "mysql" ? "selected" : "";
  145. ?>
  146. <fieldset>
  147. <label>Database type</label>
  148. <select name="DB_TYPE">
  149. <option <?php echo $issel_pgsql ?> value="pgsql">PostgreSQL</option>
  150. <option <?php echo $issel_mysql ?> value="mysql">MySQL</option>
  151. </select>
  152. </fieldset>
  153. <fieldset>
  154. <label>Username</label>
  155. <input class="input input-text" required name="DB_USER" size="20" value="<?php echo $DB_USER ?>"/>
  156. </fieldset>
  157. <fieldset>
  158. <label>Password</label>
  159. <input class="input input-text" name="DB_PASS" size="20" type="password" value="<?php echo $DB_PASS ?>"/>
  160. </fieldset>
  161. <fieldset>
  162. <label>Database name</label>
  163. <input class="input input-text" required name="DB_NAME" size="20" value="<?php echo $DB_NAME ?>"/>
  164. </fieldset>
  165. <fieldset>
  166. <label>Host name</label>
  167. <input class="input input-text" name="DB_HOST" size="20" value="<?php echo $DB_HOST ?>"/>
  168. <span class="hint">If needed</span>
  169. </fieldset>
  170. <fieldset>
  171. <label>Port</label>
  172. <input class="input input-text" name="DB_PORT" type="number" size="20" value="<?php echo $DB_PORT ?>"/>
  173. <span class="hint">Usually 3306 for MySQL or 5432 for PostgreSQL</span>
  174. </fieldset>
  175. <h2>Other settings</h2>
  176. <p>This should be set to the location your Tiny Tiny RSS will be available on.</p>
  177. <fieldset>
  178. <label>Tiny Tiny RSS URL</label>
  179. <input class="input input-text" type="url" name="SELF_URL_PATH" placeholder="<?php echo $SELF_URL_PATH; ?>" size="60" value="<?php echo $SELF_URL_PATH ?>"/>
  180. </fieldset>
  181. <p><input type="submit" value="Test configuration"></p>
  182. </form>
  183. <?php if ($op == 'testconfig') { ?>
  184. <h2>Checking configuration</h2>
  185. <?php
  186. $errors = sanity_check($DB_TYPE);
  187. if (count($errors) > 0) {
  188. print "<p>Some configuration tests failed. Please correct them before continuing.</p>";
  189. print "<ul>";
  190. foreach ($errors as $error) {
  191. print "<li style='color : red'>$error</li>";
  192. }
  193. print "</ul>";
  194. exit;
  195. }
  196. $notices = array();
  197. if (!function_exists("curl_init")) {
  198. array_push($notices, "It is highly recommended to enable support for CURL in PHP.");
  199. }
  200. if (function_exists("curl_init") && ini_get("open_basedir")) {
  201. array_push($notices, "CURL and open_basedir combination breaks support for HTTP redirects. See the FAQ for more information.");
  202. }
  203. if (!function_exists("idn_to_ascii")) {
  204. array_push($notices, "PHP support for Internationalization Functions is required to handle Internationalized Domain Names.");
  205. }
  206. if ($DB_TYPE == "mysql" && !function_exists("mysqli_connect")) {
  207. array_push($notices, "PHP extension for MySQL (mysqli) is missing. This may prevent legacy plugins from working.");
  208. }
  209. if ($DB_TYPE == "pgsql" && !function_exists("pg_connect")) {
  210. array_push($notices, "PHP extension for PostgreSQL is missing. This may prevent legacy plugins from working.");
  211. }
  212. if (count($notices) > 0) {
  213. print_notice("Configuration check succeeded with minor problems:");
  214. print "<ul>";
  215. foreach ($notices as $notice) {
  216. print "<li>$notice</li>";
  217. }
  218. print "</ul>";
  219. } else {
  220. print_notice("Configuration check succeeded.");
  221. }
  222. ?>
  223. <h2>Checking database</h2>
  224. <?php
  225. $pdo = pdo_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, $DB_TYPE, $DB_PORT);
  226. if (!$pdo) {
  227. print_error("Unable to connect to database using specified parameters.");
  228. exit;
  229. }
  230. print_notice("Database test succeeded."); ?>
  231. <h2>Initialize database</h2>
  232. <p>Before you can start using tt-rss, database needs to be initialized. Click on the button below to do that now.</p>
  233. <?php
  234. $res = $pdo->query("SELECT true FROM ttrss_feeds");
  235. if ($res && $res->fetch()) {
  236. print_error("Some tt-rss data already exists in this database. If you continue with database initialization your current data will be lost.");
  237. $need_confirm = true;
  238. } else {
  239. $need_confirm = false;
  240. }
  241. ?>
  242. <table><tr><td>
  243. <form method="post">
  244. <input type="hidden" name="op" value="installschema">
  245. <input type="hidden" name="DB_USER" value="<?php echo $DB_USER ?>"/>
  246. <input type="hidden" name="DB_PASS" value="<?php echo $DB_PASS ?>"/>
  247. <input type="hidden" name="DB_NAME" value="<?php echo $DB_NAME ?>"/>
  248. <input type="hidden" name="DB_HOST" value="<?php echo $DB_HOST ?>"/>
  249. <input type="hidden" name="DB_PORT" value="<?php echo $DB_PORT ?>"/>
  250. <input type="hidden" name="DB_TYPE" value="<?php echo $DB_TYPE ?>"/>
  251. <input type="hidden" name="SELF_URL_PATH" value="<?php echo $SELF_URL_PATH ?>"/>
  252. <?php if ($need_confirm) { ?>
  253. <p><input onclick="return confirm('Please read the warning above. Continue?')" type="submit" value="Initialize database" style="color : red"></p>
  254. <?php } else { ?>
  255. <p><input type="submit" value="Initialize database" style="color : red"></p>
  256. <?php } ?>
  257. </form>
  258. </td><td>
  259. <form method="post">
  260. <input type="hidden" name="DB_USER" value="<?php echo $DB_USER ?>"/>
  261. <input type="hidden" name="DB_PASS" value="<?php echo $DB_PASS ?>"/>
  262. <input type="hidden" name="DB_NAME" value="<?php echo $DB_NAME ?>"/>
  263. <input type="hidden" name="DB_HOST" value="<?php echo $DB_HOST ?>"/>
  264. <input type="hidden" name="DB_PORT" value="<?php echo $DB_PORT ?>"/>
  265. <input type="hidden" name="DB_TYPE" value="<?php echo $DB_TYPE ?>"/>
  266. <input type="hidden" name="SELF_URL_PATH" value="<?php echo $SELF_URL_PATH ?>"/>
  267. <input type="hidden" name="op" value="skipschema">
  268. <p><input type="submit" value="Skip initialization"></p>
  269. </form>
  270. </td></tr></table>
  271. <?php
  272. } else if ($op == 'installschema' || $op == 'skipschema') {
  273. $pdo = pdo_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, $DB_TYPE, $DB_PORT);
  274. if (!$pdo) {
  275. print_error("Unable to connect to database using specified parameters.");
  276. exit;
  277. }
  278. if ($op == 'installschema') {
  279. print "<h2>Initializing database...</h2>";
  280. $lines = explode(";", preg_replace("/[\r\n]/", "",
  281. file_get_contents("../schema/ttrss_schema_".basename($DB_TYPE).".sql")));
  282. foreach ($lines as $line) {
  283. if (strpos($line, "--") !== 0 && $line) {
  284. $res = $pdo->query($line);
  285. if (!$res) {
  286. print_notice("Query: $line");
  287. print_error("Error: " . implode(", ", $this->pdo->errorInfo()));
  288. }
  289. }
  290. }
  291. print_notice("Database initialization completed.");
  292. } else {
  293. print_notice("Database initialization skipped.");
  294. }
  295. print "<h2>Generated configuration file</h2>";
  296. print "<p>Copy following text and save as <code>config.php</code> in tt-rss main directory. It is suggested to read through the file to the end in case you need any options changed fom default values.</p>";
  297. print "<p>After copying the file, you will be able to login with default username and password combination: <code>admin</code> and <code>password</code>. Don't forget to change the password immediately!</p>"; ?>
  298. <form action="" method="post">
  299. <input type="hidden" name="op" value="saveconfig">
  300. <input type="hidden" name="DB_USER" value="<?php echo $DB_USER ?>"/>
  301. <input type="hidden" name="DB_PASS" value="<?php echo $DB_PASS ?>"/>
  302. <input type="hidden" name="DB_NAME" value="<?php echo $DB_NAME ?>"/>
  303. <input type="hidden" name="DB_HOST" value="<?php echo $DB_HOST ?>"/>
  304. <input type="hidden" name="DB_PORT" value="<?php echo $DB_PORT ?>"/>
  305. <input type="hidden" name="DB_TYPE" value="<?php echo $DB_TYPE ?>"/>
  306. <input type="hidden" name="SELF_URL_PATH" value="<?php echo $SELF_URL_PATH ?>"/>
  307. <?php print "<textarea cols=\"80\" rows=\"20\">";
  308. echo make_config($DB_TYPE, $DB_HOST, $DB_USER, $DB_NAME, $DB_PASS,
  309. $DB_PORT, $SELF_URL_PATH);
  310. print "</textarea>"; ?>
  311. <?php if (is_writable("..")) { ?>
  312. <p>We can also try saving the file automatically now.</p>
  313. <p><input type="submit" value="Save configuration"></p>
  314. </form>
  315. <?php } else {
  316. print_error("Unfortunately, parent directory is not writable, so we're unable to save config.php automatically.");
  317. }
  318. print_notice("You can generate the file again by changing the form above.");
  319. } else if ($op == "saveconfig") {
  320. print "<h2>Saving configuration file to parent directory...</h2>";
  321. if (!file_exists("../config.php")) {
  322. $fp = fopen("../config.php", "w");
  323. if ($fp) {
  324. $written = fwrite($fp, make_config($DB_TYPE, $DB_HOST,
  325. $DB_USER, $DB_NAME, $DB_PASS,
  326. $DB_PORT, $SELF_URL_PATH));
  327. if ($written > 0) {
  328. print_notice("Successfully saved config.php. You can try <a href=\"..\">loading tt-rss now</a>.");
  329. } else {
  330. print_notice("Unable to write into config.php in tt-rss directory.");
  331. }
  332. fclose($fp);
  333. } else {
  334. print_error("Unable to open config.php in tt-rss directory for writing.");
  335. }
  336. } else {
  337. print_error("config.php already present in tt-rss directory, refusing to overwrite.");
  338. }
  339. }
  340. ?>
  341. </div>
  342. </body>
  343. </html>