r1708 + warn about HTTP, not HTTPs
[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
01539ac3
DO
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
0e7f2155
DO
135 echo "</table>\n";
136 return !$nerrs;
137}
f48dc2cc 138
0e7f2155
DO
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.
144function init_config ()
145{
f92287f4
DO
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");
e0dcbf1e
DO
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");
f92287f4
DO
210 fwrite ($conf, "?>\n");
211 fclose ($conf);
212 echo "The configuration file has been written successfully.<br>";
0e7f2155 213 return TRUE;
f48dc2cc
DO
214}
215
96e102b2
DO
216function 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
230function init_database_static ()
0e7f2155 231{
01539ac3
DO
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 }
0e7f2155 245 echo 'Initializing the database...<br>';
96e102b2
DO
246 echo '<table border=1>';
247 echo "<tr><th>file</th><th>queries</th></tr>";
f92287f4
DO
248 foreach (array ('structure', 'dictbase', 'dictvendors') as $part)
249 {
96e102b2
DO
250 $filename = "install/init-${part}.sql";
251 echo "<tr><td>${filename}</td>";
252 $f = fopen ("install/init-${part}.sql", 'r');
253 $longq = '';
254 if ($f === FALSE)
255 {
256 echo "Failed to open install/init-${part}.sql for reading";
257 return FALSE;
258 }
259 while (!feof ($f))
260 {
261 $line = fgets ($f);
262 if (ereg ('^--', $line))
263 continue;
264 $longq .= str_replace ("\n", '', $line);
265 }
266 fclose ($f);
267 $qlist = explode (';', $longq);
96e102b2
DO
268 $nq = 0;
269 foreach ($qlist as $query)
270 {
271 if (empty ($query))
272 continue;
273 $nq++;
274 $dbxlink->exec ($query);
275 }
276 echo "<td>${nq}</td></tr>\n";
f92287f4 277 }
96e102b2 278 echo '</table>';
0e7f2155
DO
279 return TRUE;
280}
f48dc2cc 281
96e102b2
DO
282function init_database_dynamic ()
283{
284 connect_to_db();
285 global $dbxlink;
43f06d53 286 if (!isset ($_REQUEST['password']) or empty ($_REQUEST['password']))
96e102b2
DO
287 {
288 $result = $dbxlink->query ('select count(user_id) from UserAccount where user_id = 1');
289 $row = $result->fetch (PDO::FETCH_NUM);
290 $nrecs = $row[0];
291 $result->closeCursor();
292 if (!$nrecs)
293 {
294 echo '<table border=1>';
295 echo '<caption>Administrator password not set</caption>';
296 echo '<tr><td><input type=password name=password></td></tr>';
297 echo '</table>';
298 }
43f06d53 299 return FALSE;
96e102b2
DO
300 }
301 else
302 {
43f06d53
DO
303 $query = "INSERT INTO `UserAccount` (`user_id`, `user_name`, `user_enabled`, `user_password_hash`, `user_realname`) " .
304 "VALUES (1,'admin','yes',sha1('${_REQUEST['password']}'),'RackTables Administrator')";
305 $result = $dbxlink->exec ($query);
306 echo "Administrator password has been set successfully.<br>";
307 return TRUE;
96e102b2 308 }
96e102b2
DO
309}
310
0e7f2155 311function congrats ()
f48dc2cc 312{
01539ac3
DO
313 echo 'Congratulations! RackTables installation is complete. After pressing Proceed you will ';
314 echo 'enter the system. Authenticate with <strong>admin</strong> username.<br>';
315 echo "RackTables web-site runs some <a href='http://racktables.org/trac/wiki'>wiki</a> pages ";
316 echo "and <a href='http://racktables.org/trac/report/1'>a bug tracker</a>.<br>We have also got ";
317 echo "a <a href='http://www.freelists.org/list/racktables-users'>mailing list</a> for users. Have fun.<br>";
0e7f2155 318 return TRUE;
f48dc2cc
DO
319}
320
321?>