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