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