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