root/tags/RackTables-0.15.1/install.php

Revision 1791, 9.4 kB (checked in by pilot, 10 months ago)

+ suppress warning about missing file, cause the case is handled anyway

Line 
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 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
32 <link rel=stylesheet type='text/css' href=pi.css />
33 </head>
34 <body>
35 <center>
36 <?php
37 echo "<h1>${title}</h1><p>";
38
39 echo "</p><form method=post>\n";
40 $testres = $stepfunc[$step] ();
41 if ($testres)
42 {
43     $next_step = $step + 1;
44     echo "<input type=submit value='proceed'>";
45 }
46 else
47 {
48     $next_step = $step;
49     echo "<input type=submit value='retry'>";
50 }
51 echo "<input type=hidden name=step value='${next_step}'>\n";
52
53 ?>
54 </form>
55 </center>
56 </body>
57 </html>
58
59 <?php
60 // Check if the software is already installed.
61 function not_already_installed()
62 {
63     @include ('inc/secret.php');
64     if (isset ($pdo_dsn))
65     {
66         echo 'Your configuration file exists and seems to hold necessary data already.<br>';
67         return FALSE;
68     }
69     else
70     {
71         echo 'There seem to be no existing installation here, I am going to setup one now.<br>';
72         return TRUE;
73     }
74 }
75
76 // Check for PHP extensions.
77 function platform_is_ok ()
78 {
79     $nerrs = 0;
80     echo "<table border=1><tr><th>check item</th><th>result</th></tr>\n";
81
82     echo '<tr><td>PDO extension</td>';
83     if (class_exists ('PDO'))
84         echo '<td class=msg_success>Ok';
85     else
86     {
87         echo '<td class=msg_error>not found';
88         $nerrs++;
89     }
90     echo '</td></tr>';
91
92     echo '<tr><td>PDO-MySQL</td>';
93     if (defined ('PDO::MYSQL_ATTR_READ_DEFAULT_FILE'))
94         echo '<td class=msg_success>Ok';
95     else
96     {
97         echo '<td class=msg_error>not found';
98         $nerrs++;
99     }
100     echo '</td></tr>';
101
102     echo '<tr><td>hash functions</td>';
103     if (function_exists ('hash_algos'))
104         echo '<td class=msg_success>Ok';
105     else
106     {
107         echo '<td class=msg_error>not found';
108         $nerrs++;
109     }
110     echo '</td></tr>';
111
112     echo '<tr><td>SNMP extension</td>';
113     if (defined ('SNMP_NULL'))
114         echo '<td class=msg_success>Ok';
115     else
116         echo '<td class=msg_warning>Not found. Live SNMP tab will not function properly until the extension is installed.';
117     echo '</td></tr>';
118
119     echo '<tr><td>GD functions</td>';
120     if (defined ('IMG_PNG'))
121         echo '<td class=msg_success>Ok';
122     else
123     {
124         echo '<td class=msg_error>not found';
125         $nerrs++;
126     }
127     echo '</td></tr>';
128
129     echo '<tr><td>HTTP scheme</td>';
130     if (!empty($_SERVER['HTTPS']))
131         echo '<td class=msg_success>HTTPs';
132     else
133         echo '<td class=msg_warning>HTTP (all your passwords will be transmitted in cleartext)';
134     echo '</td></tr>';
135
136     echo "</table>\n";
137     return !$nerrs;
138 }
139
140 // Check that we can write to configuration file.
141 // If so, ask for DB connection paramaters and test
142 // the connection. Neither save the parameters nor allow
143 // going further until we succeed with the given
144 // credentials.
145 function init_config ()
146 {
147     if (!is_writable ('inc/secret.php'))
148     {
149         echo "The inc/secret.php file is not writable by web-server. Make sure it is.";
150         echo "The following commands should suffice:<pre>touch inc/secret.php\nchmod 666 inc/secret.php</pre>";
151         return FALSE;
152     }
153     if
154     (
155         !isset ($_REQUEST['save_config']) or
156         empty ($_REQUEST['mysql_host']) or
157         empty ($_REQUEST['mysql_db']) or
158         empty ($_REQUEST['mysql_username']) or
159         empty ($_REQUEST['mysql_password'])
160     )
161     {
162         echo "<input type=hidden name=save_config value=1>\n";
163         echo '<table>';
164         echo "<tr><td><label for=mysql_host>MySQL host:</label></td>";
165         echo "<td><input type=text name=mysql_host id=mysql_host value=localhost></td></tr>\n";
166         echo "<tr><td><label for=mysql_host>database:</label></td>";
167         echo "<td><input type=text name=mysql_db id=mysql_db value=racktables></td></tr>\n";
168         echo "<tr><td><label for=mysql_username>username:</label></td>";
169         echo "<td><input type=text name=mysql_username></td></tr>\n";
170         echo "<tr><td><label for=mysql_password>password:</label></td>";
171         echo "<td><input type=password name=mysql_password></td></tr>\n";
172         echo '</table>';
173         return FALSE;
174     }
175     $pdo_dsn = 'mysql:host=' . $_REQUEST['mysql_host'] . ';dbname=' . $_REQUEST['mysql_db'];
176     try
177     {
178         $dbxlink = new PDO ($pdo_dsn, $_REQUEST['mysql_username'], $_REQUEST['mysql_password']);
179     }
180     catch (PDOException $e)
181     {
182         echo "<input type=hidden name=save_config value=1>\n";
183         echo '<table>';
184         echo "<tr><td><label for=mysql_host>MySQL host:</label></td>";
185         echo "<td><input type=text name=mysql_host id=mysql_host value='" . $_REQUEST['mysql_host'] . "'></td></tr>\n";
186         echo "<tr><td><label for=mysql_host>database:</label></td>";
187         echo "<td><input type=text name=mysql_db id=mysql_db value='" . $_REQUEST['mysql_db'] . "'></td></tr>\n";
188         echo "<tr><td><label for=mysql_username>username:</label></td>";
189         echo "<td><input type=text name=mysql_username value='" . $_REQUEST['mysql_username'] . "'></td></tr>\n";
190         echo "<tr><td><label for=mysql_password>password:</label></td>";
191         echo "<td><input type=password name=mysql_password value='" . $_REQUEST['mysql_password'] . "'></td></tr>\n";
192         echo "<tr><td colspan=2>The above parameters did not work. Check and try again.</td></tr>\n";
193         echo '</table>';
194         return FALSE;
195     }
196     $conf = fopen ('inc/secret.php', 'w+');
197     if ($conf === FALSE)
198     {
199         echo "Error: failed to open inc/secret.php for writing";
200         return FALSE;
201     }
202     fwrite ($conf, "<?php\n/* This file has been generated automatically by RackTables installer.\n");
203     fwrite ($conf, " * you shouldn't normally edit it unless your database setup has changed.\n");
204     fwrite ($conf, " */\n");
205     fwrite ($conf, "\$pdo_dsn = '${pdo_dsn}';\n");
206     fwrite ($conf, "\$db_username = '" . $_REQUEST['mysql_username'] . "';\n");
207     fwrite ($conf, "\$db_password = '" . $_REQUEST['mysql_password'] . "';\n\n");
208     fwrite ($conf, "// This is only necessary for 'ldap' USER_AUTH_SRC\n");
209     fwrite ($conf, "\$ldap_server = 'some.server';\n");
210     fwrite ($conf, "\$ldap_domain = 'some.domain';\n");
211     fwrite ($conf, "?>\n");
212     fclose ($conf);
213     echo "The configuration file has been written successfully.<br>";
214     return TRUE;
215 }
216
217 function connect_to_db ()
218 {
219     require ('inc/secret.php');
220     global $dbxlink;
221     try
222     {
223         $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
224     }
225     catch (PDOException $e)
226     {
227         die ('Error connecting to the database');
228     }
229 }
230
231 function init_database_static ()
232 {
233     connect_to_db ();
234     global $dbxlink;
235     $result = $dbxlink->query ('show tables');
236     $tables = $result->fetchAll (PDO::FETCH_NUM);
237     $result->closeCursor();
238     unset ($result);
239     if (count ($tables))
240     {
241         echo 'Your database is already holding ' . count ($tables);
242         echo ' tables, so I will stop here and let you check it yourself.<br>';
243         echo 'There is some important data there probably.<br>';
244         return FALSE;
245     }
246     echo 'Initializing the database...<br>';
247     echo '<table border=1>';
248     echo "<tr><th>file</th><th>queries</th><th>errors</th></tr>";
249     $errlist = array();
250     foreach (array ('structure', 'dictbase', 'dictvendors') as $part)
251     {
252         $filename = "install/init-${part}.sql";
253         echo "<tr><td>${filename}</td>";
254         $f = fopen ("install/init-${part}.sql", 'r');
255         if ($f === FALSE)
256         {
257             echo "Failed to open install/init-${part}.sql for reading";
258             return FALSE;
259         }
260         $longq = '';
261         while (!feof ($f))
262         {
263             $line = fgets ($f);
264             if (ereg ('^--', $line))
265                 continue;
266             $longq .= $line;
267         }
268         fclose ($f);
269         $nq = $nerrs = 0;
270         foreach (explode (";\n", $longq) as $query)
271         {
272             if (empty ($query))
273                 continue;
274             $nq++;
275             if ($dbxlink->exec ($query) === FALSE)
276             {
277                 $nerrs++;
278                 $errlist[] = $query;
279             }
280         }
281         echo "<td>${nq}</td><td>${nerrs}</td></tr>\n";
282     }
283     echo '</table>';
284     if (count ($errlist))
285     {
286         echo '<pre>The following queries failed:\n';
287         foreach ($errlist as $q)
288             echo "${q}\n\n";
289         echo '</pre>';
290         return FALSE;
291     }
292     return TRUE;
293 }
294
295 function init_database_dynamic ()
296 {
297     connect_to_db();
298     global $dbxlink;
299     if (!isset ($_REQUEST['password']) or empty ($_REQUEST['password']))
300     {
301         $result = $dbxlink->query ('select count(user_id) from UserAccount where user_id = 1');
302         $row = $result->fetch (PDO::FETCH_NUM);
303         $nrecs = $row[0];
304         $result->closeCursor();
305         if (!$nrecs)
306         {
307             echo '<table border=1>';
308             echo '<caption>Administrator password not set</caption>';
309             echo '<tr><td><input type=password name=password></td></tr>';
310             echo '</table>';
311         }
312         return FALSE;
313     }
314     else
315     {
316         $query = "INSERT INTO `UserAccount` (`user_id`, `user_name`, `user_enabled`, `user_password_hash`, `user_realname`) " .
317             "VALUES (1,'admin','yes',sha1('${_REQUEST['password']}'),'RackTables Administrator')";
318         $result = $dbxlink->exec ($query);
319         echo "Administrator password has been set successfully.<br>";
320         return TRUE;
321     }
322 }
323
324 function congrats ()
325 {
326     echo 'Congratulations! RackTables installation is complete. After pressing Proceed you will ';
327     echo 'enter the system. Authenticate with <strong>admin</strong> username.<br>';
328     echo "RackTables web-site runs some <a href='http://racktables.org/trac/wiki'>wiki</a> pages ";
329     echo "and <a href='http://racktables.org/trac/report/1'>a bug tracker</a>.<br>We have also got ";
330     echo "a <a href='http://www.freelists.org/list/racktables-users'>mailing list</a> for users. Have fun.<br>";
331     return TRUE;
332 }
333
334 ?>
335
Note: See TracBrowser for help on using the browser.