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