web pages and shite
[CBFWR.git] / libcbfwr / web.php
diff --git a/libcbfwr/web.php b/libcbfwr/web.php
new file mode 100644 (file)
index 0000000..8e4967c
--- /dev/null
@@ -0,0 +1,295 @@
+<?php
+$MENU_ITEMS["Zones"]["link"] = "$BASE_URL/zones"; 
+$MENU_ITEMS["Zones"]["name"] = "Zones"; 
+$MENU_ITEMS["Interfaces"]["link"] = "$BASE_URL/interfaces"; 
+$MENU_ITEMS["Interfaces"]["name"] = "Interfaces"; 
+$MENU_ITEMS["Objects"]["link"] = "$BASE_URL/objects"; 
+$MENU_ITEMS["Objects"]["name"] = "Objects"; 
+$MENU_ITEMS["Rules"]["link"] = "$BASE_URL/rules"; 
+$MENU_ITEMS["Rules"]["name"] = "Rules"; 
+
+// if i believed in name spacing in php, i'd use it.
+error_log("cbfwweb loaded");
+
+class CBFWWeb {
+       
+       function __construct()
+       {
+
+       }
+       
+       function go()
+       {
+               $url = "/";
+               $bodycontent = null;
+               if(isset($_REQUEST["q"])) {
+                       $url = $_REQUEST["q"];
+                       
+                       // ok, now we try and find the basics
+                       $url_s = explode("/", $url);
+                       
+                       switch($url_s[0]) {
+                               case "zones":
+                                       $bodycontent = CBFWZonesPage($url_s);
+                                       break;
+                               case "interfaces":
+                                       $bodycontent = CBFWInterfacesPage($url_s);
+                                       break;
+                               case "objects":
+                                       $bodycontent = CBFWObjectsPage($url_s);
+                                       break;
+                               case "rules":
+                                       $bodycontent = CBFWRulesPage($url_s);
+                                       break;
+                               default:
+                                       $bodycontent = findUrl($url_s);
+                       }
+               }
+               CBFWpageBuilder(null, null, $bodycontent);
+       }       
+}
+
+function CBFWMenuBuilder()
+{
+       global $BASE_URL, $MENU_ITEMS;
+       
+       ksort($MENU_ITEMS);
+       ?>
+<table border="1"><tr><th>Menu</th><td>
+<a href="<?php echo $BASE_URL ?>">Home</a></td>
+       <?php
+       foreach($MENU_ITEMS as $key => $val) {
+               $link = $val["link"];
+               $name = $val["name"];
+               echo "<td><a href=$link>$name</a></td>";
+       }
+       
+       echo "</table>";
+}
+
+// just a short one to call the long one
+function gcam($m, $s)
+{
+       global $glconfig;
+       CBFWAddMessage($m, $s);
+}
+
+function CBFWAddMessage($messagelev, $message)
+{
+       
+}
+
+function CBFWMessageBuilder()
+{
+
+}
+
+function CBFWpageBuilder($bodyClass, $bodyFunction, $bodycontent=null, $title="CBFW")
+{
+       global $WEB_ROOT_FS, $BASE_URL;
+       
+       // TODO: load css
+       // header
+       echo "<html><head><title>$title</title>";
+       
+       // load css
+       if(file_exists("$WEB_ROOT_FS/css")) {
+               $dh = opendir("$WEB_ROOT_FS/css");
+               if($dh) {
+                       while(($file = readdir($dh))!==false) {
+                               $mt = preg_match("/.*.css$/", $file);
+                               if($mt > 0) {
+                                       error_log("loading css $file");
+                                       echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"$BASE_URL/css/$file\">";
+                                       //echo "required $basedir/$file\n";
+                               }
+                       }
+               }               
+       }
+
+       // load js
+       if(file_exists("$WEB_ROOT_FS/js")) {
+               $dh = opendir("$WEB_ROOT_FS/js");
+               if($dh) {
+                       while(($file = readdir($dh))!==false) {
+                               $mt = preg_match("/.*.js$/", $file);
+                               if($mt > 0) {
+                                       error_log("loading js $file");
+                                       echo "<script type=\"text/javascript\" src=\"$BASE_URL/js/$file\"></script>";
+                                       //echo "required $basedir/$file\n";
+                               }
+                       }
+               }               
+       }
+       
+       
+       // start body
+       echo "</head><body>";
+       
+       // page top
+       echo "<h1>CBFW</h1><br>";
+       echo "<table><tr><td>";
+       CBFWMessageBuilder();
+       echo "<td></tr><tr><td>";
+       
+       // menu, then body
+       echo "<table><tr><td>";
+       CBFWMenuBuilder();
+       echo "</td></tr><tr><td>";
+       // body
+       $url = "/";
+       if(isset($_REQUEST["q"])) {
+               $url = $_REQUEST["q"];
+       }
+       
+       if($bodyClass != null) {
+               $bodyClass->$bodyFunction($url);
+       } else if( $bodyFunction != null) {
+               $bodyFunction($url);
+       } else echo $bodycontent;
+       echo "</td></tr></table>";
+       
+       
+       // close the big wrap-around table
+       echo "</td></tr></table>";
+       
+       // footer
+       echo "<br><font size=\"-1\">Copyright 2011, PJR</font><br></body></html>";
+       
+}
+
+function cbfw_getLastSeen($ip, $data)
+{
+       $last = "never";
+       
+       if(!$data) return $last;
+       
+       
+       
+       foreach($data as $key=>$val) {
+               $last = $val["name"];
+               error_log("got $last for $ip");
+       }
+       
+       if($last == "never") return $last;
+       
+       $last = cbfw_tdiffToAgo(time()-$last);
+       
+       error_log("last for $ip set to $last");
+       
+       return $last;
+       
+}
+
+function cbfw_tdiffToAgo($time_in_sec)
+{
+       $tdiff = $time_in_sec;
+       $tdiff_min = (int)($tdiff/60);
+       $tdiff_hour = (int)($tdiff/3600);
+       $tdiff_days = (int)($tdiff/86400);
+       
+       if($tdiff < 60) $last = "Less then a minute";
+       if($tdiff >= 60 && $tdiff < 7200) $last = "$tdiff_min minutes ago";
+       if($tdiff >= 7200 && $tdiff < 86400) $last = "$tdiff_hour hours ago";
+       if($tdiff >= 86400) $last = "$tdiff_days days ago";
+       
+       return $last;
+}
+
+function cbfw_isRemoteDir($url)
+{
+       file_get_contents($url);
+       //error_log("did file_get_contents on $url");
+       foreach($http_response_header as $key => $val) {
+               //error_log("got header of $key for $val");
+               if(preg_match("/.*Location:.*/", $val)) {
+                       //error_log("in details apt got location as $val from $url");
+                       $realloc = preg_replace("/.*: /", "", $val);
+                       $urlwithslash = $url."/";
+                       //echo "<br>in comp with<br>$urlwithslash<br>$realloc<br>";
+                       if(strcasecmp($realloc,$urlwithslash)==0) {
+                               //error_log("I believe $realloc is a directory redir for $url");
+                               return true;
+                       } else {
+                               //error_log("I dont believe $realloc is a directory redir for $url");
+                       }
+               }
+       }
+       return false;
+}
+
+function cbfw_fileExists($url)
+{
+       $fp = fopen($url, "r");
+       //error_og("did file_get_contents on $url");
+       $retval = false;
+       foreach($http_response_header as $key => $val) {
+               //error_log("got header of $key for $val");
+               if(preg_match("/.*HTTP\/.*200.*/", $val)) {
+                       //echo "is true\n";
+                       
+                       $retval = true;
+               }
+       }
+       fclose($fp);
+       return $retval;
+}
+
+function cbfw_startInstaller()
+{
+       global $WEB_ROOT_FS, $BASE_URL;
+       
+       $uid = posix_geteuid();
+       $gid = posix_getegid();
+       $uid_a = posix_getpwuid($uid);
+       $uid = $uid_a["name"];
+       
+       $gid_a = posix_getgrgid($gid);
+       $gid = $gid_a["name"];
+       error_log("user id is $uid, group id is $gid");
+       
+       if(isset($_REQUEST["installdir"])) {
+               $c = new CBFWConfig();
+               touch($_REQUEST["installdir"]."/webconfig");
+               $c->loadConfig($_REQUEST["installdir"]."/webconfig");
+               header("Location: index.php");
+               return;
+       }
+       
+       $underroot = realpath($WEB_ROOT_FS."/../");
+       
+?>
+<html>
+<h1>Welcome to CBFW</h1>
+Welcome to CBFW, I cant find my configuration file so im assuming you installing me for the first time<br>
+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
+config, cause that will make life easier.<br><br>
+However, if this is the first time you have run this app, then all is good with the world. All I need right
+now is a place where i can store my config. I search the following directories for the location of my 
+configuration (webconfig)<br>
+<li> /var/run/glcas/
+<li> /var/lib/glcas/
+<li> <?php echo $underroot?>/var/glcas/
+<br><br>
+
+As root, you must now create one of these directories and change the ownership of the directory to the web owner.
+(chown <?php echo $uid?>:<?php echo $gid ?> the_path_you_choose).<br>
+
+Now, tell me where you want me to create the webconfig file:<br>
+<form method="post">
+<select name="installdir">
+<option value="/var/run/glcas">/var/run/glcas</option>
+<option value="/var/lib/glcas">/var/lib/glcas</option>
+<option value="<?php echo $underroot?>/var/glcas/"><?php echo $underroot?>/var/glcas/</option>
+</select>
+<input type="submit" name="Go" value="Go">
+
+</select>
+</form>
+
+</html>
+<?php 
+}
+
+
+?>
\ No newline at end of file