index.php 14 KB

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