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