making config changes all happen in "addConfigLine"
[CBFWR.git] / libcbfwr / config.php
index 9ca0ff9..f2aaa52 100644 (file)
@@ -4,11 +4,10 @@ class Config {
        // here we load a config if we can find it
        // there are two sides to every class, the fwd side
        // and the web page site (command line is web for all intents)
-       function __construct()
+       function __construct($look_for_config = false)
        {
-               global $AM_DAEMON;
                
-               if($AM_DAEMON) {
+               if($look_for_config) {
                        $this->config_file = null;
                        if(file_exists("../var/fw.conf")) {
                                $this->config_file = realpath("../var/fw.conf");
@@ -28,47 +27,18 @@ class Config {
                                        $this->config_file = "/var/lib/fwd/fw.conf";
                                } else if(is_dir("../var/")) {
                                        echo "no config file found. Will use ../var/fw.conf for now\n";
-                                       $this->config_file = "../var/";
+                                       $this->config_file = "../var/fw.conf";
                                } else {
                                        echo "No directory where i can create a config, bailing\n";
-                                       $this->config["status"] = "unconf";
+                                       $this->config["status"] = "nodir";
                                }
                        }
-                       
-                       
-                       
                }
        }
        
-       function bootHardware()
+       function loadConfig($config)
        {
-               $this->findHardware();
-
-               $mycomm = new Comms();
-               $mycomm->putConfig($this->config, 1);
-               
-       }
-       
-       function loadConfig()
-       {
-               global $AM_DAEMON;
-               
-               if($AM_DAEMON) {
-                       if($this->config["status"] == "conf") { 
-                               $this->loadConfigFile($this->config_file);
-                               $this->applyConfig();
-                               $this->findHardware();
-                               
-                               $mycomm = new Comms();
-                               $mycomm->putConfig($this->config);
-                               $this->boot_config = $mycomm->getConfig(1);
-                       }
-               } else {
-                       $mycomm = new Comms();
-                       $this->config = $mycomm->getConfig(0);
-                       $this->boot_config = $mycomm->getConfig(1);
-                       
-               }
+               $this->config = $config;
        }
        
        function findHardware()
@@ -93,6 +63,8 @@ class Config {
                                        }
                                        fclose($fp);
                                }
+                               if(file_exists("/sys/class/net/$fname/mtu")) $this->config["hardware"]["netdev"][$fname]["mtu"] = file_get_contents("/sys/class/net/$fname/mtu");
+                               if(file_exists("/sys/class/net/$fname/address")) $this->config["hardware"]["netdev"][$fname]["hwaddress"] = file_get_contents("/sys/class/net/$fname/address");
                                if(file_exists("/sys/class/net/$fname/bonding")) $this->config["hardware"]["netdev"][$fname]["bonding"] = true;
                                if(file_exists("/sys/class/net/$fname/bridge")) $this->config["hardware"]["netdev"][$fname]["bridge"] = true;
                        }
@@ -104,9 +76,8 @@ class Config {
                return $this->config;
        }
        
