Code

Updated translation, fixed some typo/errors
[gosa.git] / plugins / admin / systems / class_termDNS.inc
index 299335cb8f2bf3ce08eab97890d0304c008b04f3..065a6e1129204b5aab84c4447748bf8400edd2cb 100644 (file)
@@ -2,13 +2,9 @@
 
 class termDNS extends plugin
 {
-  /* CLI vars */
-  var $cli_summary      = "Manage server basic objects";
-  var $cli_description  = "Some longer text\nfor help";
-  var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
-
   /* attribute list for save action */
-  var $ignore_account = TRUE;
+  var $ignore_account = true;
+  var $autonet        = false;
 
   /* Basic informations 
    */
@@ -21,6 +17,20 @@ class termDNS extends plugin
   var $OrigCn         = "";    // Initial cn
   var $IPisMust       = false;
   var $MACisMust      = false;
+  var $dialog;
+
+  /* DCHP Attributes 
+   */
+  var $dhcpAttributes           = array("dhcpParentNode");
+  var $dhcpEnabled              = FALSE;
+  var $dhcp_is_Account          = FALSE;
+  var $dhcpParentNodes          = array();
+  var $dhcpParentNode           = "";
+  var $dhcpHostEntry            = array();
+  var $initial_dhcpParentNode   = "";
+  var $initial_dhcpHostEntry    = array();
+  var $initial_dhcp_is_Account  = FALSE;
+
 
   /* DNS attributes  
    */
@@ -43,9 +53,49 @@ class termDNS extends plugin
     plugin::plugin ($config, $dn);
 
     if(isset($this->attrs['cn'][0])){
-      $this->OrigCn = $this->attrs['cn'][0];
+      $this->OrigCn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
+      $this->cn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
     }
 
+    /************
+     * DHCP 
+     ************/
+
+    /* Hide all dhcp specific code, if dhcp plugin is not present in config */
+    $dhcpEnabled = FALSE;
+    foreach($this->config->data['TABS']['SERVTABS'] as $tab){
+      if(preg_match("/^servdhcp$/",$tab['CLASS'])){
+        $this->dhcpEnabled = TRUE;
+      }
+    }
+    if($this->dhcpEnabled){
+      $this->dhcpParentNodes = $this->get_dhcp_parent_nodes();
+      $this->dhcpParentNode  = $this->get_dhcp_parent_node();
+      if($this->dhcpParentNode){
+        $this->dhcp_is_Account = TRUE;
+        $this->initial_dhcp_is_Account = TRUE;
+        $this->dhcpHostEntry  = $this->get_dhcp_host_entry();    
+      }
+      $this->initial_dhcpHostEntry = $this->dhcpHostEntry;
+      $this->initial_dhcpParentNode= $this->dhcpParentNode;
+    }
+
+
+    /************
+     * Autonetwork hook 
+     ************/
+    /* Do we have autonet support? */
+    if (isset($this->config->data['MAIN']['AUTO_NETWORK_HOOK'])){
+      $this->autonet= true;
+    }
+
+
+    /************
+     * DNS
+     ************/
     /* Hide all dns specific code, if dns is not available 
      */
     $DNSenabled = false;
@@ -59,40 +109,59 @@ class termDNS extends plugin
       return;
     }
 
-    /* Get Zones  
-     */
-    $this->Zones        = getAvailableZones($config);
+    if($this->DNSenabled){
 
-    /* Get Entry 
-     */
-    $this->dnsEntry     = getDNSHostEntries($config,$this->OrigCn);
+      /* Get Zones  
+       */
+      $this->Zones        = getAvailableZones($config);
 
-    /* Remove A record which equals $this->ipHostNumber
-     */
-    foreach($this->dnsEntry['RECORDS'] as $key => $rec){
-      if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
-        unset($this->dnsEntry['RECORDS'][$key]);
+      /* Get Entry 
+       */
+      $this->dnsEntry     = getDNSHostEntries($config,$this->OrigCn);
+
+      /* Remove A record which equals $this->ipHostNumber
+       */
+      $ptr = $this->get_pTRRecord();
+      foreach($this->dnsEntry['RECORDS'] as $key => $rec){
+        if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
+          unset($this->dnsEntry['RECORDS'][$key]);
+        }
+        if(($rec['type'] == "pTRRecord") && ($rec['value'] == $ptr)){
+          unset($this->dnsEntry['RECORDS'][$key]);
+        }
       }
-    }
 
