Code

Corrected FAQ
[gosa.git] / plugins / admin / systems / class_termDNS.inc
index c722bc59dbdfc187389e6437a4bd4a08c890c388..0f76e2f131c27845aa0b34d3a76e5e868b5aeb6b 100644 (file)
@@ -8,7 +8,8 @@ class termDNS extends plugin
   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 
    */
@@ -42,9 +43,16 @@ class termDNS extends plugin
 
     plugin::plugin ($config, $dn);
 
-    $this->OrigCn = $this->attrs['cn'][0];
+    if(isset($this->attrs['cn'][0])){
+      $this->OrigCn = $this->attrs['cn'][0];
+      $this->cn = $this->attrs['cn'][0];
+    }
+
+    /* Do we have autonet support? */
+    if (isset($this->config->data['MAIN']['AUTO_NETWORK_HOOK'])){
+      $this->autonet= true;
+    }
 
     /* Hide all dns specific code, if dns is not available 
      */
     $DNSenabled = false;
@@ -58,34 +66,38 @@ 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
+       */
+      foreach($this->dnsEntry['RECORDS'] as $key => $rec){
+        if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
+          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;
@@ -109,7 +121,27 @@ class termDNS extends plugin
     $display= "";
 
     $smarty->assign("staticAddress", ""); 
+    $smarty->assign("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];
+          }
+        }
+      }
+    }
+
     /* There is no dns available 
      */
     if($this->DNSenabled == false){
@@ -122,7 +154,9 @@ class termDNS extends plugin
       foreach($this->attributes as $attr){
         $smarty->assign($attr,$this->$attr);
       }
-      $smarty->assign("staticAddress", "");
+      $smarty->assign("staticAddress","<font class=\"must\">*</font>");
+
+      $smarty->assign("autonet", $this->autonet);
       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
       return($display);
     }else{
@@ -192,6 +226,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")); 
     }
     */
   }
@@ -210,21 +245,22 @@ class termDNS extends plugin
     }
 
     /* Get dns attributes */
-    if(isset($_POST['network_tpl_posted'])){
+    if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
 
       /* 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])){
@@ -245,7 +281,8 @@ class termDNS extends plugin
   /* Check supplied data */
   function check()
   {
-    $message= array();
+    /* Call common method to give check the hook */
+    $message= plugin::check();
 
     /* Check if ip must be given
      */  
@@ -257,12 +294,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 
@@ -273,9 +309,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 */
@@ -284,7 +319,8 @@ class termDNS extends plugin
       $checkArray = array();
       $onlyOnce   = array();
 
-      $onlyOnce['cNAMERecord'] = 0;
+                       // There can be many CNAME records
+      //$onlyOnce['cNAMERecord'] = 0;
 
       /* Walk through all entries and detect duplicates or mismatches
        */  
@@ -356,52 +392,56 @@ class termDNS extends plugin
     /* If isn't DNS account but initially was DNS account 
        remove all DNS entries 
      */ 
+    if(!$this->DNSenabled){
+      return;
+    }else{
 
-    /* Add ipHostNumber to aRecords
-     */
-    $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
+      /* 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);
-    }else{
-      $this->dnsEntry['exists'] = $this->DNS_is_account;
-      $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
-    }   
-
-    /* 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);
+      }   
 
-    /* move follwoing entries 
-     */
-    foreach($tmp['move'] as $src => $dst){
-      $this->recursive_move($src,$dst);
-    }
+      /* move follwoing entries 
+       */
+      foreach($tmp['move'] as $src => $dst){
+        $this->recursive_move($src,$dst);
+      }
 
-    /* Add || Update new DNS entries 
-     */
-    foreach($tmp['add'] as $dn => $attrs){
-      $ldap->cd($dn);
-      $ldap->cat($dn);
-      if(count($ldap->fetch())){
+      /* 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()); 
     }
   }
 
@@ -412,7 +452,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;
     }