r1707 + list console HW chapter as stock one
[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>
f48dc2cc
DO
31<link rel=stylesheet type='text/css' href=pi.css />
32</head>
33<body>
0e7f2155 34<center>
f48dc2cc 35<?php
f92287f4
DO
36echo "<h1>${title}</h1><p>";
37
38echo "</p><form method=post>\n";
39$testres = $stepfunc[$step] ();
40if ($testres)
41{
42 $next_step = $step + 1;
43 echo "<input type=submit value='proceed'>";
44}
45else
46{
47 $next_step = $step;
48 echo "<input type=submit value='retry'>";
49}
50echo "<input type=hidden name=step value='${next_step}'>\n";
51
52?>
53</form>
54</center>
55</body>
56</html>
57
58<?php
0e7f2155
DO
59// Check if the software is already installed.
60function not_already_installed()
f48dc2cc 61{
f92287f4
DO
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 {
43f06d53 70 echo 'There seem to be no existing installation here, I am going to setup one now.<br>';
f92287f4
DO
71 return TRUE;
72 }
f48dc2cc
DO
73}
74
0e7f2155
DO
75// Check for PHP extensions.
76function platform_is_ok ()
f48dc2cc 77{
0e7f2155
DO
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
f48dc2cc 85 {
0e7f2155
DO
86 echo '<td class=msg_error>not found';
87 $nerrs++;
88 }
89 echo '</td></tr>';
f48dc2cc 90
0e7f2155
DO
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>';
f48dc2cc 100
0e7f2155
DO
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>';
f48dc2cc 110
0e7f2155
DO
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>';
f48dc2cc 117
0e7f2155
DO
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>';
f48dc2cc 127
0e7f2155
DO
128 echo "</table>\n";
129 return !$nerrs;
130}
f48dc2cc 131
0e7f2155
DO
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.
137function init_config ()
138{
f92287f4
DO
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");
e0dcbf1e
DO
199 fwrite ($conf, "\$db_password = '" . $_REQUEST['mysql_password'] . "';\n\n");
200 fwrite ($conf, "// This is only necessary for 'ldap' USER_AUTH_SRC\n");
201 fwrite ($conf, "\$ldap_server = 'some.server';\n");
202 fwrite ($conf, "\$ldap_domain = 'some.domain';\n");
f92287f4
DO
203 fwrite ($conf, "?>\n");
204 fclose ($conf);
205 echo "The configuration file has been written successfully.<br>";
0e7f2155 206 return TRUE;
f48dc2cc
DO
207}
208
96e102b2
DO
209function connect_to_db ()
210{
211 require ('inc/secret.php');
212 global $dbxlink;
213 try
214 {
215 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
216 }
217 catch (PDOException $e)
218 {
219 die ('Error connecting to the database');
220 }
221}
222
223function init_database_static ()
0e7f2155
DO
224{
225 echo 'Initializing the database...<br>';
96e102b2
DO
226 echo '<table border=1>';
227 echo "<tr><th>file</th><th>queries</th></tr>";
f92287f4
DO
228 foreach (array ('structure', 'dictbase', 'dictvendors') as $part)
229 {
96e102b2
DO
230 $filename = "install/init-${part}.sql";
231 echo "<tr><td>${filename}</td>";
232 $f = fopen ("install/init-${part}.sql", 'r');
233 $longq = '';
234 if ($f === FALSE)
235 {
236 echo "Failed to open install/init-${part}.sql for reading";
237 return FALSE;
238 }
239 while (!feof ($f))
240 {
241 $line = fgets ($f);
242 if (ereg ('^--', $line))
243 continue;
244 $longq .= str_replace ("\n", '', $line);
245 }
246 fclose ($f);
247 $qlist = explode (';', $longq);
248 connect_to_db ();
249 global $dbxlink;
250 $nq = 0;
251 foreach ($qlist as $query)
252 {
253 if (empty ($query))
254 continue;
255 $nq++;
256 $dbxlink->exec ($query);
257 }
258 echo "<td>${nq}</td></tr>\n";
f92287f4 259 }
96e102b2 260 echo '</table>';
0e7f2155
DO
261 return TRUE;
262}
f48dc2cc 263
96e102b2
DO
264function init_database_dynamic ()
265{
266 connect_to_db();
267 global $dbxlink;
43f06d53 268 if (!isset ($_REQUEST['password']) or empty ($_REQUEST['password']))
96e102b2
DO
269 {
270 $result = $dbxlink->query ('select count(user_id) from UserAccount where user_id = 1');
271 $row = $result->fetch (PDO::FETCH_NUM);
272 $nrecs = $row[0];
273 $result->closeCursor();
274 if (!$nrecs)
275 {
276 echo '<table border=1>';
277 echo '<caption>Administrator password not set</caption>';
278 echo '<tr><td><input type=password name=password></td></tr>';
279 echo '</table>';
280 }
43f06d53 281 return FALSE;
96e102b2
DO
282 }
283 else
284 {
43f06d53
DO
285 $query = "INSERT INTO `UserAccount` (`user_id`, `user_name`, `user_enabled`, `user_password_hash`, `user_realname`) " .
286 "VALUES (1,'admin','yes',sha1('${_REQUEST['password']}'),'RackTables Administrator')";
287 $result = $dbxlink->exec ($query);
288 echo "Administrator password has been set successfully.<br>";
289 return TRUE;
96e102b2 290 }
96e102b2
DO
291}
292
0e7f2155 293function congrats ()
f48dc2cc 294{
43f06d53 295 echo 'Congratulations! RackTables installation is complete. Press Proceed to open your main page.<br>';
0e7f2155 296 return TRUE;
f48dc2cc
DO
297}
298
299?>