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