Code

Updated serverService : Set acl base/category for services
[gosa.git] / plugins / admin / systems / class_servDNSeditZoneEntries.inc
index 35b2143c12cb74743152c18e957844b611ad98aa..3a13bde7dbfac17fc1f0b18238cb336979970ad1 100644 (file)
@@ -8,109 +8,102 @@ class servDNSeditZoneEntries 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("zoneName");
-  var $objectclasses= array("whatever");
+  var $ignore_account     = TRUE;
+  var $attributes         = array();
+  var $objectclasses      = array("whatever");
 
-  var $Devices    = array();
-  var $InitDevices = array();
-  var $zoneName       = ""; 
-  var $reverseName    = ""; 
-  var $RecordTypes= array();
+  var $Devices            = array();
 
-  function servDNSeditZoneEntries ($config, $dn= NULL,$zoneName,$reverseName,$RTypes)
+  var $zoneName           = "";   // ZoneName of currently edited Zone
+  var $reverseName        = "";   // ReverseZone of the currently edited Zone
+
+  var $RecordTypes        = array();  // Possible record type.
+
+  var $disableDialog      = false; // Dialog will be disabled, if this zone is new 
+
+  function servDNSeditZoneEntries ($config,$dn, $zoneObject)
   {
     plugin::plugin ($config, $dn);
 
-    $this->zoneName     = $zoneName;
-    $this->reverseName  = $reverseName;
-    $this->RecordTypes  = $RTypes;
-    $this->RecordTypes['cNAMERecord']  = "relativeDomainName";
+    /* Initialise class
+     */
+    $this->RecordTypes  = getDnsRecordTypes();
+    $this->dn           = "zoneName=".$zoneObject['InitialzoneName'].",".$dn; 
+    $this->zoneName     = $zoneObject['InitialzoneName'];
+    $this->reverseName  = $zoneObject['InitialReverseZone'];
 
+    /* Get ldap connection 
+     */
     $ldap = $this->config->get_ldap_link();
     $ldap->cd($this->config->current['BASE']);
 
-    /* Get all objects it this zone ... 
+    /* Get zone content
      */
-    $res = $ldap->search("(&(objectClass=dNSZone)(|(zoneName=".$zoneName.")(zoneName=".$reverseName.".in-addr.arpa))(!(relativeDomainName=@)))");
-    while($attrs = $ldap->fetch($res)){
-      foreach($this->RecordTypes as $atr => $val){
-        if((isset($attrs[$atr])) && (is_array($attrs[$atr]))){
-          unset($attrs[$val]['count']);
-          $dn = preg_replace("/^.*,cn/","cn",$attrs['dn']);
-          $this->Devices[$dn]['RECORDS'][$atr] = $attrs[$val]; 
-        }
-      }
-    }
+    $ldap->ls("(&(objectClass=dNSZone)(!(relativeDomainName=@)))",$this->dn,array("relativeDomainName"));
     
-    /* Add additional informations 
-       to all catched objects 
-     */
-    foreach($this->Devices as $dn => $Recs){
-      $this->Devices[$dn]['OBJECT']  = $ldap->fetch($ldap->cat($dn,array("objectClass","cn")));
+    while($attrs = $ldap->fetch()){
+      $this->Devices[$attrs['relativeDomainName'][0]] = getDNSHostEntries($config,$attrs['relativeDomainName'][0],true);
+      $this->Devices[$attrs['relativeDomainName'][0]]['OrigCn'] = $attrs['relativeDomainName'][0];
     }
 
-    $this->Devices;
-    $this->InitDevices = $this->Devices;
+    $ldap->cat($this->dn,array("objectClass"));
+
+    $this->disableDialog = true;
+    if(count($this->Devices)|| $ldap->count()){
+      $this->disableDialog = false;
+    }
   }
 
   function execute()
   {
-         plugin::execute();
+    plugin::execute();
+
 
     /* Check posts for operations ...  
      */
     $once = true;
     foreach($_POST as $name => $value){
-    
+
       /* Add a new Record in given object  
        */
-      if((preg_match("/^AddRecord_/",$name)) && ($once)){
-        $once   = false;
-        $tmp    = preg_replace("/^AddRecord_/","",$name);
-        $tmp    = preg_replace("/_.*$/","",$tmp);
-        $tmp2   = split("\|",$tmp);
       
-        $dn     = base64_decode($tmp2[0]);
-        $record = $tmp2[1];
-        $numrec = $tmp2[2];        
-     
-        $this->Devices[$dn]['RECORDS'][$record][] = ""; 
+      $tmp    = preg_replace("/^.*_(.*)_.*$/","\\1",$name);
+      $tmp2   = split("\|",$tmp);
+
+      /* Add new host entry
+       */
+      if((preg_match("/^UserRecord_?/",$name)) && ($once)){
+        $once = false;
+        $entry = getDNSHostEntries($this->config,"",true);     
+        $entry['exists']    = true;
+        $entry['zoneName']  = $this->zoneName; 
+        $entry['RECORDS'][] = array("type" => "aRecord" , "value"=>"");
+        $this->Devices[_("New entry")] = $entry;
       }
-      
+
+      if(count($tmp2) != 2) continue;
+
+      $Name     = base64_decode($tmp2[0]);
+      $RecordID = $tmp2[1];
+
+      /* Add new REcord
+       */
+      if((preg_match("/^AddRecord_/",$name)) && ($once)){
+        $once = false;
+        $this->Devices[$Name]['RECORDS'][] = $this->Devices[$Name]['RECORDS'][$RecordID];
+      }
+
       /* Remove record from given dn
        */
       if((preg_match("/^RemoveRecord_/",$name)) && ($once)){
         $once   = false;
-        $tmp    = preg_replace("/^RemoveRecord_/","",$name);
-        $tmp    = preg_replace("/_.*$/","",$tmp);
-        $tmp2   = split("\|",$tmp);
-      
-        $dn     = base64_decode($tmp2[0]);
-        $record = $tmp2[1];
-        $numrec = $tmp2[2];        
-     
-        unset($this->Devices[$dn]['RECORDS'][$record][$numrec]); 
+        if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
+          unset($this->Devices[$Name]['RECORDS'][$RecordID]);
+        }
       }
-    
-      /* Don't know how i should implement this ...
-       */
-      if((preg_match("/^UserRecord_/",$name)) && ($once)){
-        $once   = false;
-        $tmp    = preg_replace("/^UserRecord_/","",$name);
-        $tmp    = preg_replace("/_.*$/","",$tmp);
-        $tmp2   = split("\|",$tmp);
-
-        $dn     = base64_decode($tmp2[0]);
-        $record = $tmp2[1];
-        $numrec = $tmp2[2];
 
-        $this->FreeDevices[$dn]['RECORDS']['User'][$numrec] = ""; 
-      }
     }
 
-
     /* Fill templating stuff */
     $smarty= get_smarty();
     $display= "";
@@ -120,156 +113,195 @@ class servDNSeditZoneEntries extends plugin
       $table .= $this->generateRecordConfigurationRow($key);
     }
 
+    $smarty->assign("disableDialog",$this->disableDialog);
     $smarty->assign("table",$table);;
     $display.= $smarty->fetch(get_template_path('servDNSeditZoneEntries.tpl', TRUE));
     return($display);
   }
 
