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