Code

Updated dhcp options
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Fri, 26 Jun 2009 09:27:14 +0000 (09:27 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Fri, 26 Jun 2009 09:27:14 +0000 (09:27 +0000)
-We are now able to set variables twice.

git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@13800 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-plugins/dhcp/admin/systems/services/dhcp/class_dhcpAdvanced.inc
gosa-plugins/dhcp/admin/systems/services/dhcp/class_dhcpHost.inc
gosa-plugins/dhcp/admin/systems/services/dhcp/class_dhcpNetwork.inc
gosa-plugins/dhcp/admin/systems/services/dhcp/class_dhcpPlugin.inc

index 755214411ca6755a50ef79febf51316bd4c0c99d..8dd90a62774767c2d53641b9caaf539082c78e1f 100644 (file)
@@ -32,6 +32,23 @@ class dhcpAdvanced extends plugin
   var $objectclasses= array();
   var $parent;
 
+  static function getOption($needle, $heystack, $value_only = false){
+    foreach($heystack as $key => $val){
+      if($val != $key){
+        echo "<b>$key => $val</b>";
+      }
+
+      if(preg_match("/^{$needle} /i",$val)){
+        if($value_only){
+          return(trim(preg_replace('/^[^\s]*/')));
+        }else{
+          return($val);
+        }
+      }
+    }
+    return("");
+  }
+
   function dhcpAdvanced()
   {
     /* This is always an account */
@@ -53,22 +70,22 @@ class dhcpAdvanced extends plugin
     if ($acl_writeable && isset($_POST['delete_statement']) && isset($_POST['dhcpstatements'])){
       $key= preg_replace('/([a-z0-9-]+)\s(.*)$/', '\\1', get_post('dhcpstatements'));
       if (in_array($key, $this->autoStatements)){
-       msg_dialog::display(_("Error"), _("Cannot delete automatic statements!"), ERROR_DIALOG);
+        msg_dialog::display(_("Error"), _("Cannot delete automatic statements!"), ERROR_DIALOG);
       } else {
         unset($this->statements[$key]);        
       }
     }
     if ($acl_writeable && isset($_POST['add_option']) && $_POST['addoption'] != ""){
-      $key= preg_replace('/^([a-z0-9-]+)\s(.*)$/', '\\1', get_post('addoption'));
-      $val= preg_replace("/^$key\s*/", '', get_post('addoption'));
-      $this->options[$key]= $val;
+      $val= trim(get_post('addoption'));
+      $this->options[$val]= $val;
     }
     if ($acl_writeable && isset($_POST['delete_option']) && isset($_POST['dhcpoptions'])){
       $key= preg_replace('/([a-z0-9-]+)\s(.*)$/', '\\1', get_post('dhcpoptions'));
+      $val= get_post('dhcpoptions');
       if (in_array($key, $this->autoOptions)){
-       msg_dialog::display(_("Error"), _("Cannot delete automatic statements!"), ERROR_DIALOG);
+        msg_dialog::display(_("Error"), _("Cannot delete automatic statements!"), ERROR_DIALOG);
       } else {
-        unset($this->options[$key]);   
+        unset($this->options[$val]);   
       }
     }
 
@@ -89,10 +106,11 @@ class dhcpAdvanced extends plugin
     $smarty->assign("dhcpstatements", $statements);
     $options= array();
     foreach ($this->options as $key => $val){
+      $key = preg_replace("/ .*$/","",$val);
       if (in_array($key, $this->autoOptions)){
-        $options[$key]= "$key $val ["._("automatic")."]";
+        $options[$val]= "$val ["._("automatic")."]";
       } else {
-        $options[$key]= "$key $val";
+        $options[$val]= "$val";
       }
     }
     $smarty->assign("dhcpoptions", $options);
index c7569d2c95f46da2445ff64076f60cc721ba2dd5..e407ba54d2bbbaed06379cb6438916c56f9c704f 100644 (file)
@@ -121,7 +121,8 @@ class dhcpHost extends dhcpPlugin
           unset ($this->statements['fixed-address']);
         }
       }
-      $this->options['host-name']= $this->cn;
+      $hn = "host-name {$this->cn}";
+      $this->options[$hn]= $hn;
     }
 
     dhcpPlugin::save_object();
