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