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