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