-       function getBootConfig()
-       {
-               return $this->boot_config;
+       function setConfig($config) {
+               $this->config = $config;
        }
        
        function applyConfig()
@@ -118,10 +89,18 @@ class Config {
                return true;
        }
        
-       function loadConfigFile($file)
+
+       
+       function loadConfigFile($file=null)
        {
-               global $AM_DAEMON;
-               if(!$AM_DAEMON) return true;
+               
+               if($file == null) {
+                       if($this->config["status"] == "nodir") {
+                               return "nodir";
+                       } else $file = $this->config_file;
+               }
+               
+               if(!file_exists($file)) return "noconf";
                
                $fp = fopen($file, "r");
                
@@ -129,11 +108,11 @@ class Config {
                while($line = fgets($fp)) {
                        $line = trim($line);
                        echo "read line $line\n";
-                       if($line != "") $this->parseLine($line, $i++);
+                       if($line != "") $this->addConfigLine($line, $i++);
                }
        }
        
-       function parseLine($line, $lineno)
+       function addConfigLine($line, $lineno=0)
        {
                $expl = preg_split("/ +/", $line);
                
@@ -162,22 +141,51 @@ class Config {
                        $expl = $expl_r;
                }
                
+               $delete = false;
+               // check for delete on the line
+               if($expl[0] == "delete") {
+                       $delete = 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
-                               $this->config["hostname"] = $expl[1];
+                               if($delete) {
+                                       unset($this->config["hostname"]);                               
+                               } else {
+                                       $this->config["hostname"] = $expl[1];
+                               }
                                break;
                                
                        case "domainname":
                                // set the hostname to $1
-                               $this->config["domainname"] = $expl[1];
+                               if($delete) {
+                                       unset($this->config["domainname"]);
+                               } else {
+                                       $this->config["domainname"] = $expl[1];
+                               }
                                break;
                                
                        case "zone":
-                               if($hasdescription) {
-                                       $this->config["zone"][$expl[2]]["description"] = $description;
+                               if($delete) {
+                                       error_log("delete zone ".$expl[2]);
+                                       unset($this->config["zone"][$expl[2]]);
+                                       if(count($this->config["zone"]) < 1) {
+                                               error_log("zone now empty, delete zones");
+                                               unset($this->config["zone"]);
+                                       }
+                               } else {
+                                       if($hasdescription) {
+                                               $this->config["zone"][$expl[2]]["description"] = $description;
+                                       }
+                                       $this->config["zone"][$expl[2]]["name"] = true;
                                }
-                               $this->config["zone"][$expl[2]]["name"] = true;
                                break;
                                
                        case "interface":
@@ -253,54 +261,65 @@ class Config {
                                
                                
                        case "login":
-                               $this->config["login"][$expl[1]] = $expl[3];
-                               if($hasdescription) {
-                                       $this->config["login"][$expl[1]]["description"] = $description;
+                               if($delete) {
+                                       unset($this->config["login"][$expl[1]]);
+                               } else {
+                                       $this->config["login"][$expl[1]] = $expl[3];
+                                       if($hasdescription) {
+                                               $this->config["login"][$expl[1]]["description"] = $description;
+                                       }
                                }
                                break;
                        
                        case "route4":
-                               $route = $expl[1];
-                               $via = $expl[2];
-                               $dest = $expl[3];
-                               if($via == "to") {
-                                       $this->config["route4"][$route]["address"] = $dest;
+                               if($delete) {
+                                       unset($this->config["route4"][$expl[1]]);
                                } else {
-                                       $this->config["route4"][$route]["device"] = $dest;
-                               }
-                               if(isset($expl[4])) {
-                                       if($expl[4] == "dev") {
-                                               if(isset($expl[5])) {
-                                                       $this->config["route4"][$route]["device"] = $expl[5];
+                                       $route = $expl[1];
+                                       $via = $expl[2];
+                                       $dest = $expl[3];
+                                       if($via == "to") {
+                                               $this->config["route4"][$route]["address"] = $dest;
+                                       } else {
+                                               $this->config["route4"][$route]["device"] = $dest;
+                                       }
+                                       if(isset($expl[4])) {
+                                               if($expl[4] == "dev") {
+                                                       if(isset($expl[5])) {
+                                                               $this->config["route4"][$route]["device"] = $expl[5];
+                                                       }
                                                }
                                        }
-                               }
-                               if($hasdescription) {
-                                       $this->config["route4"][$route]["description"] = $description;
+                                       if($hasdescription) {
+                                               $this->config["route4"][$route]["description"] = $description;
+                                       }
                                }
                                break;
                                
                                
                        case "route6":
-                               $route = $expl[1];
-                               $via = $expl[2];
-                               $dest = $expl[3];
-                               if($via == "to") {
-                                       $this->config["route6"][$route]["address"] = $dest;
+                               if($delete) {
+                                       unset($this->config["route6"][$expl[1]]);
                                } else {
-                                       $this->config["route6"][$route]["device"] = $dest;
-                               }
-                               if(isset($expl[4])) {
-                                       if($expl[4] == "dev") {
-                                               if(isset($expl[5])) {
-                                                       $this->config["route6"][$route]["device"] = $expl[5];
+                                       $route = $expl[1];
+                                       $via = $expl[2];
+                                       $dest = $expl[3];
+                                       if($via == "to") {
+                                               $this->config["route6"][$route]["address"] = $dest;
+                                       } else {
+                                               $this->config["route6"][$route]["device"] = $dest;
+                                       }
+                                       if(isset($expl[4])) {
+                                               if($expl[4] == "dev") {
+                                                       if(isset($expl[5])) {
+                                                               $this->config["route6"][$route]["device"] = $expl[5];
+                                                       }
                                                }
                                        }
+                                       if($hasdescription) {
+                                               $this->config["route6"][$route]["description"] = $description;
+                                       }
                                }
-                               if($hasdescription) {
-                                       $this->config["route6"][$route]["description"] = $description;
-                               }
-                               
                                // here we should check "$route"
                                break;
                                
@@ -350,7 +369,6 @@ class Config {
        
        private $config_file;
        private $config;
-       private $boot_config;
 };
 
 ?>
\ No newline at end of file