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