]> git.wh0rd.org - tt-rss.git/blame - install/index.php
installer: use dijit styles
[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
42 if (version_compare(PHP_VERSION, '5.3.0', '<')) {
43 array_push($errors, "PHP version 5.3.0 or newer required.");
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
ae35bb87 54 if ($db_type == "mysql" && !function_exists("mysql_connect") && !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
70 if (!function_exists("ctype_lower")) {
71 array_push($errors, "PHP support for ctype functions are required by HTMLPurifier.");
72 }
73
74 if (!function_exists("iconv")) {
75 array_push($errors, "PHP support for iconv is required to handle multiple charsets.");
76 }
77
78 /* if (ini_get("safe_mode")) {
79 array_push($errors, "PHP safe mode setting is not supported.");
80 } */
81
d0c6dd29
AD
82 if (!class_exists("DOMDocument")) {
83 array_push($errors, "PHP support for DOMDocument is required, but was not found.");
84 }
85
86 return $errors;
87 }
88
89 function print_error($msg) {
6759dde1 90 print "<div class='alert alert-error'>$msg</div>";
d0c6dd29
AD
91 }
92
93 function print_notice($msg) {
6759dde1 94 print "<div class=\"alert alert-info\">$msg</div>";
d0c6dd29
AD
95 }
96
6f7798b6 97 function db_connect($host, $user, $pass, $db, $type, $port = false) {
d0c6dd29
AD
98 if ($type == "pgsql") {
99
100 $string = "dbname=$db user=$user";
101
102 if ($pass) {
103 $string .= " password=$pass";
104 }
105
106 if ($host) {
107 $string .= " host=$host";
108 }
109
bbffc43e
AD
110 if ($port) {
111 $string = "$string port=" . $port;
d0c6dd29
AD
112 }
113
114 $link = pg_connect($string);
115
116 return $link;
117
118 } else if ($type == "mysql") {
bbffc43e 119 if (function_exists("mysqli_connect")) {
92c9a20c
AD
120 if ($port)
121 return mysqli_connect($host, $user, $pass, $db, $port);
122 else
123 return mysqli_connect($host, $user, $pass, $db);
bbffc43e
AD
124
125 } else {
126 $link = mysql_connect($host, $user, $pass);
127 if ($link) {
128 $result = mysql_select_db($db, $link);
129 if ($result) return $link;
130 }
d0c6dd29
AD
131 }
132 }
133 }
134
b4cec374
AD
135 function make_config($DB_TYPE, $DB_HOST, $DB_USER, $DB_NAME, $DB_PASS,
136 $DB_PORT, $SELF_URL_PATH) {
137
138 $data = explode("\n", file_get_contents("../config.php-dist"));
139
140 $rv = "";
141
c93f98e1
AD
142 $finished = false;
143
044cff2d
AD
144 if (function_exists("mcrypt_decrypt")) {
145 $crypt_key = make_password(24);
146 } else {
147 $crypt_key = "";
148 }
149
b4cec374
AD
150 foreach ($data as $line) {
151 if (preg_match("/define\('DB_TYPE'/", $line)) {
152 $rv .= "\tdefine('DB_TYPE', '$DB_TYPE');\n";
153 } else if (preg_match("/define\('DB_HOST'/", $line)) {
154 $rv .= "\tdefine('DB_HOST', '$DB_HOST');\n";
155 } else if (preg_match("/define\('DB_USER'/", $line)) {
156 $rv .= "\tdefine('DB_USER', '$DB_USER');\n";
157 } else if (preg_match("/define\('DB_NAME'/", $line)) {
158 $rv .= "\tdefine('DB_NAME', '$DB_NAME');\n";
159 } else if (preg_match("/define\('DB_PASS'/", $line)) {
160 $rv .= "\tdefine('DB_PASS', '$DB_PASS');\n";
161 } else if (preg_match("/define\('DB_PORT'/", $line)) {
162 $rv .= "\tdefine('DB_PORT', '$DB_PORT');\n";
163 } else if (preg_match("/define\('SELF_URL_PATH'/", $line)) {
164 $rv .= "\tdefine('SELF_URL_PATH', '$SELF_URL_PATH');\n";
044cff2d
AD
165 } else if (preg_match("/define\('FEED_CRYPT_KEY'/", $line)) {
166 $rv .= "\tdefine('FEED_CRYPT_KEY', '$crypt_key');\n";
c93f98e1 167 } else if (!$finished) {
b4cec374
AD
168 $rv .= "$line\n";
169 }
c93f98e1
AD
170
171 if (preg_match("/\?\>/", $line)) {
172 $finished = true;
173 }
b4cec374
AD
174 }
175
176 return $rv;
177 }
178
d0c6dd29
AD
179 function db_query($link, $query, $type, $die_on_error = true) {
180 if ($type == "pgsql") {
181 $result = pg_query($link, $query);
182 if (!$result) {
183 $query = htmlspecialchars($query); // just in case
184 if ($die_on_error) {
185 die("Query <i>$query</i> failed [$result]: " . ($link ? pg_last_error($link) : "No connection"));
186 }
187 }
188 return $result;
189 } else if ($type == "mysql") {
bbffc43e
AD
190
191 if (function_exists("mysqli_connect")) {
192 $result = mysqli_query($link, $query);
193 } else {
194 $result = mysql_query($query, $link);
195 }
d0c6dd29
AD
196 if (!$result) {
197 $query = htmlspecialchars($query);
198 if ($die_on_error) {
4e46e90e 199 die("Query <i>$query</i> failed: " . ($link ? function_exists("mysqli_connect") ? mysqli_error($link) : mysql_error($link) : "No connection"));
d0c6dd29
AD
200 }
201 }
202 return $result;
203 }
204 }
205
056c537b 206 function make_self_url_path() {
3a3aec22 207 $url_path = ((!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") ? 'http://' : 'https://') . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
056c537b
AD
208
209 return $url_path;
210 }
211
d0c6dd29
AD
212?>
213
884d1650 214<div class="floatingLogo"><img src="../images/logo_small.png"></div>
d0c6dd29
AD
215
216<h1>Tiny Tiny RSS Installer</h1>
217
884d1650
AD
218<div class='content'>
219
d0c6dd29 220<?php
056c537b 221
d0c6dd29
AD
222 if (file_exists("../config.php")) {
223 require "../config.php";
224
225 if (!defined('_INSTALLER_IGNORE_CONFIG_CHECK')) {
b4cec374 226 print_error("Error: config.php already exists in tt-rss directory; aborting.");
d0c6dd29
AD
227 exit;
228 }
229 }
230
231 @$op = $_REQUEST['op'];
232
233 @$DB_HOST = strip_tags($_POST['DB_HOST']);
234 @$DB_TYPE = strip_tags($_POST['DB_TYPE']);
235 @$DB_USER = strip_tags($_POST['DB_USER']);
236 @$DB_NAME = strip_tags($_POST['DB_NAME']);
237 @$DB_PASS = strip_tags($_POST['DB_PASS']);
238 @$DB_PORT = strip_tags($_POST['DB_PORT']);
056c537b 239 @$SELF_URL_PATH = strip_tags($_POST['SELF_URL_PATH']);
d0c6dd29 240
056c537b
AD
241 if (!$SELF_URL_PATH) {
242 $SELF_URL_PATH = preg_replace("/\/install\/$/", "/", make_self_url_path());
243 }
d0c6dd29
AD
244?>
245
d0c6dd29
AD
246<form action="" method="post">
247<input type="hidden" name="op" value="testconfig">
248
056c537b
AD
249<h2>Database settings</h2>
250
d0c6dd29
AD
251<?php
252 $issel_pgsql = $DB_TYPE == "pgsql" ? "selected" : "";
253 $issel_mysql = $DB_TYPE == "mysql" ? "selected" : "";
254?>
255
256<fieldset>
257 <label>Database type</label>
258 <select name="DB_TYPE">
259 <option <?php echo $issel_pgsql ?> value="pgsql">PostgreSQL</option>
260 <option <?php echo $issel_mysql ?> value="mysql">MySQL</option>
261 </select>
262</fieldset>
263
264<fieldset>
265 <label>Username</label>
9044e2e0 266 <input class="input input-text" required name="DB_USER" size="20" value="<?php echo $DB_USER ?>"/>
d0c6dd29
AD
267</fieldset>
268
269<fieldset>
270 <label>Password</label>
9044e2e0 271 <input class="input input-text" name="DB_PASS" size="20" type="password" value="<?php echo $DB_PASS ?>"/>
d0c6dd29
AD
272</fieldset>
273
274<fieldset>
275 <label>Database name</label>
9044e2e0 276 <input class="input input-text" required name="DB_NAME" size="20" value="<?php echo $DB_NAME ?>"/>
d0c6dd29
AD
277</fieldset>
278
279<fieldset>
280 <label>Host name</label>
9044e2e0 281 <input class="input input-text" name="DB_HOST" size="20" value="<?php echo $DB_HOST ?>"/>
bbffc43e 282 <span class="hint">If needed</span>
d0c6dd29
AD
283</fieldset>
284
285<fieldset>
286 <label>Port</label>
9044e2e0 287 <input class="input input-text" name="DB_PORT" type="number" size="20" value="<?php echo $DB_PORT ?>"/>
bbffc43e 288 <span class="hint">Usually 3306 for MySQL or 5432 for PostgreSQL</span>
d0c6dd29
AD
289</fieldset>
290
056c537b
AD
291<h2>Other settings</h2>
292
293<p>This should be set to the location your Tiny Tiny RSS will be available on.</p>
294
295<fieldset>
296 <label>Tiny Tiny RSS URL</label>
9044e2e0 297 <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
298</fieldset>
299
300
d0c6dd29
AD
301<p><input type="submit" value="Test configuration"></p>
302
303</form>
304
305<?php if ($op == 'testconfig') { ?>
306
307 <h2>Checking configuration</h2>
308
309 <?php
310 $errors = sanity_check($DB_TYPE);
311
312 if (count($errors) > 0) {
313 print "<p>Some configuration tests failed. Please correct them before continuing.</p>";
314
315 print "<ul>";
316
317 foreach ($errors as $error) {
318 print "<li style='color : red'>$error</li>";
319 }
320
321 print "</ul>";
322
323 exit;
324 }
325
a55857db
AD
326 $notices = array();
327
328 if (!function_exists("curl_init")) {
329 array_push($notices, "It is highly recommended to enable support for CURL in PHP.");
330 }
331
332 if (count($notices) > 0) {
333 print_notice("Configuration check succeeded with minor problems:");
334
335 print "<ul>";
d0c6dd29 336
a55857db
AD
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 ?>
884d1650 347
d0c6dd29
AD
348 <h2>Checking database</h2>
349
350 <?php
bbffc43e 351 $link = db_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, $DB_TYPE, $DB_PORT);
d0c6dd29
AD
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
be9d5df1 365 $result = @db_query($link, "SELECT true FROM ttrss_feeds", $DB_TYPE, false);
d0c6dd29
AD
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 ?>"/>
056c537b 385 <input type="hidden" name="SELF_URL_PATH" value="<?php echo $SELF_URL_PATH ?>"/>
d0c6dd29
AD
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 ?>"/>
056c537b 402 <input type="hidden" name="SELF_URL_PATH" value="<?php echo $SELF_URL_PATH ?>"/>
d0c6dd29
AD
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
50d5645b 414 $link = db_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME, $DB_TYPE, $DB_PORT);
d0c6dd29
AD
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
b4cec374
AD
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.");
d0c6dd29
AD
466 }
467
b4cec374
AD
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));
d0c6dd29 482
b4cec374
AD
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 }
d0c6dd29
AD
497 }
498 ?>
499
884d1650 500</div>
d0c6dd29
AD
501
502</body>
503</html>