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