r1592 + a little more done for the installer script
[racktables] / install.php
1 <?php
2 $stepfunc[1] = 'not_already_installed';
3 $stepfunc[2] = 'platform_is_ok';
4 $stepfunc[3] = 'init_config';
5 $stepfunc[4] = 'init_database_static';
6 $stepfunc[5] = 'init_database_dynamic';
7 $stepfunc[6] = 'congrats';
8 $dbxlink = NULL;
9
10 if (isset ($_REQUEST['step']))
11 $step = $_REQUEST['step'];
12 else
13 $step = 1;
14
15 if ($step > count ($stepfunc))
16 {
17 require 'inc/init.php';
18 global $root;
19 header ("Location: " . $root);
20 exit;
21 }
22 $title = "RackTables installation: step ${step} of " . count ($stepfunc);
23 ?>
24 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
25 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
26 <head><title><?php echo $title; ?></title>
27 <link rel=stylesheet type='text/css' href=pi.css />
28 </head>
29 <body>
30 <center>
31 <?php
32 echo "<h1>${title}</h1><p>";
33
34 echo "</p><form method=post>\n";
35 $testres = $stepfunc[$step] ();
36 if ($testres)
37 {
38 $next_step = $step + 1;
39 echo "<input type=submit value='proceed'>";
40 }
41 else
42 {
43 $next_step = $step;
44 echo "<input type=submit value='retry'>";
45 }
46 echo "<input type=hidden name=step value='${next_step}'>\n";
47
48 ?>
49 </form>
50 </center>
51 </body>
52 </html>
53
54 <?php
55 // Check if the software is already installed.
56 function not_already_installed()
57 {
58 include ('inc/secret.php');
59 if (isset ($pdo_dsn))
60 {
61 echo 'Your configuration file exists and seems to hold necessary data already.<br>';
62 return FALSE;
63 }
64 else
65 {
66 echo 'There seem to be no existing installation here.<br>';
67 return TRUE;
68 }
69 }
70
71 // Check for PHP extensions.
72 function platform_is_ok ()
73 {
74 $nerrs = 0;
75 echo "<table border=1><tr><th>check item</th><th>result</th></tr>\n";
76
77 echo '<tr><td>PDO extension</td>';
78 if (class_exists ('PDO'))
79 echo '<td class=msg_success>Ok';
80 else
81 {
82 echo '<td class=msg_error>not found';
83 $nerrs++;
84 }
85 echo '</td></tr>';
86
87 echo '<tr><td>PDO-MySQL</td>';
88 if (defined ('PDO::MYSQL_ATTR_READ_DEFAULT_FILE'))
89 echo '<td class=msg_success>Ok';
90 else
91 {
92 echo '<td class=msg_error>not found';
93 $nerrs++;
94 }
95 echo '</td></tr>';
96
97 echo '<tr><td>hash functions</td>';
98 if (function_exists ('hash_algos'))
99 echo '<td class=msg_success>Ok';
100 else
101 {
102 echo '<td class=msg_error>not found';
103 $nerrs++;
104 }
105 echo '</td></tr>';
106
107 echo '<tr><td>SNMP extension</td>';
108 if (defined ('SNMP_NULL'))
109 echo '<td class=msg_success>Ok';
110 else
111 echo '<td class=msg_warning>Not found. Live SNMP tab will not function properly until the extension is installed.';
112 echo '</td></tr>';
113
114 echo '<tr><td>GD functions</td>';
115 if (defined ('IMG_PNG'))
116 echo '<td class=msg_success>Ok';
117 else
118 {
119 echo '<td class=msg_error>not found';
120 $nerrs++;
121 }
122 echo '</td></tr>';
123
124 echo "</table>\n";
125 return !$nerrs;
126 }
127
128 // Check that we can write to configuration file.
129 // If so, ask for DB connection paramaters and test
130 // the connection. Neither save the parameters nor allow
131 // going further until we succeed with the given
132 // credentials.
133 function init_config ()
134 {
135 if (!is_writable ('inc/secret.php'))
136 {
137 echo "The inc/secret.php file is not writable by web-server. Make sure it is.";
138 echo "The following commands should suffice:<pre>touch inc/secret.php\nchmod 666 inc/secret.php</pre>";
139 return FALSE;
140 }
141 if
142 (
143 !isset ($_REQUEST['save_config']) or
144 empty ($_REQUEST['mysql_host']) or
145 empty ($_REQUEST['mysql_db']) or
146 empty ($_REQUEST['mysql_username']) or
147 empty ($_REQUEST['mysql_password'])
148 )
149 {
150 echo "<input type=hidden name=save_config value=1>\n";
151 echo '<table>';
152 echo "<tr><td><label for=mysql_host>MySQL host:</label></td>";
153 echo "<td><input type=text name=mysql_host id=mysql_host value=localhost></td></tr>\n";
154 echo "<tr><td><label for=mysql_host>database:</label></td>";
155 echo "<td><input type=text name=mysql_db id=mysql_db value=racktables></td></tr>\n";
156 echo "<tr><td><label for=mysql_username>username:</label></td>";
157 echo "<td><input type=text name=mysql_username></td></tr>\n";
158 echo "<tr><td><label for=mysql_password>password:</label></td>";
159 echo "<td><input type=password name=mysql_password></td></tr>\n";
160 echo '</table>';
161 return FALSE;
162 }
163 $pdo_dsn = 'mysql:host=' . $_REQUEST['mysql_host'] . ';dbname=' . $_REQUEST['mysql_db'];
164 try
165 {
166 $dbxlink = new PDO ($pdo_dsn, $_REQUEST['mysql_username'], $_REQUEST['mysql_password']);
167 }
168 catch (PDOException $e)
169 {
170 echo "<input type=hidden name=save_config value=1>\n";
171 echo '<table>';
172 echo "<tr><td><label for=mysql_host>MySQL host:</label></td>";
173 echo "<td><input type=text name=mysql_host id=mysql_host value='" . $_REQUEST['mysql_host'] . "'></td></tr>\n";
174 echo "<tr><td><label for=mysql_host>database:</label></td>";
175 echo "<td><input type=text name=mysql_db id=mysql_db value='" . $_REQUEST['mysql_db'] . "'></td></tr>\n";
176 echo "<tr><td><label for=mysql_username>username:</label></td>";
177 echo "<td><input type=text name=mysql_username value='" . $_REQUEST['mysql_username'] . "'></td></tr>\n";
178 echo "<tr><td><label for=mysql_password>password:</label></td>";
179 echo "<td><input type=password name=mysql_password value='" . $_REQUEST['mysql_password'] . "'></td></tr>\n";
180 echo "<tr><td colspan=2>The above parameters did not work. Check and try again.</td></tr>\n";
181 echo '</table>';
182 return FALSE;
183 }
184 $conf = fopen ('inc/secret.php', 'w+');
185 if ($conf === FALSE)
186 {
187 echo "Error: failed to open inc/secret.php for writing";
188 return FALSE;
189 }
190 fwrite ($conf, "<?php\n/* This file has been generated automatically by RackTables installer.\n");
191 fwrite ($conf, " * you shouldn't normally edit it unless your database setup has changed.\n");
192 fwrite ($conf, " */\n");
193 fwrite ($conf, "\$pdo_dsn = '${pdo_dsn}';\n");
194 fwrite ($conf, "\$db_username = '" . $_REQUEST['mysql_username'] . "';\n");
195 fwrite ($conf, "\$db_password = '" . $_REQUEST['mysql_password'] . "';\n");
196 fwrite ($conf, "?>\n");
197 fclose ($conf);
198 echo "The configuration file has been written successfully.<br>";
199 return TRUE;
200 }
201
202 function connect_to_db ()
203 {
204 require ('inc/secret.php');
205 global $dbxlink;
206 try
207 {
208 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
209 }
210 catch (PDOException $e)
211 {
212 die ('Error connecting to the database');
213 }
214 }
215
216 function init_database_static ()
217 {
218 echo 'Initializing the database...<br>';
219 echo '<table border=1>';
220 echo "<tr><th>file</th><th>queries</th></tr>";
221 foreach (array ('structure', 'dictbase', 'dictvendors') as $part)
222 {
223 $filename = "install/init-${part}.sql";
224 echo "<tr><td>${filename}</td>";
225 $f = fopen ("install/init-${part}.sql", 'r');
226 $longq = '';
227 if ($f === FALSE)
228 {
229 echo "Failed to open install/init-${part}.sql for reading";
230 return FALSE;
231 }
232 while (!feof ($f))
233 {
234 $line = fgets ($f);
235 if (ereg ('^--', $line))
236 continue;
237 $longq .= str_replace ("\n", '', $line);
238 }
239 fclose ($f);
240 $qlist = explode (';', $longq);
241 connect_to_db ();
242 global $dbxlink;
243 $nq = 0;
244 foreach ($qlist as $query)
245 {
246 if (empty ($query))
247 continue;
248 $nq++;
249 $dbxlink->exec ($query);
250 }
251 echo "<td>${nq}</td></tr>\n";
252 }
253 echo '</table>';
254 return TRUE;
255 }
256
257 function init_database_dynamic ()
258 {
259 connect_to_db();
260 global $dbxlink;
261 if (!isset ($_REQUEST['password']))
262 {
263 $result = $dbxlink->query ('select count(user_id) from UserAccount where user_id = 1');
264 $row = $result->fetch (PDO::FETCH_NUM);
265 $nrecs = $row[0];
266 $result->closeCursor();
267 if (!$nrecs)
268 {
269 echo '<table border=1>';
270 echo '<caption>Administrator password not set</caption>';
271 echo '<tr><td><input type=password name=password></td></tr>';
272 echo '</table>';
273 }
274 }
275 else
276 {
277 $query = "INSERT INTO `UserAccount` (`user_id`, `user_name`, `user_enabled`, `user_password_hash`, `user_realname`)" .
278 "VALUES (1,'admin','yes',sha1(${_REQUEST['password']}),'RackTables Administrator')";
279 }
280 return FALSE;
281 }
282
283 function congrats ()
284 {
285 echo 'Congratulations! RackTables installation is complete. Press Next to open your main page.';
286 return TRUE;
287 }
288
289 ?>