Code

Added delete icon to ppd list if ppd file is removeable
[gosa.git] / plugins / admin / systems / class_servDNS.inc
index c4e7f178daa7111e4560c2bbbb0775ab1924148b..c109d44a6fa2d4fbf5d92be5a5ce1a1c69f74223 100644 (file)
@@ -8,144 +8,61 @@ class servdns 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 $attributes       = array(""); 
+  var $ignore_account   = FALSE;
+  var $attributes       = array(); 
   var $objectclasses    = array("whatever");
 
   var $RecordTypes      = array();
+  var $Zones            = array();
+  var $dialog           = NULL;
 
-  var $Zones  = array();
-  var $dialog = NULL;
+  var $orig_dn          = "";
 
-  function servdns ($config, $dn= NULL)
-  {
-    plugin::plugin ($config, $dn);
-
-    /* All types with required attrs */
-    $this->RecordTypes['aRecord']       = "aRecord";           // ok
-    $this->RecordTypes['mDRecord']      = "mDRecord";          // ok
-    $this->RecordTypes['mXRecord']      = "mXRecord";          // ok
-    $this->RecordTypes['nSRecord']      = "nSRecord";          // ok
-    $this->RecordTypes['pTRRecord']     = "relativeDomainName";// ok
-    $this->RecordTypes['hInfoRecord']   = "hInfoRecord";       // ok
-    $this->RecordTypes['mInfoRecord']   = "mInfoRecord";       // ok
-    $this->RecordTypes['tXTRecord']     = "tXTRecord";         // ok
-    $this->RecordTypes['aFSDBRecord']   = "aFSDBRecord";       // ok
-    $this->RecordTypes['SigRecord']     = "SigRecord";         // ok
-    $this->RecordTypes['KeyRecord']     = "KeyRecord";         // ok
-    $this->RecordTypes['aAAARecord']    = "aAAARecord";        // ok
-    $this->RecordTypes['LocRecord']     = "LocRecord";         // ok
-    $this->RecordTypes['nXTRecord']     = "nXTRecord";         // ok
-    $this->RecordTypes['sRVRecord']     = "sRVRecord";         // ok
-    $this->RecordTypes['nAPTRRecord']   = "nAPTRRecord";       // ok
-    $this->RecordTypes['kXRecord']      = "kXRecord";          // ok
-    $this->RecordTypes['certRecord']    = "certRecord";        // ok
-    $this->RecordTypes['a6Record']      = "a6Record";          // ok
-    $this->RecordTypes['dSRecord']      = "dSRecord";          // ok
-    $this->RecordTypes['sSHFPRecord']   = "sSHFPRecord";       // ok
-    $this->RecordTypes['rRSIGRecord']   = "rRSIGRecord";       // ok
-    $this->RecordTypes['nSECRecord']    = "nSECRecord";        // ok
-
-    $this->cn = $this->attrs['cn'][0];
-
-    $types = array();
-
-    /* Get all records */
-    $ldap = $this->config->get_ldap_link();
-    $ldap->cd($this->dn);
-    $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@))",array("*"));
-
-    while($attrs = $ldap->fetch()){
-      /* If relative domainname == cn
-       * Try to read dnsclass / TTl / zone
-       */
-      if((isset($attrs['tXTRecord'][0]))&&(preg_match("/zoneName\=/",$attrs['tXTRecord'][0]))){
-        $zoneName= preg_replace("/zoneName\=/","",$attrs['tXTRecord'][0]);  
-        $types[$zoneName]['ReverseZone']  = $attrs['zoneName'][0];
-        $types[$zoneName]['ReverseDN']    = $attrs['dn']; 
-      }else{
+  var $DNSinitially_was_account;
 
-        /* Generate SOA entry */
-        if(isset($attrs['sOARecord'][0])){
-          $tmp = split("\ ",$attrs['sOARecord'][0]) ;
-          $tmp2 = array();
-          $ar = array("0"=>"sOAprimary","1"=>"sOAmail","2"=>"sOAserial","3"=>"sOArefresh","4"=>"sOAretry","5"=>"sOAexpire","6"=>"sOAttl");
-
-          /* Assign soa vars */
-          foreach($ar as $key => $name){
-            if(isset($tmp[$key])){
-              $types[$attrs['zoneName'][0]][$name] = $tmp[$key];
-            }else{
-              $types[$attrs['zoneName'][0]][$name] = "";
-            }
-          }
-        }
 
-        /* Set TTL value */
-        if(isset($attrs['dNSTTL'][0])){
-          $types[$attrs['zoneName'][0]]['dNSTTL'] = $attrs['dNSTTL'][0];
-        }
+  function servdns ($config, $dn= NULL, $parent= NULL)
+  {
+    plugin::plugin ($config, $dn, $parent);
 
-        /* Set dns Class*/
-        if(isset($attrs['dNSClass'][0])){
-          $types[$attrs['zoneName'][0]]['dNSClass'] = $attrs['dNSClass'][0];
-        }
+    $this->orig_dn = $dn;
 
-        /* Set zone Name */
-        if(isset($attrs['zoneName'][0])){
-          $types[$attrs['zoneName'][0]]['zoneName'] = $attrs['zoneName'][0];
-        }
+    /* Get record types for zones
+     */
+    $this->RecordTypes = getDnsRecordTypes(true);
 
-        /* Set status */
-        $types[$attrs['zoneName'][0]]['status']   ="edited"; 
-        $types[$attrs['zoneName'][0]]['ReverseDN']= $attrs['dn']; 
-
-        /* Create list with all used records */
-        foreach($this->RecordTypes as $name => $value){
-
-          /* If there is a record attribute  */
-          if(isset($attrs[$name])){
-
-            /* get all entries */
-            for($i = 0 ; $i < $attrs[$value]['count']; $i ++){
-              $types[$attrs['zoneName'][0]]['Records'][] =array("type"      =>$name,
-                  "inittype"  =>$name,
-                  "value"     =>$attrs[$value][$i],
-                  "status"    =>"edited",
-                  "dn"        =>$attrs['dn']);
-            }
-          }
-        }
-      }
-    }
-
-    /* If there is at least one entry in this -> types, we have DNS enabled */
-    $this->Zones = $types;
+    /* Get all zone Informations
+     */
+    $this->Zones = getDNSZoneEntries($config,$dn);
+    
+    /* If there is at least one entry in this -> types, we have DNS enabled 
+     */
     if(count($this->Zones) == 0){
       $this->is_account = false;
     }else{
       $this->is_account = true;
     }
-
-    /* Store initally account settings */
     $this->DNSinitially_was_account = $this->is_account;
   }
 
