Code

Replaced in_array calls for gosa-plugins
[gosa.git] / gosa-plugins / dns / admin / systems / services / dns / class_DNS.inc
index 7338000e0e8ec3b95e0e7c98375ca8a2c998f3a3..61e492c9ab8f4377cba91b5aa7c1e5ed11a44810 100644 (file)
@@ -45,12 +45,12 @@ class DNS
 
   /* This fucntion is used to flip the ip address, for example
      12.3.45  ->  45.3.12
-     Because some entries (like zones) are store like that 45.3.12.in-addr.arpa
+     Because some entries (like zones) are store like that 45.3.12.in-addr.arpa.
      but we want to display 12.3.45.
    */
   static function FlipIp($ip)
   {
-    $tmp = array_reverse(split("\.",$ip));
+    $tmp = array_reverse(explode(".",$ip));
     $new = "";
     foreach($tmp as $section){
       $new .= $section.".";
@@ -119,7 +119,7 @@ class DNS
       /* Generate SOA entry
        */
       if(isset($attrs['sOARecord'][0])){
-        $tmp = split("\ ",$attrs['sOARecord'][0]) ;
+        $tmp = explode(" ",$attrs['sOARecord'][0]) ;
         $tmp2 = array();
 
         /* Assign soa vars */
@@ -138,7 +138,7 @@ class DNS
 
         /* Skip some attributes 
          */
-        if(in_array($name,$SkipRecords)) continue;
+        if(in_array_strict($name,$SkipRecords)) continue;
 
         /* If there is a record attribute
          */
@@ -168,8 +168,8 @@ class DNS
         unset($Zones[$zoneName]);
       }else{
         $tmp = $ldap->fetch();
-        $Zones[$zoneName]['ReverseZone']        = DNS::FlipIp(str_replace(".in-addr.arpa","",$tmp['zoneName'][0]));
-        $Zones[$zoneName]['InitialReverseZone'] = DNS::FlipIp(str_replace(".in-addr.arpa","",$tmp['zoneName'][0]));
+        $Zones[$zoneName]['ReverseZone']        = DNS::FlipIp(str_replace(".in-addr.arpa.","",$tmp['zoneName'][0]));
+        $Zones[$zoneName]['InitialReverseZone'] = DNS::FlipIp(str_replace(".in-addr.arpa.","",$tmp['zoneName'][0]));
       }
     }
     return($Zones);
@@ -249,9 +249,9 @@ class DNS
       if(!empty($zone['InitialReverseZone'])){
         if($zone['InitialReverseZone'] != $zone['ReverseZone']){
           $base = "zoneName=".$zone['zoneName'].",".$HostDn;
-          $oldRDn = "zoneName=". DNS::FlipIp($zone['InitialReverseZone']).".in-addr.arpa,".$base; 
-          $newRDn = "zoneName=". DNS::FlipIp($zone['ReverseZone']).".in-addr.arpa,".$base;
-          $PrePareZoneEntries[DNS::FlipIp($zone['InitialReverseZone']).".in-addr.arpa"] = DNS::FlipIp($zone['ReverseZone']).".in-addr.arpa";
+          $oldRDn = "zoneName=". DNS::FlipIp($zone['InitialReverseZone']).".in-addr.arpa.,".$base; 
+          $newRDn = "zoneName=". DNS::FlipIp($zone['ReverseZone']).".in-addr.arpa.,".$base;
+          $PrePareZoneEntries[DNS::FlipIp($zone['InitialReverseZone']).".in-addr.arpa."] = DNS::FlipIp($zone['ReverseZone']).".in-addr.arpa.";
           $move [$oldRDn] = $newRDn;
         }
       }
@@ -282,7 +282,7 @@ class DNS
       /* Create Reverse Entry 
        * And append it to our add queue
        */
-      $zone['ReverseZone'] = DNS::FlipIp($zone['ReverseZone']).".in-addr.arpa";
+      $zone['ReverseZone'] = DNS::FlipIp($zone['ReverseZone']).".in-addr.arpa.";
       $base = "zoneName=".$zone['zoneName'].",".$HostDn;
       $newRDn = "zoneName=".$zone['ReverseZone'].",".$base;
       $rObj = $obj;
@@ -335,7 +335,7 @@ class DNS
     $ret['RECORDS']   = array();
     $ret['dNSClass']  = "IN";
     $ret['zoneName']  = "";
-    $ret['dNSTTL']    = "7440";
+    $ret['dNSTTL']    = "";
     $ret['exists']    = false;
 
     $ldap = $config->get_ldap_link();
@@ -346,10 +346,9 @@ class DNS
      */
     $foundIn = array();
     $zones = DNS::getAvailableZones($config);
-
     $zonesArr = array();
     foreach($zones as $zoneMix){
-      $zoneIndex = split("/",$zoneMix);
+      $zoneIndex = explode("/",$zoneMix);
       if(!array_key_exists($zoneIndex[0],$zonesArr)) {
         $zonesArr[$zoneIndex[0]] = array();
       }
@@ -359,9 +358,19 @@ class DNS
     foreach($zonesArr as $nameServer => $nameServerArr){
       $foundInTmp = array();
       foreach($nameServerArr as $zoneArr => $zone){
+
+        /* Strip eventually domain part from hostname 
+         */
+        $zN = trim($zone,".");
+        $nN = trim($name,".");  
+        if(preg_match("/".preg_quote($zN, '/')."$/",$nN)){
+          $nN = preg_replace("/[\.]*".preg_quote($zN, '/')."[\.]*$/","",$nN);
+        }else{
+          $nN = $name;
+        }
         $zoneMix = $nameServer."/".$zone;
         $zoneDn = DNS::getDNSZoneDN($config,$zoneMix);
-        $ldap->ls("(&(objectClass=dNSZone)(zoneName=*)(relativeDomainName=".$name.")(!(relativeDomainName=@)))", $zoneDn,$attrs = array("*"));
+        $ldap->ls("(&(objectClass=dNSZone)(zoneName=*)(relativeDomainName=".$nN.")(!(relativeDomainName=@)))", $zoneDn,$attrs = array("*"));
         while($attrs = $ldap->fetch()){
           $foundInTmp [$zoneMix] = $attrs['dn'];
           $foundIn [$zoneMix] = $attrs['dn'];
@@ -382,10 +391,15 @@ class DNS
     $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("*"));
     while($attrs = $ldap->fetch()){
 
-      /* If relative domainname == cn
-       * Try to read dnsclass / TTl / zone
+      /* Strip eventually domain part from hostname 
        */
-      if($attrs['relativeDomainName'][0] == $name){
+      $zN = trim($attrs['zoneName'][0],".");
+      $nN = trim($name,"."); 
+      $testname = $attrs['relativeDomainName'][0].".".$zN;
+
+      /* Check given host name with zone settings 
+       */ 
+      if(preg_match("/".preg_quote($testname, '/')."[\.]*$/",$nN) || $attrs['relativeDomainName'][0] == $name){
         $ret['exists'] = true;
         $ret['zoneName'] = $id_tmp;
         foreach(array("dNSClass","dNSTTL") as $atr){
@@ -444,6 +458,14 @@ class DNS
     $tmp                = array_flip($zones);
     $zoneName                                  = DNS::getNameFromMix($zoneNameMix);
 
+    /* Strip domain part out of dns host entry 
+     */
+    $zN = trim($zoneName,".");
+    $nN = trim($newName,".");
+    $oN = trim($oldName,".");
+    $newName = preg_replace("/[\.]*".preg_quote($zN, '/')."$/i","",$nN);
+    $oldName = preg_replace("/[\.]*".preg_quote($zN, '/')."$/i","",$oN);
+
     /* If reverseZone can't be resolved ... this 
      *  can't be a valid entry, so remove this account
      */ 
@@ -515,7 +537,9 @@ class DNS
      */
     $baseObj =  array();
     $baseObj['objectClass']       = array("top","dNSZone");
-    $baseObj['dNSTTL']            = $newEntry['dNSTTL'];
+    if(!empty($newEntry['dNSTTL'])){
+      $baseObj['dNSTTL']            = $newEntry['dNSTTL'];
+    }
     $baseObj['dNSClass']          = $newEntry['dNSClass'];
     $baseObj['zoneName']          = $zoneName; 
     $baseObj['relativeDomainName']= $newName; 
@@ -536,7 +560,7 @@ class DNS
      *  Or overwritten and filled with new values.
      */
     foreach($newRecords as $name => $def){
-      if(!in_array($name,$specialAttributes)){
+      if(!in_array_strict($name,$specialAttributes)){
         $add[$stdDn][$name] = $def;
       }
     }
@@ -544,7 +568,7 @@ class DNS
     /* Delete all OLD special attributes.
      */
     foreach($oldEntry['RECORDS'] as $id => $rec){
-      if(in_array($rec['type'],$specialAttributes)){
+      if(in_array_strict($rec['type'],$specialAttributes)){
         $deldn= "relativeDomainName=".$rec['value'].",".$baseDn;
         $del[$deldn] = "";
       }
@@ -593,7 +617,7 @@ class DNS
   static function getNameFromMix($zoneMix){
     $ret = "";
     if(!strstr($zoneMix, '/')) return($ret);   
-    $zoneIndex                 = split("/",$zoneMix);
+    $zoneIndex                 = explode("/",$zoneMix);
     return($zoneIndex[1]);
   }
 
@@ -607,7 +631,7 @@ class DNS
       return($ret);
     }
 
-    $zoneNameIndex             = split("/",$zoneNameMix); 
+    $zoneNameIndex             = explode("/",$zoneNameMix); 
     $zoneName           = $zoneNameIndex[1];
     $nameServer                                = strtolower($zoneNameIndex[0]);
     $ldap               = $config->get_ldap_link();
@@ -643,11 +667,11 @@ class DNS
     
       $ret = array();
       $ldap = $config->get_ldap_link();
-      $tmp = get_sub_list("(&(objectClass=dNSZone)(sOARecord=*))","",get_ou("serverou"),
+      $tmp = get_sub_list("(&(objectClass=dNSZone)(sOARecord=*))","",get_ou("serverRDN"),
           $config->current['BASE'],array("zoneName"),GL_NO_ACL_CHECK | GL_SUBSEARCH);
 
       foreach($tmp as $at){
-        if(preg_match("/\.in\-addr\.arpa/",$at['zoneName'][0])){
+        if(preg_match("/\.in\-addr\.arpa\./",$at['zoneName'][0])){
           $ReverseZones[$at['dn']] = $at;
         }else{
           $ForwardZones[$at['dn']] = $at;
@@ -670,6 +694,62 @@ class DNS
     }
     return($runtime_cache['DNS']['getAvailableZones']);
   }
+
+
+  static function touchDNSZone($config,$zoneName)
+  {
+    if(empty($zoneName)){
+      return null;
+    }
+
+    preg_match('@(?<server>[^/]*)/(?<name>.*)@',$zoneName,$matches);
+    $name = $matches['name'];
+    $server = strtolower($matches['server']);
+
+    // Search for the zone entry and its reverse entry.
+    $ldap = $config->get_ldap_link();
+    $ldap-> cd($config->current['BASE']);
+    $ldap->search("(&(objectClass=dNSZone)(zoneName=$name)(sOARecord=*))",array("sOARecord"));
+    if($ldap->count() != 1){
+      trigger_error("Invalid Zone ".$zoneName);
+    }else{
+      $to_update= array();
+      $forward = $ldap->fetch();
+      $to_update[$forward['dn']] = $forward;
+      $ldap->cd($forward['dn']);
+      $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(sOARecord=*))",array("sOARecord"));
+  
+      // We may have multiple reverse zones later.
+      while($attrs = $ldap->fetch()){
+        $to_update[$attrs['dn']] = $attrs;
+      }
+
+      // Increase the sAONumber for each zone
+      foreach($to_update as $zone){
+        $tmp = explode(' ',$zone['sOARecord'][0]);
+        $sOA = $tmp[2];
+        $sOAdate = substr($sOA,0,8);
+        $sOAnumber = substr($sOA,-2);
+        $date = date('Ymd');
+        $number = '01';
+        if($sOAdate < $date){
+          $sOA = $date.$number;
+        } else {
+          $number = sprintf("%02d", $sOAnumber+1);
+          $sOA = $sOAdate.$number;
+        }
+        $tmp[2] = $sOA;
+        $zone['sOARecord'][0] = implode(' ', $tmp);
+        $attrs = array('sOARecord' => $zone['sOARecord'][0]);
+        $ldap = $config->get_ldap_link();
+        $ldap->cd($zone['dn']);
+        $ldap->modify($attrs);
+        if (!$ldap->success()){
+          msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_DEL, get_class()));
+        }
+      }
+    }
+  }
 }
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>