r1570 + further enhancements to the installer page
[racktables] / install.php
CommitLineData
0e7f2155
DO
1<?php
2$stepfunc[1] = 'not_already_installed';
3$stepfunc[2] = 'platform_is_ok';
4$stepfunc[3] = 'init_config';
5$stepfunc[4] = 'init_database';
6$stepfunc[5] = 'congrats';
7
8if (isset ($_REQUEST['step']))
9 $step = $_REQUEST['step'];
10else
11 $step = 1;
12
13if ($step > count ($stepfunc))
14{
15 require 'inc/init.php';
16 global $root;
17 header ("Location: " . $root);
18 exit;
19}
20$title = "RackTables installation: step ${step} of " . count ($stepfunc);
21?>
f48dc2cc
DO
22<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
23<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
0e7f2155 24<head><title><?php echo $title; ?></title>
f48dc2cc
DO
25<link rel=stylesheet type='text/css' href=pi.css />
26</head>
27<body>
0e7f2155 28<center>
f48dc2cc 29<?php
f92287f4
DO
30echo "<h1>${title}</h1><p>";
31
32echo "</p><form method=post>\n";
33$testres = $stepfunc[$step] ();
34if ($testres)
35{
36 $next_step = $step + 1;
37 echo "<input type=submit value='proceed'>";
38}
39else
40{
41 $next_step = $step;
42 echo "<input type=submit value='retry'>";
43}
44echo "<input type=hidden name=step value='${next_step}'>\n";
45
46?>
47</form>
48</center>
49</body>
50</html>
51
52<?php
0e7f2155
DO
53// Check if the software is already installed.
54function not_already_installed()
f48dc2cc 55{
f92287f4
DO
56 include ('inc/secret.php');
57 if (isset ($pdo_dsn))
58 {
59 echo 'Your configuration file exists and seems to hold necessary data already.<br>';
60 return FALSE;
61 }
62 else
63 {
64 echo 'There seem to be no existing installation here.<br>';
65 return TRUE;
66 }
f48dc2cc
DO
67}
68
0e7f2155
DO
69// Check for PHP extensions.
70function platform_is_ok ()
f48dc2cc 71{
0e7f2155
DO
72 $nerrs = 0;
73 echo "<table border=1><tr><th>check item</th><th>result</th></tr>\n";
74
75 echo '<tr><td>PDO extension</td>';
76 if (class_exists ('PDO'))
77 echo '<td class=msg_success>Ok';
78 else
f48dc2cc 79 {
0e7f2155
DO
80 echo '<td class=msg_error>not found';
81 $nerrs++;
82 }
83 echo '</td></tr>';
f48dc2cc 84
0e7f2155
DO
85 echo '<tr><td>PDO-MySQL</td>';
86 if (defined ('PDO::MYSQL_ATTR_READ_DEFAULT_FILE'))
87 echo '<td class=msg_success>Ok';
88 else
89 {
90 echo '<td class=msg_error>not found';
91 $nerrs++;
92 }
93 echo '</td></tr>';
f48dc2cc 94
0e7f2155
DO
95 echo '<tr><td>hash functions</td>';
96 if (function_exists ('hash_algos'))
97 echo '<td class=msg_success>Ok';
98 else
99 {
100 echo '<td class=msg_error>not found';
101 $nerrs++;
102 }
103 echo '</td></tr>';
f48dc2cc 104
0e7f2155
DO
105 echo '<tr><td>SNMP extension</td>';
106 if (defined ('SNMP_NULL'))
107 echo '<td class=msg_success>Ok';
108 else
109 echo '<td class=msg_warning>Not found. Live SNMP tab will not function properly until the extension is installed.';
110 echo '</td></tr>';
f48dc2cc 111
0e7f2155
DO
112 echo '<tr><td>GD functions</td>';
113 if (defined ('IMG_PNG'))
114 echo '<td class=msg_success>Ok';
115 else
116 {
117 echo '<td class=msg_error>not found';
118 $nerrs++;
119 }
120 echo '</td></tr>';
f48dc2cc 121
0e7f2155
DO
122 echo "</table>\n";
123 return !$nerrs;
124}
f48dc2cc 125
0e7f2155
DO
126// Check that we can write to configuration file.
127// If so, ask for DB connection paramaters and test
128// the connection. Neither save the parameters nor allow
129// going further until we succeed with the given
130// credentials.
131function init_config ()
132{
f92287f4
DO
133 if (!is_writable ('inc/secret.php'))
134 {
135 echo "The inc/secret.php file is not writable by web-server. Make sure it is.";
136 echo "The following commands should suffice:<pre>touch inc/secret.php\nchmod 666 inc/secret.php</pre>";
137 return FALSE;
138 }
139 if
140 (
141 !isset ($_REQUEST['save_config']) or
142 empty ($_REQUEST['mysql_host']) or
143 empty ($_REQUEST['mysql_db']) or
144 empty ($_REQUEST['mysql_username']) or
145 empty ($_REQUEST['mysql_password'])
146 )
147 {
148 echo "<input type=hidden name=save_config value=1>\n";
149 echo '<table>';
150 echo "<tr><td><label for=mysql_host>MySQL host:</label></td>";
151 echo "<td><input type=text name=mysql_host id=mysql_host value=localhost></td></tr>\n";
152 echo "<tr><td><label for=mysql_host>database:</label></td>";
153 echo "<td><input type=text name=mysql_db id=mysql_db value=racktables></td></tr>\n";
154 echo "<tr><td><label for=mysql_username>username:</label></td>";
155 echo "<td><input type=text name=mysql_username></td></tr>\n";
156 echo "<tr><td><label for=mysql_password>password:</label></td>";
157 echo "<td><input type=password name=mysql_password></td></tr>\n";
158 echo '</table>';
159 return FALSE;
160 }
161 $pdo_dsn = 'mysql:host=' . $_REQUEST['mysql_host'] . ';dbname=' . $_REQUEST['mysql_db'];
162 try
163 {
164 $dbxlink = new PDO ($pdo_dsn, $_REQUEST['mysql_username'], $_REQUEST['mysql_password']);
165 }
166 catch (PDOException $e)
167 {
168 echo "<input type=hidden name=save_config value=1>\n";
169 echo '<table>';
170 echo "<tr><td><label for=mysql_host>MySQL host:</label></td>";
171 echo "<td><input type=text name=mysql_host id=mysql_host value='" . $_REQUEST['mysql_host'] . "'></td></tr>\n";
172 echo "<tr><td><label for=mysql_host>database:</label></td>";
173 echo "<td><input type=text name=mysql_db id=mysql_db value='" . $_REQUEST['mysql_db'] . "'></td></tr>\n";
174 echo "<tr><td><label for=mysql_username>username:</label></td>";
175 echo "<td><input type=text name=mysql_username value='" . $_REQUEST['mysql_username'] . "'></td></tr>\n";
176 echo "<tr><td><label for=mysql_password>password:</label></td>";
177 echo "<td><input type=password name=mysql_password value='" . $_REQUEST['mysql_password'] . "'></td></tr>\n";
178 echo "<tr><td colspan=2>The above parameters did not work. Check and try again.</td></tr>\n";
179 echo '</table>';
180 return FALSE;
181 }
182 $conf = fopen ('inc/secret.php', 'w+');
183 if ($conf === FALSE)
184 {
185 echo "Error: failed to open inc/secret.php for writing";
186 return FALSE;
187 }
188 fwrite ($conf, "<?php\n/* This file has been generated automatically by RackTables installer.\n");
189 fwrite ($conf, " * you shouldn't normally edit it unless your database setup has changed.\n");
190 fwrite ($conf, " */\n");
191 fwrite ($conf, "\$pdo_dsn = '${pdo_dsn}';\n");
192 fwrite ($conf, "\$db_username = '" . $_REQUEST['mysql_username'] . "';\n");
193 fwrite ($conf, "\$db_password = '" . $_REQUEST['mysql_password'] . "';\n");
194 fwrite ($conf, "?>\n");
195 fclose ($conf);
196 echo "The configuration file has been written successfully.<br>";
0e7f2155 197 return TRUE;
f48dc2cc
DO
198}
199
0e7f2155
DO
200function init_database ()
201{
202 echo 'Initializing the database...<br>';
f92287f4
DO
203 foreach (array ('structure', 'dictbase', 'dictvendors') as $part)
204 {
205 echo $part;
206 }
0e7f2155
DO
207 return TRUE;
208}
f48dc2cc 209
0e7f2155 210function congrats ()
f48dc2cc 211{
0e7f2155
DO
212 echo 'Congratulations! RackTables installation is complete. Press Next to open your main page.';
213 return TRUE;
f48dc2cc
DO
214}
215
216?>