god hates me
[configmanager.git] / lib / www.php
1 <?php
2
3
4 // some global menu stuff
5 $MENUS["home"]["url"] = "?action=home";
6 $MENUS["home"]["name"] = "Home";
7 //$MENUS["hosts"]["url"] = "?action=hosts";
8 //$MENUS["hosts"]["name"] = "Hosts";
9 $MENUS["add"]["url"] = "?action=addhost";
10 $MENUS["add"]["name"] = "Add Host";
11 $MENUS["config"]["url"] = "?action=config";
12 $MENUS["config"]["name"] = "Configure";
13
14 $FUNCTIONS["config"] = "www_configure";
15 $FUNCTIONS["addhost"] = "www_addhost";
16 $FUNCTIONS["addhostnext"] = "www_addHostStageTwo";
17 $FUNCTIONS["addhostthree"] = "www_addHostStageThree";
18 $FUNCTIONS["delete"] = "www_deleteHost";
19 $FUNCTIONS["update"] = "www_updateHost";
20 $FUNCTIONS["detail"] = "www_hostDetail";
21 $FUNCTIONS["getconfig"] = "www_getConfig";
22 $FUNCTIONS["downloadconfig"] = "www_downloadConfig";
23
24 function www_header()
25 {
26         ?>
27 <html>
28 <title>Network Config Manager</title>
29 <body>
30         <?php
31 }
32
33 function www_pageLayout()
34 {
35         ?>
36 <table>
37 <tr><td colspan="2"><h1>Network Config Manager</h1></td></tr>
38 <tr valign="top"><td><?php www_menu() ?></td><td><?php www_body() ?></td></tr>
39 </table>
40         
41         <?php 
42 }
43
44 function www_footer()
45 {
46         ?>
47 <br><font size="-1"><i>Copyright 2003-2011, Paul J Robinson</i></font><br>
48 </body>
49 </html>
50         <?php 
51 }
52
53 function www_menu()
54 {
55         global $MENUS;
56         foreach($MENUS as $menuitems) {
57                 $url = $menuitems["url"];
58                 $name = $menuitems["name"];
59                 echo "<a href=\"$url\">$name</a><br>";
60         }
61 }
62
63 function www_body()
64 {
65         global $FUNCTIONS;
66         
67         $called = false;
68         if(isset($_REQUEST["action"])) {
69                 $req = $_REQUEST["action"];
70                 if(isset($FUNCTIONS["$req"])) {
71                         $func = $FUNCTIONS["$req"];
72                         if(function_exists($func)) {
73                                 $func();
74                                 return true;
75                         }
76                 }
77         }
78         
79         $db = db_getDB();
80         
81         // here we do the normal body
82         // we'll list the hosts, the size of the current config and
83         // the last time it was updated, plus the number of versions
84         $data = db_selectData("hosts");
85         if(!$data) {
86                 echo "No hosts currently exist, add them from the right<br>"; 
87         } else {
88                 echo "<table border=\"1\">";
89                 echo "<tr><th>Name</th><th>Address</th><th>Last Update</th><th>Schedule</th><th>Versions</th><th>Controls</th></tr>";
90                 foreach($data as $dstruct) {
91                         //print_r($dstruct);
92                         $name = $dstruct["hostname"];
93                         $addr = $dstruct["hostip"];
94                         if($dstruct["lastupdate"] != 0) {
95                                 $dold = round((time()-$dstruct["lastupdate"])/86400);
96                                 
97                                 $lupdate = strftime("%T %e %b %G", $dstruct["lastupdate"])." ($dold days old)";
98                         } else {
99                                 $lupdate = "Never";
100                         }
101                         $sched = $dstruct["updateschedule"];
102                         $vd = count(db_selectData("configs", "hostip", "$addr"));
103                         echo "<tr><td><a href=\"?action=detail&hip=$addr\">$name</a></td><td>$addr</td><td>$lupdate</td><td>$sched</td><td>$vd</td><td>";
104                         echo "<a href=\"?action=delete&hip=$addr\">Delete</a> <a href=\"?action=edit&hip=$addr\">Edit</a> <a href=\"?action=update&hip=$addr\">Update</a>";
105                         echo "</td></tr>";
106                 
107                 }
108                 echo "</table>";
109         }
110         
111 }
112
113 function www_getConfig()
114 {
115         global $HOST_TYPE;
116         
117         $cid = $_REQUEST["cid"];        
118         $hip = $_REQUEST["hip"];
119         $data = db_selectData("hosts", "hostip", "$hip");
120         
121         $hostname = $data[0]["hostname"];
122         $htype = $data[0]["hosttype"];
123         
124         $func = $HOST_TYPE["$htype"]["getconfig"];
125         
126         error_log("calling $func with $hip, and $cid");
127         $rdata = $func("$hip", "$cid");
128         
129         echo "Config:<br>";
130         echo "<pre>";
131         echo $rdata;
132         echo "</pre>";
133 }
134
135 function www_downloadConfig()
136 {
137         global $HOST_TYPE;
138         
139         $cid = $_REQUEST["cid"];        
140         $hip = $_REQUEST["hip"];
141         $data = db_selectData("hosts", "hostip", "$hip");
142         
143         $hostname = $data[0]["hostname"];
144         $htype = $data[0]["hosttype"];
145         
146         $func = $HOST_TYPE["$htype"]["getconfig"];
147         
148         error_log("calling $func with $hip, and $cid");
149         $rdata = $func("$hip", "$cid");
150         
151         echo "Config:<br>";
152         echo "<pre>";
153         echo $rdata;
154         echo "</pre>";
155 }
156
157 function www_hostDetail()
158 {
159         global $HOST_TYPE;
160         
161         //db_createTable("configs", "hostip", "configtime", "configdata");
162         //db_createTable("hosts", "hostname", "hostip", "hosttype", "hostconfig", "lastupdate", "updateschedule");
163         //function db_updateData($tablename, $column, $newdata, $wherecol, $wheredata, $exact=true)
164         $hip = $_REQUEST["hip"];
165         $data = db_selectData("hosts", "hostip", "$hip");
166         
167         $hconfig = $data[0]["hostconfig"];
168         $hostname = $data[0]["hostname"];
169         $htype = $data[0]["hosttype"];
170         
171         $func = $HOST_TYPE["$htype"]["detail"];
172         
173         error_log("calling $func with $hip, and $hconfig");
174         $func("$hip");
175 }
176
177 function www_updateHost()
178 {
179         global $HOST_TYPE;
180         
181         //db_createTable("configs", "hostip", "configtime", "configdata");
182         //db_createTable("hosts", "hostname", "hostip", "hosttype", "hostconfig", "lastupdate", "updateschedule");
183         //function db_updateData($tablename, $column, $newdata, $wherecol, $wheredata, $exact=true)
184         $hip = $_REQUEST["hip"];
185         $data = db_selectData("hosts", "hostip", "$hip");
186         
187         $hconfig = $data[0]["hostconfig"];
188         $hostname = $data[0]["hostname"];
189         $htype = $data[0]["hosttype"];
190         
191         $func = $HOST_TYPE["$htype"]["getconfig"];
192         $logdets = unserialize(base64_decode($hconfig));
193
194         // now check enable, username and password bits for completeness
195         $need_user = false;
196         $need_pass = false;
197         $need_enable = false;
198
199         // this aint the right way.
200         if($logdets["username"] == "") {
201                 if(!isset($_REQUEST["username"])) {
202                         // we need a user
203                         $need_user = true;
204                 } else {
205                         $act_user = $_REQUEST["username"];
206                 }
207         } else {
208                 $act_user = $logdets["username"];
209         }
210         if($logdets["password"] == "") {
211                 if(!isset($_REQUEST["password"])) {
212                         // we need a user
213                         $need_pass = true;
214                 } else {
215                         $act_pass = $_REQUEST["password"];
216                 }
217         } else {
218                 $act_pass = $logdets["password"];
219         }
220         if($HOST_TYPE["$htype"]["needsenable"]) {
221                 if($logdets["enable"] == "") {
222                         if(!isset($_REQUEST["enable"])) {
223                                 $need_enable = true;
224                         } else {
225                                 $act_enable = $_REQUEST["enable"];
226                         }
227                 } else {
228                         $act_enable = $logdets["enable"];
229                 }
230         }
231         
232         if($need_user || $need_pass || $need_enable) {
233                 echo "No Username/Password information stored, please enter them now (they will not be saved)<br>";
234                 echo "<form method=\"post\" action=\"?action=update&hip=10.172.192.1\">";
235                 if($need_user) {
236                         echo "User <input type=\"text\" name=\"username\"><br>";
237                 }
238                 if($need_pass) echo "Password <input type=\"password\" name=\"password\"><br>";
239                 if($need_enable) echo "Enable Password <input type=\"text\" name=\"enable\"><br>";
240                 echo "<input type=\"submit\" name=\"Update\" value=\"Update\">";
241                 echo "<form>";
242         } else {
243         
244                 $conf = $func($hip, $act_user, $act_pass, $act_enable);
245         
246                 // function db_getMaxValue($tablename, $column, $columnsel="", $wheresel="")
247                 $oldconf_rid = db_getMaxValue("configs", "configs_id", "hostip", "$hip");
248                 $oldconf_id = $oldconf_rid[0][0];
249                 error_log("oldconf_id is $oldconf_id");
250                 
251                 // function db_selectData($tablename, $column="", $value="", $orderby = "")
252                 $oldconf_r = db_selectData("configs", "configs_id", "$oldconf_id");
253                 $oldconf = base64_decode($oldconf_r[0]["configdata"]);
254                 
255                 if(md5($oldconf) != md5($conf)) {
256                         echo "New config found, updating db<br>";
257                         db_insertData("configs", "$hip", time(), base64_encode($conf));
258                         echo "Config added<br>";
259                 } else {
260                         echo "New config and old config are the same, not updating<br>";
261                 }
262         }
263 }
264
265 function www_hostTypesDropDown()
266 {
267         global $HOST_TYPE;
268         
269         foreach($HOST_TYPE as $tloop => $types) {
270                 $typename = $types["name"];
271                 $typever = $tloop;
272                 echo "<option value=\"$typever\">$typename</option>";
273         }
274 }
275
276 function www_deleteHost()
277 {
278         $hip = $_REQUEST["hip"];
279         $data = db_selectData("hosts", "hostip", "$hip");
280         
281         $hostname = $data[0]["hostname"];
282
283         if(isset($_REQUEST["confirmed"])) {
284                 db_deleteData("hosts", "hostip", "$hip");
285                 db_deleteData("configs", "hostip", "$hip");
286                 echo "Done!";
287         } else {
288                 echo "Are you use you wish to delete the host, $hostname with IP address $hip? (and all associated config info)<br>";
289                 echo "<a href=\"?action=delete&hip=$hip&confirmed=yes\">Yes</a>";
290         }
291 }
292
293 function www_updateSchedule()
294 {
295 ?>
296 <option value="0">Manual Only</option>
297 <option value="900">Every 15 Minutes</option>
298 <option value="3600">Every Hour</option>
299 <option value="14400">Every 4 Hours</option>
300 <option value="86400">Once a Day</option>
301 <option value="604800">Once a Week</option>
302 <option value="2592000">Once a Month (30 Days)</option>
303 <option value="7776000">Every 3 Months (90 days)</option>
304 <option value="10368000">Every 6 Months (180 Days)</option>
305 <?php
306 }
307
308 function www_addhost()
309 {
310         $db = db_getDB();
311         
312         db_createTable("hosts", "hostname", "hostip", "hosttype", "hostconfig", "lastupdate", "updateschedule");
313         db_createTable("configs", "hostip", "configtime", "configdata");
314         ?>
315 <form method="post" action="?action=addhostnext">
316 <i>Note: if you leave the username/password fields blank, you'll be prompted when
317 manually updating on the home page</i>
318 <table>
319 <tr><td>Name</td><td><input type="text" name="cname"></td></tr>
320 <tr><td>Hostname/IP Address</td><td><input type="text" name="hname"></td></tr>
321 <tr><td>Username</td><td><input type="text" name="username"></td></tr>
322 <tr><td>Password</td><td><input type="password" name="password"></td></tr>
323 <tr><td>Enable Password</td><td><input type="password" name="enable"></td></tr>
324 <tr><td>Host Type</td><td><select name="hosttype"><?php www_hostTypesDropDown() ?></select></td></tr>
325 <tr><td>Update Schedule</td><td><select name="updatesched"><?php www_updateSchedule() ?></select></td></tr></table>
326 <input type="submit" value="Add" name="Add">
327 </form>
328         <?php
329 }
330
331 function www_addHostStageTwo()
332 {
333         global $HOST_TYPE;
334         
335         $data = db_selectData("hosts", "hostip", $_REQUEST["hname"]);
336         if($data) {
337                 echo "Host already exists in database\n";
338                 return false;
339         } else {
340                 db_insertData("hosts", $_REQUEST["cname"], $_REQUEST["hname"], $_REQUEST["hosttype"], "", "", $_REQUEST["updatesched"]);
341         }
342         if(isset($_REQUEST["hosttype"])) {
343                 $htype = $_REQUEST["hosttype"];
344                 $hip = $_REQUEST["hname"];
345                 $user = $_REQUEST["username"];
346                 $pass = $_REQUEST["password"];
347                 $enable = $_REQUEST["enable"];
348                 $hname = $_REQUEST["hname"];
349                 $upsched = $_REQUEST["updatesched"];
350                 
351                 $confdetails["username"] = $user;
352                 $confdetails["password"] = $pass;
353                 $confdetails["enable"] = $enable;
354                 
355                 $logdata = base64_encode(serialize($confdetails));
356                 
357                 db_updateData("hosts", "hostconfig", "$logdata", "hostip", "$hip");
358                 //function db_updateData($tablename, $column, $newdata, $wherecol, $wheredata, $exact=true)
359                 
360
361                 if($user != "" && $pass != "") {
362                         echo "Host added successfully, running configuration test<br>";
363                         $func = $HOST_TYPE["$htype"]["getconfig"];
364                         $conf = $func($hip, $user, $pass, $enable);
365                         echo "The config I got is below, hope its correct as it'll be inserted into the DB as config version 1<br><pre>$conf</pre>";
366                 } else {
367                         echo "Host added. but cant run config test until you manually update<br>";
368                 }
369                 
370                 //db_createTable("configs", "hostip", "configtime", "configdata");
371                 db_insertData("configs", "$hip", time(), base64_encode($conf));
372         }
373 }
374
375 ?>