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