3e236213b21fa45bd38a3fb42b93e0b7418aab5b
[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 </head>
135 <body><h1><?php echo $title ?></h1>
136                 <?php
137         }
138         
139         function footer()
140         {
141                 echo "<pre>";
142                 //print_r($_SERVER);
143                 
144                 //print_r($_REQUEST);
145                 
146                 //print_r($GLOBALS);
147                 echo "</pre>";
148                 ?>
149 </body></html>
150                 <?php
151         }
152         
153         
154         function doInstaller()
155         {
156                 header("Location: install.php");
157         }
158 }
159
160 function www_ip_scansub()
161 {
162         global $db, $wwwConnector;
163         
164         $id = $_REQUEST["id"];
165         
166         $res = $db->dbobject->query("select * from subnet where sn_id=='$id'");
167         
168         foreach($res as $row) {
169                 $snip = $row["sn_ip"];
170                 $snmask = $row["sn_mask"];
171                 $snname = $row["sn_name"];
172         }
173         $wwwConnector->header();
174         $wwwConnector->printError();
175         
176 ?>
177 You are about to scan the subnet <?php echo "$snname ($snip/$snmask)" ?> how would you like entries added:<br>
178 <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)
179 <li> <a href="index.php?action=scansubreal&id=<?php echo $id?>&method=2">Replace</a> all entries where hosts are UP (reply to ping)
180 <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
181 <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
182 <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
183 <?php 
184
185         $wwwConnector->footer();
186 }
187
188 function www_ip_scansubReal()
189 {
190         global $db, $wwwConnector;
191
192         $id = $_REQUEST["id"];
193         
194         system("/usr/bin/php ../lib/runscan.php $id > /tmp/out.phpipman 2>&1 &");
195         error_log("would exec... nmap -sP -oX $fname $sn/$sm");
196         
197         header("Location: index.php?notice=Scan Initiated");
198 }
199
200 function www_ip_addSuperRange()
201 {
202         global $db;
203         
204         $name = $_REQUEST["name"];
205         $sn = $_REQUEST["subnet"];
206         $mask = $_REQUEST["mask"];
207         $desc = $_REQUEST["desc"];
208         
209         $myip = new ip();
210         
211         if($myip->addSupernet($name, $sn, $mask, $desc)) {
212                 header("Location: index.php?notice=range added");
213         } else {
214                 header("Location: index.php?error=invalid ipaddress");
215         }
216 }
217
218 function www_ip_allocateRange()
219 {
220         global $db, $wwwConnector;
221
222         $id = $_REQUEST["id"];
223         
224         $res = $db->dbobject->query("select * from supernet where sn_id=='$id'");
225         
226         foreach($res as $row) {
227                 $sn = $row["sn_ip"];
228         }
229         $wwwConnector->header();
230         $wwwConnector->printError();
231         ?>
232 <form method="post" action="?action=allocatesub&id=<?php echo $id ?>">
233 <input type="hidden" name="superid" value="<?php echo $id ?>">
234 <table>
235 <tr><td>Subnet Name</td><td><input type="text" name="subname"></td></tr>
236 <tr><td>Subnet IP</td><td><input type="text" name="subip" value="<?php echo $sn?>"></td></tr>
237 <tr><td>Subnet Mask</td><td><input type="text" name="submask"></td></tr>
238 <tr><td>Description</td><td><input type="text" name="subdesc"></td></tr>
239 <tr><td><input type="submit" name="add" value="Add"></td></tr>
240 </table>
241 </form>
242 <?php
243
244         $wwwConnector->footer();
245 }
246
247 function www_ip_deleteSubRange()
248 {
249         global $db, $wwwConnector;
250         
251         $id = $_REQUEST["id"];
252         //("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT);';
253         $sql = "delete from subnet where sn_id=='$id'";
254         $res = $db->dbobject->query($sql);
255         
256         header("Location: index.php?notice=Deleted\n");
257 }
258
259 function www_ip_allocateSubRange()
260 {
261         global $db, $wwwConnector;
262         
263         $superid = $_REQUEST["superid"];
264         $name = $_REQUEST["subname"];
265         $subip = $_REQUEST["subip"];
266         $mask = $_REQUEST["submask"];
267         $desc = $_REQUEST["subdesc"];
268         
269         $myip = new ip();
270         $err = $myip->addSubnet($name, $subip, $mask, $desc, $superid);
271         if($err ===true) {
272                 header("Location: index.php?notice=range added");
273         } else {
274                 header("Location: index.php?error=$err");
275         }
276 }
277
278 function www_ip_modifySubnet()
279 {
280         global $db, $wwwConnector;
281
282         $id = $_REQUEST["id"];
283         //("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT);';
284         $sql = "select sn_name,sn_desc,sn_ip from subnet where sn_id=='$id'";
285         $res = $db->dbobject->query($sql);
286         
287         foreach($res as $row) {
288                 $sn_name = $row["sn_name"];
289                 $sn_desc = $row["sn_desc"];
290                 $sn_ip = $row["sn_ip"];
291         }
292         
293         $wwwConnector->header();
294         $wwwConnector->printError();
295         
296         // ("ho_id" INTEGER PRIMARY KEY AUTOINCREMENT,"ho_sn_id" INTEGER,"ho_ip" TEXT,"ho_name" TEXT,"ho_desc" TEXT)
297         ?>
298 <form method="post" action="?action=changesubdetails&id=<?php echo $id ?>">
299 Subnet Name: <input type="text" name="subname" value="<?php echo $sn_name?>">
300 Subnet Description: <input type="text" name="subdesc" value="<?php echo $sn_desc?>">
301 <input type="submit" name="change" value="Update">
302 </form>
303
304 <form method="post" action="?action=addhosts&id=<?php echo $id ?>">
305 <h3>Add Host</h3>
306 Hostname <input type="text" name="hostname">
307 IP Address <input type="text" name="hostip" value="<?php echo $sn_ip?>">
308 Description <input type="text" name="hostdesc" >
309 <input type="submit" name="change" value="Add">
310 </form>
311
312
313
314 <table border=1>
315 <tr><th>Host IP</th><th>Hostname</th><th>Description</th></tr>
316 <?php
317         $sql = "select * from hosts where ho_sn_id='$id'";
318         $res = $db->dbobject->query($sql);
319         
320         foreach($res as $row) {
321                 $name = $row["ho_name"];
322                 $desc = $row["ho_desc"];
323                 $ip = $row["ho_ip"];
324                 $hid = $row["ho_id"];
325                 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>";
326         } 
327 ?>
328 </table>
329
330
331 <a href="index.php">Back</a>
332 <?php
333
334         $wwwConnector->footer();
335 }
336
337 function www_ip_updatehost()
338 {
339         global $db, $wwwConnector;
340         
341         $hname = $_REQUEST["hostname"];
342         $hip = $_REQUEST["hostip"];
343         $hdesc = $_REQUEST["hostdesc"];
344         $sid = $_REQUEST["superid"];
345         $hid = $_REQUEST["hid"];
346         
347         // ("ho_id" INTEGER PRIMARY KEY AUTOINCREMENT,"ho_sn_id" INTEGER,"ho_ip" TEXT,"ho_name" TEXT,"ho_desc" TEXT)
348         $sql = "update hosts set ho_name='$hname',ho_ip='$hip',ho_desc='$hdesc' where ho_id='$hid'";
349         $db->dbobject->query($sql);
350         
351         header("Location: ?action=modsubnet&id=$sid");
352 }
353
354 function www_ip_modifyhost()
355 {
356         global $db, $wwwConnector;
357         
358         $id = $_REQUEST["hid"];
359         
360         $res = $db->dbobject->query("select * from hosts where ho_id=='$id'");
361         
362         foreach($res as $row) {
363                 $hname = $row["ho_name"];
364                 $hip = $row["ho_ip"];
365                 $sid = $row["ho_sn_id"];
366                 $hdesc = $row["ho_desc"];
367         }
368         
369         $wwwConnector->header();
370         $wwwConnector->printError();
371         ?>
372 <form method="post" action="?action=updatehost&hid=<?php echo $id ?>">
373 <input type="hidden" name="superid" value="<?php echo $sid ?>">
374 <table>
375 <tr><td>Host Name</td><td><input type="text" name="hostname" value="<?php echo $hname?>"></td></tr>
376 <tr><td>Host IP</td><td><input type="text" name="hostip" value="<?php echo $hip?>"></td></tr>
377 <tr><td>Description</td><td><input type="text" name="hostdesc" value="<?php echo $hdesc?>"></td></tr>
378 <tr><td><input type="submit" name="add" value="Update"></td></tr>
379 </table>
380 </form>
381 <a href="<?php echo $_SERVER["HTTP_REFERER"]?>">Back</a>
382 <?php
383
384         $wwwConnector->footer();
385 }
386
387 function www_ip_deletehost()
388 {
389         global $db, $wwwConnector;
390         
391         $id = $_REQUEST["hostid"];
392         $sid = $_REQUEST["snid"];
393         
394         $sql = "delete from hosts where ho_id=='$id'";
395         $db->dbobject->query($sql);
396         
397         header("Location: ?action=modsubnet&id=$sid");
398 }
399
400 function www_ip_addhost()
401 {
402         global $db, $wwwConnector;
403         
404         $id = $_REQUEST["id"];
405         $hn = $_REQUEST["hostname"];
406         $hip = $_REQUEST["hostip"];
407         $desc = $_REQUEST["hostdesc"];
408         
409         $sql = "insert into hosts values (NULL, '$id', '$hip', '$hn', '$desc')";
410         $db->dbobject->query($sql);
411         
412         header("Location: ?action=modsubnet&id=$id");
413 }
414
415 function www_ip_changesubdetails()
416 {
417         global $db, $wwwConnector;
418         
419         $id = $_REQUEST["id"];
420         $name = $_REQUEST["subname"];
421         $desc = $_REQUEST["subdesc"];
422
423         $sql = "update subnet set sn_name='$name', sn_desc='$desc' where sn_id=='$id'";
424         $db->dbobject->query($sql);
425         
426         header("Location: ?action=modsubnet&id=$id");
427 }
428
429 function www_db_dumpdb()
430 {
431         global $db;
432         
433         $datestamp = strftime("%d-%m-%Y-%H.%M.%S");
434         $name = "ipmandbdump-$datestamp.db";
435         header("Content-type: application/octet-stream;\n");
436         header("Content-Disposition: attachment; filename=\"$name\";\n\n");
437         $db->dump();
438 }
439
440 function www_db_restoredb()
441 {
442 }
443
444 ?>