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