Code

Updated dhcp plugin
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Fri, 26 Jun 2009 13:24:35 +0000 (13:24 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Fri, 26 Jun 2009 13:24:35 +0000 (13:24 +0000)
-Allow to set multiple options.

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

gosa-plugins/dhcp/admin/systems/services/dhcp/class_dhcpAdvanced.inc
gosa-plugins/dhcp/admin/systems/services/dhcp/class_dhcpGroup.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_dhcpOption.inc [new file with mode: 0644]
gosa-plugins/dhcp/admin/systems/services/dhcp/class_dhcpPlugin.inc
gosa-plugins/dhcp/admin/systems/services/dhcp/class_dhcpPool.inc
gosa-plugins/dhcp/admin/systems/services/dhcp/class_dhcpService.inc
gosa-plugins/dhcp/admin/systems/services/dhcp/class_dhcpSharedNetwork.inc
gosa-plugins/dhcp/admin/systems/services/dhcp/class_dhcpSubnet.inc
gosa-plugins/dhcp/admin/systems/services/dhcp/dhcp_advanced.tpl

index 755214411ca6755a50ef79febf51316bd4c0c99d..46eda0f6e96fe0dde7e970248d6307811cdf3373 100644 (file)
@@ -21,7 +21,7 @@
 class dhcpAdvanced extends plugin
 {
   /* Used attributes */
-  var $options= array();
+  var $options= null;
   var $statements= array();
   var $show_advanced= FALSE;
   var $autoStatements= array();
@@ -61,14 +61,15 @@ class dhcpAdvanced extends plugin
     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;
+      $this->options->add($key,$val);
     }
     if ($acl_writeable && isset($_POST['delete_option']) && isset($_POST['dhcpoptions'])){
-      $key= preg_replace('/([a-z0-9-]+)\s(.*)$/', '\\1', get_post('dhcpoptions'));
-      if (in_array($key, $this->autoOptions)){
-       msg_dialog::display(_("Error"), _("Cannot delete automatic statements!"), ERROR_DIALOG);
+      $name = preg_replace('/_[0-9]*$/', '', get_post('dhcpoptions'));
+      $key  = preg_replace('/^.*_/', '', get_post('dhcpoptions'));
+      if (in_array($name, $this->autoOptions)){
+        msg_dialog::display(_("Error"), _("Cannot delete automatic statements!"), ERROR_DIALOG);
       } else {
-        unset($this->options[$key]);   
+        $this->options->remove($name,$key);    
       }
     }
 
@@ -88,11 +89,15 @@ class dhcpAdvanced extends plugin
     }
     $smarty->assign("dhcpstatements", $statements);
     $options= array();
-    foreach ($this->options as $key => $val){
+    foreach ($this->options->getAll() as $key => $val){
       if (in_array($key, $this->autoOptions)){
-        $options[$key]= "$key $val ["._("automatic")."]";
+        foreach($val as $id => $entry){
+          $options[$key."_".$id]= "$key $entry ["._("automatic")."]";
+        }       
       } else {
-        $options[$key]= "$key $val";
+        foreach($val as $id => $entry){
+          $options[$key."_".$id]= "$key $entry";
+        }       
       }
     }
     $smarty->assign("dhcpoptions", $options);
index 6a0d78b00fe9a3f66c5ab507d790a116d14a18a3..29148fdf9504cf77c448b756b79c6e3b601b0e4b 100644 (file)
@@ -41,15 +41,14 @@ class dhcpGroup extends dhcpPlugin
     $display= $smarty->fetch (get_template_path('dhcp_group.tpl', TRUE, dirname(__FILE__))).$this->network->execute();
 
     /* Merge arrays for advanced view */
-    $this->fix_options();
-    foreach (array("options", "statements") as $type){
+    foreach (array("statements") as $type){
       $this->advanced->$type= $this->$type + $this->network->$type;
     }
 
     $display.= $this->advanced->execute();
 
     /* Merge back for removals */
-    foreach (array("options", "statements") as $type){
+    foreach (array("statements") as $type){
       $this->$type= $this->advanced->$type;
       $this->network->$type= $this->advanced->$type;
     }
index c7569d2c95f46da2445ff64076f60cc721ba2dd5..12f4f559e58fc01b027e439d6f8ea63e35faec49 100644 (file)
@@ -69,14 +69,13 @@ class dhcpHost extends dhcpPlugin
     $display= $smarty->fetch(get_template_path('dhcp_host.tpl', TRUE,dirname(__FILE__))).$this->network->execute();
 
     /* Merge arrays for advanced view */
-    $this->fix_options();
-    foreach (array("options", "statements") as $type){
+    foreach (array("statements") as $type){
       $this->advanced->$type= $this->$type + $this->network->$type;
     }
     $display.= $this->advanced->execute();
 
     /* Merge back for removals */
-    foreach (array("options", "statements") as $type){
+    foreach (array("statements") as $type){
       $this->$type= $this->advanced->$type;
       $this->network->$type= $this->advanced->$type;
     }
@@ -121,7 +120,7 @@ class dhcpHost extends dhcpPlugin
           unset ($this->statements['fixed-address']);
         }
       }
-      $this->options['host-name']= $this->cn;
+      $this->options->set('host-name',$this->cn);
     }
 
     dhcpPlugin::save_object();
index 0f99ce23b3b6ec4f4b99525ef4bc26391b78b756..a19cb1d26118c0f47803e980c48946d65836d077 100644 (file)
@@ -21,7 +21,7 @@
 class dhcpNetwork extends plugin
 {
   /* Used attributes */
-  var $options= array();
+  var $options= null;
   var $statements= array();
 
   /* attribute list for save action */
@@ -46,8 +46,8 @@ 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){
+        if($this->options->exists('domain-name-servers')){
+          foreach(split(",", $this->options->get('domain-name-servers')) as $val){
             $servers[$val]= $val;
           }
         }
@@ -57,17 +57,18 @@ class dhcpNetwork extends plugin
         foreach($servers as $val){
           $tmp.= $val.",";
         }
-        $this->options['domain-name-servers']= preg_replace('/,$/', '', $tmp);
+        $this->options->removeAll('domain-name-servers');
+        $this->options->set('domain-name-servers',preg_replace('/,$/', '', $tmp));
       }
     }
     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*)?".get_post('dnsserver')."/i", '', 
+        $this->options->get('domain-name-servers'));
       $tmp= preg_replace("/(\s*)?,(\s*)?$/", '', $tmp);
       if ($tmp != ""){
-        $this->options['domain-name-servers']= $tmp;
+        $this->options->set('domain-name-servers', $tmp);
       } else {
-        unset($this->options['domain-name-servers']);
+        $this->options->remove('domain-name-servers');
       }
     }
 
