working on the interfaces page
[CBFWR.git] / libcbfwr / web.php
1 <?php
2 $MENU_ITEMS["15_Zones"]["link"] = "$BASE_URL/zones"; 
3 $MENU_ITEMS["15_Zones"]["name"] = "Zones"; 
4 $MENU_ITEMS["20_Objects"]["link"] = "$BASE_URL/objects"; 
5 $MENU_ITEMS["20_Objects"]["name"] = "Objects"; 
6 $MENU_ITEMS["30_Rules"]["link"] = "$BASE_URL/rules"; 
7 $MENU_ITEMS["30_Rules"]["name"] = "Rules"; 
8 $MENU_ITEMS["15_Routes"]["link"] = "$BASE_URL/routing"; 
9 $MENU_ITEMS["15_Routes"]["name"] = "Routing"; 
10 $MENU_ITEMS["10_Interfaces"]["link"] = "$BASE_URL/interfaces"; 
11 $MENU_ITEMS["10_Interfaces"]["name"] = "Interfaces"; 
12 $MENU_ITEMS["05_Admin"]["link"] = "$BASE_URL/admin"; 
13 $MENU_ITEMS["05_Admin"]["name"] = "Admin"; 
14
15 // if i believed in name spacing in php, i'd use it.
16 error_log("cbfwweb loaded");
17
18 class CBFWWeb {
19         
20         function __construct()
21         {
22
23         }
24         
25         function go()
26         {
27                 $url = "/";
28                 $bodycontent = null;
29                 
30                 $conf = new Config();
31                 $comms = new Comms(); 
32                 $conf->loadConfig($comms->getConfig(0));
33                 $rconfig = $conf->getConfig();
34                 
35                 if($rconfig["status"] != "conf") {
36                         if($rconfig["status"] == "nodir") {
37                                 cbfw_startnodir();
38                                 return 0;
39                         }
40                         cbfw_startinstaller();
41                         return 0;
42                 }
43                 
44                 
45                 if(isset($_REQUEST["q"])) {
46                         $url = $_REQUEST["q"];
47                         
48                         // ok, now we try and find the basics
49                         $url_s = explode("/", $url);
50                         
51                         $bodyFunction = null;
52                         switch($url_s[0]) {
53                                 case "zones":
54                                         CBFWZonesPage($url_s);
55                                         break;
56                                 case "interfaces":
57                                         CBFWInterfacesPage($url_s);
58                                         break;
59                                 case "objects":
60                                         CBFWObjectsPage($url_s);
61                                         break;
62                                 case "routing":
63                                         CBFWRoutingPage($url_s);
64                                         break;
65                                 case "rules":
66                                         $bodyFunction = "CBFWRulesPage";
67                                         break;
68                                 case "changes":
69                                         CBFWChangesPage($url_s);
70                                         break;
71                                 case "admin":
72                                         CBFWAdminPage($url_s);
73                                         break;
74                                 default:
75                                         $bodyFunction = findUrl($url_s);
76                         }
77                 } else {
78                         $bodyFunction = "CBFBuildHomePage";
79                 }
80                 if($bodyFunction != null) CBFWpageBuilder(null, $bodyFunction);
81         }       
82 }
83
84
85 function CBFBuildHomePage($urls)
86 {
87         echo "Must remember this, gotta rules could apply to multiple zones not just one<br>";
88         echo "i.e.: add rule reject from object/host/hostname to address6/2003::123 in zones Zone/zonename/rulenum Zone/zonename/rulenum";
89         
90         $comms = new Comms();
91         
92         echo "<pre>";
93         print_r($comms->getConfig(0));
94         echo "\n\n\n";
95         print_r($comms->getConfig(1));
96         echo "\n\n\n";
97         print_r($comms->getConfig(2));
98         echo "\n\n\n";
99         print_r($comms->getConfig(3));
100         echo "\n\n\n";
101         print_r($comms->getConfig(4));
102         echo "\n\n\n";
103         print_r($comms->getConfig(5));
104         echo "</pre>";
105         
106 }
107
108 function CBFWMenuBuilder()
109 {
110         global $BASE_URL, $MENU_ITEMS;
111         
112         ksort($MENU_ITEMS);
113         ?>
114 <table border="1"><tr><th>Menu</th><td>
115 <a href="<?php echo $BASE_URL ?>">Home</a></td>
116         <?php
117         foreach($MENU_ITEMS as $key => $val) {
118                 $link = $val["link"];
119                 $name = $val["name"];
120                 echo "<td><a href=$link>$name</a></td>";
121         }
122         
123         echo "</table>";
124 }
125
126 // just a short one to call the long one
127 function gcam($m, $s)
128 {
129         global $glconfig;
130         CBFWAddMessage($m, $s);
131 }
132
133 function CBFWAddMessage($messagelev, $message)
134 {
135         
136 }
137
138 function CBFWMessageBuilder()
139 {
140         echo "message";
141 }
142
143 function CBFWpageBuilder($bodyClass, $bodyFunction, $bodycontent=null, $title="CBFW", $bodyfunctiondata=null)
144 {
145         global $WEB_ROOT_FS, $BASE_URL;
146         
147         // TODO: load css
148         // header
149         echo "<html><head><title>$title</title>";
150         
151         // load css
152         if(file_exists("$WEB_ROOT_FS/css")) {
153                 $dh = opendir("$WEB_ROOT_FS/css");
154                 if($dh) {
155                         while(($file = readdir($dh))!==false) {
156                                 $mt = preg_match("/.*.css$/", $file);
157                                 if($mt > 0) {
158                                         error_log("loading css $file");
159                                         echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$BASE_URL/css/$file\">";
160                                         //echo "required $basedir/$file\n";
161                                 }
162                         }
163                 }               
164         }
165
166         // load js
167         if(file_exists("$WEB_ROOT_FS/js")) {
168                 $dh = opendir("$WEB_ROOT_FS/js");
169                 if($dh) {
170                         while(($file = readdir($dh))!==false) {
171                                 $mt = preg_match("/.*.js$/", $file);
172                                 if($mt > 0) {
173                                         error_log("loading js $file");
174                                         echo "<script type=\"text/javascript\" src=\"$BASE_URL/js/$file\"></script>";
175                                         //echo "required $basedir/$file\n";
176                                 }
177                         }
178                 }               
179         }
180         
181         
182         // start body
183         echo "</head><body>";
184         
185         // page top
186         echo "<h1>CBFW</h1><br>";
187         echo "<table width=\"100%\"><tr width=\"100%\"><td>";
188         CBFWMessageBuilder();
189         echo "<td><td align=\"right\">";
190         CBFWConfigStatus();
191         echo "</td></tr><tr><td>";
192         
193         // menu, then body
194         echo "<table><tr><td>";
195         CBFWMenuBuilder();
196         echo "</td></tr><tr><td>";
197         // body
198         $url = "/";
199         if(isset($_REQUEST["q"])) {
200                 $url = $_REQUEST["q"];
201         }
202         
203         if($bodyClass != null) {
204                 $bodyClass->$bodyFunction($bodyfunctiondata);
205         } else if( $bodyFunction != null) {
206                 $bodyFunction($bodyfunctiondata);
207         } else echo $bodycontent;
208         echo "</td></tr></table>";
209         
210         
211         // close the big wrap-around table
212         echo "</td></tr></table>";
213         
214         // footer
215         echo "<br><font size=\"-1\">Copyright 2011, PJR</font><br></body></html>";
216         
217 }
218
219 function cbfw_getLastSeen($ip, $data)
220 {
221         $last = "never";
222         
223         if(!$data) return $last;
224         
225         
226         
227         foreach($data as $key=>$val) {
228                 $last = $val["name"];
229                 error_log("got $last for $ip");
230         }
231         
232         if($last == "never") return $last;
233         
234         $last = cbfw_tdiffToAgo(time()-$last);
235         
236         error_log("last for $ip set to $last");
237         
238         return $last;
239         
240 }
241
242 function CBFWConfigStatus()
243 {
244         global $WEB_ROOT_FS, $BASE_URL;
245         
246         $comms = new Comms();
247         
248         $conf0 = $comms->getConfig(0);
249         $conf1 = $comms->getConfig(2);
250         $conf2 = $comms->getConfig(3);
251         
252         if($conf1["status"] == "nochange") {
253                 $saved = "Current";
254                 $running = "Current";
255         } else {
256                 $saved = "Old";
257                 $running = "Current";           
258         }
259         if($conf2["status"] == "nochange") {
260                 $client = "Current";
261         } else {
262                 $ch = $conf2["status"];
263                 $client = "$ch Changes <a href=\"$BASE_URL/changes/show\">Show</a>";
264         }
265         
266         
267         echo "Saved Config: $saved<br>";
268         echo "Running Config: $running<br>";
269         echo "Client Config: $client<br>";
270 }
271
272 function cbfw_tdiffToAgo($time_in_sec)
273 {
274         $tdiff = $time_in_sec;
275         $tdiff_min = (int)($tdiff/60);
276         $tdiff_hour = (int)($tdiff/3600);
277         $tdiff_days = (int)($tdiff/86400);
278         
279         if($tdiff < 60) $last = "Less then a minute";
280         if($tdiff >= 60 && $tdiff < 7200) $last = "$tdiff_min minutes ago";
281         if($tdiff >= 7200 && $tdiff < 86400) $last = "$tdiff_hour hours ago";
282         if($tdiff >= 86400) $last = "$tdiff_days days ago";
283         
284         return $last;
285 }
286
287 function cbfw_startInstaller()
288 {
289         global $WEB_ROOT_FS, $BASE_URL;
290         
291         $uid = posix_geteuid();
292         $gid = posix_getegid();
293         $uid_a = posix_getpwuid($uid);
294         $uid = $uid_a["name"];
295         
296         $gid_a = posix_getgrgid($gid);
297         $gid = $gid_a["name"];
298         error_log("user id is $uid, group id is $gid");
299         
300         if(isset($_REQUEST["installdir"])) {
301                 $c = new CBFWConfig();
302                 touch($_REQUEST["installdir"]."/webconfig");
303                 $c->loadConfig($_REQUEST["installdir"]."/webconfig");
304                 header("Location: index.php");
305                 return;
306         }
307         
308         $underroot = realpath($WEB_ROOT_FS."/../");
309         
310 ?>
311 <html>
312 <h1>Welcome to CBFW</h1>
313 Welcome to CBFW, I cant find my configuration file so im assuming you installing me for the first time<br>
314 If this is not correct then we have a big problem that needs to be solved, i hope you have a backup of the old
315 config, cause that will make life easier.<br><br>
316 However, if this is the first time you have run this app, then all is good with the world. All I need right
317 now is a place where i can store my config. I search the following directories for the location of my 
318 configuration (webconfig)<br>
319 <li> /var/run/glcas/
320 <li> /var/lib/glcas/
321 <li> <?php echo $underroot?>/var/glcas/
322 <br><br>
323
324 As root, you must now create one of these directories and change the ownership of the directory to the web owner.
325 (chown <?php echo $uid?>:<?php echo $gid ?> the_path_you_choose).<br>
326
327 Now, tell me where you want me to create the webconfig file:<br>
328 <form method="post">
329 <select name="installdir">
330 <option value="/var/run/glcas">/var/run/glcas</option>
331 <option value="/var/lib/glcas">/var/lib/glcas</option>
332 <option value="<?php echo $underroot?>/var/glcas/"><?php echo $underroot?>/var/glcas/</option>
333 </select>
334 <input type="submit" name="Go" value="Go">
335
336 </select>
337 </form>
338
339 </html>
340 <?php 
341 }
342
343 function cbfw_startnodir()
344 {
345 ?>
346 <html>
347 <h1>Cant Run</h1>
348 There is no config and no directory where i can store one. Login to the server and either create /var/run/cbfwr or <?php echo $LIB_ROOT_FS?>/var
349 </html>
350 <?php
351 }
352
353
354 ?>