+
   function execute()
   {
-    /* Call parent execute */
+    /* Call parent execute 
+     */
     plugin::execute();
 
-    /* Fill templating stuff */
+    /* Fill templating stuff 
+     */
     $smarty= get_smarty();
     $display= "";
 
-    /* Do we need to flip is_account state? */
+    /* Do we need to flip is_account state? 
+     */
     if (isset($_POST['modify_state'])){
       $this->is_account= !$this->is_account;
     }
 
-    /* Show tab dialog headers */
     if ($this->is_account){
       $display= $this->show_header(_("Remove DNS service"),
           _("This server has DNS features enabled. You can disable them by clicking below."));
@@ -155,169 +72,246 @@ class servdns extends plugin
       return ($display);
     }
 
+
+    /* Edited or Added zone 
+     */
     if(isset($_POST['SaveZoneChanges'])){
       $this->dialog->save_object();
-    
+
+      /* Check for errors  
+       */
       if(count($this->dialog->check())){
         foreach($this->dialog->check() as $msgs){
           print_red($msgs); 
         }
       }else{
+        /* add new/edited zone 
+         */
         $ret = $this->dialog->save();
-        if($this->dialog->isNew == true){
-          $this->Zones[$ret['zoneName']] = $ret;
-          $this->Zones[$ret['zoneName']] = "new";
-        }else{
-
-          if(!$this->dialog->isNew){
-
-            /* Edited but not renamed */
-            if($ret['zoneName'] == $this->dialog->InitiallyZoneName){
-              $this->Zones[$ret['zoneName']] = $ret;
-
-            }else{
-              /* Edited and renamed, we have to check if we must delete the old entry from ldap */
-
-              if($this->Zones[$this->dialog->InitiallyZoneName]['status'] == "new"){
-                /* this was a new entry (currently not saved to ldap) so we can simply unset the zone */
-                unset($this->Zones[$this->dialog->InitiallyZoneName]);
-              
-              }elseif($this->Zones[$this->dialog->InitiallyZoneName]['status'] == "edited") {
-                $this->Zones[$this->dialog->InitiallyZoneName]['status'] = "deleted";
-              } 
-              $this->Zones[$ret['zoneName']] = $ret;
-              $this->Zones[$ret['zoneName']]['status'] = "new";
-            }
-          }else{
-            $this->Zones[$ret['zoneName']] = $ret;
-            $this->Zones[$ret['zoneName']]['status'] = "new";
-          }
+        if(!$this->dialog->isNew){
+          unset($this->Zones[$this->dialog->OldZoneName]);
         }
+        $this->Zones[$ret['zoneName']] = $ret;
         $this->dialog = NULL;
       }
     }
+
+    /* Cancel zone edit / new 
+     */
     if(isset($_POST['CancelZoneChanges'])){
       $this->dialog = NULL;
     }
-    /* Add empty new zone */
+
+    /* Add empty new zone 
+     */
     if(isset($_POST['AddZone'])){
-      $this->dialog = new servdnseditZone($this->config,$this->dn,$this->RecordTypes);
+      $this->dialog = new servdnseditZone($this->config,$this->dn);
     }
 
+    /* Check for edit zone request 
+     */
     $once = false;
     foreach( $_POST as $name => $value){
+  
+      /* check all post for edit request 
+       */
       if(preg_match("/^editZone_/",$name)&&!$once){
         $once =true;
         $tmp = preg_replace("/^editZone_/","",$name);
         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
-        $this->dialog= new servdnseditZone($this->config,$this->dn,$this->RecordTypes,$this->Zones[$tmp]);
+        $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$tmp]);
+      }
+
+      /* check posts for delete zone 
+       */
+      if(preg_match("/^delZone_/",$name)&&!$once){
+
+        $once =true;
+        $tmp = preg_replace("/^delZone_/","",$name);
+        $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
+     
+        /* Initiate deletion
+         */ 
+        $this->RemoveZone($tmp); 
       }
     }
 