-    /* Get Record types 
-     */
-    $this->RecordTypes  = getDnsRecordTypes();
+      /* Get Record types 
+       */
+      $this->RecordTypes  = getDnsRecordTypes();
 
-    /* If there is at least one entry in this -> types, we have DNS enabled 
-     */
-    if($this->dnsEntry['exists']){
-      $this->DNS_is_account = true;
-    }else{
-      $this->DNS_is_account = false;
+      /* If there is at least one entry in this -> types, we have DNS enabled 
+       */
+      if($this->dnsEntry['exists']){
+        $this->DNS_is_account = true;
+      }else{
+        $this->DNS_is_account = false;
+      }
+   
     }
-    
     /* Store initally account settings 
      */
     $this->DNSinitially_was_account = $this->DNS_is_account;
   }
 
 
+  function netmaskIsCoherent($idZone) 
+  {
+    $netmask = FlipIp(str_replace(".in-addr.arpa","",getNameFromMix($idZone)));
+    if(!strstr($this->ipHostNumber, $netmask)){
+      return false;
+    }else{
+      return true;
+    }
+  }
+
+
   function getVarsForSaving($attrs) 
   {
     foreach($this->attributes as $attr){
@@ -110,7 +179,76 @@ class termDNS extends plugin
     $display= "";
 
     $smarty->assign("staticAddress", ""); 
+    $smarty->assign("autonet", $this->autonet);
+    /* Check for autonet button */
+    if ($this->autonet && isset($_POST['autonet'])){
+      $cmd= $this->config->data['MAIN']['AUTO_NETWORK_HOOK'];
+      if(!empty($cmd) && $this->cn != ""){
+        $res = shell_exec($cmd." ".$this->cn);
+        if(!$res){
+          print_red(sprintf(_("Can't execute specified AUTO_NETWORK_HOOK '%s'. Please check your gosa.conf."),$cmd));
+        } else {
+          $res= split(';', trim($res));
+          if (isset($res[0]) && $res[0] != ""){
+            $this->ipHostNumber= $res[0];
+          }
+          if (isset($res[1]) && $res[1] != ""){
+            $this->macAddress= $res[1];
+          }
+        }
+      }
+    }
+
+    
+  
+    /**********
+     * DHCP Handling
+     **********/
  
+    if(isset($_POST['dhcpEditOptions'])){
+
+      if(count($this->dhcpHostEntry) == 0){
+        $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
+      }else{
+        $this->dialog = new dhcpHost($this->dhcpHostEntry,TRUE);
+      }
+      $this->dialog->cn = $this->cn; 
+      $this->dialog->dhcpHWAddress = $this->macAddress; 
+      if(!empty($this->ipHostNumber)){
+        $this->dialog->statements['fixed-address'] = $this->ipHostNumber; 
+      }
+    }
+
+    if(isset($_POST['cancel_dhcp'])){
+      $this->dialog = FALSE; 
+    }
+
+    if(isset($_POST['save_dhcp'])){
+      $this->dialog->save_object();
+      
+      $msgs = $this->dialog->check(array());
+      if(count($msgs)){
+        foreach($msgs as $msg){
+          print_red($msg);
+        }
+      }else{
+        $this->dhcpHostEntry = $this->dialog->save();
+        $this->dialog = FALSE; 
+      }
+    }
+
+    if(isset($this->dialog) && $this->dialog != FALSE){
+      $this->dialog->save_object();
+      return($this->dialog->execute());
+    }
+    $smarty->assign("dhcpEnabled",    $this->dhcpEnabled);
+    $smarty->assign("dhcp_is_Account",$this->dhcp_is_Account);
+    $smarty->assign("dhcpParentNode", $this->dhcpParentNode);
+    $smarty->assign("dhcpParentNodes",$this->dhcpParentNodes);
+    $smarty->assign("dhcpParentNodeCnt",count($this->dhcpParentNodes));
+
     /* There is no dns available 
      */
     if($this->DNSenabled == false){
@@ -122,65 +260,83 @@ class termDNS extends plugin
       /* Assign smarty all non DNs attributes */
       foreach($this->attributes as $attr){
         $smarty->assign($attr,$this->$attr);
+        $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
       }
-      $smarty->assign("staticAddress", "");
+      $smarty->assign("staticAddress","<font class=\"must\">*</font>");
+
       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
-      return($display);
     }else{
       $smarty->assign("DNS_is_account",true); 
-    }
-    /* Add new empty array to our record list */
-    if(isset($_POST['AddNewRecord'])){
-      $this->dnsEntry['RECORDS'][]  =array("type"=>"aRecord","value"=>"");
-    }
-   
-    /* Handle all posts */ 
-    $only_once =true;
-    foreach($_POST as $name => $value){
 
-      /* Check if we have to delete a record entry */
-      if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
-      
-        /* Avoid performing this once again */
-        $only_once = false;
+      /* Add new empty array to our record list */
+      if(isset($_POST['AddNewRecord'])){
+        $this->dnsEntry['RECORDS'][]  =array("type"=>"aRecord","value"=>"");
+      }
 
-        /* Extract id for specified entry */
-        $id = preg_replace("/RemoveRecord_/","",$name);
-        $id = preg_replace("/_.*$/","",$id);
-    
-        /* Delete this record, mark edited entries to be able to delete them */
-        if(isset($this->dnsEntry['RECORDS'][$id])){
-          unset($this->dnsEntry['RECORDS'][$id]);
+      /* Handle all posts */ 
+      $only_once =true;
+      foreach($_POST as $name => $value){
+
+        /* Check if we have to delete a record entry */
+        if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
+
+          /* Avoid performing this once again */
+          $only_once = false;
+
+          /* Extract id for specified entry */
+          $id = preg_replace("/RemoveRecord_/","",$name);
+          $id = preg_replace("/_.*$/","",$id);
+
+          /* Delete this record, mark edited entries to be able to delete them */
+          if(isset($this->dnsEntry['RECORDS'][$id])){
+            unset($this->dnsEntry['RECORDS'][$id]);
+          }
         }
       }
-    }
 
-    /* Assign smarty all non DNs attributes */
-    foreach($this->attributes as $attr){
-      $smarty->assign($attr,$this->$attr);
-    }
+      /* Assign smarty all non DNs attributes */
+      foreach($this->attributes as $attr){
+        $smarty->assign($attr,$this->$attr);
+        $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
+      }
 
-    /* Assign smarty all DNS attributes */
-    foreach($this->DNSattributes as $attr){
-      $smarty->assign($attr,$this->dnsEntry[$attr]);
-    }
-    
-    /* Assign all needed vars */
-    $smarty->assign("DNSAccount",$this->DNS_is_account);
-    $smarty->assign("Zones",$this->Zones);
-    $smarty->assign("ZoneKeys",($this->Zones));
-    $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
-  
-    $tmp = $this->generateRecordsList();
-    
-    $changeStateForRecords = $tmp['changeStateForRecords'];
+      /* Assign smarty all DNS attributes */
+      foreach($this->DNSattributes as $attr){
+        $smarty->assign($attr,$this->dnsEntry[$attr]);
+        $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
+      }
+
+      /* Assign all needed vars */
+      $smarty->assign("DNSaccountACL",chkacl($this->acl,"termDNS"));
+
+      $smarty->assign("DNSAccount",$this->DNS_is_account);
+      $smarty->assign("Zones",$this->Zones);
+      $smarty->assign("ZoneCnt",count($this->Zones));
+      $smarty->assign("ZoneKeys",($this->Zones));
+      $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
+
+      /* Create zone array */
+      $idZones = array();
+      foreach($this->Zones as $id => $zone){
+        if($this->netmaskIsCoherent($id)) {
+          $idZones[$id] = $zone;
+        }else{
+          $idZones[$id] = $zone."&nbsp;("._("Not matching").")";
+        }
+      }    
+      $smarty->assign("Zones",$idZones);
+      $smarty->assign("ZoneKeys", $this->Zones);
 
-    $smarty->assign("records",$tmp['str']);
-    $smarty->assign("changeStateForRecords",$changeStateForRecords);
-    $smarty->assign("staticAddress","<font class=\"must\">*</font>");
+      $tmp = $this->generateRecordsList();
 
-    $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
+      $changeStateForRecords = $tmp['changeStateForRecords'];
+
+      $smarty->assign("records",$tmp['str']);
+      $smarty->assign("changeStateForRecords",$changeStateForRecords);
+      $smarty->assign("staticAddress","<font class=\"must\">*</font>");
+
+      $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
+    }
     return($display);
   }
 
