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