+    /* Show dialog 
+     */
     if($this->dialog!= NULL){
       $this->dialog->save_object();
       $this->dialog->parent = $this;
       return($this->dialog->execute());
     }
 
-    /* Create Listbox with existing Zones */
+    /* Create Listbox with existing Zones 
+     */
     $ZoneList = new divSelectBox("dNSZones");
     $ZoneList -> SetHeight(254);
 
-    /* Add entries to divlist*/
-    $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>";
+    /* Add entries to divlist
+     */
+    $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
+      <input type='image' src='images/edittrash.png' name='delZone_%s'>";
     foreach($this->Zones as $zone => $values ){
-      if($values['status'] == "deleted") continue;
-      $ZoneList->AddEntry(array(array("string" => $zone.str_replace("%s",base64_encode($zone),$editImg))));
+      $ZoneList->AddEntry(array(
+            array("string" => $zone),
+            array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
+            array("string" => _("TTL")." : ".$values['sOAttl']),
+            array("string" => _("Class")." : ".$values['dNSClass']),
+            array("string" =>str_replace("%s",base64_encode($zone),$editImg))
+            ));
     }    
 
-    /* Display tempalte */
+    /* Display tempalte 
+     */
     $smarty->assign("ZoneList",$ZoneList->DrawList());
     $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
     return($display);
   }
 
-  function remove_from_parent()
+
+  /* Delete specified zone
+   */
+  function RemoveZone($id)
   {
-  }
+    $zones =  $this->getUsedZoneNames();
 
+    if(isset($this->Zones[$id]['InitialReverseZone'])){
+      $rev = FlipIp($this->Zones[$id]['InitialReverseZone']);
+    }else{
+      $rev = FlipIp($this->Zones[$id]['ReverseZone']);
+    }
+
+    $zonename = "";
+    if(isset($this->Zones[$id]['InitialzoneName'])){
+      $zonename= $this->Zones[$id]['InitialzoneName'];
+    }
 
-  /* Save data to object */
-  function save_object()
+    $used = array();
+
+    /* Add Records which use this zoneName
+     */
+    if(isset($zones[$zonename])){
+      $used = array_merge($used,$zones[$zonename]);
+    }
+
+    /* Add Records which uses this reverse zone
+     */
+    if(isset($zones[$rev.".in-addr.arpa"])){
+      $used = array_merge($used,$zones[$rev.".in-addr.arpa"]);
+    } 
+
+    /* There are still entries using this configuration
+     *  Abort deletion
+     */ 
+    if(count($used)){
+      $i = 2;
+      $str ="";
+      foreach($used as $dn){
+        if($i > 0 ){
+          $i --;
+          $str.=$dn." ";
+        }
+      }
+
+      /*  Only show 2 dns in the error message 
+       */
+      if(count($used)> 2) {
+        $str .=" ... ";
+      }
+      print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
+
+    }else{
+      unset($this->Zones[$id]);
+    }
+  } 
+
+
+  /* This funtion returns all used Zonenames 
+   */
+  function getUsedZoneNames()
   {
+    $ret = array();
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+    $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
+    while($attr = $ldap->fetch()){
+      $ret[$attr['zoneName'][0]][] = $attr['dn'];
+    }
+    return($ret);
   }
 
 
