index.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 ($db_type == "mysql" && !function_exists("mysqli_connect")) {
  41. array_push($errors, "PHP support for MySQL is required for configured $db_type in config.php.");
  42. }
  43. if ($db_type == "pgsql" && !function_exists("pg_connect")) {
  44. array_push($errors, "PHP support for PostgreSQL is required for configured $db_type in config.php");
  45. }
  46. if (!function_exists("mb_strlen")) {
  47. array_push($errors, "PHP support for mbstring functions is required but was not found.");
  48. }
  49. if (!function_exists("hash")) {
  50. array_push($errors, "PHP support for hash() function is required but was not found.");
  51. }
  52. if (!function_exists("iconv")) {
  53. array_push($errors, "PHP support for iconv is required to handle multiple charsets.");
  54. }
  55. if (ini_get("safe_mode")) {
  56. array_push($errors, "PHP safe mode setting is obsolete and not supported by tt-rss.");
  57. }
  58. if (!class_exists("DOMDocument")) {
  59. array_push($errors, "PHP support for DOMDocument is required, but was not found.");
  60. }
  61. return $errors;
  62. }
  63. function print_error($msg) {
  64. print "<div class='alert alert-error'>$msg</div>";
  65. }
  66. function print_notice($msg) {
  67. print "<div class=\"alert alert-info\">$msg</div>";
  68. }
  69. function db_connect($host, $user, $pass, $db, $type, $port = false) {
  70. if ($type == "pgsql") {
  71. $string = "dbname=$db user=$user";
  72. if ($pass) {
  73. $string .= " password=$pass";
  74. }
  75. if ($host) {
  76. $string .= " host=$host";
  77. }
  78. if ($port) {
  79. $string = "$string port=" . $port;
  80. }
  81. $link = pg_connect($string);
  82. return $link;
  83. } else if ($type == "mysql") {
  84. if ($port)
  85. return mysqli_connect($host, $user, $pass, $db, $port);
  86. else
  87. return mysqli_connect($host, $user, $pass, $db);
  88. }
  89. }
  90. function make_config($DB_TYPE, $DB_HOST, $DB_USER, $DB_NAME, $DB_PASS,
  91. $DB_PORT, $SELF_URL_PATH) {
  92. $data = explode("\n", file_get_contents("../config.php-dist"));
  93. $rv = "";
  94. $finished = false;
  95. foreach ($data as $line) {
  96. if (preg_match("/define\('DB_TYPE'/", $line)) {
  97. $rv .= "\tdefine('DB_TYPE', '$DB_TYPE');\n";
  98. } else if (preg_match("/define\('DB_HOST'/", $line)) {
  99. $rv .= "\tdefine('DB_HOST', '$DB_HOST');\n";
  100. } else if (preg_match("/define\('DB_USER'/", $line)) {
  101. $rv .= "\tdefine('DB_USER', '$DB_USER');\n";
  102. } else if (preg_match("/define\('DB_NAME'/", $line)) {
  103. $rv .= "\tdefine('DB_NAME', '$DB_NAME');\n";
  104. } else if (preg_match("/define\('DB_PASS'/", $line)) {
  105. $rv .= "\tdefine('DB_PASS', '$DB_PASS');\n";
  106. } else if (preg_match("/define\('DB_PORT'/", $line)) {
  107. $rv .= "\tdefine('DB_PORT', '$DB_PORT');\n";
  108. } else if (preg_match("/define\('SELF_URL_PATH'/", $line)) {
  109. $rv .= "\tdefine('SELF_URL_PATH', '$SELF_URL_PATH');\n";
  110. } else if (!$finished) {
  111. $rv .= "$line\n";
  112. }
  113. if (preg_match("/\?\>/", $line)) {
  114. $finished = true;
  115. }
  116. }
  117. return $rv;
  118. }
  119. function db_query($link, $query, $type, $die_on_error = true) {
  120. if ($type == "pgsql") {
  121. $result = pg_query($link, $query);
  122. if (!$result) {
  123. $query = htmlspecialchars($query); // just in case
  124. if ($die_on_error) {
  125. die("Query <i>$query</i> failed [$result]: " . ($link ? pg_last_error($link) : "No connection"));
  126. }
  127. }
  128. return $result;
  129. } else if ($type == "mysql") {
  130. $result = mysqli_query($link, $query);
  131. if (!$result) {
  132. $query = htmlspecialchars($query);
  133. if ($die_on_error) {
  134. die("Query <i>$query</i> failed: " . ($link ? mysqli_error($link) : "No connection"));
  135. }
  136. }
  137. return $result;
  138. }
  139. }
  140. function is_server_https() {
  141. return (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] != 'off')) || $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https';
  142. }
  143. function make_self_url_path() {
  144. $url_path = (is_server_https() ? 'https://' : 'http://') . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
  145. return $url_path;
  146. }
  147. ?>
  148. <div class="floatingLogo"><img src="../images/logo_small.png"></div>
  149. <h1>Tiny Tiny RSS Installer</h1>
  150. <div class='content'>
  151. <?php
  152. if (file_exists("../config.php")) {
  153. require "../config.php";
  154. if (!defined('_INSTALLER_IGNORE_CONFIG_CHECK')) {
  155. print_error("Error: config.php already exists in tt-rss directory; aborting.");
  156. exit;
  157. }
  158. }
  159. @$op = $_REQUEST['op'];
  160. @$DB_HOST = strip_tags($_POST['DB_HOST']);
  161. @$DB_TYPE = strip_tags($_POST['DB_TYPE']);
  162. @$DB_USER = strip_tags($_POST['DB_USER']);
  163. @$DB_NAME = strip_tags($_POST['DB_NAME']);
  164. @$DB_PASS = strip_tags($_POST['DB_PASS']);
  165. @$DB_PORT = strip_tags($_POST['DB_PORT']);
  166. @$SELF_URL_PATH = strip_tags($_POST['SELF_URL_PATH']);
  167. if (!$SELF_URL_PATH) {
  168. $SELF_URL_PATH = preg_replace("/\/install\/$/", "/", make_self_url_path());
  169. }
  170. ?>
  171. <form action="" method="post">
  172. <input type="hidden" name="op" value="testconfig">
  173. <h2>Database settings</h2>
  174. <?php
  175. $issel_pgsql = $DB_TYPE == "pgsql" ? "selected" : "";
  176. $issel_mysql = $DB_TYPE == "mysql" ? "selected" : "";
  177. ?>
  178. <fieldset>
  179. <label>Database type</label>
  180. <select name="DB_TYPE">
  181. <option <?php echo $issel_pgsql ?> value="pgsql">PostgreSQL</option>
  182. <option <?php echo $issel_mysql ?> value="mysql">MySQL</option>
  183. </select>
  184. </fieldset>
  185. <fieldset>
  186. <label>Username</label>
  187. <input class="input input-text" required name="DB_USER" size="20" value="<?php echo $DB_USER ?>"/>
  188. </fieldset>
  189. <fieldset>
  190. <label>Password</label>
  191. <input class="input input-text" name="DB_PASS" size="20" type="password" value="<?php echo $DB_PASS ?>"/>
  192. </fieldset>
  193. <fieldset>
  194. <label>Database name</label>
  195. <input class="input input-text" required name="DB_NAME" size="20" value="<?php echo $DB_NAME ?>"/>
  196. </fieldset>
  197. <fieldset>
  198. <label>Host name</label>
  199. <input class="input input-text" name="DB_HOST" size="20" value="<?php echo $DB_HOST ?>"/>
  200. <span class="hint">If needed</span>
  201. </fieldset>
  202. <fieldset>
  203. <label>Port</label>
  204. <input class="input input-text" name="DB_PORT" type="number" size="20" value="<?php echo $DB_PORT ?>"/>
  205. <span class="hint">Usually 3306 for MySQL or 5432 for PostgreSQL</span>
  206. </fieldset>
  207. <h2>Other settings</h2>
  208. <p>This should be set to the location your Tiny Tiny RSS will be available on.</p>
  209. <fieldset>
  210. <label>Tiny Tiny RSS URL</label>
  211. <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 ?>"/>
  212. </fieldset>
  213. <p><input type="submit" value="Test configuration"></p>
  214. </form>
  215. <?php if ($op == 'testconfig') { ?>
  216. <h2>Checking configuration</h2>
  217. <?php
  218. $errors = sanity_check($DB_TYPE);
  219. if (count($errors) > 0) {
  220. print "<p>Some configuration tests failed. Please correct them before continuing.</p>";
  221. print "<ul>";
  222. foreach ($errors as $error) {
  223. print "<li style='color : red'>$error</li>";
  224. }
  225. print "</ul>";
  226. exit;
  227. }
  228. $notices = array();
  229. if (!function_exists("curl_init")) {
  230. array_push($notices, "It is highly recommended to enable support for CURL in PHP.");
  231. }
  232. if (function_exists("curl_init") && ini_get("open_basedir")) {
  233. array_push($notices, "CURL and open_basedir combination breaks support for HTTP redirects. See the FAQ for more information.");
  234. }
  235. if (!function_exists("idn_to_ascii")) {
  236. array_push($notices, "PHP support for Internationalization Functions is required to handle Internationalized Domain Names.");
  237. }
  238. if (count($notices) > 0) {
  239. print_notice("Configuration check succeeded with minor problems:");
  240. print "<ul>";
  241. foreach ($notices as $notice) {
  242. print "<li>$notice</li>";
  243. }
  244. print "</ul>";
  245. } else {
  246. print_notice("Configuration check succeeded.");
  247. }
  248. ?>
  249. <h2>Checking database</h2>
  250. <?php
  251. $link = db_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, $DB_TYPE, $DB_PORT);
  252. if (!$link) {
  253. print_error("Unable to connect to database using specified parameters.");
  254. exit;
  255. }
  256. print_notice("Database test succeeded."); ?>
  257. <h2>Initialize database</h2>
  258. <p>Before you can start using tt-rss, database needs to be initialized. Click on the button below to do that now.</p>
  259. <?php
  260. $result = @db_query($link, "SELECT true FROM ttrss_feeds", $DB_TYPE, false);
  261. if ($result) {
  262. print_error("Existing tt-rss tables will be removed from the database. If you would like to keep your data, skip database initialization.");
  263. $need_confirm = true;
  264. } else {
  265. $need_confirm = false;
  266. }
  267. ?>
  268. <table><tr><td>
  269. <form method="post">
  270. <input type="hidden" name="op" value="installschema">
  271. <input type="hidden" name="DB_USER" value="<?php echo $DB_USER ?>"/>
  272. <input type="hidden" name="DB_PASS" value="<?php echo $DB_PASS ?>"/>
  273. <input type="hidden" name="DB_NAME" value="<?php echo $DB_NAME ?>"/>
  274. <input type="hidden" name="DB_HOST" value="<?php echo $DB_HOST ?>"/>
  275. <input type="hidden" name="DB_PORT" value="<?php echo $DB_PORT ?>"/>
  276. <input type="hidden" name="DB_TYPE" value="<?php echo $DB_TYPE ?>"/>
  277. <input type="hidden" name="SELF_URL_PATH" value="<?php echo $SELF_URL_PATH ?>"/>
  278. <?php if ($need_confirm) { ?>
  279. <p><input onclick="return confirm('Please read the warning above. Continue?')" type="submit" value="Initialize database" style="color : red"></p>
  280. <?php } else { ?>
  281. <p><input type="submit" value="Initialize database" style="color : red"></p>
  282. <?php } ?>
  283. </form>
  284. </td><td>
  285. <form method="post">
  286. <input type="hidden" name="DB_USER" value="<?php echo $DB_USER ?>"/>
  287. <input type="hidden" name="DB_PASS" value="<?php echo $DB_PASS ?>"/>
  288. <input type="hidden" name="DB_NAME" value="<?php echo $DB_NAME ?>"/>
  289. <input type="hidden" name="DB_HOST" value="<?php echo $DB_HOST ?>"/>
  290. <input type="hidden" name="DB_PORT" value="<?php echo $DB_PORT ?>"/>
  291. <input type="hidden" name="DB_TYPE" value="<?php echo $DB_TYPE ?>"/>
  292. <input type="hidden" name="SELF_URL_PATH" value="<?php echo $SELF_URL_PATH ?>"/>
  293. <input type="hidden" name="op" value="skipschema">
  294. <p><input type="submit" value="Skip initialization"></p>
  295. </form>
  296. </td></tr></table>
  297. <?php
  298. } else if ($op == 'installschema' || $op == 'skipschema') {
  299. $link = db_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, $DB_TYPE, $DB_PORT);
  300. if (!$link) {
  301. print_error("Unable to connect to database using specified parameters.");
  302. exit;
  303. }
  304. if ($op == 'installschema') {
  305. print "<h2>Initializing database...</h2>";
  306. $lines = explode(";", preg_replace("/[\r\n]/", "", file_get_contents("../schema/ttrss_schema_".basename($DB_TYPE).".sql")));
  307. foreach ($lines as $line) {
  308. if (strpos($line, "--") !== 0 && $line) {
  309. db_query($link, $line, $DB_TYPE);
  310. }
  311. }
  312. print_notice("Database initialization completed.");
  313. } else {
  314. print_notice("Database initialization skipped.");
  315. }
  316. print "<h2>Generated configuration file</h2>";
  317. 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>";
  318. 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>"; ?>
  319. <form action="" method="post">
  320. <input type="hidden" name="op" value="saveconfig">
  321. <input type="hidden" name="DB_USER" value="<?php echo $DB_USER ?>"/>
  322. <input type="hidden" name="DB_PASS" value="<?php echo $DB_PASS ?>"/>
  323. <input type="hidden" name="DB_NAME" value="<?php echo $DB_NAME ?>"/>
  324. <input type="hidden" name="DB_HOST" value="<?php echo $DB_HOST ?>"/>
  325. <input type="hidden" name="DB_PORT" value="<?php echo $DB_PORT ?>"/>
  326. <input type="hidden" name="DB_TYPE" value="<?php echo $DB_TYPE ?>"/>
  327. <input type="hidden" name="SELF_URL_PATH" value="<?php echo $SELF_URL_PATH ?>"/>
  328. <?php print "<textarea cols=\"80\" rows=\"20\">";
  329. echo make_config($DB_TYPE, $DB_HOST, $DB_USER, $DB_NAME, $DB_PASS,
  330. $DB_PORT, $SELF_URL_PATH);
  331. print "</textarea>"; ?>
  332. <?php if (is_writable("..")) { ?>
  333. <p>We can also try saving the file automatically now.</p>
  334. <p><input type="submit" value="Save configuration"></p>
  335. </form>
  336. <?php } else {
  337. print_error("Unfortunately, parent directory is not writable, so we're unable to save config.php automatically.");
  338. }
  339. print_notice("You can generate the file again by changing the form above.");
  340. } else if ($op == "saveconfig") {
  341. print "<h2>Saving configuration file to parent directory...</h2>";
  342. if (!file_exists("../config.php")) {
  343. $fp = fopen("../config.php", "w");
  344. if ($fp) {
  345. $written = fwrite($fp, make_config($DB_TYPE, $DB_HOST,
  346. $DB_USER, $DB_NAME, $DB_PASS,
  347. $DB_PORT, $SELF_URL_PATH));
  348. if ($written > 0) {
  349. print_notice("Successfully saved config.php. You can try <a href=\"..\">loading tt-rss now</a>.");
  350. } else {
  351. print_notice("Unable to write into config.php in tt-rss directory.");
  352. }
  353. fclose($fp);
  354. } else {
  355. print_error("Unable to open config.php in tt-rss directory for writing.");
  356. }
  357. } else {
  358. print_error("config.php already present in tt-rss directory, refusing to overwrite.");
  359. }
  360. }
  361. ?>
  362. </div>
  363. </body>
  364. </html>