index.php 13 KB

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