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