-  /* Check supplied data */
-  function check()
+  /* Remove dns service 
+   */
+  function remove_from_parent()
   {
-    $message= array();
-    return ($message);
+    if(!$this->DNSinitially_was_account){
+      return;
+    }
+    print_red("Can't remove dns yet. returning without remove.");
+    return;
+    $ldap = $this->config->get_ldap_link();
+    $ldap->ls("(&(objectClass=dNSZone)(zoneName=*)(relativeDomainName=@))",$this->orig_dn,array("relativeDomainName","zoneName"));
+    while($attr = $ldap->fetch()){
+      $ldap->cd($attr['dn']);
+      $ldap->rmDir($attr['dn']);
+    }
+    show_ldap_error($ldap->get_error(), _("Removing DNS service failed"));
   }
 
 
   /* Save to LDAP */
   function save()
   {
-  return(true);
-//    plugin::save();
-    foreach($this->Zones as $zone){
-      $tmp = array();
-      if($zone['status'] == "deleted"){
-        echo "deleted";
-      }elseif($zone['status'] == "new" ){
-        echo "new";
-      }elseif($zone['status'] == "edited"){  
-        echo "edit";
-      }else{
-        echo "undefined type";
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);  
+  
+    /* Get differences 
+     */
+    $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$this->orig_dn);
+
+    /* Updated zone entries if reverser or forward name has changed  
+     * Must be done before moving entries, else the given dn is invalid
+     */
+    if(isset($tmp['zoneUpdates'])){
+      foreach($tmp['zoneUpdates'] as $dn => $attrs){
+        $ldap->cd($dn);
+        $ldap->modify($attrs);
+        show_ldap_error("Zone:".$ldap->get_error(), _("Updating DNS service failed"));
       }
-
-      //$tmp = $this->generate_LDAP_entries($tmp,$zone,$delete);
     }
-  }
 
-  function generate_LDAP_entries($tmp,$zone,$delete)
-  {
-    $delete  = array();
-
-    $tmp['objectClass']           = array("top","dNSZone");
-    $tmp['dNSTTL']                = $zone['dNSTTL']; 
-    $tmp['dNSClass']              = $zone['dNSClass']; 
-    $tmp['relativeDomainName']    = $zone['relativeDomainName']; 
-
-    $str = "";
-    foreach($zone['sOARecord'] as $name => $value){
-      $str .= $value." "; 
+    /* Delete dns 
+     */
+    foreach($tmp['del'] as $dn => $del){
+      $ldap->cd($dn);
+      $ldap->rmdir_recursive($dn);
+      show_ldap_error($ldap->get_error(), _("Removing DNS entries failed"));
     }
-    $tmp['sOARecord'] = $str;
-
-    $dn = "zoneName=".$zone['zoneName'].",".$this->dn;
-
-    /* Generate some attrs  */
-    $arr = array("SigRecord","KeyRecord","aAAARecord","nSRecord","iaFSDBRecord","mInfoRecord","hInfoRecord","mXRecord","mDRecord","tXTRecord",
-        "LocRecord","nXTRecord","sRVRecord","nAPTRRecord","kXRecord","certRecord","a6Record","dSRecord","sSHFPRecord","rRSIGRecord","nSECRecord");
-    $aRecords = array();
-    foreach($arr as $ar){
-      foreach($zone['types'] as $type){
-        if(($type['type'] == $ar)&&($type['status']!="deleted")){
-          $tmp[$ar][] = $type['value'];
-        }
-      }
+
+    /* move follwoing entries
+     */
+    foreach($tmp['move'] as $src => $dst){
+      $this->recursive_move($src,$dst);
     }
-    foreach($zone['types'] as $type){
-      if(isset($type['inittype'])){
-        if($type['type'] != $type['inittype']){
-          $tmp[$type['inittype']] = array();
-        }
+
+    /* Add || Update new DNS entries
+     */
+    foreach($tmp['add'] as $dn => $attrs){
+      $ldap->cd($dn);
+      $ldap->cat($dn, array('dn'));
+      if(count($ldap->fetch())){
+        $ldap->cd($dn);
+        $ldap->modify ($attrs);
+      }else{
+        $ldap->cd($dn);
+        $ldap->add($attrs);
       }
+      show_ldap_error($ldap->get_error(), _("Saving DNS entries failed"));
     }
-    return($tmp);
   }
-
-
-
 }
-
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>