moving the configuration changes into the single config class
authorpaulr <me@pjr.cc>
Sun, 2 Oct 2011 18:18:47 +0000 (05:18 +1100)
committerpaulr <me@pjr.cc>
Sun, 2 Oct 2011 18:18:47 +0000 (05:18 +1100)
location

libcbfwr/config.php
libcbfwr/fwui.php
libcbfwr/web.php
unittests/deletechanges.php [new file with mode: 0644]
unittests/mergetest.php [new file with mode: 0644]

index f2aaa52..e0b4f37 100644 (file)
@@ -114,8 +114,15 @@ class Config {
        
        function addConfigLine($line, $lineno=0)
        {
+               $line_n = trim($line);
+               $line = $line_n;
                $expl = preg_split("/ +/", $line);
                
+               if(count($expl) < 2) {
+                       $c = count($expl);
+                       error_log("Not a valid config line ($c), $line");
+                       return 0;
+               }
                echo "process command ".$expl[0]."\n";
                
                // find a description
@@ -133,7 +140,7 @@ class Config {
                        }
                }
                
-               // now rebuild the array if there was one
+               // now rebuild the array if there was a description
                if($hasdescription) {
                        for($i=0; $i<$hasdescription; $i++) {
                                $expl_r[$i] = $expl[$i];
@@ -153,6 +160,19 @@ class Config {
                        unset($expl[count($expl)-1]);
                }
                
+               // now for modify
+               $modify = false;
+               // check for delete on the line
+               if($expl[0] == "modify") {
+                       $modify = true;
+                       
+                       // re-order the array
+                       for($i=0; $i < (count($expl)-1); $i++) {
+                       $expl[$i] = $expl[$i+1];
+                       }
+                       unset($expl[count($expl)-1]);
+               }
+               
                switch($expl[0]) {
                        case "hostname":
                                // set the hostname to $1
index e827c2c..63c6613 100644 (file)
@@ -58,15 +58,8 @@ function CBFWZonesPage($urls)
                                
                        case "delete":
                                $zonetodelete = $urls[2];
-                               
-                               $conf = $comms->getConfig(3);
-                               $changes = $conf["status"];
-                               if($changes == "nochange") $conf["status"] = 1;
-                               else $conf["status"] = $changes + 1;
-                               $cnum = $conf["status"];
-                               
-                               $conf[$cnum]["delete"] = "zone $zonetodelete";
-                               $comms->putConfig($conf, 3);
+
+                               addWebUserChange("delete zone name $zonetodelete");
                                header("Location: $BASE_URL/zones");
                                break;
                                
@@ -74,21 +67,16 @@ function CBFWZonesPage($urls)
                        case "add":
                                
                                // UGLY
-                               $cnum = 0;
                                $nametoadd = $_REQUEST["toadd"];
                                $desc = $_REQUEST["desc"];
+                               
                                $descbit = "";
                                if($desc != "") $descbit = " # $desc";
                                if($nametoadd == "" || $nametoadd == null) {
                                        return;
                                }
-                               $conf = $comms->getConfig(3);
-                               $changes = $conf["status"];
-                               if($changes == "nochange") $conf["status"] = 1;
-                               else $conf["status"] = $changes + 1;
-                               $cnum = $conf["status"];
-                               $conf[$cnum]["add"] = "zone $nametoadd$descbit";
-                               $comms->putConfig($conf, 3);
+                               
+                               addWebUserChange("zone name $nametoadd $descbit");
                                header("Location: $BASE_URL/zones");
                                break;
                                
@@ -122,6 +110,28 @@ function CBFWZonesEditPage($urls)
        echo "</form>";
 }
 
+function addWebUserChange($change)
+{
+       $comms = new Comms();
+       $myconf = new Config();
+       
+       $conf = $comms->getConfig(3);
+       
+       if(!isset($conf["status"])) {
+               $conf["status"] = "nochange";
+       }
+       
+       $changes = $conf["status"];
+       if($changes == "nochange") $conf["status"] = 1;
+       else $conf["status"] = $changes + 1;
+       $cnum = $conf["status"];
+       
+       $conf["changes"][$cnum] = "$change";
+       $comms->putConfig($conf, 3);
+       
+}
+
+// TODO: we need to really sit and think about this one
 function getWebUserConfig()
 {
        $comms = new Comms();
@@ -140,13 +150,20 @@ function getWebUserConfig()
        }
        
        // it did not, do a new merge and cache
-       $config = $myconf->mergeConfig($comms->getConfig(), $conf2);
+       // TODO: need to do this part
+       $config = $comms->getConfig();
        $reconfig = $config;
+       $myconf->setConfig($config);
+       if(isset($conf2["changes"])) foreach($conf2["changes"] as $key => $var) {
+               error_log("Adding config line $key, $var");
+               $myconf->addConfigLine($var);
+       }
+       $reconfig = $myconf->getConfig();
        $reconfig["status"] = $conf2["status"];
        error_log("put pre-cache config");
        $comms->putConfig($reconfig, 5);        
        
-       return $config;
+       return $reconfig;
 }
 
 function CBFWZonesPageDisplay()
@@ -414,6 +431,45 @@ function CBFWObjectsDisplay()
        
 }
 
+function CBFWChangesPage($urls)
+{
+       global $BASE_URL, $MENU_ITEMS;
+       
+       if(isset($urls[1])) {
+               switch($urls[1]) {
+                       case "show":
+                               CBFWpageBuilder(null, "CBFWChangesDisplay");
+                               break;
+                       case "delete":
+                               $delete = $urls[2];
+                               error_log("would delete change $delete");
+                               header("Location: $BASE_URL/changes/show");
+                               break;
+               }
+       }
+}
+
+function CBFWChangesDisplay()
+{
+       global $BASE_URL, $MENU_ITEMS;
+       $comms = new Comms();
+       
+       $config = $comms->getConfig(3);
+       
+       echo "<h2>Changes</h2>";
+       echo "This page shows the changes on the web from the currently running configuration<br>";
+       if($config["status"] == "nochange") {
+               echo "There are currently no changes from the running configuraiton.";
+       } else {
+               echo "<table border=\"1\">";
+               echo "<tr><th>Change No</th><th>Change</th><th>Control</th></tr>";
+               foreach($config["changes"] as $key => $var) {
+                       echo "<tr><td>$key</td><td>$var</td><td><a href=\"$BASE_URL/changes/delete/$key\">Delete</a></td></tr>";
+               }
+               echo "</table>";
+       }
+}
+
 function CBFWRulesPage()
 {
        $comms = new Comms();
index f28b21f..6977dc6 100644 (file)
@@ -60,6 +60,9 @@ class CBFWWeb {
                                case "rules":
                                        $bodyFunction = "CBFWRulesPage";
                                        break;
+                               case "changes":
+                                       CBFWChangesPage($url_s);
+                                       break;
                                case "admin":
                                        CBFWAdminPage($url_s);
                                        break;
@@ -229,6 +232,8 @@ function cbfw_getLastSeen($ip, $data)
 
 function CBFWConfigStatus()
 {
+       global $WEB_ROOT_FS, $BASE_URL;
+       
        $comms = new Comms();
        
        $conf0 = $comms->getConfig(0);
@@ -246,7 +251,7 @@ function CBFWConfigStatus()
                $client = "Current";
        } else {
                $ch = $conf2["status"];
-               $client = "$ch Changes";
+               $client = "$ch Changes <a href=\"$BASE_URL/changes/show\">Show</a>";
        }
        
        
diff --git a/unittests/deletechanges.php b/unittests/deletechanges.php
new file mode 100644 (file)
index 0000000..088bb28
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+$CMD_ROOT_FS = realpath(dirname(__FILE__));
+$AM_DAEMON = false;
+
+global $CMD_ROOT_FS;
+global $AM_DAEMON;
+
+// add libglcas as if it were a path in ../libglcas
+if(file_exists("../libcbfwr")) {
+       $path = realpath($CMD_ROOT_FS."/../");
+       error_log("added cbfwr path as $path");
+       set_include_path(get_include_path().PATH_SEPARATOR.$path);
+}
+
+// include the based library
+require_once("libcbfwr/lib.php");
+
+glcas_pluginLoader();
+
+$comms = new Comms();
+
+$comms->deleteConfig(3);
+
+$config["status"] = "nochange";
+$comms->putConfig($config, 3);
+
+?>
diff --git a/unittests/mergetest.php b/unittests/mergetest.php
new file mode 100644 (file)
index 0000000..e2d613f
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+$CMD_ROOT_FS = realpath(dirname(__FILE__));
+$AM_DAEMON = false;
+
+global $CMD_ROOT_FS;
+global $AM_DAEMON;
+
+// add libglcas as if it were a path in ../libglcas
+if(file_exists("../libcbfwr")) {
+       $path = realpath($CMD_ROOT_FS."/../");
+       error_log("added cbfwr path as $path");
+       set_include_path(get_include_path().PATH_SEPARATOR.$path);
+}
+
+// include the based library
+require_once("libcbfwr/lib.php");
+
+glcas_pluginLoader();
+
+$config = new Config();
+$comms = new Comms();
+
+$conf1 = $comms->getConfig(0);
+$config->setConfig($conf1);
+$config->addConfigLine("delete hostname");
+$config->addConfigLine("delete zone name internet");
+$conf2 = $config->getConfig();
+
+print_r($conf1);
+print_r($conf2);
+$config->addConfigLine("delete");
+
+?>