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