added some db 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["dumpdb"] = "www_db_dumpdb";
7 $actionRegister["restoredb"] = "www_db_restoredb";
8
9 class www {
10         function Go() {
11                 // this is the web page entry function.
12                 global $db;
13                 
14                 $db->connect();
15                 //if($db->connect()!=0) {
16                         //$this->doInstaller();
17                         //exit(0);
18                 //}
19                 
20                 // its up to auth as to wether "this" url requires auth.
21                 $authThis = new auth();
22                 $authThis->Go();
23                 
24                 if(isset($_REQUEST["action"])) {
25                         $this->doAction($_REQUEST["action"]);
26                 } else {
27                         $this->mainAction();
28                 }
29         }
30         
31         function doAction($actionName)
32         {
33                 global $actionRegister;
34                 
35                 error_log("im here");
36                 if(isset($actionRegister[$actionName])) {
37                         $func = $actionRegister[$actionName];
38                         error_log("im here 2");
39                         $func();
40                 } else {
41                         error_log("im here 3");
42                         $this->mainAction();
43                 }
44         }
45         
46         function mainAction()
47         {
48                 // we build a frame of framey's
49                 $this->header();
50                 $this->printError();
51                 $this->mainPage();              
52                 $this->footer();
53         }
54         
55         function mainPage()
56         {
57                 global $db;
58                 
59                 ?>
60 <form method="post" action="?action=addsuper">
61 Create Supernet: name <input type="text" name="name"></input>
62 Subnet Address <input type="text" name="subnet"></input>
63 Mask <input type="text" name="mask"></input>
64 Description <input type="text" name="desc"></input>
65 <input type="submit" name="go" Value="Create"></input>
66 </form>
67                 <?php
68
69                 // now print the super nets
70                 $res = $db->dbobject->query("select * from supernet");
71                 foreach($res as $row) {
72                         //echo "<pre>";
73                 //      print_r($row);
74                 //      echo "</pre><hr>";
75                         echo "<table border=\"1\"><tr><th>".$row["sn_name"]."</th><td>".$row["sn_ip"]."/".$row["sn_mask"]."</td><td>".$row["sn_desc"]."</td>";
76                         echo "<td><a href=\"?action=allocate&id=".$row["sn_id"]."\">Allocate Subnet</a></td>";
77                         echo "<td><a href=\"?action=delete&id=".$row["sn_id"]."\">Delete Supernet</a></td>";
78                         // now we search for sub's
79                         echo "</table><br>";
80                 }
81                 
82                 echo "<a href=\"?action=dumpdb\">dump database</a> <a href=\"?action=restoredb\">restore database</a>";  
83         }
84         
85         function printError()
86         {
87                 if(isset($_REQUEST["error"]))
88                 {
89                         echo "<font color=\"red\">".$_REQUEST["error"]."</font>";
90                 }
91                 if(isset($_REQUEST["notice"]))
92                 {
93                         echo "<font color=\"green\">".$_REQUEST["notice"]."</font>";
94                 }
95                 
96         }
97         
98         function header($title = "Welcome to PHP IP Manager")
99         {
100                 ?>
101 <html>
102 <head><title><?php echo $title ?></title>
103 </head>
104 <body><h1><?php echo $title ?></h1>
105                 <?php
106         }
107         
108         function footer()
109         {
110                 ?>
111 </body></html>
112                 <?php
113         }
114         
115         
116         function doInstaller()
117         {
118                 header("Location: install.php");
119         }
120 }
121
122 function www_ip_addSuperRange()
123 {
124         global $db;
125         
126         $name = $_REQUEST["name"];
127         $sn = $_REQUEST["subnet"];
128         $mask = $_REQUEST["mask"];
129         $desc = $_REQUEST["desc"];
130         
131         $myip = new ip();
132         
133         if($myip->addSupernet($name, $sn, $mask, $desc)) {
134                 header("Location: index.php?notice=range added");
135         } else {
136                 header("Location: index.php?error=invalid ipaddress");
137         }
138 }
139
140 function www_ip_allocateRange()
141 {
142         global $db, $wwwConnector;
143
144         $id = $_REQUEST["id"];
145         
146         $res = $db->dbobject->query("select * from supernet where sn_id=='$id'");
147         
148         foreach($res as $row) {
149                 $sn = $row["sn_ip"];
150         }
151         $wwwConnector->header();
152         $wwwConnector->printError();
153         ?>
154 <form method="post" action="?action=allocatesub&id=<?php echo $id ?>">
155 <input type="hidden" name="superid" value="<?php echo $id ?>">
156 <table>
157 <tr><td>Subnet Name</td><td><input type="text" name="subname"></td></tr>
158 <tr><td>Subnet IP</td><td><input type="text" name="subip" value="<?php echo $sn?>"></td></tr>
159 <tr><td>Subnet Mask</td><td><input type="text" name="submask"></td></tr>
160 <tr><td>Description</td><td><input type="text" name="subdesc"></td></tr>
161 <tr><td><input type="submit" name="add" value="Add"></td></tr>
162 </table>
163 </form>
164 <?php
165
166         $wwwConnector->footer();
167 }
168
169 function www_ip_allocateSubRange()
170 {
171         global $db, $wwwConnector;
172         
173         $superid = $_REQUEST["superid"];
174         $name = $_REQUEST["subname"];
175         $subip = $_REQUEST["subip"];
176         $mask = $_REQUEST["submask"];
177         $desc = $_REQUEST["subdesc"];
178         
179         $myip = new ip();
180         if($myip->addSubnet($name, $subip, $mask, $desc, $superid)) {
181                 header("Location: index.php?notice=range added");
182         } else {
183                 header("Location: index.php?error=invalid ipaddress");
184         }
185 }
186
187 function www_db_dumpdb()
188 {
189 }
190
191 function www_db_restoredb()
192 {
193 }
194
195 ?>