index.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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.6.0', '<')) {
  32. array_push($errors, "PHP version 5.6.0 or newer required. You're using " . PHP_VERSION . ".");
  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')) || (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_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. <h1>Tiny Tiny RSS Installer</h1>
  117. <div class='content'>
  118. <?php
  119. if (file_exists("../config.php")) {
  120. require "../config.php";
  121. if (!defined('_INSTALLER_IGNORE_CONFIG_CHECK')) {
  122. print_error("Error: config.php already exists in tt-rss directory; aborting.");
  123. exit;
  124. }
  125. }
  126. @$op = $_REQUEST['op'];
  127. @$DB_HOST = strip_tags($_POST['DB_HOST']);
  128. @$DB_TYPE = strip_tags($_POST['DB_TYPE']);
  129. @$DB_USER = strip_tags($_POST['DB_USER']);
  130. @$DB_NAME = strip_tags($_POST['DB_NAME']);
  131. @$DB_PASS = strip_tags($_POST['DB_PASS']);
  132. @$DB_PORT = strip_tags($_POST['DB_PORT']);
  133. @$SELF_URL_PATH = strip_tags($_POST['SELF_URL_PATH']);
  134. if (!$SELF_URL_PATH) {
  135. $SELF_URL_PATH = preg_replace("/\/install\/$/", "/", make_self_url_path());
  136. }
  137. ?>
  138. <form action="" method="post">
  139. <input type="hidden" name="op" value="testconfig">
  140. <h2>Database settings</h2>
  141. <?php
  142. $issel_pgsql = $DB_TYPE == "pgsql" ? "selected" : "";
  143. $issel_mysql = $DB_TYPE == "mysql" ? "selected" : "";
  144. ?>
  145. <fieldset>
  146. <label>Database type</label>
  147. <select name="DB_TYPE">
  148. <option <?php echo $issel_pgsql ?> value="pgsql">PostgreSQL</option>
  149. <option <?php echo $issel_mysql ?> value="mysql">MySQL</option>
  150. </select>
  151. </fieldset>
  152. <fieldset>
  153. <label>Username</label>
  154. <input class="input input-text" required name="DB_USER" size="20" value="<?php echo $DB_USER ?>"/>
  155. </fieldset>
  156. <fieldset>
  157. <label>Password</label>
  158. <input class="input input-text" name="DB_PASS" size="20" type="password" value="<?php echo $DB_PASS ?>"/>
  159. </fieldset>
  160. <fieldset>
  161. <label>Database name</label>
  162. <input class="input input-text" required name="DB_NAME" size="20" value="<?php echo $DB_NAME ?>"/>
  163. </fieldset>
  164. <fieldset>
  165. <label>Host name</label>
  166. <input class="input input-text" name="DB_HOST" size="20" value="<?php echo $DB_HOST ?>"/>
  167. <span class="hint">If needed</span>
  168. </fieldset>
  169. <fieldset>
  170. <label>Port</label>
  171. <input class="input input-text" name="DB_PORT" type="number" size="20" value="<?php echo $DB_PORT ?>"/>
  172. <span class="hint">Usually 3306 for MySQL or 5432 for PostgreSQL</span>
  173. </fieldset>
  174. <h2>Other settings</h2>
  175. <p>This should be set to the location your Tiny Tiny RSS will be available on.</p>
  176. <fieldset>
  177. <label>Tiny Tiny RSS URL</label>
  178. <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 ?>"/>
  179. </fieldset>
  180. <p><input type="submit" value="Test configuration"></p>
  181. </form>
  182. <?php if ($op == 'testconfig') { ?>
  183. <h2>Checking configuration</h2>
  184. <?php
  185. $errors = sanity_check($DB_TYPE);
  186. if (count($errors) > 0) {
  187. print "<p>Some configuration tests failed. Please correct them before continuing.</p>";
  188. print "<ul>";
  189. foreach ($errors as $error) {
  190. print "<li style='color : red'>$error</li>";
  191. }
  192. print "</ul>";
  193. exit;
  194. }
  195. $notices = array();
  196. if (!function_exists("curl_init")) {
  197. array_push($notices, "It is highly recommended to enable support for CURL in PHP.");
  198. }
  199. if (function_exists("curl_init") && ini_get("open_basedir")) {
  200. array_push($notices, "CURL and open_basedir combination breaks support for HTTP redirects. See the FAQ for more information.");
  201. }
  202. if (!function_exists("idn_to_ascii")) {
  203. array_push($notices, "PHP support for Internationalization Functions is required to handle Internationalized Domain Names.");
  204. }
  205. if ($DB_TYPE == "mysql" && !function_exists("mysqli_connect")) {
  206. array_push($notices, "PHP extension for MySQL (mysqli) is missing. This may prevent legacy plugins from working.");
  207. }
  208. if ($DB_TYPE == "pgsql" && !function_exists("pg_connect")) {
  209. array_push($notices, "PHP extension for PostgreSQL is missing. This may prevent legacy plugins from working.");
  210. }
  211. if (count($notices) > 0) {
  212. print_notice("Configuration check succeeded with minor problems:");
  213. print "<ul>";
  214. foreach ($notices as $notice) {
  215. print "<li>$notice</li>";
  216. }
  217. print "</ul>";
  218. } else {
  219. print_notice("Configuration check succeeded.");
  220. }
  221. ?>
  222. <h2>Checking database</h2>
  223. <?php
  224. $pdo = pdo_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, $DB_TYPE, $DB_PORT);
  225. if (!$pdo) {
  226. print_error("Unable to connect to database using specified parameters.");
  227. exit;
  228. }
  229. print_notice("Database test succeeded."); ?>
  230. <h2>Initialize database</h2>
  231. <p>Before you can start using tt-rss, database needs to be initialized. Click on the button below to do that now.</p>
  232. <?php
  233. $res = $pdo->query("SELECT true FROM ttrss_feeds");
  234. if ($res && $res->fetch()) {
  235. print_error("Some tt-rss data already exists in this database. If you continue with database initialization your current data will be lost.");
  236. $need_confirm = true;
  237. } else {
  238. $need_confirm = false;
  239. }
  240. ?>
  241. <table><tr><td>
  242. <form method="post">
  243. <input type="hidden" name="op" value="installschema">
  244. <input type="hidden" name="DB_USER" value="<?php echo $DB_USER ?>"/>
  245. <input type="hidden" name="DB_PASS" value="<?php echo $DB_PASS ?>"/>
  246. <input type="hidden" name="DB_NAME" value="<?php echo $DB_NAME ?>"/>
  247. <input type="hidden" name="DB_HOST" value="<?php echo $DB_HOST ?>"/>
  248. <input type="hidden" name="DB_PORT" value="<?php echo $DB_PORT ?>"/>
  249. <input type="hidden" name="DB_TYPE" value="<?php echo $DB_TYPE ?>"/>
  250. <input type="hidden" name="SELF_URL_PATH" value="<?php echo $SELF_URL_PATH ?>"/>
  251. <?php if ($need_confirm) { ?>
  252. <p><input onclick="return confirm('Please read the warning above. Continue?')" type="submit" value="Initialize database" style="color : red"></p>
  253. <?php } else { ?>
  254. <p><input type="submit" value="Initialize database" style="color : red"></p>
  255. <?php } ?>
  256. </form>
  257. </td><td>
  258. <form method="post">
  259. <input type="hidden" name="DB_USER" value="<?php echo $DB_USER ?>"/>
  260. <input type="hidden" name="DB_PASS" value="<?php echo $DB_PASS ?>"/>
  261. <input type="hidden" name="DB_NAME" value="<?php echo $DB_NAME ?>"/>
  262. <input type="hidden" name="DB_HOST" value="<?php echo $DB_HOST ?>"/>
  263. <input type="hidden" name="DB_PORT" value="<?php echo $DB_PORT ?>"/>
  264. <input type="hidden" name="DB_TYPE" value="<?php echo $DB_TYPE ?>"/>
  265. <input type="hidden" name="SELF_URL_PATH" value="<?php echo $SELF_URL_PATH ?>"/>
  266. <input type="hidden" name="op" value="skipschema">
  267. <p><input type="submit" value="Skip initialization"></p>
  268. </form>
  269. </td></tr></table>
  270. <?php
  271. } else if ($op == 'installschema' || $op == 'skipschema') {
  272. $pdo = pdo_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, $DB_TYPE, $DB_PORT);
  273. if (!$pdo) {
  274. print_error("Unable to connect to database using specified parameters.");
  275. exit;
  276. }
  277. if ($op == 'installschema') {
  278. print "<h2>Initializing database...</h2>";
  279. $lines = explode(";", preg_replace("/[\r\n]/", "",
  280. file_get_contents("../schema/ttrss_schema_".basename($DB_TYPE).".sql")));
  281. foreach ($lines as $line) {
  282. if (strpos($line, "--") !== 0 && $line) {
  283. $res = $pdo->query($line);
  284. if (!$res) {
  285. print_notice("Query: $line");
  286. print_error("Error: " . implode(", ", $this->pdo->errorInfo()));
  287. }
  288. }
  289. }
  290. print_notice("Database initialization completed.");
  291. } else {
  292. print_notice("Database initialization skipped.");
  293. }
  294. print "<h2>Generated configuration file</h2>";
  295. 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>";
  296. 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>"; ?>
  297. <form action="" method="post">
  298. <input type="hidden" name="op" value="saveconfig">
  299. <input type="hidden" name="DB_USER" value="<?php echo $DB_USER ?>"/>
  300. <input type="hidden" name="DB_PASS" value="<?php echo $DB_PASS ?>"/>
  301. <input type="hidden" name="DB_NAME" value="<?php echo $DB_NAME ?>"/>
  302. <input type="hidden" name="DB_HOST" value="<?php echo $DB_HOST ?>"/>
  303. <input type="hidden" name="DB_PORT" value="<?php echo $DB_PORT ?>"/>
  304. <input type="hidden" name="DB_TYPE" value="<?php echo $DB_TYPE ?>"/>
  305. <input type="hidden" name="SELF_URL_PATH" value="<?php echo $SELF_URL_PATH ?>"/>
  306. <?php print "<textarea cols=\"80\" rows=\"20\">";
  307. echo make_config($DB_TYPE, $DB_HOST, $DB_USER, $DB_NAME, $DB_PASS,
  308. $DB_PORT, $SELF_URL_PATH);
  309. print "</textarea>"; ?>
  310. <?php if (is_writable("..")) { ?>
  311. <p>We can also try saving the file automatically now.</p>
  312. <p><input type="submit" value="Save configuration"></p>
  313. </form>
  314. <?php } else {
  315. print_error("Unfortunately, parent directory is not writable, so we're unable to save config.php automatically.");
  316. }
  317. print_notice("You can generate the file again by changing the form above.");
  318. } else if ($op == "saveconfig") {
  319. print "<h2>Saving configuration file to parent directory...</h2>";
  320. if (!file_exists("../config.php")) {
  321. $fp = fopen("../config.php", "w");
  322. if ($fp) {
  323. $written = fwrite($fp, make_config($DB_TYPE, $DB_HOST,
  324. $DB_USER, $DB_NAME, $DB_PASS,
  325. $DB_PORT, $SELF_URL_PATH));
  326. if ($written > 0) {
  327. print_notice("Successfully saved config.php. You can try <a href=\"..\">loading tt-rss now</a>.");
  328. } else {
  329. print_notice("Unable to write into config.php in tt-rss directory.");
  330. }
  331. fclose($fp);
  332. } else {
  333. print_error("Unable to open config.php in tt-rss directory for writing.");
  334. }
  335. } else {
  336. print_error("config.php already present in tt-rss directory, refusing to overwrite.");
  337. }
  338. }
  339. ?>
  340. </div>
  341. </body>
  342. </html>