4bcfed5513ae07cc14624e34daac0a8e76ef4963
[CBFWR.git] / libcbfwr / fwui.php
1 <?php
2
3 function CBFWZonesPage($urls)
4 {
5         global $BASE_URL, $MENU_ITEMS;
6         $comms = new Comms();
7         
8         if(isset($urls[1])) {
9                 switch($urls[1]) {
10                         case "modify":
11                                 
12                                 if($_REQUEST["description"] != "") $desc = $_REQUEST["description"];
13                                 else $desc = null;
14                                 
15                                 $oldname = $_REQUEST["oldname"];
16                                 $newname = $_REQUEST["zonename"];
17                                 
18                                 $renewname = null;
19                                 error_log("comparing \"$oldname\" to \"$newname\"");
20                                 if($oldname != $newname) {
21                                         $renewname = " rename $newname";
22                                 }
23                                 
24                                 $oldconf = getWebUserConfig();
25                                 $olddesc = $oldconf["zone"][$oldname]["description"];
26                                 error_log("Comparing \"$olddesc\" and \"$desc\"");
27                                 if($desc != $olddesc) {
28                                         $newdesc = " # $desc";
29                                 } else $newdesc = null;
30                                 
31                                 //echo "confs: \"$olddesc\", \"$newdesc\", $oldname, $newname<br>";
32                                 //exit(0);
33                                 
34                                 if($renewname == null && $newdesc == null) {
35                                         header("Location: $BASE_URL/zones");
36                                         error_log("no changes?");
37                                         return;
38                                 }
39                                 
40                                 
41                                 $conf = $comms->getConfig(3);
42                                 $changes = $conf["status"];
43                                 if($changes == "nochange") $conf["status"] = 1;
44                                 else $conf["status"] = $changes + 1;
45                                 $cnum = $conf["status"];
46                                 
47                                 $conf[$cnum]["modify"] = "zone $oldname$renewname$newdesc";
48                                 $comms->putConfig($conf, 3);
49                                 header("Location: $BASE_URL/zones");
50                                 
51                                 break;
52                                 
53                                 
54                         case "edit":
55                                 CBFWpageBuilder(null, "CBFWZonesEditPage", null, null, $urls);
56                                 break;
57                                 
58                                 
59                         case "delete":
60                                 $zonetodelete = $urls[2];
61                                 
62                                 $conf = $comms->getConfig(3);
63                                 $changes = $conf["status"];
64                                 if($changes == "nochange") $conf["status"] = 1;
65                                 else $conf["status"] = $changes + 1;
66                                 $cnum = $conf["status"];
67                                 
68                                 $conf[$cnum]["delete"] = "zone $zonetodelete";
69                                 $comms->putConfig($conf, 3);
70                                 header("Location: $BASE_URL/zones");
71                                 break;
72                                 
73                                 
74                         case "add":
75                                 
76                                 // UGLY
77                                 $cnum = 0;
78                                 $nametoadd = $_REQUEST["toadd"];
79                                 $desc = $_REQUEST["desc"];
80                                 $descbit = "";
81                                 if($desc != "") $descbit = " # $desc";
82                                 if($nametoadd == "" || $nametoadd == null) {
83                                         return;
84                                 }
85                                 $conf = $comms->getConfig(3);
86                                 $changes = $conf["status"];
87                                 if($changes == "nochange") $conf["status"] = 1;
88                                 else $conf["status"] = $changes + 1;
89                                 $cnum = $conf["status"];
90                                 $conf[$cnum]["add"] = "zone $nametoadd$descbit";
91                                 $comms->putConfig($conf, 3);
92                                 header("Location: $BASE_URL/zones");
93                                 break;
94                                 
95                                 
96                         default:
97                                 CBFWpageBuilder(null, "CBFWZonesPageDisplay");
98                 }
99         } else {
100                 CBFWpageBuilder(null, "CBFWZonesPageDisplay");
101         }
102 }
103
104 function CBFWZonesEditPage($urls)
105 {
106         global $BASE_URL, $MENU_ITEMS;
107         
108         $zone = $urls[2];
109         
110         $config = getWebUserConfig();
111         
112         
113         echo "<h2>Edit Zone $zone</h2>";
114         echo "<form method=\"post\" action=\"$BASE_URL/zones/modify/$zone\">";
115         echo "<input type=\"hidden\" name=\"oldname\" value=\"$zone\">";
116         echo "Name: <input type=\"text\" name=\"zonename\" value=\"$zone\"><br>";
117         
118         if(isset($config["zone"][$zone]["description"])) $desc = $config["zone"][$zone]["description"];
119         
120         echo "Description: <input type=\"text\" name=\"description\" value=\"$desc\"><br>";
121         echo "<input type=\"submit\" name=\"Go\" value=\"Go\">";
122         echo "</form>";
123 }
124
125 function getWebUserConfig()
126 {
127         $comms = new Comms();
128         $myconf = new Config();
129         
130         
131         // check to see if pre-cache config matches change level of config change buffer
132         $config = $comms->getConfig(5);
133         $conf2 = $comms->getConfig(3);
134         if(isset($config["status"])) {
135                 if($config["status"] == $conf2["status"]) {
136                         // it did, return the pre-cache
137                         error_log("got pre-cache config");
138                         return $config;
139                 }
140         }
141         
142         // it did not, do a new merge and cache
143         $config = $myconf->mergeConfig($comms->getConfig(), $conf2);
144         $reconfig = $config;
145         $reconfig["status"] = $conf2["status"];
146         error_log("put pre-cache config");
147         $comms->putConfig($reconfig, 5);        
148         
149         return $config;
150 }
151
152 function CBFWZonesPageDisplay()
153 {
154         global $BASE_URL, $MENU_ITEMS;
155         
156         $config = getWebUserConfig();
157
158         echo "<h2>Zones</h2>";
159         echo "<table border=\"1\"><tr><th>Zone</th><th>Interfaces</th><th>Description</th><th>Edit</th></tr>";
160         foreach($config["zone"] as $key => $var) {
161                 $name = $key;
162                 $url = "<a href=\"$BASE_URL/zones/edit/$name\">Edit</a>";
163                 $url2 = "<a href=\"$BASE_URL/zones/delete/$name\">Delete</a>";
164                 $desc = $var["description"];
165                 echo "<tr><td>$name</td><td>...</td><td>$desc</td><td>$url $url2</td></tr>";
166         }
167                 
168         echo "</table><br>";
169         echo "<form method=\"post\" action=\"$BASE_URL/zones/add\">";
170         echo "<hr><h3>Add Zone</h3><br>";
171         echo "Name: <input type=\"text\" name=\"toadd\"><br>";
172         echo "Description: <input type=\"text\" name=\"desc\"><br>";
173         echo "<input type=\"submit\" name=\"Add\" value=\"Add\">";
174         echo "</form>";
175         
176 }
177
178 function CBFWInterfacesPage($urls)
179 {
180         if(isset($urls[1])) {
181                 switch($urls[1]) {
182                         case "edit":
183                                 CBFWpageBuilder(null, "CBFWInterfacesPageEdit", null, null, $urls[2]);
184                                 break;
185                         case "change":
186                                 CBFWInterfacesChange();
187                                 break;
188                         default:
189                                 CBFWpageBuilder(null, "CBFWInterfacesPageDisplay");
190                 }
191         } else {
192                 error_log("main interface page");
193                 CBFWpageBuilder(null, "CBFWInterfacesPageDisplay");
194         }
195 }
196
197 function CBFWInterfacesChange()
198 {
199         global $BASE_URL, $MENU_ITEMS;
200         $comms = new Comms();
201         
202         $comms->sendMessage("interface eth0 changename poof");
203         header("Location: $BASE_URL/interfaces");
204 }
205
206 function CBFWInterfacesPageEdit($urls)
207 {
208         global $BASE_URL, $MENU_ITEMS;
209         $interface = $urls;
210         
211         echo "<h3>Edit Interface $interface</h3><br>";
212         echo "<form method=\"post\" action=\"$BASE_URL/interfaces/change/$interface\">";
213         echo "<table>";
214         echo "<tr><td>Name</td><td><input type=\"text\" name=\"name\"></td></tr>";
215         echo "<tr><td>HW Address</td><td><input type=\"text\" name=\"hwaddr\"></td></tr>";
216         echo "<tr><td>Zone</td><td><input type=\"text\" name=\"zone\"></td></tr>";
217         echo "<tr><td>IPv4 Address</td><td><input type=\"text\" name=\"ipv4addr\"></td></tr>";
218         echo "<tr><td>IPv6 Address</td><td><input type=\"text\" name=\"ipv6addr\"></td></tr>";
219         echo "<tr><td>Speed</td><td><input type=\"text\" name=\"speed\"></td></tr>";
220         echo "<tr><td>Duplex</td><td><input type=\"text\" name=\"duplex\"></td></tr>";
221         echo "</table>";
222         echo "<input type=\"submit\" name=\"Change\" value=\"Change\"><br>";
223         //echo "<a href=\"$BASE_URL/interfaces/\">Back</a>";
224         echo "</form>";
225 }
226
227 function CBFWInterfacesPageDisplay()
228 {
229         global $BASE_URL, $MENU_ITEMS;
230         $comms = new Comms();
231         
232         $config = $comms->getConfig(0);
233         $boot_config = $comms->getConfig(1);
234         
235         // now build an interface table
236         $table = array();
237         foreach($boot_config["hardware"]["netdev"] as $key=>$val) {
238                 $ename = $key;
239                 $table[$key]["realname"] = $val["name"];
240                 $table[$key]["name"] = $val["name"];
241                 
242                 
243                 // driver
244                 if(isset($val["driver"])) $table[$key]["type"] = $val["driver"];
245                 else $table[$key]["type"] = "Unknown";
246                 
247                 // hw address
248                 if(isset($val["hwaddress"])) $table[$key]["hwaddress"] = $val["hwaddress"];
249                 
250                 // current mtu
251                 if(isset($val["mtu"])) $table[$key]["mtu"] = $val["mtu"];
252         }
253         
254         foreach($config["hardware"]["netdev"] as $key=>$val) {
255                 $ename = $key;
256                 if(isset($config["interface"]["$key"]["name"])) $table[$key]["name"] = $config["interface"]["$key"]["name"];
257                 $table[$key]["address4"] = null;
258                 $table[$key]["address6"] = null;
259                 
260                 if(isset($config["interface"][$table[$key]["name"]]["address4"])) {
261                         $table[$key]["address4"] = $config["interface"][$table[$key]["name"]]["address4"];
262                 }
263                 
264                 if(isset($config["interface"][$table[$key]["name"]]["address6"])) {
265                         $table[$key]["address6"] = $config["interface"][$table[$key]["name"]]["address6"];
266                 }
267                 
268                 if(isset($config["interface"][$table[$key]["name"]]["mtu"])) {
269                         $table[$key]["mtu"] = $config["interface"][$table[$key]["name"]]["mtu"];
270                 } else {
271                         $table[$key]["mtu"] = "1500"; // TODO: this needs to actually come from somewhere
272                 }
273                 
274                 if(isset($config["interface"][$table[$key]["name"]]["speed"])) {
275                         $table[$key]["speed"] = $config["interface"][$table[$key]["name"]]["speed"];
276                 } else {
277                         $table[$key]["speed"] = null;
278                 }
279
280                 if(isset($config["interface"][$table[$key]["name"]]["duplex"])) {
281                         $table[$key]["duplex"] = $config["interface"][$table[$key]["name"]]["duplex"];
282                 } else {
283                         $table[$key]["duplex"] = null;
284                 }
285                 
286                 if(isset($config["interface"][$table[$key]["name"]]["zone"])) {
287                         $table[$key]["zone"] = $config["interface"][$table[$key]["name"]]["zone"];
288                 } else {
289                         $table[$key]["zone"] = null;
290                 }
291         }
292         
293         
294         echo "<table border=\"1\"><th>Device</th><th>Zone</th><th>Type</th><th>MAC Address</th><th>Address</th><th>MTU</th><th>Speed/Duplex</th><th>Control</th></tr>";
295         foreach($table as $key =>$val) {
296                 if($val["realname"] == $val["name"]) $name = $val["name"];
297                 else $name = $val["name"]." (".$val["realname"].")";
298                 
299                 // zone
300                 if($val["zone"] == null) $zone = "-";
301                 else $zone = $val["zone"];
302                 
303                 // mtu
304                 $mtu = $val["mtu"];
305                 
306                 // driver type
307                 $type = $val["type"];
308                 
309                 // hardware address
310                 $mac = $val["hwaddress"];
311                 
312                 // speed and duplex
313                 if($val["duplex"] == null) $dup = "default";
314                 else $dup = $val["duplex"];
315                 
316                 if($val["speed"] == null) $spd = "default";
317                 else $spd = $val["speed"];
318                 
319                 $spanddu = "$spd/$dup";
320                 
321                 // network address
322                 if($val["address4"]!=null && $val["address6"]!=null) {
323                         $address = $val["address4"]."<br>".$val["address6"];
324                 } else if($val["address4"] != null) {
325                         $address = $val["address4"];
326                 } else if($val["address6"] != null) {
327                         $address = $val["address6"];
328                 } else {
329                         $address = "-";
330                 }
331                 
332                 echo "<tr><th>$name</th><td>$zone</td><td>$type</td><td>$mac</td><td>$address</td><td>$mtu</td><td>$spanddu</td><td><a href=\"$BASE_URL/interfaces/edit/".$val["realname"]."\">Edit</a></td></tr>";
333         }
334         echo "</table>";
335 }
336
337 function CBFWObjectsPage()
338 {
339         $comms = new Comms();
340         
341         $config = $comms->getConfig();
342 }
343
344 function CBFWRulesPage()
345 {
346         $comms = new Comms();
347         
348         $config = $comms->getConfig();
349 }
350
351 function findUrl($url_s)
352 {
353         global $URL_HANDLERS;
354         
355         foreach($URL_HANDLERS as $key => $val) {
356                 
357         }
358 }
359 ?>