6bfcad6ed1e526f8a55fb49f046725aba6301f86
[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                 $conf->loadConfig();
30                 $rconfig = $conf->getConfig();
31                 
32                 if($rconfig["status"] != "conf") {
33                         cbfw_startinstaller();
34                         return 0;
35                 }
36                 
37                 
38                 if(isset($_REQUEST["q"])) {
39                         $url = $_REQUEST["q"];
40                         
41                         // ok, now we try and find the basics
42                         $url_s = explode("/", $url);
43                         
44                         switch($url_s[0]) {
45                                 case "zones":
46                                         $bodyFunction = "CBFWZonesPage";
47                                         break;
48                                 case "interfaces":
49                                         $bodyFunction = "CBFWInterfacesPage";
50                                         break;
51                                 case "objects":
52                                         $bodyFunction = "CBFWObjectsPage";
53                                         break;
54                                 case "rules":
55                                         $bodyFunction = "CBFWRulesPage";
56                                         break;
57                                 case "admin":
58                                         $bodyFunction = "CBFWAdminPage";
59                                         break;
60                                 default:
61                                         $bodyFunction = findUrl($url_s);
62                         }
63                 } else {
64                         $bodyFunction = "CBFBuildHomePage";
65                 }
66                 CBFWpageBuilder(null, $bodyFunction);
67         }       
68 }
69
70
71 function CBFWAdminPage($urls)
72 {
73         echo "im an admin page";
74 }
75
76 function CBFBuildHomePage($urls)
77 {
78         echo "Must remember this, gotta rules could apply to multiple zones not just one<br>";
79         echo "i.e.: add rule reject from object/host/hostname to address6/2003::123 in zones Zone/zonename/rulenum Zone/zonename/rulenum";
80         $conf = new Config();
81         $conf->loadConfig();
82         
83         echo "<pre>";
84         print_r($conf->getConfig());
85         echo "\n\n\n";
86         print_r($conf->getBootConfig());
87         echo "</pre>";
88         
89 }
90
91 function CBFWMenuBuilder()
92 {
93         global $BASE_URL, $MENU_ITEMS;
94         
95         ksort($MENU_ITEMS);
96         ?>
97 <table border="1"><tr><th>Menu</th><td>
98 <a href="<?php echo $BASE_URL ?>">Home</a></td>
99         <?php
100         foreach($MENU_ITEMS as $key => $val) {
101                 $link = $val["link"];
102                 $name = $val["name"];
103                 echo "<td><a href=$link>$name</a></td>";
104         }
105         
106         echo "</table>";
107 }
108
109 // just a short one to call the long one
110 function gcam($m, $s)
111 {
112         global $glconfig;
113         CBFWAddMessage($m, $s);
114 }
115
116 function CBFWAddMessage($messagelev, $message)
117 {
118         
119 }
120
121 function CBFWMessageBuilder()
122 {
123
124 }
125
126 function CBFWpageBuilder($bodyClass, $bodyFunction, $bodycontent=null, $title="CBFW")
127 {
128         global $WEB_ROOT_FS, $BASE_URL;
129         
130         // TODO: load css
131         // header
132         echo "<html><head><title>$title</title>";
133         
134         // load css
135         if(file_exists("$WEB_ROOT_FS/css")) {
136                 $dh = opendir("$WEB_ROOT_FS/css");
137                 if($dh) {
138                         while(($file = readdir($dh))!==false) {
139                                 $mt = preg_match("/.*.css$/", $file);
140                                 if($mt > 0) {
141                                         error_log("loading css $file");
142                                         echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$BASE_URL/css/$file\">";
143                                         //echo "required $basedir/$file\n";
144                                 }
145                         }
146                 }               
147         }
148
149         // load js
150         if(file_exists("$WEB_ROOT_FS/js")) {
151                 $dh = opendir("$WEB_ROOT_FS/js");
152                 if($dh) {
153                         while(($file = readdir($dh))!==false) {
154                                 $mt = preg_match("/.*.js$/", $file);
155                                 if($mt > 0) {
156                                         error_log("loading js $file");
157                                         echo "<script type=\"text/javascript\" src=\"$BASE_URL/js/$file\"></script>";
158                                         //echo "required $basedir/$file\n";
159                                 }
160                         }
161                 }               
162         }
163         
164         
165         // start body
166         echo "</head><body>";
167         
168         // page top
169         echo "<h1>CBFW</h1><br>";
170         echo "<table><tr><td>";
171         CBFWMessageBuilder();
172         echo "<td></tr><tr><td>";
173         
174         // menu, then body
175         echo "<table><tr><td>";
176         CBFWMenuBuilder();
177         echo "</td></tr><tr><td>";
178         // body
179         $url = "/";
180         if(isset($_REQUEST["q"])) {
181                 $url = $_REQUEST["q"];
182         }
183         
184         if($bodyClass != null) {
185                 $bodyClass->$bodyFunction($url);
186         } else if( $bodyFunction != null) {
187                 $bodyFunction($url);
188         } else echo $bodycontent;
189         echo "</td></tr></table>";
190         
191         
192         // close the big wrap-around table
193         echo "</td></tr></table>";
194         
195         // footer
196         echo "<br><font size=\"-1\">Copyright 2011, PJR</font><br></body></html>";
197         
198 }
199
200 function cbfw_getLastSeen($ip, $data)
201 {
202         $last = "never";
203         
204         if(!$data) return $last;
205         
206         
207         
208         foreach($data as $key=>$val) {
209                 $last = $val["name"];
210                 error_log("got $last for $ip");
211         }
212         
213         if($last == "never") return $last;
214         
215         $last = cbfw_tdiffToAgo(time()-$last);
216         
217         error_log("last for $ip set to $last");
218         
219         return $last;
220         
221 }
222
223 function cbfw_tdiffToAgo($time_in_sec)
224 {
225         $tdiff = $time_in_sec;
226         $tdiff_min = (int)($tdiff/60);
227         $tdiff_hour = (int)($tdiff/3600);
228         $tdiff_days = (int)($tdiff/86400);
229         
230         if($tdiff < 60) $last = "Less then a minute";
231         if($tdiff >= 60 && $tdiff < 7200) $last = "$tdiff_min minutes ago";
232         if($tdiff >= 7200 && $tdiff < 86400) $last = "$tdiff_hour hours ago";
233         if($tdiff >= 86400) $last = "$tdiff_days days ago";
234         
235         return $last;
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 ?>