index 0f99ce23b3b6ec4f4b99525ef4bc26391b78b756..dce548ba3a5af1b64fb58e103c8ca6ee4613dc91 100644 (file)
@@ -46,8 +46,9 @@ class dhcpNetwork extends plugin
               htmlentities(get_post('addserver')),"/[0-9a-z.-]/"), ERROR_DIALOG);
       } else {
         $servers= array();
-        if (isset($this->options['domain-name-servers'])){
-          foreach(split(",", $this->options['domain-name-servers']) as $val){
+        $chk = dhcpAdvanced::getOption('domain-name-servers',$this->options,true);
+        if($chk != ""){
+          foreach(split(",", $chk) as $val){
             $servers[$val]= $val;
           }
         }
@@ -57,18 +58,28 @@ class dhcpNetwork extends plugin
         foreach($servers as $val){
           $tmp.= $val.",";
         }
-        $this->options['domain-name-servers']= preg_replace('/,$/', '', $tmp);
+        $chk = dhcpAdvanced::getOption('domain-name-servers',$this->options);
+        if($chk != ""){
+          unset($this->options[$chk]);
+        }
+        $new = "domain-name-servers ".preg_replace('/,$/', '', $tmp);
+        $this->options[$new]=$new; 
       }
     }
     if ($acl_writeable && isset($_POST['delete_dns']) && isset($_POST['dnsserver'])){
-      $tmp= preg_replace("/(\s*,\s*)?".get_post('dnsserver')."/i", '',
-          $this->options['domain-name-servers']);
-      $tmp= preg_replace("/(\s*)?,(\s*)?$/", '', $tmp);
-      if ($tmp != ""){
-        $this->options['domain-name-servers']= $tmp;
-      } else {
-        unset($this->options['domain-name-servers']);
+      $val = dhcpAdvanced::getOption('domain-name-servers', $this->options, true);
+      $key = dhcpAdvanced::getOption('domain-name-servers', $this->options);
+  
+      if($key != ""){
+        unset($this->options[$key]);
+        $tmp= preg_replace("/(\s*,\s*)?".get_post('dnsserver')."/i",'', $val);
+        $tmp= preg_replace("/(\s*)?,(\s*)?$/", '', $tmp);
+        if ($tmp != ""){
+          $tmp ="domain-name-servers {$tmp}";
+          $this->options[$tmp]= $tmp;
+        }
       }
+
     }
 
     /* Show main page */
@@ -82,21 +93,13 @@ class dhcpNetwork extends plugin
      */
 
     /* Router */
-    if (isset($this->options['routers'])){
-      $smarty->assign("routers", $this->options['routers']);
-    } else {
-      $smarty->assign("routers", "");
-    }
+    $smarty->assign("routers", dhcpAdvanced::getOption('router',$this->options));
 
     /* DNS */
-    if (isset($this->options['domain-name'])){
-      $smarty->assign("domain", trim($this->options['domain-name'], '"'));
-    } else {
-      $smarty->assign("domain", "");
-    }
-    if (isset($this->options['domain-name-servers'])){
+    $smarty->assign("domain", dhcpAdvanced::getOption('domain-name',$this->options));
+    if (dhcpAdvanced::getOption('domain-name-servers',$this->options) != ""){
       $servers= array();
-      foreach(split(",", $this->options['domain-name-servers']) as $val){
+      foreach(split(",", dhcpAdvanced::getOption('domain-name-servers',$this->options, true)) as $val){
         $servers[$val]= $val;
       }
       $smarty->assign("dnsservers", $servers);
@@ -105,17 +108,18 @@ class dhcpNetwork extends plugin
     }
 
     /* Netmask / Broadcast */
-    if (isset($this->options['subnet-mask'])){
-      $this->options['subnet-mask']= normalize_netmask($this->options['subnet-mask']);
-      $smarty->assign("subnet_mask", $this->options['subnet-mask']);
+    if (dhcpAdvanced::getOption('subnet-mask', $this->options) != ""){
+      $key = dhcpAdvanced::getOption('subnet-mask', $this->options);
+      $val = dhcpAdvanced::getOption('subnet-mask', $this->options,true);
+
+      unset($this->options[$key]);
+      $new = 'subnet-mask '.normalize_netmask($val);
+      $this->options[$new]=$new;
+      $smarty->assign("subnet_mask", $new);
     } else {
       $smarty->assign("subnet_mask", "");
     }
-    if (isset($this->options['broadcast-address'])){
-      $smarty->assign("broadcast_address", $this->options['broadcast-address']);
-    } else {
-      $smarty->assign("broadcast_address", "");
-    }
+    $smarty->assign("broadcast_address", dhcpAdvanced::getOption('broadcast-address',$this->options));
 
     /* Boot stuff */
     if (isset($this->statements['filename'])){
@@ -165,10 +169,13 @@ class dhcpNetwork extends plugin
       foreach (array("routers" => "routers", "domain-name" => "domain", "subnet-mask" => "subnet_mask",
                      "broadcast-address" => "broadcast_address") as $key => $val){
 
-        if ($_POST["$val"] == ''){
-          unset($this->options["$key"]);
-        } else {
-          $this->options["$key"]= get_post("$val");
+        $ikey = dhcpAdvanced::getOption($key, $this->options);
+        if($ikey != ""){
+          unset($this->options[$ikey]);
+        }
+        if ($_POST["$val"] != ''){
+          $new = $key." ".$get_post($val);
+          $this->options[$new]=$new; 
         }
       }
 
@@ -209,11 +216,12 @@ class dhcpNetwork extends plugin
 
     /* Check netmask and broadcast */
     foreach(array("subnet-mask" => _("Netmask"), "broadcast-address" => _("Broadcast")) as $key => $typ){
-      if (!isset($this->options["$key"])){
+      $ikey = dhcpAdvanced::getOption($key,$this->options);
+      $ival = dhcpAdvanced::getOption($key,$this->options,true);
+      if ($ikey == ""){
         continue;
       }
-      $tmp= preg_replace('/^[^\s]+\s/', '', $this->options["$key"]);
-
+      $tmp= preg_replace('/^[^\s]+\s/', '', $ival);
       if (!tests::is_ip($tmp)){
         $message[]= sprintf(_("Error in definition of '%s'!"), $typ);
       }
index f6b9f39b3c5d0e819e37e97b388cce5d35ae349e..89575229dc8911978a0ad446a71643cf9ab9a8f7 100644 (file)
@@ -47,13 +47,8 @@ class dhcpPlugin extends plugin
       /* Load options */
       if (isset($attrs['dhcpOption'])){
         foreach ($attrs['dhcpOption'] as $opt){
-          $idx= preg_replace('/\s.+$/', '', $opt);
-          if ($idx == $opt){
-            $value= "";
-          } else {
-            $value= preg_replace('/^[^\s]+\s/', '', $opt);
-          }
-          $this->options[$idx]= $value;
+          $opt = trim($opt);
+          $this->options[$opt]= $opt;
         }
       }
 
@@ -111,7 +106,10 @@ class dhcpPlugin extends plugin
   {
     /* Strip network objects */
     foreach (array("routers", "domain-name", "subnet-mask", "broadcast-address") as $toberemoved){
-      unset($this->options[$toberemoved]);
+      $ikey = dhcpAdvanced::getOption($toberemoved, $this->options);
+      if($ikey != ""){
+        unset($this->options[$ikey]);
+      }
     }
     foreach (array("filename", "next-server","get-lease-hostnames","use-host-decl-names") as $toberemoved){
       unset($this->statements[$toberemoved]);
@@ -155,7 +153,7 @@ class dhcpPlugin extends plugin
     $this->attrs['dhcpOption']= array();
     if (isset ($this->options) && count ($this->options)){
       foreach ($this->options as $key => $val){
-        $this->attrs['dhcpOption'][]= "$key $val";
+        $this->attrs['dhcpOption'][]= "$val";
       }
     }
 
@@ -208,7 +206,10 @@ class dhcpPlugin extends plugin
   function fix_options()
   {
     foreach (array('domain-name-servers') as $key){
-      unset ($this->options[$key]);
+      $ikey = dhcpAdvanced::getOption($key,$this->options);
+      if($ikey != ""){
+        unset ($this->options[$ikey]);
+      }
     }
   }