getting towards base functionality
[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["modsubnet"] = "www_ip_modifySubnet";
7 $actionRegister["dumpdb"] = "www_db_dumpdb";
8 $actionRegister["restoredb"] = "www_db_restoredb";
9 $actionRegister["changesubdetails"] = "www_ip_changesubdetails";
10 $actionRegister["addhosts"] = "www_ip_addhost";
11 $actionRegister["deletehost"] = "www_ip_deletehost";
12
13 class www {
14         function Go() {
15                 // this is the web page entry function.
16                 global $db;
17                 
18                 $db->connect();
19                 //if($db->connect()!=0) {
20                         //$this->doInstaller();
21                         //exit(0);
22                 //}
23                 
24                 // its up to auth as to wether "this" url requires auth.
25                 $authThis = new auth();
26                 $authThis->Go();
27                 
28                 if(isset($_REQUEST["action"])) {
29                         $this->doAction($_REQUEST["action"]);
30                 } else {
31                         $this->mainAction();
32                 }
33         }
34         
35         function doAction($actionName)
36         {
37                 global $actionRegister;
38                 
39                 error_log("im here");
40                 if(isset($actionRegister[$actionName])) {
41                         $func = $actionRegister[$actionName];
42                         error_log("im here 2");
43                         $func();
44                 } else {
45                         error_log("im here 3");
46                         $this->mainAction();
47                 }
48         }
49         
50         function mainAction()
51         {
52                 // we build a frame of framey's
53                 $this->header();
54                 $this->printError();
55                 $this->mainPage();              
56                 $this->footer();
57         }
58         
59         function mainPage()
60         {
61                 global $db;
62                 
63                 ?>
64 <form method="post" action="?action=addsuper">
65 Create Supernet: name <input type="text" name="name"></input>
66 Subnet Address <input type="text" name="subnet"></input>
67 Mask <input type="text" name="mask"></input>
68 Description <input type="text" name="desc"></input>
69 <input type="submit" name="go" Value="Create"></input>
70 </form>
71                 <?php
72
73                 // now print the super nets
74                 $res = $db->dbobject->query("select * from supernet");
75                 foreach($res as $row) {
76                         //echo "<pre>";
77                 //      print_r($row);
78                 //      echo "</pre><hr>";
79                         echo "<table border=\"1\"><tr><th align=\"left\">".$row["sn_name"]."</th><td>".$row["sn_ip"]."::0/".$row["sn_mask"]."</td><td>".$row["sn_desc"]."</td>";
80                         echo "<td><a href=\"?action=allocate&id=".$row["sn_id"]."\">Allocate Subnet</a></td>";
81                         echo "<td><a href=\"?action=delete&id=".$row["sn_id"]."\">Delete Supernet</a></td>";
82                         echo "</tr>";
83                         // now we search for sub's
84                         $res2 = $db->dbobject->query("select * from subnet where snid_id='".$row["sn_id"]."'");
85                         foreach($res2 as $row2) {
86                                 // ("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT);';
87                                 $name = $row2["sn_name"];
88                                 $subid = $row2["sn_id"];
89                                 $subnet = $row2["sn_ip"];
90                                 $mask = $row2["sn_mask"];
91                                 $desc = $row2["sn_desc"];
92                                 echo "<tr>";
93                                 echo "<td><li> <i><a href=\"?action=modsubnet&id=".$row2["sn_id"]."\">$name</a></i></td><td>$subnet::0/$mask</td><td>$desc</td>";
94                                 echo "<td><a href=\"?action=deletesub&id=".$row2["sn_id"]."\">Delete Subnet</a></td>";
95                                 //echo "<td><a href=\"?action=addhost&id=".$row["sn_id"]."\">Add Host</a></td>";
96                                 echo "</tr>";
97                         }
98                         echo "</table><br>";
99                 }
100                 
101                 echo "<a href=\"?action=dumpdb\">dump database</a> <a href=\"?action=restoredb\">restore database</a>";  
102         }
103         
104         function printError()
105         {
106                 if(isset($_REQUEST["error"]))
107                 {
108                         echo "<font color=\"red\">".$_REQUEST["error"]."</font>";
109                 }
110                 if(isset($_REQUEST["notice"]))
111                 {
112                         echo "<font color=\"green\">".$_REQUEST["notice"]."</font>";
113                 }
114                 
115         }
116         
117         function header($title = "Welcome to PHP IP Manager")
118         {
119                 ?>
120 <html>
121 <head><title><?php echo $title ?></title>
122 </head>
123 <body><h1><?php echo $title ?></h1>
124                 <?php
125         }
126         
127         function footer()
128         {
129                 ?>
130 </body></html>
131                 <?php
132         }
133         
134         
135         function doInstaller()
136         {
137                 header("Location: install.php");
138         }
139 }
140
141 function www_ip_addSuperRange()
142 {
143         global $db;
144         
145         $name = $_REQUEST["name"];
146         $sn = $_REQUEST["subnet"];
147         $mask = $_REQUEST["mask"];
148         $desc = $_REQUEST["desc"];
149         
150         $myip = new ip();
151         
152         if($myip->addSupernet($name, $sn, $mask, $desc)) {
153                 header("Location: index.php?notice=range added");
154         } else {
155                 header("Location: index.php?error=invalid ipaddress");
156         }
157 }
158
159 function www_ip_allocateRange()
160 {
161         global $db, $wwwConnector;
162
163         $id = $_REQUEST["id"];
164         
165         $res = $db->dbobject->query("select * from supernet where sn_id=='$id'");
166         
167         foreach($res as $row) {
168                 $sn = $row["sn_ip"];
169         }
170         $wwwConnector->header();
171         $wwwConnector->printError();
172         ?>
173 <form method="post" action="?action=allocatesub&id=<?php echo $id ?>">
174 <input type="hidden" name="superid" value="<?php echo $id ?>">
175 <table>
176 <tr><td>Subnet Name</td><td><input type="text" name="subname"></td></tr>
177 <tr><td>Subnet IP</td><td><input type="text" name="subip" value="<?php echo $sn?>"></td></tr>
178 <tr><td>Subnet Mask</td><td><input type="text" name="submask"></td></tr>
179 <tr><td>Description</td><td><input type="text" name="subdesc"></td></tr>
180 <tr><td><input type="submit" name="add" value="Add"></td></tr>
181 </table>
182 </form>
183 <?php
184
185         $wwwConnector->footer();
186 }
187
188 function www_ip_allocateSubRange()
189 {
190         global $db, $wwwConnector;
191         
192         $superid = $_REQUEST["superid"];
193         $name = $_REQUEST["subname"];
194         $subip = $_REQUEST["subip"];
195         $mask = $_REQUEST["submask"];
196         $desc = $_REQUEST["subdesc"];
197         
198         $myip = new ip();
199         if($myip->addSubnet($name, $subip, $mask, $desc, $superid)) {
200                 header("Location: index.php?notice=range added");
201         } else {
202                 header("Location: index.php?error=invalid ipaddress");
203         }
204 }
205
206 function www_ip_modifySubnet()
207 {
208         global $db, $wwwConnector;
209
210         $id = $_REQUEST["id"];
211         //("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT);';
212         $sql = "select sn_name,sn_desc,sn_ip from subnet where sn_id=='$id'";
213         $res = $db->dbobject->query($sql);
214         
215         foreach($res as $row) {
216                 $sn_name = $row["sn_name"];
217                 $sn_desc = $row["sn_desc"];
218                 $sn_ip = $row["sn_ip"];
219         }
220         
221         $wwwConnector->header();
222         $wwwConnector->printError();
223         
224         // ("ho_id" INTEGER PRIMARY KEY AUTOINCREMENT,"ho_sn_id" INTEGER,"ho_ip" TEXT,"ho_name" TEXT,"ho_desc" TEXT)
225         ?>
226 <form method="post" action="?action=changesubdetails&id=<?php echo $id ?>">
227 Subnet Name: <input type="text" name="subname" value="<?php echo $sn_name?>">
228 Subnet Description: <input type="text" name="subdesc" value="<?php echo $sn_desc?>">
229 <input type="submit" name="change" value="Update">
230 </form>
231
232 <form method="post" action="?action=addhosts&id=<?php echo $id ?>">
233 <h3>Add Host</h3>
234 Hostname <input type="text" name="hostname">
235 IP Address <input type="text" name="hostip" value="<?php echo $sn_ip.":"?>">
236 Description <input type="text" name="hostdesc" >
237 <input type="submit" name="change" value="Add">
238 </form>
239
240
241
242 <table border=1>
243 <tr><th>Host IP</th><th>Hostname</th><th>Description</th></tr>
244 <?php
245         $sql = "select * from hosts where ho_sn_id='$id'";
246         $res = $db->dbobject->query($sql);
247         
248         foreach($res as $row) {
249                 $name = $row["ho_name"];
250                 $desc = $row["ho_desc"];
251                 $ip = $row["ho_ip"];
252                 $hid = $row["ho_id"];
253                 echo "<tr><td>$ip</td><td>$name</td><td>$desc</td><td><a href=\"?action=deletehost&hostid=$hid&snid=$id\">Delete</a></td></tr>";
254         } 
255 ?>
256 </table>
257
258
259 <a href="index.php">Back</a>
260 <?php
261
262         $wwwConnector->footer();
263 }
264
265 function www_ip_deletehost()
266 {
267         global $db, $wwwConnector;
268         
269         $id = $_REQUEST["hostid"];
270         $sid = $_REQUEST["snid"];
271         
272         $sql = "delete from hosts where ho_id=='$id'";
273         $db->dbobject->query($sql);
274         
275         header("Location: ?action=modsubnet&id=$sid");
276 }
277
278 function www_ip_addhost()
279 {
280         global $db, $wwwConnector;
281         
282         $id = $_REQUEST["id"];
283         $hn = $_REQUEST["hostname"];
284         $hip = $_REQUEST["hostip"];
285         $desc = $_REQUEST["hostdesc"];
286         
287         $sql = "insert into hosts values (NULL, '$id', '$hip', '$hn', '$desc')";
288         $db->dbobject->query($sql);
289         
290         header("Location: ?action=modsubnet&id=$id");
291 }
292
293 function www_ip_changesubdetails()
294 {
295         global $db, $wwwConnector;
296         
297         $id = $_REQUEST["id"];
298         $name = $_REQUEST["subname"];
299         $desc = $_REQUEST["subdesc"];
300
301         $sql = "update subnet set sn_name='$name', sn_desc='$desc' where sn_id=='$id'";
302         $db->dbobject->query($sql);
303         
304         header("Location: ?action=modsubnet&id=$id");
305 }
306
307 function www_db_dumpdb()
308 {
309         global $db;
310         
311         $datestamp = strftime("%d-%m-%Y-%H.%M.%S");
312         $name = "ipmandbdump-$datestamp.db";
313         header("Content-type: application/octet-stream;\n");
314         header("Content-Disposition: attachment; filename=\"$name\";\n\n");
315         $db->dump();
316 }
317
318 function www_db_restoredb()
319 {
320 }
321
322 ?>