@@ -82,21 +83,14 @@ class dhcpNetwork extends plugin
      */
 
     /* Router */
-    if (isset($this->options['routers'])){
-      $smarty->assign("routers", $this->options['routers']);
-    } else {
-      $smarty->assign("routers", "");
-    }
+    $smarty->assign("routers", $this->options->get('routers'));
 
     /* 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",$this->options->get('domain-name'));
+
+    if($this->options->exists('domain-name-servers')){
       $servers= array();
-      foreach(split(",", $this->options['domain-name-servers']) as $val){
+      foreach(split(",", $this->options->get('domain-name-servers')) as $val){
         $servers[$val]= $val;
       }
       $smarty->assign("dnsservers", $servers);
@@ -105,17 +99,12 @@ 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']);
-    } else {
-      $smarty->assign("subnet_mask", "");
-    }
-    if (isset($this->options['broadcast-address'])){
-      $smarty->assign("broadcast_address", $this->options['broadcast-address']);
-    } else {
-      $smarty->assign("broadcast_address", "");
+    if ($this->options->exists('subnet-mask')){
+      $this->options->removeAll('subnet-mask');
+      $this->options->set('subnet-mask',normalize_netmask($this->options->get('subnet-mask')));
     }
+    $smarty->assign("subnet_mask", $this->options->get('subnet-mask'));
+    $smarty->assign("broadcast_address", $this->options->get('broadcast-address'));
 
     /* Boot stuff */
     if (isset($this->statements['filename'])){
@@ -164,11 +153,10 @@ class dhcpNetwork extends plugin
       /* Options */
       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"]);
+          $this->options->remove($key);
         } else {
-          $this->options["$key"]= get_post("$val");
+          $this->options->set($key,get_post($val));
         }
       }
 