@@ -193,6 +349,7 @@ class termDNS extends plugin
     while($attr = $ldap->fetch()){  
       $ldap->cd($attr['dn']);
       $ldap->rmDir($attr['dn']);
+      show_ldap_error("Record:".$ldap->get_error(), _("Removing terminal from DNS object failed")); 
     }
     */
   }
@@ -202,30 +359,40 @@ class termDNS extends plugin
   {
     /* Save all posted vars */
     plugin::save_object();
-    
-    /* Ge all non dns attributes (IP/MAC)*/
-    foreach($this->attributes as $attr){
-      if(isset($_POST[$attr])){
-        $this->$attr = $_POST[$attr];
+  
+
+    /* Handle DHCP Posts*/ 
+    if($this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
+      foreach($this->dhcpAttributes as $attr){
+        if(isset($_POST[$attr])){
+          $this->$attr = $_POST[$attr];
+        }
+      }
+      if(isset($_POST['dhcp_is_Account'])){
+        $this->dhcp_is_Account = TRUE;
+      }else{
+        $this->dhcp_is_Account = FALSE;
       }
     }
-
     /* Get dns attributes */
-    if(isset($_POST['network_tpl_posted'])){
+    if(($this->DNSenabled) && (isset($_POST['network_tpl_posted'])) && chkacl($this->acl,"termDNS") == ""){
 
       /* Check for posted record changes */
-      foreach($this->dnsEntry['RECORDS'] as $key => $value){
-      
-        /* Check if type has changed */
-        if(isset($_POST['RecordTypeSelectedFor_'.$key])){
-          $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
-        }
-        /* Check if value has changed */
-        if(isset($_POST['RecordValue_'.$key])){
-          $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
+      if(is_array($this->dnsEntry['RECORDS'])){
+        foreach($this->dnsEntry['RECORDS'] as $key => $value){
+
+          /* Check if type has changed */
+          if(isset($_POST['RecordTypeSelectedFor_'.$key])){
+            $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
+          }
+          /* Check if value has changed */
+          if(isset($_POST['RecordValue_'.$key])){
+            $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
+          }
         }
       }
-      
       /* Get all basic DNS attributes (TTL, Clas ..)*/
       foreach($this->DNSattributes as $attr){
         if(isset($_POST[$attr])){
@@ -249,6 +416,10 @@ class termDNS extends plugin
     /* Call common method to give check the hook */
     $message= plugin::check();
 
+    if($this->dhcpEnabled && $this->dhcp_is_Account && $this->dhcpParentNode != "" && count($this->dhcpHostEntry) == 0){
+      $message[] =_("You have not configured your dhcp settings yet.");
+    }
+
     /* Check if ip must be given
      */  
     if(($this->IPisMust)||($this->DNS_is_account)){
@@ -259,12 +430,11 @@ class termDNS extends plugin
         $message[]= _("The required field 'IP-address' is not set.");
       }
 
-      /* check if given ip is valid ipi
-       */
-      $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
-      if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
-        $message[]= _("Wrong IP format in field IP-address.");
-      }
+    }
+
+    /* check if given ip is valid ip */
+    if ($this->ipHostNumber != "" && !is_ip($this->ipHostNumber)){
+      $message[]= _("Wrong IP format in field IP-address.");
     }
 
     /* Check if mac is empty 
@@ -275,9 +445,8 @@ class termDNS extends plugin
 
     /* Check if given mac is valid mac 
      */
-    $tr = count(split(":",$this->macAddress));
-    if($tr!=6){
-      $message[]=(_("The given macaddress is invalid. There must be 6 1byte segments seperated by ':'."));
+    if(!is_mac($this->macAddress)){
+      $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
     }
 
     /* only perfrom this checks if this is a valid DNS account */
@@ -286,7 +455,15 @@ class termDNS extends plugin
       $checkArray = array();
       $onlyOnce   = array();
 
-      $onlyOnce['cNAMERecord'] = 0;
+      $tmp = array_flip($this->Zones);
+      $tmp2 = $tmp[$this->dnsEntry['zoneName']];
+      if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
+        $tmp2 = preg_replace("/^.*\//","",$tmp2);
+        $message[] =sprintf(_("The specified IP address '%s' is not matching the selected reverse zone entry '%s'."),$this->ipHostNumber,$tmp2);
+      }
+
+                       // There can be many CNAME records
+      //$onlyOnce['cNAMERecord'] = 0;
 
       /* Walk through all entries and detect duplicates or mismatches
        */  
@@ -351,6 +528,70 @@ class termDNS extends plugin
     $this->cleanup();
     $ldap->modify ($this->attrs); 
 
+    /****************/ 
+    /* DHCP HANDLING */
+    /****************/ 
+  
+    /* New entry */
+    if($this->dhcpEnabled){
+
+      /* Unset dhcpStatements if this attribute is empty  */
+      if(isset($this->dhcpHostEntry['dhcpStatements']) && 
+          ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
+        unset($this->dhcpHostEntry['dhcpStatements']);
+      }
+  
+      /* DHCP removed */
+      if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
+        $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
+        show_ldap_error($ldap->get_error(),_("Removing dhcp entry for this object failed."));
+      }
+
+      /* DHCP Added */
+      if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
+        $attrs = $this->dhcpHostEntry;
+        unset($attrs['MODIFIED']);
+        unset($attrs['dn']);
+        $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
+        $res = $ldap->add($attrs);
+#        print_a($attrs);
+#        print("cn=".$this->cn.",".$this->dhcpParentNode);
+        show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
+      }
+
+      /* DHCP still activated */
+      if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
+
+        /* DHCP node changed */
+        if(($this->initial_dhcpParentNode != $this->dhcpParentNode) || 
+           ($this->cn != $this->OrigCn)){
+          $attrs = $this->dhcpHostEntry;
+          $attrs['cn'] = $this->cn;
+          unset($attrs['dn']);
+          unset($attrs['MODIFIED']);
+          $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
+          $res = $ldap->add($attrs);
+          show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
+          if($res){
+            $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
+            show_ldap_error($ldap->get_error(),_("Removing old dhcp entry failed."));
+          }
+        }
+         
+        /* SAME node but modified */ 
+        if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1  && 
+            $this->initial_dhcpParentNode == $this->dhcpParentNode){
+          $attrs = $this->dhcpHostEntry;
+          unset($attrs['dn']);
+          unset($attrs['MODIFIED']);
+          $ldap->cd($this->dhcpHostEntry['dn']);
+          $ldap->modify($attrs);
+          show_ldap_error($ldap->get_error(),_("Modifying dhcp entry failed."));
+        }    
+      }
+    }
+      
+
     /****************/ 
     /* DNS HANDLING */
     /****************/ 
@@ -358,52 +599,62 @@ class termDNS extends plugin
     /* If isn't DNS account but initially was DNS account 
        remove all DNS entries 
      */ 
-
-    /* Add ipHostNumber to aRecords
-     */
-    $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
-
-    /* Create diff and follow instructions 
-     * If Account was disabled, remove account by setting exists to false
-     */
-    if((!$this->DNS_is_account)&&($this->DNSinitially_was_account)){  
-      $this->dnsEntry['exists'] = false;
-      $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
+    if((!$this->DNSenabled) || (!$this->DNS_is_account && !$this->DNSinitially_was_account)){
+      return;
     }else{
-      $this->dnsEntry['exists'] = $this->DNS_is_account;
-      $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
-    }   
 
-    /* move follwoing entries 
-     */
-    foreach($tmp['move'] as $src => $dst){
-      $this->recursive_move($src,$dst);
-    }
+      /* Add ipHostNumber to aRecords
+       */
+      if(!empty($this->ipHostNumber)){
+        $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
+        $ptr = $this->get_pTRRecord();
+        if(!empty($ptr)){
+          $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
+        }
+      }
 
-    /* Delete dns */
-    foreach($tmp['del'] as $dn => $del){
-      $ldap->cd($dn);
-      $ldap->rmdir_recursive($dn);
-    }
+      /* Create diff and follow instructions 
+       * If Account was disabled, remove account by setting exists to false
+       */
+      if((!$this->DNS_is_account)&&($this->DNSinitially_was_account)){  
+        $this->dnsEntry['exists'] = false;
+        $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
+      }else{
+        $this->dnsEntry['exists'] = $this->DNS_is_account;
+        $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
+      }   
 
-    /* Add || Update new DNS entries 
-     */
-    foreach($tmp['add'] as $dn => $attrs){
-      $ldap->cd($dn);
-      $ldap->cat($dn);
-      if(count($ldap->fetch())){
+      /* move follwoing entries 
+       */
+      foreach($tmp['move'] as $src => $dst){
+        $this->recursive_move($src,$dst);
+      }
+
+      /* Delete dns */
+      foreach($tmp['del'] as $dn => $del){
         $ldap->cd($dn);
-        $ldap->modify ($attrs); 
-      }else{
+        $ldap->rmdir_recursive($dn);
+      }
+
+      /* Add || Update new DNS entries 
+       */
+      foreach($tmp['add'] as $dn => $attrs){
         $ldap->cd($dn);
-        $ldap->add($attrs);
+        $ldap->cat($dn, array('dn'));
+        if(count($ldap->fetch())){
+          $ldap->cd($dn);
+          $ldap->modify ($attrs); 
+        }else{
+          $ldap->cd($dn);
+          $ldap->add($attrs);
+        }
+      }
+
+      /* Display errors 
+       */
+      if($ldap->get_error() != "Success"){
+        show_ldap_error("Record:".$ldap->get_error(), _("Saving terminal to DNS object failed")); 
       }
-    }
-    
-    /* Display errors 
-     */
-    if($ldap->get_error() != "Success"){
-      show_ldap_error("Record:".$ldap->get_error()); 
     }
   }
 
@@ -414,7 +665,7 @@ class termDNS extends plugin
     $changeStateForRecords = "";
     
     if(!$this->DNS_is_account) {
-      $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled>";
+      $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
       return $str;
     }
  
@@ -427,14 +678,14 @@ class termDNS extends plugin
 
         $str.=" <tr>".
           "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
-          "   <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
-          "   <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
+          "   <td><input ".chkacl($this->acl,"termDNS")." type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
+          "   <td><input ".chkacl($this->acl,"termDNS")." type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
           "</tr>";
     }
 
     $str.= "  <tr>".
            "    <td colspan=2 width='50%'></td><td>".
-           "      <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
+           "      <input type='submit' value='"._("Add")."' name='AddNewRecord' ".chkacl($this->acl,"termDNS")." >".
            "    </td>".
            "  </tr>".
            "</table>";
@@ -447,7 +698,7 @@ class termDNS extends plugin
    */
   function generateRecordListBox($selected,$name)
   {
-    $str = "<select name='".$name."' id='".$name."'>";
+    $str = "<select ".chkacl($this->acl,"termDNS")."  name='".$name."' id='".$name."'>";
     foreach($this->RecordTypes as $type => $value){
       $use = "";
       if($type == $selected){
@@ -458,6 +709,121 @@ class termDNS extends plugin
     $str.="</select>";
     return($str); 
   }
+
+  
+  function get_dhcp_host_entry()
+  {
+    $attrs = array();
+    $dn = $this->get_dhcp_host_entry_dn();
+    if($dn){
+      $ldap = $this->config->get_ldap_link();
+      $ldap->cd($this->config->current['BASE']);
+      $ldap->cat($dn,array("*"));
+      if($ldap->count()){
+        $attrs = $ldap->fetch();
+        foreach($attrs as $key => $value){
+          if(is_numeric($key) || ($key == "count")){
+            unset($attrs[$key]);
+          }
+          if(is_array($value) && isset($value['count'])){
+            unset($attrs[$key]['count']);
+          }
+        }
+      }
+    }
+    return($attrs);
+  }
+
+
+  function get_dhcp_host_entry_dn()
+  {
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+    $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
+
+    if($ldap->count()){
+      $attr = $ldap->fetch();
+      return($attr['dn']);
+    }else{
+      return("");
+    }
+  }  
+
+
+  function get_dhcp_parent_node()
+  {
+    return(preg_replace("/^cn=".normalizePreg($this->cn).",/","",$this->get_dhcp_host_entry_dn()));
+  }
+
+
+  function get_dhcp_parent_nodes()
+  {
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+    $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
+    
+    $dhcp_dns = array();
+    while($attr = $ldap->fetch()){
+      $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
+    }
+    foreach($dhcp_dns as $key => $pri_dns){
+      $ldap->cat($pri_dns,array("cn"));
+      $tmp = $ldap->fetch();
+      if(isset($tmp['cn'][0])){
+        $dhcp_dns[$key] = $tmp['cn'][0];
+      }else{
+        unset($dhcp_dns[$key]);
+      }
+    }
+
+    $tmp = $tmp2 = array();
+    foreach($dhcp_dns as $dn => $cn){
+      $ldap->cd($dn);
+      $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
+                    "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
+      while($attr = $ldap->fetch()){
+        $tmp[$attr['dn']] = $attr['cn'][0];
+      }
+      $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.")&nbsp;"));
+    }
+    return($tmp2);
+  }
+
+  
+  /* this function returns the default ptr record entry */
+  function get_pTRRecord()
+  {
+    if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
+      $ldap = $this->config->get_ldap_link();
+      $ldap->cat(getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
+      $attrs = $ldap->fetch();
+      $tmp = array_flip($this->Zones);
+      $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
+      $tmp = trim(preg_replace("/\.in-addr.arpa$/","",$tmp));
+      $ptr = preg_replace("/^".normalizePreg(FlipIp($tmp))."\./","",$this->ipHostNumber);
+      return($ptr);
+    }else{
+      return(FALSE);
+    }
+  }
+
+  
+  function create_tree($arr,$base,$current = "")
+  {
+    $ret = array();
+    foreach($arr as $r => $name){
+      $base_part = str_replace($base,"",$r);
+      if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
+        $ret[$r] = $current.$name;
+        $tmp = $this->create_tree($arr,$r,$current.".&nbsp;");
+        foreach($tmp as $sub_key => $sub_name){
+          $ret[$sub_key] = $sub_name;
+        }
+      } 
+    }
+    return($ret);
+  }
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: