lots of changed, though it'll probably be all backed-out because
[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"]["updateconfig"];
192         
193         error_log("calling $func with $hip, and $hconfig");
194         $func("$hip", $hconfig);
195         
196 }
197
198 function www_hostTypesDropDown()
199 {
200         global $HOST_TYPE;
201         
202         foreach($HOST_TYPE as $tloop => $types) {
203                 $typename = $types["name"];
204                 $typever = $tloop;
205                 echo "<option value=\"$typever\">$typename</option>";
206         }
207 }
208
209 function www_deleteHost()
210 {
211         $hip = $_REQUEST["hip"];
212         $data = db_selectData("hosts", "hostip", "$hip");
213         
214         $hostname = $data[0]["hostname"];
215
216         if(isset($_REQUEST["confirmed"])) {
217                 db_deleteData("hosts", "hostip", "$hip");
218                 db_deleteData("configs", "hostip", "$hip");
219                 echo "Done!";
220         } else {
221                 echo "Are you use you wish to delete the host, $hostname with IP address $hip? (and all associated config info)<br>";
222                 echo "<a href=\"?action=delete&hip=$hip&confirmed=yes\">Yes</a>";
223                 echo "<pre>";
224                 print_r($data);
225                 echo "</pre>";
226         }
227 }
228
229 function www_updateSchedule()
230 {
231 ?>
232 <option value="0">Manual Only</option>
233 <option value="900">Every 15 Minutes</option>
234 <option value="3600">Every Hour</option>
235 <option value="14400">Every 4 Hours</option>
236 <option value="86400">Once a Day</option>
237 <option value="604800">Once a Week</option>
238 <option value="2592000">Once a Month (30 Days)</option>
239 <option value="7776000">Every 3 Months (90 days)</option>
240 <option value="10368000">Every 6 Months (180 Days)</option>
241 <?php
242 }
243
244 function www_addhost()
245 {
246         $db = db_getDB();
247         
248         db_createTable("hosts", "hostname", "hostip", "hosttype", "hostconfig", "lastupdate", "updateschedule");
249         db_createTable("configs", "hostip", "configtime", "configdata");
250         ?>
251 <form method="post" action="?action=addhostnext">
252 <table>
253 <tr><td>Name</td><td><input type="text" name="cname"></td></tr>
254 <tr><td>Hostname/IP Address</td><td><input type="text" name="hname"></td></tr>
255 <tr><td>Host Type</td><td><select name="hosttype"><?php www_hostTypesDropDown() ?></select></td></tr>
256 <tr><td>Update Schedule</td><td><select name="updatesched"><?php www_updateSchedule() ?></select></td></tr></table>
257 <input type="submit" value="Next" name="Next">
258 </form>
259         <?php
260 }
261
262 function www_addHostStageTwo()
263 {
264         global $HOST_TYPE;
265         
266         $data = db_selectData("hosts", "hostip", $_REQUEST["hname"]);
267         if($data) {
268                 echo "Host already exists in database\n";
269                 return false;
270         } else {
271                 db_insertData("hosts", $_REQUEST["cname"], $_REQUEST["hname"], $_REQUEST["hosttype"], "", "", $_REQUEST["updatesched"]);
272         }
273         if(isset($_REQUEST["hosttype"])) {
274                 $htype = $_REQUEST["hosttype"];
275                 $func = $HOST_TYPE["$htype"]["configform"];
276                 $hip = $_REQUEST["hname"];
277                 $hname = $_REQUEST["hname"];
278                 $upsched = $_REQUEST["updatesched"];
279                 if(function_exists($func)) {
280                         
281                         echo "<form method=\"post\" action=\"?action=addhostthree&hosttype=$htype&hostname=$hname&hostip=$hip&upsched=$upsched\">";
282                         $func();
283                         echo "<input type=\"submit\" value=\"Add\" name=\"Add\">";
284                 } else echo "would call $func for $htype but it doesnt exist\n";
285         }
286 }
287
288 function www_addHostStageThree()
289 {
290         global $HOST_TYPE;
291         if(isset($_REQUEST["hosttype"])) {
292                 $htype = $_REQUEST["hosttype"];
293                 $func = $HOST_TYPE["$htype"]["postfunction"];
294                 if(function_exists($func)) {
295                         $func();
296                 } else echo "would call $func for $htype but it doesnt exist\n";
297         }
298         
299 }
300 ?>