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