Code

Updated DHCP service
authorcajus <cajus@594d385d-05f5-0310-b6e9-bd551577e9d8>
Fri, 3 Aug 2007 13:34:45 +0000 (13:34 +0000)
committercajus <cajus@594d385d-05f5-0310-b6e9-bd551577e9d8>
Fri, 3 Aug 2007 13:34:45 +0000 (13:34 +0000)
git-svn-id: https://oss.gonicus.de/repositories/gosa/branches/2.5@6961 594d385d-05f5-0310-b6e9-bd551577e9d8

plugins/admin/systems/class_dhcpHost.inc
plugins/admin/systems/class_servDHCP.inc

index 2c710f986a20c0604129dac3a9d2446846239d88..cac159b2f37fdfb421900e3e369d9ae843cc1660 100644 (file)
@@ -52,20 +52,24 @@ class dhcpHost extends plugin
 
       /* Load options */
       if (isset($attrs['dhcpOption'])){
-        for($i= 0; $i<$attrs['dhcpOption']['count']; $i++){
-          $tmp= $attrs['dhcpOption'][$i];
-          $idx= preg_replace('/\s.+$/', '', $tmp);
-          $value= preg_replace('/^[^\s]+\s/', '', $tmp);
+        foreach ($attrs['dhcpOption'] as $opt){
+          if ($opt == "count"){
+            continue;
+          }
+          $idx= preg_replace('/\s.+$/', '', $opt);
+          $value= preg_replace('/^[^\s]+\s/', '', $opt);
           $this->options[$idx]= $value;
         }
       }
 
       /* Load statements */
       if (isset($attrs['dhcpStatements'])){
-        for($i= 0; $i<$attrs['dhcpStatements']['count']; $i++){
-          $tmp= $attrs['dhcpStatements'][$i];
-          $idx= preg_replace('/\s.+$/', '', $tmp);
-          $value= preg_replace('/^[^\s]+\s/', '', $tmp);
+        foreach ($attrs['dhcpStatements'] as $opt){
+          if ($opt == "count"){
+            continue;
+          }
+          $idx= preg_replace('/\s.+$/', '', $opt);
+          $value= preg_replace('/^[^\s]+\s/', '', $opt);
           $this->statements[$idx]= $value;
         }
       }
@@ -119,6 +123,12 @@ class dhcpHost extends plugin
 
     $display.= $this->advanced->execute();
 
+    /* Merge back for removals */
+    foreach (array("options", "statements") as $type){
+      $this->$type= $this->advanced->$type;
+      $this->network->$type= $this->advanced->$type;
+    }
+
     /* Add footer */
     $display.= "<div style='width:100%;text-align:right;margin-top:5px;'><input type=submit name='save_dhcp' value='"._("Save")."'>".
                "&nbsp;<input type=submit name='cancel_dhcp' value='"._("Cancel")."'></div>";
@@ -179,7 +189,7 @@ class dhcpHost extends plugin
     if ($this->orig_cn != $this->cn || $this->new){
       
       foreach($cache as $dn => $dummy){
-        if (preg_match("/^cn=".$this->cn.",/", $dn)){
+        if (preg_match("/^cn=".$this->cn.",/", $dn) && count($dummy)){
           $message[]= _("The name for this host section is already used!");
           break;
         }
@@ -218,16 +228,16 @@ class dhcpHost extends plugin
     }
 
     /* Assemble new entry - options */
+    $this->attrs['dhcpOption']= array();
     if (isset ($this->options) && count ($this->options)){
-      $this->attrs['dhcpOption']= array();
       foreach ($this->options as $key => $val){
         $this->attrs['dhcpOption'][]= "$key $val";
       }
     }
 
     /* Assemble new entry - statements */
+    $this->attrs['dhcpStatements']= array();
     if (isset ($this->statements) && count ($this->statements)){
-      $this->attrs['dhcpStatements']= array();
       foreach ($this->statements as $key => $val){
         $this->attrs['dhcpStatements'][]= "$key $val";
       }
@@ -235,6 +245,14 @@ class dhcpHost extends plugin
 
     /* Move dn to the result */
     $this->attrs['dn']= $this->dn;
+    $this->attrs['cn']= array($this->cn);
+    if ($this->dhcpHWAddress != ""){
+      $this->attrs['dhcpHWAddress']= array($this->dhcpHWAddress);
+    } else {
+      $this->attrs['dhcpHWAddress']= array();
+    }
+    $this->attrs['objectClass']= array('top', 'dhcpHost');
+    $this->attrs['MODIFIED']= TRUE;
 
     return ($this->attrs);
   }
index 42dd962a3663964761ca69943781f91e30ef1d78..752c5738ef84202541e53384c1ab15cf2ed8aff0 100644 (file)
@@ -11,11 +11,25 @@ class servdhcp extends plugin
   var $dhcpSections= array();
   var $dhcpObjectCache= array();
   var $current_object= "";
+  var $types= array();
+  var $serviceDN= "";
 
   function servdhcp ($config, $dn= NULL, $parent= NULL)
   {
     plugin::plugin ($config, $dn, $parent);
 
+    $this->types= array(  "dhcpLog" => _("Logging"),
+            "dhcpService" => _("Global options"),
+            "dhcpClass" => _("Class"),
+            "dhcpSubClass" => _("Subclass"),
+            "dhcpLeases" => _("Lease"),
+            "dhcpHost" => _("Host"),
+            "dhcpGroup" => _("Group"),
+            "dhcpPool" => _("Pool"),
+            "dhcpSubnet" => _("Subnet"),
+            "dhcpFailOverPeer" => _("Failover peer"),
+            "dhcpSharedNetwork" => _("Shared network"));
+
     /* Load information about available services */
     $this->reload(); 
   }
@@ -34,7 +48,7 @@ class servdhcp extends plugin
     if (isset($_POST['create_section']) && isset($_POST['section'])){
       $section= $_POST['section'];
       if (isset(dhcpNewSectionDialog::$sectionMap[$section])){
-        $this->dialog= new $section($this->dn);
+        $this->dialog= new $section($this->current_object);
         $this->current_object= "";
       } else {
         $this->dialog= NULL;
@@ -54,16 +68,32 @@ class servdhcp extends plugin
         show_errors($messages);
       } else {
         $dn= $this->dialog->dn;
+        $class= get_class($this->dialog);
+        $type= $this->types[$class];
+        $indent= substr_count(preg_replace("/".$this->serviceDN."/", '', $dn), ",");
+        $spaces= "";
+        for ($i= 0; $i<$indent; $i++){
+          $spaces.= "&nbsp;&nbsp;&nbsp;&nbsp;";
+        }
         $data= $this->dialog->save();
-        if ($this->current_object= ""){
-          echo "SAVE new entry";
+        if ($this->current_object == ""){
+          /* New object */
+          $this->dhcpObjectCache[$data['dn']]= $data;
+          $spaces.= "&nbsp;&nbsp;&nbsp;&nbsp;";
+          $this->dhcpSections[$data['dn']]= "$spaces$type '".preg_replace('/^[^=]+=([^,]+),.*$/', '\1', $data['dn'])."'";
         } else {
           if ($dn != $data['dn']){
-            echo "SAVE old entry with new dn";
+            /* Old object, new name */
+            $this->dhcpObjectCache[$dn]= array();
+            unset($this->dhcpSections[$dn]);
+            $this->dhcpObjectCache[$data['dn']]= $data;
+            $this->dhcpSections[$data['dn']]= "$spaces$type '".preg_replace('/^[^=]+=([^,]+),.*$/', '\1', $data['dn'])."'";
           } else {
-            echo "SAVE old entry";
+            /* Old object, old name */
+            $this->dhcpObjectCache[$data['dn']]= $data;
           }
         }
+        $this->dialog= NULL;
       }
     }
 
@@ -206,10 +236,58 @@ class servdhcp extends plugin
   /* Save to LDAP */
   function save()
   {
-    plugin::save();
+    $ldap= $this->config->get_ldap_link();
+
+    foreach ($this->dhcpObjectCache as $dn => $data){
+
+      /* Remove entry? */
+      if (count($data) == 0){
+        /* Check if exists, then remove... */
+        if($ldap->cat($dn)){
+          $ldap->rmdir($dn);
+          show_ldap_error($ldap->get_error(), _("Can't remove DHCP object!"));
+        }
+        continue;
+      }
+
+      /* Modify existing entry? */
+      if (isset($data['MODIFIED'])){
 
-    /* Optionally execute a command after we're done */
-    #$this->handle_post_events($mode);
+        if($ldap->cat($dn)){
+          $modify= TRUE;
+        } else {
+          $modify= FALSE;
+        }
+
+        /* Build new entry */
+        $attrs= array();
+        foreach ($data as $attribute => $values){
+          if ($attribute == "MODIFIED" || $attribute == "dn"){
+            continue;
+          }
+          if (count($values)){
+            if (count($values) == 1){
+              $attrs[$attribute]= $values[0];
+            } else {
+              $attrs[$attribute]= $values;
+            }
+          } else {
+            if ($modify){
+              $attrs[$attribute]= array();
+            }
+          }
+        }
+
+        $ldap->cd($dn);
+        if ($modify){
+          $ldap->modify($attrs);
+          show_ldap_error($ldap->get_error(), _("Can't save DHCP object!"));
+        } else {
+          $ldap->add($attrs);
+          show_ldap_error($ldap->get_error(), _("Can't save DHCP object!"));
+        }
+      }
+    }
   }
 
 
@@ -234,28 +312,24 @@ class servdhcp extends plugin
       /* Read all sub entries to place here */
       $ldap->cd($value['dn']);
       $ldap->search("(|(objectClass=dhcpLog)(objectClass=dhcpClass)(objectClass=dhcpSubClass)(objectClass=dhcpLeases)(objectClass=dhcpHost)(objectClass=dhcpGroup)(objectClass=dhcpPool)(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork)(objectClass=dhcpOptions)(objectClass=dhcpTSigKey)(objectClass=dhcpDnsZone)(objectClass=dhcpFailOverPeer))", array());
+      $this->serviceDN= $value['dn'];
 
       while ($attrs= $ldap->fetch()){
-        $this->dhcpObjectCache[$ldap->getDN()]= $attrs;
-        $tmp= preg_replace("/".$value['dn']."/", "", $ldap->getDN());
+        $sattrs= array();
+        for ($i= 0; $i<$attrs['count']; $i++){
+          $sattrs[$attrs[$i]]= $attrs[$attrs[$i]];
+          unset($sattrs[$attrs[$i]]['count']);
+        }
+        $sattrs['dn']= $ldap->getDN();
+        $this->dhcpObjectCache[$ldap->getDN()]= $sattrs;
+        $tmp= preg_replace("/".$this->serviceDN."/", "", $ldap->getDN());
         $indent= substr_count($tmp, ",");
         $spaces= "";
         for ($i= 0; $i<$indent; $i++){
           $spaces.= "&nbsp;&nbsp;&nbsp;&nbsp;";
         }
-        $types= array(  "dhcpLog" => _("Logging"),
-            "dhcpService" => _("Global options"),
-            "dhcpClass" => _("Class"),
-            "dhcpSubClass" => _("Subclass"),
-            "dhcpLeases" => _("Lease"),
-            "dhcpHost" => _("Host"),
-            "dhcpGroup" => _("Group"),
-            "dhcpPool" => _("Pool"),
-            "dhcpSubnet" => _("Subnet"),
-            "dhcpFailOverPeer" => _("Failover peer"),
-            "dhcpSharedNetwork" => _("Shared network"));
 
-        foreach ($types as $key => $val){
+        foreach ($this->types as $key => $val){
           if (in_array("$key", $attrs['objectClass'])){
             $type= $val;
             break;