index.php 14 KB

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