r2787 - also list 0.16.5 and 0.16.6; address some records accordingly
[racktables] / install.php
CommitLineData
0e7f2155 1<?php
5b4523dc 2
d3372430
DO
3// This script is intended for execution through a web-browser, e.g.:
4// https://example.com/racktables/install.php
5// See README file for more information.
5b4523dc 6
0e7f2155
DO
7$stepfunc[1] = 'not_already_installed';
8$stepfunc[2] = 'platform_is_ok';
9$stepfunc[3] = 'init_config';
96e102b2
DO
10$stepfunc[4] = 'init_database_static';
11$stepfunc[5] = 'init_database_dynamic';
12$stepfunc[6] = 'congrats';
13$dbxlink = NULL;
0e7f2155
DO
14
15if (isset ($_REQUEST['step']))
16 $step = $_REQUEST['step'];
17else
18 $step = 1;
19
20if ($step > count ($stepfunc))
21{
22 require 'inc/init.php';
23 global $root;
24 header ("Location: " . $root);
25 exit;
26}
27$title = "RackTables installation: step ${step} of " . count ($stepfunc);
28?>
f48dc2cc
DO
29<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
30<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
0e7f2155 31<head><title><?php echo $title; ?></title>
21fd978f 32<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
f48dc2cc
DO
33<link rel=stylesheet type='text/css' href=pi.css />
34</head>
35<body>
0e7f2155 36<center>
f48dc2cc 37<?php
f92287f4
DO
38echo "<h1>${title}</h1><p>";
39
40echo "</p><form method=post>\n";
41$testres = $stepfunc[$step] ();
42if ($testres)
43{
44 $next_step = $step + 1;
45 echo "<input type=submit value='proceed'>";
46}
47else
48{
49 $next_step = $step;
50 echo "<input type=submit value='retry'>";
51}
52echo "<input type=hidden name=step value='${next_step}'>\n";
53
54?>
55</form>
56</center>
57</body>
58</html>
59
60<?php
0e7f2155
DO
61// Check if the software is already installed.
62function not_already_installed()
f48dc2cc 63{
80138748 64 @include ('inc/secret.php');
f92287f4
DO
65 if (isset ($pdo_dsn))
66 {
67 echo 'Your configuration file exists and seems to hold necessary data already.<br>';
68 return FALSE;
69 }
70 else
71 {
43f06d53 72 echo 'There seem to be no existing installation here, I am going to setup one now.<br>';
f92287f4
DO
73 return TRUE;
74 }
f48dc2cc
DO
75}
76
0e7f2155
DO
77// Check for PHP extensions.
78function platform_is_ok ()
f48dc2cc 79{
0e7f2155
DO
80 $nerrs = 0;
81 echo "<table border=1><tr><th>check item</th><th>result</th></tr>\n";
82
83 echo '<tr><td>PDO extension</td>';
84 if (class_exists ('PDO'))
85 echo '<td class=msg_success>Ok';
86 else
f48dc2cc 87 {
0e7f2155
DO
88 echo '<td class=msg_error>not found';
89 $nerrs++;
90 }
91 echo '</td></tr>';
f48dc2cc 92
0e7f2155
DO
93 echo '<tr><td>PDO-MySQL</td>';
94 if (defined ('PDO::MYSQL_ATTR_READ_DEFAULT_FILE'))
95 echo '<td class=msg_success>Ok';
96 else
97 {
98 echo '<td class=msg_error>not found';
99 $nerrs++;
100 }
101 echo '</td></tr>';
f48dc2cc 102
93bdb7ba
DO
103 echo '<tr><td>hash function</td>';
104 if (function_exists ('sha1'))
0e7f2155
DO
105 echo '<td class=msg_success>Ok';
106 else
107 {
108 echo '<td class=msg_error>not found';
109 $nerrs++;
110 }
111 echo '</td></tr>';
f48dc2cc 112
0e7f2155
DO
113 echo '<tr><td>SNMP extension</td>';
114 if (defined ('SNMP_NULL'))
115 echo '<td class=msg_success>Ok';
116 else
117 echo '<td class=msg_warning>Not found. Live SNMP tab will not function properly until the extension is installed.';
118 echo '</td></tr>';
f48dc2cc 119
0e7f2155
DO
120 echo '<tr><td>GD functions</td>';
121 if (defined ('IMG_PNG'))
122 echo '<td class=msg_success>Ok';
123 else
124 {
125 echo '<td class=msg_error>not found';
126 $nerrs++;
127 }
128 echo '</td></tr>';
f48dc2cc 129
01539ac3 130 echo '<tr><td>HTTP scheme</td>';
fff18256 131 if (!empty($_SERVER['HTTPS']) and $_SERVER['HTTPS'] != 'off')
01539ac3
DO
132 echo '<td class=msg_success>HTTPs';
133 else
134 echo '<td class=msg_warning>HTTP (all your passwords will be transmitted in cleartext)';
135 echo '</td></tr>';
136
9ddee5ea
DO
137 echo '<tr><td>Multibyte string extension</td>';
138 if (defined ('MB_CASE_LOWER'))
139 echo '<td class=msg_success>Ok';
140 else
141 {
142 echo '<td class=msg_error>not found';
143 $nerrs++;
144 }
145 echo '</td></tr>';
146
147 echo '<tr><td>LDAP extension</td>';
148 if (defined ('LDAP_OPT_DEREF'))
149 echo '<td class=msg_success>Ok';
150 else
151 {
152 echo '<td class=msg_warning>not found, LDAP authentication will not work';
153 }
154 echo '</td></tr>';
155
0e7f2155
DO
156 echo "</table>\n";
157 return !$nerrs;
158}
f48dc2cc 159
0e7f2155
DO
160// Check that we can write to configuration file.
161// If so, ask for DB connection paramaters and test
162// the connection. Neither save the parameters nor allow
163// going further until we succeed with the given
164// credentials.
165function init_config ()
166{
80138748 167 if (!is_writable ('inc/secret.php'))
f92287f4 168 {
80138748
DO
169 echo "The inc/secret.php file is not writable by web-server. Make sure it is.";
170 echo "The following commands should suffice:<pre>touch inc/secret.php\nchmod 666 inc/secret.php</pre>";
9b16b1f1 171 echo 'Fedora Linux with SELinux may require this file to be owned by specific user (apache) and/or executing "setenforce 0" for the time of installation. ';
029563a4 172 echo 'SELinux may be turned back on with "setenforce 1" command.';
f92287f4
DO
173 return FALSE;
174 }
175 if
176 (
177 !isset ($_REQUEST['save_config']) or
178 empty ($_REQUEST['mysql_host']) or
179 empty ($_REQUEST['mysql_db']) or
180 empty ($_REQUEST['mysql_username']) or
181 empty ($_REQUEST['mysql_password'])
182 )
183 {
184 echo "<input type=hidden name=save_config value=1>\n";
185 echo '<table>';
186 echo "<tr><td><label for=mysql_host>MySQL host:</label></td>";
187 echo "<td><input type=text name=mysql_host id=mysql_host value=localhost></td></tr>\n";
188 echo "<tr><td><label for=mysql_host>database:</label></td>";
189 echo "<td><input type=text name=mysql_db id=mysql_db value=racktables></td></tr>\n";
190 echo "<tr><td><label for=mysql_username>username:</label></td>";
191 echo "<td><input type=text name=mysql_username></td></tr>\n";
192 echo "<tr><td><label for=mysql_password>password:</label></td>";
193 echo "<td><input type=password name=mysql_password></td></tr>\n";
194 echo '</table>';
195 return FALSE;
196 }
197 $pdo_dsn = 'mysql:host=' . $_REQUEST['mysql_host'] . ';dbname=' . $_REQUEST['mysql_db'];
198 try
199 {
200 $dbxlink = new PDO ($pdo_dsn, $_REQUEST['mysql_username'], $_REQUEST['mysql_password']);
201 }
202 catch (PDOException $e)
203 {
204 echo "<input type=hidden name=save_config value=1>\n";
205 echo '<table>';
206 echo "<tr><td><label for=mysql_host>MySQL host:</label></td>";
207 echo "<td><input type=text name=mysql_host id=mysql_host value='" . $_REQUEST['mysql_host'] . "'></td></tr>\n";
208 echo "<tr><td><label for=mysql_host>database:</label></td>";
209 echo "<td><input type=text name=mysql_db id=mysql_db value='" . $_REQUEST['mysql_db'] . "'></td></tr>\n";
210 echo "<tr><td><label for=mysql_username>username:</label></td>";
211 echo "<td><input type=text name=mysql_username value='" . $_REQUEST['mysql_username'] . "'></td></tr>\n";
212 echo "<tr><td><label for=mysql_password>password:</label></td>";
213 echo "<td><input type=password name=mysql_password value='" . $_REQUEST['mysql_password'] . "'></td></tr>\n";
214 echo "<tr><td colspan=2>The above parameters did not work. Check and try again.</td></tr>\n";
215 echo '</table>';
216 return FALSE;
217 }
e1ae3fb4
AD
218
219 // Make sure InnoDB is supported
220 require_once 'inc/database.php';
221 if (!isInnoDBSupported ($dbxlink))
222 {
223 echo 'Error: InnoDB support is disabled. See the README for details.';
224 return FALSE;
225 }
226
80138748 227 $conf = fopen ('inc/secret.php', 'w+');
f92287f4
DO
228 if ($conf === FALSE)
229 {
80138748 230 echo "Error: failed to open inc/secret.php for writing";
f92287f4
DO
231 return FALSE;
232 }
233 fwrite ($conf, "<?php\n/* This file has been generated automatically by RackTables installer.\n");
234 fwrite ($conf, " * you shouldn't normally edit it unless your database setup has changed.\n");
235 fwrite ($conf, " */\n");
236 fwrite ($conf, "\$pdo_dsn = '${pdo_dsn}';\n");
237 fwrite ($conf, "\$db_username = '" . $_REQUEST['mysql_username'] . "';\n");
e0dcbf1e 238 fwrite ($conf, "\$db_password = '" . $_REQUEST['mysql_password'] . "';\n\n");
204284ba
DO
239 fwrite ($conf, "// More info http://racktables.org/trac/wiki/RackTablesUserAuthentication\n");
240 fwrite ($conf, "\$user_auth_src = 'database';\n");
b82cce3f 241 fwrite ($conf, "\$require_local_account = TRUE;\n\n");
204284ba 242 fwrite ($conf, "// This is only necessary for 'ldap' authentication source\n");
e0dcbf1e 243 fwrite ($conf, "\$ldap_server = 'some.server';\n");
204284ba
DO
244 fwrite ($conf, "\$ldap_domain = 'some.domain';\n\n");
245 fwrite ($conf, "#\$ldap_search_dn = 'ou=people,O=YourCompany';\n");
246 fwrite ($conf, "\$ldap_search_attr = 'uid';\n");
f92287f4
DO
247 fwrite ($conf, "?>\n");
248 fclose ($conf);
249 echo "The configuration file has been written successfully.<br>";
0e7f2155 250 return TRUE;
f48dc2cc
DO
251}
252
96e102b2
DO
253function connect_to_db ()
254{
80138748 255 require ('inc/secret.php');
96e102b2
DO
256 global $dbxlink;
257 try
258 {
259 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
260 }
261 catch (PDOException $e)
262 {
263 die ('Error connecting to the database');
264 }
265}
266
267function init_database_static ()
0e7f2155 268{
01539ac3
DO
269 connect_to_db ();
270 global $dbxlink;
271 $result = $dbxlink->query ('show tables');
272 $tables = $result->fetchAll (PDO::FETCH_NUM);
273 $result->closeCursor();
274 unset ($result);
275 if (count ($tables))
276 {
277 echo 'Your database is already holding ' . count ($tables);
278 echo ' tables, so I will stop here and let you check it yourself.<br>';
279 echo 'There is some important data there probably.<br>';
280 return FALSE;
281 }
0e7f2155 282 echo 'Initializing the database...<br>';
96e102b2 283 echo '<table border=1>';
b2bdfd89
DO
284 echo "<tr><th>file</th><th>queries</th><th>errors</th></tr>";
285 $errlist = array();
f92287f4
DO
286 foreach (array ('structure', 'dictbase', 'dictvendors') as $part)
287 {
96e102b2
DO
288 $filename = "install/init-${part}.sql";
289 echo "<tr><td>${filename}</td>";
290 $f = fopen ("install/init-${part}.sql", 'r');
96e102b2
DO
291 if ($f === FALSE)
292 {
293 echo "Failed to open install/init-${part}.sql for reading";
294 return FALSE;
295 }
b2bdfd89 296 $longq = '';
96e102b2
DO
297 while (!feof ($f))
298 {
299 $line = fgets ($f);
300 if (ereg ('^--', $line))
301 continue;
b2bdfd89 302 $longq .= $line;
96e102b2
DO
303 }
304 fclose ($f);
b2bdfd89 305 $nq = $nerrs = 0;
5fe42ae4 306 foreach (preg_split ("/;\s*\n/", $longq) as $query)
96e102b2 307 {
5fe42ae4 308 $query = trim($query);
96e102b2
DO
309 if (empty ($query))
310 continue;
311 $nq++;
b2bdfd89
DO
312 if ($dbxlink->exec ($query) === FALSE)
313 {
314 $nerrs++;
315 $errlist[] = $query;
316 }
96e102b2 317 }
b2bdfd89 318 echo "<td>${nq}</td><td>${nerrs}</td></tr>\n";
f92287f4 319 }
96e102b2 320 echo '</table>';
b2bdfd89
DO
321 if (count ($errlist))
322 {
323 echo '<pre>The following queries failed:\n';
324 foreach ($errlist as $q)
325 echo "${q}\n\n";
326 echo '</pre>';
327 return FALSE;
328 }
0e7f2155
DO
329 return TRUE;
330}
f48dc2cc 331
96e102b2
DO
332function init_database_dynamic ()
333{
334 connect_to_db();
335 global $dbxlink;
43f06d53 336 if (!isset ($_REQUEST['password']) or empty ($_REQUEST['password']))
96e102b2
DO
337 {
338 $result = $dbxlink->query ('select count(user_id) from UserAccount where user_id = 1');
339 $row = $result->fetch (PDO::FETCH_NUM);
340 $nrecs = $row[0];
341 $result->closeCursor();
342 if (!$nrecs)
343 {
344 echo '<table border=1>';
345 echo '<caption>Administrator password not set</caption>';
346 echo '<tr><td><input type=password name=password></td></tr>';
347 echo '</table>';
348 }
43f06d53 349 return FALSE;
96e102b2
DO
350 }
351 else
352 {
93bdb7ba
DO
353 // Never send cleartext password over the wire.
354 $hash = sha1 ($_REQUEST['password']);
79b8ad1e 355 $query = "INSERT INTO `UserAccount` (`user_id`, `user_name`, `user_password_hash`, `user_realname`) " .
93bdb7ba 356 "VALUES (1,'admin','${hash}','RackTables Administrator')";
43f06d53
DO
357 $result = $dbxlink->exec ($query);
358 echo "Administrator password has been set successfully.<br>";
359 return TRUE;
96e102b2 360 }
96e102b2
DO
361}
362
0e7f2155 363function congrats ()
f48dc2cc 364{
01539ac3
DO
365 echo 'Congratulations! RackTables installation is complete. After pressing Proceed you will ';
366 echo 'enter the system. Authenticate with <strong>admin</strong> username.<br>';
367 echo "RackTables web-site runs some <a href='http://racktables.org/trac/wiki'>wiki</a> pages ";
368 echo "and <a href='http://racktables.org/trac/report/1'>a bug tracker</a>.<br>We have also got ";
369 echo "a <a href='http://www.freelists.org/list/racktables-users'>mailing list</a> for users. Have fun.<br>";
0e7f2155 370 return TRUE;
f48dc2cc
DO
371}
372
373?>