setup up host update bit
[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"] == "" && $_REQUEST["username"] == "") {
201                 // we need a user
202                 $need_user = true;
203         }
204         if($logdets["password"] == "" && $_REQUEST["password"] == "") {
205                 // we need a user
206                 $need_pass = true;
207         }
208         if($logdets["enable"] == "" && $_REQUEST["enable"] == "" && $HOST_TYPE["$htype"]["needsenable"]) {
209                 $need_enable = true;
210         }
211         
212         $conf = $func($hip, $act_user, $act_pass, $act_enable);
213
214         // function db_getMaxValue($tablename, $column, $columnsel="", $wheresel="")
215         $oldconf_rid = db_getMaxValue("configs", "configs_id", "hostip", "$hip");
216         $oldconf_id = $oldconf_rid[0][0];
217         error_log("oldconf_id is $oldconf_id");
218         
219         // function db_selectData($tablename, $column="", $value="", $orderby = "")
220         $oldconf_r = db_selectData("configs", "configs_id", "$oldconf_id");
221         $oldconf = base64_decode($oldconf_r[0]["configdata"]);
222         
223         if(md5($oldconf) != md5($conf)) {
224                 echo "New config found, updating db<br>";
225                 db_insertData("configs", "$hip", time(), base64_encode($conf));
226                 echo "Config added<br>";
227         } else {
228                 echo "New config and old config are the same, not updating<br>";
229         }
230         
231 }
232
233 function www_hostTypesDropDown()
234 {
235         global $HOST_TYPE;
236         
237         foreach($HOST_TYPE as $tloop => $types) {
238                 $typename = $types["name"];
239                 $typever = $tloop;
240                 echo "<option value=\"$typever\">$typename</option>";
241         }
242 }
243
244 function www_deleteHost()
245 {
246         $hip = $_REQUEST["hip"];
247         $data = db_selectData("hosts", "hostip", "$hip");
248         
249         $hostname = $data[0]["hostname"];
250
251         if(isset($_REQUEST["confirmed"])) {
252                 db_deleteData("hosts", "hostip", "$hip");
253                 db_deleteData("configs", "hostip", "$hip");
254                 echo "Done!";
255         } else {
256                 echo "Are you use you wish to delete the host, $hostname with IP address $hip? (and all associated config info)<br>";
257                 echo "<a href=\"?action=delete&hip=$hip&confirmed=yes\">Yes</a>";
258         }
259 }
260
261 function www_updateSchedule()
262 {
263 ?>
264 <option value="0">Manual Only</option>
265 <option value="900">Every 15 Minutes</option>
266 <option value="3600">Every Hour</option>
267 <option value="14400">Every 4 Hours</option>
268 <option value="86400">Once a Day</option>
269 <option value="604800">Once a Week</option>
270 <option value="2592000">Once a Month (30 Days)</option>
271 <option value="7776000">Every 3 Months (90 days)</option>
272 <option value="10368000">Every 6 Months (180 Days)</option>
273 <?php
274 }
275
276 function www_addhost()
277 {
278         $db = db_getDB();
279         
280         db_createTable("hosts", "hostname", "hostip", "hosttype", "hostconfig", "lastupdate", "updateschedule");
281         db_createTable("configs", "hostip", "configtime", "configdata");
282         ?>
283 <form method="post" action="?action=addhostnext">
284 <i>Note: if you leave the username/password fields blank, you'll be prompted when
285 manually updating on the home page</i>
286 <table>
287 <tr><td>Name</td><td><input type="text" name="cname"></td></tr>
288 <tr><td>Hostname/IP Address</td><td><input type="text" name="hname"></td></tr>
289 <tr><td>Username</td><td><input type="text" name="username"></td></tr>
290 <tr><td>Password</td><td><input type="password" name="password"></td></tr>
291 <tr><td>Enable Password</td><td><input type="password" name="enable"></td></tr>
292 <tr><td>Host Type</td><td><select name="hosttype"><?php www_hostTypesDropDown() ?></select></td></tr>
293 <tr><td>Update Schedule</td><td><select name="updatesched"><?php www_updateSchedule() ?></select></td></tr></table>
294 <input type="submit" value="Add" name="Add">
295 </form>
296         <?php
297 }
298
299 function www_addHostStageTwo()
300 {
301         global $HOST_TYPE;
302         
303         $data = db_selectData("hosts", "hostip", $_REQUEST["hname"]);
304         if($data) {
305                 echo "Host already exists in database\n";
306                 return false;
307         } else {
308                 db_insertData("hosts", $_REQUEST["cname"], $_REQUEST["hname"], $_REQUEST["hosttype"], "", "", $_REQUEST["updatesched"]);
309         }
310         if(isset($_REQUEST["hosttype"])) {
311                 $htype = $_REQUEST["hosttype"];
312                 $hip = $_REQUEST["hname"];
313                 $user = $_REQUEST["username"];
314                 $pass = $_REQUEST["password"];
315                 $enable = $_REQUEST["enable"];
316                 $hname = $_REQUEST["hname"];
317                 $upsched = $_REQUEST["updatesched"];
318                 
319                 $confdetails["username"] = $user;
320                 $confdetails["password"] = $pass;
321                 $confdetails["enable"] = $enable;
322                 
323                 $logdata = base64_encode(serialize($confdetails));
324                 
325                 db_updateData("hosts", "hostconfig", "$logdata", "hostip", "$hip");
326                 //function db_updateData($tablename, $column, $newdata, $wherecol, $wheredata, $exact=true)
327                 
328
329                 echo "Host added successfully, running configuration test<br>";
330                 $func = $HOST_TYPE["$htype"]["getconfig"];
331                 $conf = $func($hip, $user, $pass, $enable);
332                 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>";
333                 
334                 //db_createTable("configs", "hostip", "configtime", "configdata");
335                 db_insertData("configs", "$hip", time(), base64_encode($conf));
336         }
337 }
338
339 ?>