-  /* Create html table out of given entry 
-   */
-  function generateRecordConfigurationRow($obj_dn){
-
-    /* get some basic inforamtions 
-     */
-    $obj        = $this->Devices[$obj_dn];
-    $objectName = $obj['OBJECT']['cn'][0];
 
-    // set title 
-    $str = "<h3>".sprintf(_("Settings for '%s'"),$obj_dn)." : </h3>";
-
-    /* Walk through all defined records 
+  function save_object()
+  {
+    /* Possible attributes posted 
      */
-    foreach($obj['RECORDS'] as $id => $types){
+    foreach($_POST as $name => $value){
 
-      /* Create table
+      /* Extract informations out of post name 
        */
-      $str.= "<table cellspacing='0' cellpadding='0'>";
-      foreach($types as $key => $type){
+      $tmp    = preg_replace("/^.*_/","\\1",$name);
+      $tmp2   = split("\|",$tmp);      
+      if(count($tmp2) != 2) continue;
 
-        /* Create unique post name
-         */
-        $name = base64_encode($obj_dn)."|".$id."|".$key;
-
-        $str .= "<tr>
-          <td style='width:80px;'>
-            ".$objectName."
-          </td>
-          <td style='width:80px;'>
-            ".$this->createRecordTypeSelection($id,$name)."
-          </td>
-          <td>
-            <input type='text'  value='".$type."' name='ValueSelection_".$name."' style='width:250px;'>
-          </td>
-          <td style='width:30px;text-align:right;'>
-            <input type='image' name='AddRecord_".$name."'   src='images/crossref.png' alt='"._("Add")."' title='"._("Add")."'>
-          </td>
-          <td style='width:60px;text-align:right;'>
-            <input type='image' name='UserRecord_".$name."'   src='images/select_default.png' alt='"._("New")."' title='"._("New")."'>
-            <input type='image' name='RemoveRecord_".$name."' src='images/edittrash.png'      alt='"._("Remove")."' title='"._("Remove")."'>
-          </td>
-         </tr>";
-      }
-      $str .="</table>";
-    }
-    return($str); 
-  }
+      $Name     = base64_decode($tmp2[0]);
+      $RecordID = $tmp2[1];
 
-  /* Create selectbox with all available option types 
-   */
-  function createRecordTypeSelection($id,$refID){
-    $str = "\n<select name='RecordTypeSelection_".$refID."'>";
-    foreach($this->RecordTypes as $type => $atr) {
-      if($id == $type){
-        $str .="\n<option value='".$type."' selected >".strtoupper(preg_replace("/record/i","",$type))."</option>";
-      }else{
-        $str .="\n<option value='".$type."'>".strtoupper(preg_replace("/record/i","",$type))."</option>";
+      /* Check for value change 
+       */          
+      if(preg_match("/ValueSelection_/",$name)){
+        if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
+          $this->Devices[$Name]['RECORDS'][$RecordID]['value'] = $value;
+        }
       }
+
+      /* record type changed
+       */        
+      if(preg_match("/^RecordTypeSelection_/",$name)){
+        if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
+          $this->Devices[$Name]['RECORDS'][$RecordID]['type'] = $value;
+        }  
+      }   
     }
-    $str.= "\n</select>";
-    return($str);
-  }
+    
+    /* check for renamed entries 
+     */ 
+    foreach($_POST as $name => $value){
 
-  function remove_from_parent()
-  {
-  }
+      /* Extract informations out of post name 
+       */
+      $tmp    = preg_replace("/^.*_/","\\1",$name);
+      $tmp2   = split("\|",$tmp);      
+      if(count($tmp2) != 2) continue;
 
-  function save_object()
-  {
-    /* Save all form fields 
-     */
-    
-    /* Possible attributes posted 
-     */
-    $arr = array("RecordTypeSelection_","ValueSelection_");
-   
-    foreach($_POST as $name=>$value){
-      
-      foreach($arr as $type)
+      $Name     = base64_decode($tmp2[0]);
+      $RecordID = $tmp2[1];
+
+      /* Host renamed
+       */
+      if(preg_match("/RenameHost_/",$name)){
+        if((isset($this->Devices[$Name])) && ($Name != $value)){
     
-        /* Check if attribute was posted 
-         */
-        if(preg_match("/".$type."/",$name)){
-        
-          /* Extract informations out of postname 
-           */
-          $action = $type;
-          $tmp    = preg_replace("/^".$action."/","",$name);
-          $tmp    = preg_replace("/_.*$/","",$tmp); 
-          $tmp2   = split("\|",$tmp);      
-
-          $dn     = base64_decode($tmp2[0]);
-          $record = $tmp2[1];
-          $numrec = $tmp2[2]; 
-
-          /* Check which type of post was posted 
-           */
-        
-          // checkbox was posted 
-          if($action == "RecordTypeSelection_"){
-            if($record != $value){ 
-              $str =  $this->Devices[$dn]['RECORDS'][$record][$numrec];
-              unset($this->Devices[$dn]['RECORDS'][$record][$numrec]);
-              $this->Devices[$dn]['RECORDS'][$value][] = $str;
-            }
-          } 
-
-          // entry value was psoted 
-          if($action == "ValueSelection_"){
-            $this->Devices[$dn]['RECORDS'][$record][$numrec] = $value;
+          if(isset($this->Devices[$value])){
+            print_red(sprintf(_("Can't rename '%s' to '%s' there is already an entry with the same name in our zone editing dialog."),$Name,$value));
+          }else{
+            $this->Devices[$value] = $this->Devices[$Name];
+            unset($this->Devices[$Name]);
           }
         }
+      }
     }
   }
 
+
   /*  check something 
    */
   function check()
   {
-    $message= array();
-    // Nothing right now
+    /* Call common method to give check the hook */
+    $message= plugin::check();
+    
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+
+    $names = array();
+    foreach($this->Devices as $DevName => $device){
+
+      /* Checking entry name
+       */
+      if((!is_uid($DevName)) | (empty($DevName))){
+        $message[] = sprintf(_("Entry name '%s' contains invalid characters."), $DevName);
+      }      
+
+      /* Renaming check for existing devices 
+       */
+      if(isset($device['OrigCn'])  && ($DevName != $device['OrigCn'] )){
+        $ldap->cd($this->config->current['BASE']);
+        $ldap->search("(relativeDomainName=".$DevName.")",array("relativeDomainName"));
+        if($ldap->count()){
+          $message[] = sprintf(_("Can not rename '%s' to '%s',the destination name already exists."),$device['OrigCn'],$DevName);
+        }
+      }elseif(!isset($device['OrigCn'])){
+        $ldap->cd($this->config->current['BASE']);
+        $ldap->search("(relativeDomainName=".$DevName.")",array("relativeDomainName"));
+        if($ldap->count()){
+          $message[] = sprintf(_("Can not create '%s',the destination name already exists."),$DevName);
+        }
+      }
+
+      /* Check names 
+       */
+      if(!isset($names[$DevName])){
+        $names[$DevName] = "";
+      }else{
+        $message[] = sprintf(_("The name '%s' is used more than once."),$DevName);
+      }
+
+      /* Names should be written in lowercase
+       */
+      if(strtolower($DevName) != $DevName){
+        $message[] = sprintf(_("The host name '%s' should be written in lowercase."), $DevName);
+      }
+
+      /* Check records
+       */                 
+      $singleEntries = array("cNAMERecord","pTRRecord");
+
+      $tmp  = array();
+      $tmp2 = array();
+      foreach($device['RECORDS'] as $Num => $Rec){
+
+        /* Check for multiple use of unique record types
+         */
+        if(in_array($Rec['type'],$singleEntries)){
+          if(!isset($tmp[$Rec['type']])){
+            $tmp[$Rec['type']] = "";
+          }else{
+            $message[] = sprintf(_("The record type '%s' is a unique type and can't be defined twice."),$type);
+          }
+        }
+
+        /* Check for empty / duplicate entries in record array 
+         */
+        if(empty($Rec['value'])){
+          $message[] = sprintf(_("There is an empty '%s' for host '%s'."),$Rec['type'],$DevName);
+        }
+
+        /* Check for duplicate record entries 
+         */
+        if(!isset($tmp[$Rec['type']][$Rec['value']])){
+          $tmp[$Rec['type']][$Rec['value']] = "";
+        }else{
+          $message[] = sprintf(_("There is a duplicate entry in '%s' for '%s'."),$Rec['type'],$DevName); 
+        }
+      }
+    }
     return ($message);
   }
 
   function save()
   {
-    /* Set all initial records to array()
-       This means they will be removed from the entry
+    if($this->disableDialog) return;
+
+    $todo = array(); 
+
+    /* Create todolist
      */
-    foreach($this->InitDevices as $dn => $obj){
-      $attrs[$dn]= array();
-      foreach($obj['RECORDS'] as $rectype => $records){
-        $attrs[$dn][$rectype] = array();
+    foreach($this->Devices as $name => $dev){
+      if(isset($dev['OrigCn'])){
+        if(count($dev['RECORDS'])){
+          $todo[] = getDNSHostEntriesDiff($this->config,$dev['OrigCn'],$dev,$name);
+        }else{
+          $dev['exists'] = false;
+          $todo[] = getDNSHostEntriesDiff($this->config,$dev['OrigCn'],$dev,$name);
+        }
+      }else{
+        if(count($dev['RECORDS'])){
+          $todo[] = getDNSHostEntriesDiff($this->config,"",$dev,$name);
+        }else{
+          $dev['exists'] = false;
+          $todo[] = getDNSHostEntriesDiff($this->config,"",$dev,$name);
+        }
       }
     }
 
-    /* Add new entries
-     */
-    foreach($this->Devices as $dn => $obj){
-      foreach($obj['RECORDS'] as $rectype => $records){
-        $attrs[$dn][$rectype] = array();
-        foreach($records as $rec){
-          $attrs[$dn][$rectype][] = $rec;
-        } 
+    $tmp = array();
+    $tmp['del']   = array();
+    $tmp['add']   = array();
+    $tmp['move']  = array();
+    foreach($todo as $to){
+      foreach($to as $type => $entries){
+        $tmp[$type] = array_merge($tmp[$type],$entries);
       }
     }
 
@@ -278,107 +310,111 @@ class servDNSeditZoneEntries extends plugin
     $ldap = $this->config->get_ldap_link();
     $ldap->cd ($this->config->current['BASE']);
 
-    /* Generate 'T odo' :-)
+    /* move follwoing entries
      */
-    $tmp = ($this->generate_LDAP_entries($attrs));
+    foreach($tmp['move'] as $src => $dst){
+      $this->recursive_move($src,$dst);
+    }
 
-    /* Delete all objects which should be delted 
-     */
-    foreach($tmp['del'] as $key => $ntr){
-      $ldap->rmdir($key);      
+    /* Delete dns */
+    foreach($tmp['del'] as $dn => $del){
+      $ldap->cd($dn);
+      $ldap->rmdir_recursive($dn);
     }
 
-    /* Add append new / updated entries 
+    /* Add || Update new DNS entries
      */
-    foreach($tmp['addupdate'] as $key => $attrs){
-      $ldap->cat($key);
-      if($ldap->count() > 0){
-        $ldap->cd($key);
-        $ldap->modify($attrs);
+    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($this->config->current['BASE']);
-        $ldap->cd($key);
-        $ldap->add($attrs);        
-      }
-        
-      /* Display errors ...
-       */
-      if($ldap->error != "Success"){
-        echo "<br>".$ldap->error."<br>";
-        print_a($attrs);
+        $ldap->cd($dn);
+        $ldap->add($attrs);
       }
     }
   }
 
 
-  function generate_LDAP_entries($objs)
-  {
-    $entries = array();
-    $delete  = array();    
+  /* Create html table out of given entry 
+   */
+  function generateRecordConfigurationRow($objKey){
 
-    /* these entries are added to base object ... 
+    /* Get some basic informations 
      */
-    $normal = array("mDRecord","aRecord","tXTRecord","mXRecord","hInfoRecord","mInfoRecord","aFSDBRecord","SigRecord","KeyRecord","aAAARecord","nSRecord",
-        "LocRecord","nXTRecord","sRVRecord","nAPTRRecord","kXRecord","certRecord","a6Record","dSRecord","sSHFPRecord","rRSIGRecord","nSECRecord");
+    $obj        = $this->Devices[$objKey];
+    $objectName = $objKey;
 
-    /* Create t odo
+    /* Abort if emtpy
      */
-    foreach($objs as $dn => $recs){
-      /* Get basic info
-       */ 
-      $obj        = $this->Devices[$dn];
-      $objectName = $obj['OBJECT']['cn'][0];
+    if(count($obj['RECORDS']) == 0) return "";
+
+    /* Set title 
+     */
+    $str= "<br>";
+
+    $hostNameOnce = true;
 
-      /* Calculate records ... 
+    /* Walk through all defined records 
+     */
+    $str.= "<table cellspacing='0' cellpadding='0'>";
+    foreach($obj['RECORDS'] as $id => $record){
+
+      /* Create unique post name
        */
-      foreach($recs as $type =>  $rec){
-      
-        /*  Simply add normal entries 
-         */
-        if(in_array($type,$normal)){
-          $entries['relativeDomainName='.$objectName.','.$dn][$type] = $rec ;
-        }
+      $name = base64_encode($objKey)."|".$id;
 
-        /*  Special handling for cNAMERecords 
-         */
-        if($type == "cNAMERecord"){
-          if(isset($this->InitDevices[$dn]['RECORDS']['cNAMERecord'])){
-            foreach($this->InitDevices[$dn]['RECORDS']['cNAMERecord'] as $warmal){
-              $delete['relativeDomainName='.$warmal.','.$dn] = "";
-            }
-          }
-          foreach($rec as $r){
-            if(!empty($r)){
-            $entries['relativeDomainName='.$r.','.$dn]['cNAMERecord']        = $objectName;
-            $entries['relativeDomainName='.$r.','.$dn]['relativeDomainName'] = $r;
-            $entries['relativeDomainName='.$r.','.$dn]['objectClass']        = array("top","dNSZone");
-            $entries['relativeDomainName='.$r.','.$dn]['zoneName']           = $this->zoneName;
-            }
-          }
-        }
+      $str .= "<tr><td style='width:170px;'>\n";
 
-        /* Special handling for ptrrecord
-         */ 
-        if($type == "pTRRecord"){
-          if(isset($this->InitDevices[$dn]['RECORDS']['pTRRecord'])){
-            foreach($this->InitDevices[$dn]['RECORDS']['pTRRecord'] as $warmal){
-              $delete['relativeDomainName='.$warmal.','.$dn] = "";
-            }
-          }
-          foreach($rec as $r){
-            if(!empty($r)){
-              $entries['relativeDomainName='.$r.','.$dn]['pTRRecord']          = $objectName;
-              $entries['relativeDomainName='.$r.','.$dn]['zoneName']           = $this->reverseName.".in-addr.arpa";
-              $entries['relativeDomainName='.$r.','.$dn]['relativeDomainName'] = $r;
-              $entries['relativeDomainName='.$r.','.$dn]['objectClass']        = array("top","dNSZone");
-            }
-          }
-        } 
+      /* Only first host entry name should be editable
+       */
+      if($hostNameOnce){
+        $hostNameOnce = false;  
+        $str .="<input type='text' name='RenameHost_".$name."' value='".$objectName."'>\n";
       }
+
+      /* Create rest. Selectbox, icons ...
+       */
+      $str .="
+        </td>
+        <td style='width:90px;'>
+          ".$this->createRecordTypeSelection($record['type'],$name)."
+        </td>
+        <td>
+          <input type='text'  value='".$record['value']."' name='ValueSelection_".$name."' style='width:250px;'>
+        </td>
+        <td style='width:50px;text-align:right;'>
+          <input type='image' name='AddRecord_".$name."'   src='images/list_new.png' alt='"._("Add")."' title='"._("Add")."'>
+          <input type='image' name='RemoveRecord_".$name."' src='images/edittrash.png'      alt='"._("Remove")."' title='"._("Remove")."'>
+        </td>
+      </tr>";
     }
+    $str .="</table>";
+    return($str); 
+  }
+
+
+  /* Create selectbox with all available option types 
+   */
+  function createRecordTypeSelection($id,$refID){
 
-    return(array("del"=> $delete , "addupdate"=> $entries));
+    $str = "\n<select name='RecordTypeSelection_".$refID."'>";
+    foreach($this->RecordTypes as $type => $atr) {
+      if($id == $type){
+        $str .="\n<option value='".$type."' selected >".strtoupper(preg_replace("/record/i","",$type))."</option>";
+      }else{
+        $str .="\n<option value='".$type."'>".strtoupper(preg_replace("/record/i","",$type))."</option>";
+      }
+    }
+    $str.= "\n</select>";
+    return($str);
+  }
+
+
+  function remove_from_parent()
+  {
   }
 
 }