@@ -209,16 +197,13 @@ class dhcpNetwork extends plugin
 
     /* Check netmask and broadcast */
     foreach(array("subnet-mask" => _("Netmask"), "broadcast-address" => _("Broadcast")) as $key => $typ){
-      if (!isset($this->options["$key"])){
+      if (!$this->options->exists("$key")){
         continue;
       }
-      $tmp= preg_replace('/^[^\s]+\s/', '', $this->options["$key"]);
-
-      if (!tests::is_ip($tmp)){
+      if (!tests::is_ip($this->options->get($key))){
         $message[]= sprintf(_("Error in definition of '%s'!"), $typ);
       }
     }
-
     return $message;
   }
 
diff --git a/gosa-plugins/dhcp/admin/systems/services/dhcp/class_dhcpOption.inc b/gosa-plugins/dhcp/admin/systems/services/dhcp/class_dhcpOption.inc
new file mode 100644 (file)
index 0000000..8db57ff
--- /dev/null
@@ -0,0 +1,96 @@
+<?php
+
+  /* This class represents a set of dhcpOptions, which are used over several dhcp plugins.
+   * E.g. 'dhcpHost' includes 'dhcpAdvanced', both use the same options. 
+   * In other words, they use a references to an instance of this class.
+   */
+class dhcpOption{
+
+  var $options = array();
+
+  function getAll(){
+    return($this->options);
+  }
+
+  function get($name){
+    if(isset($this->options[$name])){
+      if(count($this->options[$name]) == 0){
+        return("");
+      }elseif(count($this->options[$name]) == 1){
+        return($this->options[$name][0]);
+      }else{
+        return($this->options[$name]);
+      }
+    } 
+  }
+
+  function removeAll($name){
+    if(isset($this->options[$name])){
+      unset($this->options[$name]);
+    }
+  }
+
+  function remove($name,$entry = 0){
+    if(!isset($this->options[$name])){
+      return;
+    }
+    if(isset($this->options[$name][$entry])){
+      unset($this->options[$name][$entry]);
+    }
+    if(count($this->options[$name]) == 0){
+      unset($this->options[$name]);
+    }else{
+      $this->options[$name] = array_unique($this->options[$name]);
+      $this->options[$name] = array_values($this->options[$name]);
+    }
+  }
+
+  function set($name,$value = null){
+    if($value == null){
+      $value = trim(preg_replace("/^[^ ]*/",'',$name));
+      $name  = trim(preg_replace("/ .*$/",'',$name));
+    }
+
+    if(!isset($this->options[$name])){
+      $this->options[$name] = array();
+    }
+  
+    $this->options[$name] = array($value);
+  }
+
+  function add($name,$value = null){
+    if($value == null){
+      $value = trim(preg_replace("/^[^ ]*/",'',$name));
+      $name  = trim(preg_replace("/ .*$/",'',$name));
+    }
+
+    if(!isset($this->options[$name])){
+      $this->options[$name] = array();
+    }
+  
+    $this->options[$name][] = $value;
+    $this->options[$name] = array_unique($this->options[$name]);
+    $this->options[$name] = array_values($this->options[$name]);
+  }
+  
+  function save(){
+    $res = array();
+    foreach($this->options as $key => $values){
+      foreach($values as $value){
+        $res[] = $key." ".$value;
+      }
+    }
+    $res=array_unique($res);
+    return($res);
+  }
+    
+  function exists($name){
+    if(isset($this->options[$name]) && count($this->options[$name]) == 0){
+      return(false);
+    }
+    return(isset($this->options[$name]));
+  }
+}
+
+// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
+?>
index f6b9f39b3c5d0e819e37e97b388cce5d35ae349e..b4fc0b6c5469b3c1d3fbace8e797e75337edaf82 100644 (file)
@@ -23,7 +23,8 @@ class dhcpPlugin extends plugin
   /* Used attributes */
   var $cn= "";
   var $orig_cn= "";
-  var $options= array();
+  var $orig_dn= "";
+  var $options= null;
   var $statements= array();
 
   /* Subobjects */
@@ -36,7 +37,9 @@ class dhcpPlugin extends plugin
 
   function dhcpPlugin(&$parent,$attrs)
   {
-       $this->parent = $parent;
+    $this->parent = $parent;
+
+    $this->options = new dhcpOption();
 
     /* Load statements / options */
     if (is_array($attrs)){
@@ -46,14 +49,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;
+        foreach($attrs['dhcpOption'] as $opt){
+          $this->options->add(trim($opt));
         }
       }
 
@@ -82,17 +79,18 @@ class dhcpPlugin extends plugin
 
     /* Load network module */
     $this->network= new dhcpNetwork();
-    $this->network->options= $this->options;
+    $this->network->options= &$this->options;
     $this->network->statements= $this->statements;
     $this->advanced= new dhcpAdvanced();
-    $this->advanced->options= $this->options;
+    $this->advanced->options= &$this->options;
     $this->advanced->statements= $this->statements;
 
-       $this->network->parent = $parent;
-       $this->advanced->parent = $parent;
+    $this->network->parent = $parent;
+    $this->advanced->parent = $parent;
 
     /* Save CN for later reference */
     $this->orig_cn= $this->cn;
+    $this->orig_dn= $this->dn;
   }
 
   function execute()
@@ -109,10 +107,6 @@ class dhcpPlugin extends plugin
   /* Save data to object */
   function save_object()
   {
-    /* Strip network objects */
-    foreach (array("routers", "domain-name", "subnet-mask", "broadcast-address") as $toberemoved){
-      unset($this->options[$toberemoved]);
-    }
     foreach (array("filename", "next-server","get-lease-hostnames","use-host-decl-names") as $toberemoved){
       unset($this->statements[$toberemoved]);
     }
@@ -122,7 +116,7 @@ class dhcpPlugin extends plugin
     $this->advanced->save_object();
 
     /* Merge arrays for advanced view */
-    foreach (array("options", "statements") as $type){
+    foreach (array("statements") as $type){
       $this->advanced->$type= $this->$type + $this->network->$type;;
     }
   }
@@ -140,24 +134,21 @@ class dhcpPlugin extends plugin
   function save()
   {
     /* Merge arrays for network and advanced view */
-    foreach (array("options", "statements") as $type){
+    foreach (array("statements") as $type){
       $this->$type= $this->$type + $this->network->$type + $this->advanced->$type;
     }
 
     /* Add cn if we're new */
     if ($this->new){
-      $this->dn= "cn=".$this->cn.",".$this->dn;
+      $this->dn= "cn=".$this->cn.",".$this->orig_dn;
     } else {
-      $this->dn= "cn=".$this->cn.preg_replace('/^cn=[^,]+/', '', $this->dn);
+      $this->dn= "cn=".$this->cn.preg_replace('/^cn=[^,]+/', '', $this->orig_dn);
     }
 
+    print_a($this);
+
     /* Assemble new entry - options */
-    $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']= $this->options->save();
 
     /* Assemble new entry - statements */
     $this->attrs['dhcpStatements']= array();
@@ -176,7 +167,6 @@ class dhcpPlugin extends plugin
     $this->attrs['cn']= array($this->cn);
     $this->attrs['objectClass']= $this->objectclasses;
     $this->attrs['MODIFIED']= TRUE;
-
     return ($this->attrs);
   }
 
@@ -204,14 +194,6 @@ class dhcpPlugin extends plugin
     $this->removeAttrs($name, 'dhcpStatement');
   }
 
-
-  function fix_options()
-  {
-    foreach (array('domain-name-servers') as $key){
-      unset ($this->options[$key]);
-    }
-  }
-
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
index 067b07c0b70f2d718f3b36c62b239714d8c82ed0..4d0a5c791bb08d79be9a9887c6ea1b3266b74a85 100644 (file)
@@ -56,15 +56,14 @@ class dhcpPool extends dhcpPlugin
     $display= $smarty->fetch(get_template_path('dhcp_pool.tpl', TRUE, dirname(__FILE__))).$this->network->execute();
 
     /* Merge arrays for advanced view */
-    $this->fix_options();
-    foreach (array("options", "statements") as $type){
+    foreach (array("statements") as $type){
       $this->advanced->$type= $this->$type + $this->network->$type;;
     }
 
     $display.= $this->advanced->execute();
 
     /* Merge back for removals */
-    foreach (array("options", "statements") as $type){
+    foreach (array("statements") as $type){
       $this->$type= $this->advanced->$type;
       $this->network->$type= $this->advanced->$type;
     }
index 5260b743f0d1273edda195fe77de2347fa5212a1..36ca957630c74762579742e0af7253e548bfbdf8 100644 (file)
@@ -80,15 +80,14 @@ class dhcpService extends dhcpPlugin
     $display= $smarty->fetch(get_template_path('dhcp_service.tpl', TRUE, dirname(__FILE__))).$this->network->execute();
 
     /* Merge arrays for advanced view */
-    $this->fix_options();
-    foreach (array("options", "statements") as $type){
+    foreach (array("statements") as $type){
       $this->advanced->$type= $this->$type + $this->network->$type;;
     }
 
     $display.= $this->advanced->execute();
 
     /* Merge back for removals */
-    foreach (array("options", "statements") as $type){
+    foreach (array("statements") as $type){
       $this->$type= $this->advanced->$type;
       $this->network->$type= $this->advanced->$type;
     }
index a2b4d69f98aa5950f0a31a9d6b28d8e3a8ff06cd..b60ebdd33a3774e3664ebd5f92b63096d6d10a3f 100644 (file)
@@ -74,28 +74,25 @@ class dhcpSharedNetwork extends dhcpPlugin
     /* Show main page */
     $display= $smarty->fetch(get_template_path('dhcp_sharedNetwork.tpl', TRUE,dirname(__FILE__))).$this->network->execute();
 
-    /* Merge arrays for advanced view */
-    $this->fix_options();
-       
-       /* Remove states configured by checkboxes. 
+    /* Remove states configured by checkboxes. 
      */
-       foreach(array("deny unknown-clients",
+    foreach(array("deny unknown-clients",
           "deny bootp", "deny booting", "allow unknown-clients",
           "allow bootp", "allow booting") as $name){
-               if(isset($this->advanced->statements[$name])){
-                       unset($this->advanced->statements[$name]);
-                       unset($this->network->statements[$name]);
-               }
-       }
+      if(isset($this->advanced->statements[$name])){
+        unset($this->advanced->statements[$name]);
+        unset($this->network->statements[$name]);
+      }
+    }
 
-    foreach (array("options", "statements") as $type){
+    foreach (array("statements") as $type){
       $this->advanced->$type= $this->$type + $this->network->$type;
     }
 
     $display.= $this->advanced->execute();
 
     /* Merge back for removals */
-    foreach (array("options", "statements") as $type){
+    foreach (array("statements") as $type){
       $this->$type= $this->advanced->$type;
       $this->network->$type= $this->advanced->$type;
     }
index 2227e14f44d20ba76b12116c16e86b75259fb024..1a900cdf699e05b79b1e1fe2c8b545186fde5511 100644 (file)
@@ -75,15 +75,14 @@ class dhcpSubnet extends dhcpPlugin
     $display= $smarty->fetch(get_template_path('dhcp_subnet.tpl', TRUE, dirname(__FILE__))).$this->network->execute();
 
     /* Merge arrays for advanced view */
-    $this->fix_options();
-    foreach (array("options", "statements") as $type){
+    foreach (array("statements") as $type){
       $this->advanced->$type= $this->$type + $this->network->$type;
     }
 
     $display.= $this->advanced->execute();
 
     /* Merge back for removals */
-    foreach (array("options", "statements") as $type){
+    foreach (array("statements") as $type){
       $this->$type= $this->advanced->$type;
       $this->network->$type= $this->advanced->$type;
     }
index 0669b20fd3943203f5634622e4f38db4912691ae..cfed5f334561571dae75cdc1dd88cb7c4560e3f4 100644 (file)
@@ -37,7 +37,7 @@
    <br>
 {render acl=$acl}
    <select name='dhcpoptions' style="width:100%;" size="14">
-    {html_options values=$dhcpoptions output=$dhcpoptions}
+    {html_options options=$dhcpoptions}
    </select>
 {/render}
    <br>