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