index.php 14 KB

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