ug... i have no idea really...
[PHPIPManager.git] / lib / www.php
1 <?php
2 // The www class file.
3 $actionRegister["addsuper"] = "www_ip_addSuperRange";
4 $actionRegister["allocate"] = "www_ip_allocateRange";
5 $actionRegister["allocatesub"] = "www_ip_allocateSubRange";
6 $actionRegister["deletesub"] = "www_ip_deleteSubRange";
7 $actionRegister["modsubnet"] = "www_ip_modifySubnet";
8 $actionRegister["dumpdb"] = "www_db_dumpdb";
9 $actionRegister["restoredb"] = "www_db_restoredb";
10 $actionRegister["changesubdetails"] = "www_ip_changesubdetails";
11 $actionRegister["addhosts"] = "www_ip_addhost";
12 $actionRegister["deletehost"] = "www_ip_deletehost";
13 $actionRegister["modifyhost"] = "www_ip_modifyhost";
14 $actionRegister["updatehost"] = "www_ip_updatehost";
15 $actionRegister["scansub"] = "www_ip_scansub";
16 $actionRegister["scansubreal"] = "www_ip_scansubReal";
17
18 class www {
19         function Go() {
20                 // this is the web page entry function.
21                 global $db;
22                 
23                 $db->connect();
24                 //if($db->connect()!=0) {
25                         //$this->doInstaller();
26                         //exit(0);
27                 //}
28                 
29                 // its up to auth as to wether "this" url requires auth.
30                 $authThis = new auth();
31                 $authThis->Go();
32                 
33                 if(isset($_REQUEST["action"])) {
34                         $this->doAction($_REQUEST["action"]);
35                 } else {
36                         $this->mainAction();
37                 }
38         }
39         
40         function doAction($actionName)
41         {
42                 global $actionRegister;
43                 
44                 error_log("im here");
45                 if(isset($actionRegister[$actionName])) {
46                         $func = $actionRegister[$actionName];
47                         error_log("im here 2");
48                         $func();
49                 } else {
50                         error_log("im here 3");
51                         $this->mainAction();
52                 }
53         }
54         
55         function mainAction()
56         {
57                 // we build a frame of framey's
58                 $this->header();
59                 $this->printError();
60                 $this->mainPage();              
61                 $this->footer();
62         }
63         
64         function mainPage()
65         {
66                 global $db;
67                 
68                 ?>
69 <form method="post" action="?action=addsuper">
70 Create Supernet: name <input type="text" name="name"></input>
71 Subnet Address <input type="text" name="subnet"></input>
72 Mask <input type="text" name="mask"></input>
73 Description <input type="text" name="desc"></input>
74 <input type="submit" name="go" Value="Create"></input>
75 </form>
76                 <?php
77
78                 // now print the super nets
79                 $res = $db->dbobject->query("select * from supernet");
80                 foreach($res as $row) {
81                         //echo "<pre>";
82                         //      print_r($row);
83                         //      echo "</pre><hr>";
84                         if(ipversion($row["sn_ip"]) == 6) $ip6trans = "::0";
85                         else $ip6trans = "";
86                         echo "<table border=\"1\"><tr><th align=\"left\">".$row["sn_name"]."</th><td>".$row["sn_ip"]."$ip6trans/".$row["sn_mask"]."</td><td>".$row["sn_desc"]."</td>";
87                         echo "<td><a href=\"?action=allocate&id=".$row["sn_id"]."\">Allocate Subnet</a></td>";
88                         echo "<td><a href=\"?action=delete&id=".$row["sn_id"]."\">Delete Supernet</a></td>";
89                         echo "</tr>";
90                         // now we search for sub's
91                         $res2 = $db->dbobject->query("select * from subnet where snid_id='".$row["sn_id"]."'");
92                         foreach($res2 as $row2) {
93                                 // ("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT);';
94                                 $name = $row2["sn_name"];
95                                 $subid = $row2["sn_id"];
96                                 $subnet = $row2["sn_ip"];
97                                 $mask = $row2["sn_mask"];
98                                 $desc = $row2["sn_desc"];
99
100                                 if(ipversion($row2["sn_ip"]) == 6) $ip6trans = "::0";
101                                 else $ip6trans = "";
102         
103                                 echo "<tr>";
104                                 echo "<td><li> <i><a href=\"?action=modsubnet&id=".$row2["sn_id"]."\">$name</a></i></td><td>$subnet$ip6trans/$mask</td><td>$desc</td>";
105                                 echo "<td><a href=\"?action=deletesub&id=".$row2["sn_id"]."\">Delete Subnet</a></td>";
106                                 //echo "<td><a href=\"?action=addhost&id=".$row["sn_id"]."\">Add Host</a></td>";
107                                 if(ipversion($row2["sn_ip"]) == 4) echo "<td><a href=\"?action=scansub&id=".$row2["sn_id"]."\">Scan Subnet</a></td>";
108                                 echo "</tr>";
109                         }
110                         echo "</table><br>";
111                 }
112                 
113                 echo "<a href=\"?action=dumpdb\">dump database</a> <a href=\"?action=restoredb\">restore database</a>";  
114         }
115         
116         function printError()
117         {
118                 if(isset($_REQUEST["error"]))
119                 {
120                         echo "<font color=\"red\">".$_REQUEST["error"]."</font>";
121                 }
122                 if(isset($_REQUEST["notice"]))
123                 {
124                         echo "<font color=\"green\">".$_REQUEST["notice"]."</font>";
125                 }
126                 
127         }
128         
129         function header($title = "Welcome to PHP IP Manager")
130         {
131                 ?>
132 <html>
133 <head><title><?php echo $title ?></title>
134 <?php 
135                 if(file_exists("style.css")) echo '<link href="style.css" rel="stylesheet" type="text/css" />'; 
136
137 ?>
138 </head>
139 <body><h1><?php echo $title ?></h1>
140                 <?php
141         }
142         
143         function footer()
144         {
145                 echo "<pre>";
146                 //print_r($_SERVER);
147                 
148                 //print_r($_REQUEST);
149                 
150                 //print_r($GLOBALS);
151                 echo "</pre>";
152                 ?>
153 </body></html>
154                 <?php
155         }
156         
157         
158         function doInstaller()
159         {
160                 header("Location: install.php");
161         }
162 }
163
164 function www_ip_scansub()
165 {
166         global $db, $wwwConnector;
167         
168         $id = $_REQUEST["id"];
169         
170         $res = $db->dbobject->query("select * from subnet where sn_id=='$id'");
171         
172         foreach($res as $row) {
173                 $snip = $row["sn_ip"];
174                 $snmask = $row["sn_mask"];
175                 $snname = $row["sn_name"];
176         }
177         $wwwConnector->header();
178         $wwwConnector->printError();
179         
180 ?>
181 You are about to scan the subnet <?php echo "$snname ($snip/$snmask)" ?> how would you like entries added:<br>
182 <li> <a href="index.php?action=scansubreal&id=<?php echo $id?>&method=1">Replace</a> all default entries (i.e. where the name and the ip address are the same) where hosts are UP (reply to ping)
183 <li> <a href="index.php?action=scansubreal&id=<?php echo $id?>&method=2">Replace</a> all entries where hosts are UP (reply to ping)
184 <li> <a href="index.php?action=scansubreal&id=<?php echo $id?>&method=3">Add</a> any entries where hosts are UP (reply to ping) and an entry does not already exist in the database
185 <li> <a href="index.php?action=scansubreal&id=<?php echo $id?>&method=4">Add</a> any entries where hosts are UP or Down and an entry does not already exist in the database, but the IP has a reverse hostname entry
186 <li> <a href="index.php?action=scansubreal&id=<?php echo $id?>&method=5">Delete</a> all entries and replace with hosts that are Up or Down and have reverse hostname entries
187 <?php 
188
189         $wwwConnector->footer();
190 }
191
192 function www_ip_scansubReal()
193 {
194         global $db, $wwwConnector;
195
196         $id = $_REQUEST["id"];
197         
198         system("/usr/bin/php ../lib/runscan.php $id > /tmp/out.phpipman 2>&1 &");
199         error_log("would exec... nmap -sP -oX $fname $sn/$sm");
200         
201         header("Location: index.php?notice=Scan Initiated");
202 }
203
204 function www_ip_addSuperRange()
205 {
206         global $db;
207         
208         $name = $_REQUEST["name"];
209         $sn = $_REQUEST["subnet"];
210         $mask = $_REQUEST["mask"];
211         $desc = $_REQUEST["desc"];
212         
213         $myip = new ip();
214         
215         if($myip->addSupernet($name, $sn, $mask, $desc)) {
216                 header("Location: index.php?notice=range added");
217         } else {
218                 header("Location: index.php?error=invalid ipaddress");
219         }
220 }
221
222 function www_ip_allocateRange()
223 {
224         global $db, $wwwConnector;
225
226         $id = $_REQUEST["id"];
227         
228         $res = $db->dbobject->query("select * from supernet where sn_id=='$id'");
229         
230         foreach($res as $row) {
231                 $sn = $row["sn_ip"];
232         }
233         $wwwConnector->header();
234         $wwwConnector->printError();
235         ?>
236 <form method="post" action="?action=allocatesub&id=<?php echo $id ?>">
237 <input type="hidden" name="superid" value="<?php echo $id ?>">
238 <table>
239 <tr><td>Subnet Name</td><td><input type="text" name="subname"></td></tr>
240 <tr><td>Subnet IP</td><td><input type="text" name="subip" value="<?php echo $sn?>"></td></tr>
241 <tr><td>Subnet Mask</td><td><input type="text" name="submask"></td></tr>
242 <tr><td>Description</td><td><input type="text" name="subdesc"></td></tr>
243 <tr><td><input type="submit" name="add" value="Add"></td></tr>
244 </table>
245 </form>
246 <?php
247
248         $wwwConnector->footer();
249 }
250
251 function www_ip_deleteSubRange()
252 {
253         global $db, $wwwConnector;
254         
255         $id = $_REQUEST["id"];
256         //("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT);';
257         $sql = "delete from subnet where sn_id=='$id'";
258         $res = $db->dbobject->query($sql);
259         
260         header("Location: index.php?notice=Deleted\n");
261 }
262
263 function www_ip_allocateSubRange()
264 {
265         global $db, $wwwConnector;
266         
267         $superid = $_REQUEST["superid"];
268         $name = $_REQUEST["subname"];
269         $subip = $_REQUEST["subip"];
270         $mask = $_REQUEST["submask"];
271         $desc = $_REQUEST["subdesc"];
272         
273         $myip = new ip();
274         $err = $myip->addSubnet($name, $subip, $mask, $desc, $superid);
275         if($err ===true) {
276                 header("Location: index.php?notice=range added");
277         } else {
278                 header("Location: index.php?error=$err");
279         }
280 }
281
282 function www_ip_modifySubnet()
283 {
284         global $db, $wwwConnector;
285
286         $id = $_REQUEST["id"];
287         //("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT);';
288         $sql = "select sn_name,sn_desc,sn_ip from subnet where sn_id=='$id'";
289         $res = $db->dbobject->query($sql);
290         
291         foreach($res as $row) {
292                 $sn_name = $row["sn_name"];
293                 $sn_desc = $row["sn_desc"];
294                 $sn_ip = $row["sn_ip"];
295         }
296         
297         $wwwConnector->header();
298         $wwwConnector->printError();
299         
300         // ("ho_id" INTEGER PRIMARY KEY AUTOINCREMENT,"ho_sn_id" INTEGER,"ho_ip" TEXT,"ho_name" TEXT,"ho_desc" TEXT)
301         ?>
302 <form method="post" action="?action=changesubdetails&id=<?php echo $id ?>">
303 Subnet Name: <input type="text" name="subname" value="<?php echo $sn_name?>">
304 Subnet Description: <input type="text" name="subdesc" value="<?php echo $sn_desc?>">
305 <input type="submit" name="change" value="Update">
306 </form>
307
308 <form method="post" action="?action=addhosts&id=<?php echo $id ?>">
309 <h3>Add Host</h3>
310 Hostname <input type="text" name="hostname">
311 IP Address <input type="text" name="hostip" value="<?php echo $sn_ip?>">
312 Description <input type="text" name="hostdesc" >
313 <input type="submit" name="change" value="Add">
314 </form>
315
316
317
318 <table border=1>
319 <tr><th>Host IP</th><th>Hostname</th><th>Description</th></tr>
320 <?php
321         $sql = "select * from hosts where ho_sn_id='$id'";
322         $res = $db->dbobject->query($sql);
323         
324         foreach($res as $row) {
325                 $name = $row["ho_name"];
326                 $desc = $row["ho_desc"];
327                 $ip = $row["ho_ip"];
328                 $hid = $row["ho_id"];
329                 echo "<tr><td><a href=\"?action=modifyhost&hid=$hid\">$ip</a></td><td>$name</td><td>$desc</td><td><a href=\"?action=deletehost&hostid=$hid&snid=$id\">Delete</a></td></tr>";
330         } 
331 ?>
332 </table>
333
334
335 <a href="index.php">Back</a>
336 <?php
337
338         $wwwConnector->footer();
339 }
340
341 function www_ip_updatehost()
342 {
343         global $db, $wwwConnector;
344         
345         $hname = $_REQUEST["hostname"];
346         $hip = $_REQUEST["hostip"];
347         $hdesc = $_REQUEST["hostdesc"];
348         $sid = $_REQUEST["superid"];
349         $hid = $_REQUEST["hid"];
350         
351         // ("ho_id" INTEGER PRIMARY KEY AUTOINCREMENT,"ho_sn_id" INTEGER,"ho_ip" TEXT,"ho_name" TEXT,"ho_desc" TEXT)
352         $sql = "update hosts set ho_name='$hname',ho_ip='$hip',ho_desc='$hdesc' where ho_id='$hid'";
353         $db->dbobject->query($sql);
354         
355         header("Location: ?action=modsubnet&id=$sid");
356 }
357
358 function www_ip_modifyhost()
359 {
360         global $db, $wwwConnector;
361         
362         $id = $_REQUEST["hid"];
363         
364         $res = $db->dbobject->query("select * from hosts where ho_id=='$id'");
365         
366         foreach($res as $row) {
367                 $hname = $row["ho_name"];
368                 $hip = $row["ho_ip"];
369                 $sid = $row["ho_sn_id"];
370                 $hdesc = $row["ho_desc"];
371         }
372         
373         $wwwConnector->header();
374         $wwwConnector->printError();
375         ?>
376 <form method="post" action="?action=updatehost&hid=<?php echo $id ?>">
377 <input type="hidden" name="superid" value="<?php echo $sid ?>">
378 <table>
379 <tr><td>Host Name</td><td><input type="text" name="hostname" value="<?php echo $hname?>"></td></tr>
380 <tr><td>Host IP</td><td><input type="text" name="hostip" value="<?php echo $hip?>"></td></tr>
381 <tr><td>Description</td><td><input type="text" name="hostdesc" value="<?php echo $hdesc?>"></td></tr>
382 <tr><td><input type="submit" name="add" value="Update"></td></tr>
383 </table>
384 </form>
385 <a href="<?php echo $_SERVER["HTTP_REFERER"]?>">Back</a>
386 <?php
387
388         $wwwConnector->footer();
389 }
390
391 function www_ip_deletehost()
392 {
393         global $db, $wwwConnector;
394         
395         $id = $_REQUEST["hostid"];
396         $sid = $_REQUEST["snid"];
397         
398         $sql = "delete from hosts where ho_id=='$id'";
399         $db->dbobject->query($sql);
400         
401         header("Location: ?action=modsubnet&id=$sid");
402 }
403
404 function www_ip_addhost()
405 {
406         global $db, $wwwConnector;
407         
408         $id = $_REQUEST["id"];
409         $hn = $_REQUEST["hostname"];
410         $hip = $_REQUEST["hostip"];
411         $desc = $_REQUEST["hostdesc"];
412         
413         $sql = "insert into hosts values (NULL, '$id', '$hip', '$hn', '$desc')";
414         $db->dbobject->query($sql);
415         
416         header("Location: ?action=modsubnet&id=$id");
417 }
418
419 function www_ip_changesubdetails()
420 {
421         global $db, $wwwConnector;
422         
423         $id = $_REQUEST["id"];
424         $name = $_REQUEST["subname"];
425         $desc = $_REQUEST["subdesc"];
426
427         $sql = "update subnet set sn_name='$name', sn_desc='$desc' where sn_id=='$id'";
428         $db->dbobject->query($sql);
429         
430         header("Location: ?action=modsubnet&id=$id");
431 }
432
433 function www_db_dumpdb()
434 {
435         global $db;
436         
437         $datestamp = strftime("%d-%m-%Y-%H.%M.%S");
438         $name = "ipmandbdump-$datestamp.db";
439         header("Content-type: application/octet-stream;\n");
440         header("Content-Disposition: attachment; filename=\"$name\";\n\n");
441         $db->dump();
442 }
443
444 function www_db_restoredb()
445 {
446 }
447
448 ?>