r1704 + add console KVM port to a new console, not the host 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 {
70 echo 'There seem to be no existing installation here.<br>';
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");
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>";
0e7f2155 203 return TRUE;
f48dc2cc
DO
204}
205
96e102b2
DO
206function 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
220function init_database_static ()
0e7f2155
DO
221{
222 echo 'Initializing the database...<br>';
96e102b2
DO
223 echo '<table border=1>';
224 echo "<tr><th>file</th><th>queries</th></tr>";
f92287f4
DO
225 foreach (array ('structure', 'dictbase', 'dictvendors') as $part)
226 {
96e102b2
DO
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";
f92287f4 256 }
96e102b2 257 echo '</table>';
0e7f2155
DO
258 return TRUE;
259}
f48dc2cc 260
96e102b2
DO
261function init_database_dynamic ()
262{
263 connect_to_db();
264 global $dbxlink;
265 if (!isset ($_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 }
279 else
280 {
281 $query = "INSERT INTO `UserAccount` (`user_id`, `user_name`, `user_enabled`, `user_password_hash`, `user_realname`)" .
282 "VALUES (1,'admin','yes',sha1(${_REQUEST['password']}),'RackTables Administrator')";
283 }
284 return FALSE;
285}
286
0e7f2155 287function congrats ()
f48dc2cc 288{
0e7f2155
DO
289 echo 'Congratulations! RackTables installation is complete. Press Next to open your main page.';
290 return TRUE;
f48dc2cc
DO
291}
292
293?>