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