r1705 + more fixes to make the installer look like working
[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 "</table>\n";
129 return !$nerrs;
130 }
131
132 // Check that we can write to configuration file.
133 // If so, ask for DB connection paramaters and test
134 // the connection. Neither save the parameters nor allow
135 // going further until we succeed with the given
136 // credentials.
137 function init_config ()
138 {
139 if (!is_writable ('inc/secret.php'))
140 {
141 echo "The inc/secret.php file is not writable by web-server. Make sure it is.";
142 echo "The following commands should suffice:<pre>touch inc/secret.php\nchmod 666 inc/secret.php</pre>";
143 return FALSE;
144 }
145 if
146 (
147 !isset ($_REQUEST['save_config']) or
148 empty ($_REQUEST['mysql_host']) or
149 empty ($_REQUEST['mysql_db']) or
150 empty ($_REQUEST['mysql_username']) or
151 empty ($_REQUEST['mysql_password'])
152 )
153 {
154 echo "<input type=hidden name=save_config value=1>\n";
155 echo '<table>';
156 echo "<tr><td><label for=mysql_host>MySQL host:</label></td>";
157 echo "<td><input type=text name=mysql_host id=mysql_host value=localhost></td></tr>\n";
158 echo "<tr><td><label for=mysql_host>database:</label></td>";
159 echo "<td><input type=text name=mysql_db id=mysql_db value=racktables></td></tr>\n";
160 echo "<tr><td><label for=mysql_username>username:</label></td>";
161 echo "<td><input type=text name=mysql_username></td></tr>\n";
162 echo "<tr><td><label for=mysql_password>password:</label></td>";
163 echo "<td><input type=password name=mysql_password></td></tr>\n";
164 echo '</table>';
165 return FALSE;
166 }
167 $pdo_dsn = 'mysql:host=' . $_REQUEST['mysql_host'] . ';dbname=' . $_REQUEST['mysql_db'];
168 try
169 {
170 $dbxlink = new PDO ($pdo_dsn, $_REQUEST['mysql_username'], $_REQUEST['mysql_password']);
171 }
172 catch (PDOException $e)
173 {
174 echo "<input type=hidden name=save_config value=1>\n";
175 echo '<table>';
176 echo "<tr><td><label for=mysql_host>MySQL host:</label></td>";
177 echo "<td><input type=text name=mysql_host id=mysql_host value='" . $_REQUEST['mysql_host'] . "'></td></tr>\n";
178 echo "<tr><td><label for=mysql_host>database:</label></td>";
179 echo "<td><input type=text name=mysql_db id=mysql_db value='" . $_REQUEST['mysql_db'] . "'></td></tr>\n";
180 echo "<tr><td><label for=mysql_username>username:</label></td>";
181 echo "<td><input type=text name=mysql_username value='" . $_REQUEST['mysql_username'] . "'></td></tr>\n";
182 echo "<tr><td><label for=mysql_password>password:</label></td>";
183 echo "<td><input type=password name=mysql_password value='" . $_REQUEST['mysql_password'] . "'></td></tr>\n";
184 echo "<tr><td colspan=2>The above parameters did not work. Check and try again.</td></tr>\n";
185 echo '</table>';
186 return FALSE;
187 }
188 $conf = fopen ('inc/secret.php', 'w+');
189 if ($conf === FALSE)
190 {
191 echo "Error: failed to open inc/secret.php for writing";
192 return FALSE;
193 }
194 fwrite ($conf, "<?php\n/* This file has been generated automatically by RackTables installer.\n");
195 fwrite ($conf, " * you shouldn't normally edit it unless your database setup has changed.\n");
196 fwrite ($conf, " */\n");
197 fwrite ($conf, "\$pdo_dsn = '${pdo_dsn}';\n");
198 fwrite ($conf, "\$db_username = '" . $_REQUEST['mysql_username'] . "';\n");
199 fwrite ($conf, "\$db_password = '" . $_REQUEST['mysql_password'] . "';\n");
200 fwrite ($conf, "?>\n");
201 fclose ($conf);
202 echo "The configuration file has been written successfully.<br>";
203 return TRUE;
204 }
205
206 function connect_to_db ()
207 {
208 require ('inc/secret.php');
209 global $dbxlink;
210 try
211 {
212 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
213 }
214 catch (PDOException $e)
215 {
216 die ('Error connecting to the database');
217 }
218 }
219
220 function init_database_static ()
221 {
222 echo 'Initializing the database...<br>';
223 echo '<table border=1>';
224 echo "<tr><th>file</th><th>queries</th></tr>";
225 foreach (array ('structure', 'dictbase', 'dictvendors') as $part)
226 {
227 $filename = "install/init-${part}.sql";
228 echo "<tr><td>${filename}</td>";
229 $f = fopen ("install/init-${part}.sql", 'r');
230 $longq = '';
231 if ($f === FALSE)
232 {
233 echo "Failed to open install/init-${part}.sql for reading";
234 return FALSE;
235 }
236 while (!feof ($f))
237 {
238 $line = fgets ($f);
239 if (ereg ('^--', $line))
240 continue;
241 $longq .= str_replace ("\n", '', $line);
242 }
243 fclose ($f);
244 $qlist = explode (';', $longq);
245 connect_to_db ();
246 global $dbxlink;
247 $nq = 0;
248 foreach ($qlist as $query)
249 {
250 if (empty ($query))
251 continue;
252 $nq++;
253 $dbxlink->exec ($query);
254 }
255 echo "<td>${nq}</td></tr>\n";
256 }
257 echo '</table>';
258 return TRUE;
259 }
260
261 function init_database_dynamic ()
262 {
263 connect_to_db();
264 global $dbxlink;
265 if (!isset ($_REQUEST['password']) or empty ($_REQUEST['password']))
266 {
267 $result = $dbxlink->query ('select count(user_id) from UserAccount where user_id = 1');
268 $row = $result->fetch (PDO::FETCH_NUM);
269 $nrecs = $row[0];
270 $result->closeCursor();
271 if (!$nrecs)
272 {
273 echo '<table border=1>';
274 echo '<caption>Administrator password not set</caption>';
275 echo '<tr><td><input type=password name=password></td></tr>';
276 echo '</table>';
277 }
278 return FALSE;
279 }
280 else
281 {
282 $query = "INSERT INTO `UserAccount` (`user_id`, `user_name`, `user_enabled`, `user_password_hash`, `user_realname`) " .
283 "VALUES (1,'admin','yes',sha1('${_REQUEST['password']}'),'RackTables Administrator')";
284 $result = $dbxlink->exec ($query);
285 echo "Administrator password has been set successfully.<br>";
286 return TRUE;
287 }
288 }
289
290 function congrats ()
291 {
292 echo 'Congratulations! RackTables installation is complete. Press Proceed to open your main page.<br>';
293 return TRUE;
294 }
295
296 ?>