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