Code

7a2e57a45d817e6106ec421c3c040efad07d80a3
[gosa.git] / plugins / admin / systems / class_servDNSeditZoneEntries.inc
1 <?php
3 class servDNSeditZoneEntries extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage server basic objects";
7   var $cli_description= "Some longer text\nfor help";
8   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account     = TRUE;
12   var $attributes         = array();
13   var $objectclasses      = array("whatever");
15   var $Devices            = array();
17   var $zoneName           = "";   // ZoneName of currently edited Zone
18   var $reverseName        = "";   // ReverseZone of the currently edited Zone
20   var $RecordTypes        = array();  // Possible record type.
22   var $disableDialog      = false; // Dialog will be disabled, if this zone is new 
24   function servDNSeditZoneEntries ($config,$dn, $zoneObject)
25   {
26     plugin::plugin ($config, $dn);
28     /* Initialise class
29      */
30     $this->RecordTypes  = getDnsRecordTypes();
31     $this->dn           = "zoneName=".$zoneObject['InitialzoneName'].",".$dn; 
32     $this->zoneName     = $zoneObject['InitialzoneName'];
33     $this->reverseName  = $zoneObject['InitialReverseZone'];
35     /* Get ldap connection 
36      */
37     $ldap = $this->config->get_ldap_link();
38     $ldap->cd($this->config->current['BASE']);
40     /* Get zone content
41      */
42     $ldap->ls("(&(objectClass=dNSZone)(!(relativeDomainName=@)))",$this->dn,array("relativeDomainName"));
43     
44     while($attrs = $ldap->fetch()){
45       $this->Devices[$attrs['relativeDomainName'][0]] = getDNSHostEntries($config,$attrs['relativeDomainName'][0],true);
46       $this->Devices[$attrs['relativeDomainName'][0]]['OrigCn'] = $attrs['relativeDomainName'][0];
47     }
49     if(!count($this->Devices)){
50       $this->disableDialog = true;
51     }
52   }
54   function execute()
55   {
56     plugin::execute();
59     /* Check posts for operations ...  
60      */
61     $once = true;
62     foreach($_POST as $name => $value){
64       /* Add a new Record in given object  
65        */
66       
67       $tmp    = preg_replace("/^.*_(.*)_.*$/","\\1",$name);
68       $tmp2   = split("\|",$tmp);
70       /* Add new host entry
71        */
72       if((preg_match("/^UserRecord_/",$name)) && ($once)){
73         $once = false;
74         $entry = getDNSHostEntries($this->config,"",true);     
75         $entry['exists']    = true;
76         $entry['zoneName']  = $this->zoneName; 
77         $entry['RECORDS'][] = array("type" => "aRecord" , "value"=>"");
78         $this->Devices['New Entry'] = $entry;
79       }
81       if(count($tmp2) != 2) continue;
83       $Name     = base64_decode($tmp2[0]);
84       $RecordID = $tmp2[1];
86       /* Add new REcord
87        */
88       if((preg_match("/^AddRecord_/",$name)) && ($once)){
89         $once = false;
90         $this->Devices[$Name]['RECORDS'][] = $this->Devices[$Name]['RECORDS'][$RecordID];
91       }
93       /* Remove record from given dn
94        */
95       if((preg_match("/^RemoveRecord_/",$name)) && ($once)){
96         $once   = false;
97         if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
98           unset($this->Devices[$Name]['RECORDS'][$RecordID]);
99         }
100       }
102     }
104     /* Fill templating stuff */
105     $smarty= get_smarty();
106     $display= "";
108     $table = "";
109     foreach($this->Devices as $key => $dev){
110       $table .= $this->generateRecordConfigurationRow($key);
111     }
113     $smarty->assign("disableDialog",$this->disableDialog);
114     $smarty->assign("table",$table);;
115     $display.= $smarty->fetch(get_template_path('servDNSeditZoneEntries.tpl', TRUE));
116     return($display);
117   }
120   function save_object()
121   {
122     /* Possible attributes posted 
123      */
124     foreach($_POST as $name => $value){
126       /* Extract informations out of post name 
127        */
128       $tmp    = preg_replace("/^.*_/","\\1",$name);
129       $tmp2   = split("\|",$tmp);      
130  
131       if(count($tmp2) != 2) continue;
133       $Name     = base64_decode($tmp2[0]);
134       $RecordID = $tmp2[1];
136       /* Check for value change 
137        */          
138       if(preg_match("/ValueSelection_/",$name)){
139         if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
140           $this->Devices[$Name]['RECORDS'][$RecordID]['value'] = $value;
141         }
142       }
144       /* record type changed
145        */        
146       if(preg_match("/^RecordTypeSelection_/",$name)){
147         if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
148           $this->Devices[$Name]['RECORDS'][$RecordID]['type'] = $value;
149         }  
150       }   
151     }
152     
153     /* check for renamed entries 
154      */ 
155     foreach($_POST as $name => $value){
157       /* Extract informations out of post name 
158        */
159       $tmp    = preg_replace("/^.*_/","\\1",$name);
160       $tmp2   = split("\|",$tmp);      
161  
162       if(count($tmp2) != 2) continue;
164       $Name     = base64_decode($tmp2[0]);
165       $RecordID = $tmp2[1];
167       /* Host renamed
168        */
169       if(preg_match("/RenameHost_/",$name)){
170         if((isset($this->Devices[$Name])) && ($Name != $value)){
171     
172           if(isset($this->Devices[$value])){
173             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));
174           }else{
175             $this->Devices[$value] = $this->Devices[$Name];
176             unset($this->Devices[$Name]);
177           }
178         }
179       }
180     }
181   }
184   /*  check something 
185    */
186   function check()
187   {
188     $message= array();
189     
190     $ldap = $this->config->get_ldap_link();
191     $ldap->cd($this->config->current['BASE']);
193     $names = array();
194     foreach($this->Devices as $DevName => $device){
196       /* Checking entry name
197        */
198       if((!is_uid($DevName)) | (empty($DevName))){
199         $message[] = sprintf(_("Entry name '%s' contains invalid characters."), $DevName);
200       }      
202       /* Renaming check for existing devices 
203        */
204       if(isset($device['OrigCn'])  && ($DevName != $device['OrigCn'] )){
205         $ldap->cd($this->config->current['BASE']);
206         $ldap->search("(relativeDomainName=".$DevName.")",array("relativeDomainName"));
207         if($ldap->count()){
208           $message[] = sprintf(_("Can not rename '%s' to '%s',the destination name already exists."),$device['OrigCn'],$DevName);
209         }
210       }elseif(!isset($device['OrigCn'])){
211         $ldap->cd($this->config->current['BASE']);
212         $ldap->search("(relativeDomainName=".$DevName.")",array("relativeDomainName"));
213         if($ldap->count()){
214           $message[] = sprintf(_("Can not create '%s',the destination name already exists."),$DevName);
215         }
216       }
218       /* Check names 
219        */
220       if(!isset($names[$DevName])){
221         $names[$DevName] = "";
222       }else{
223         $message[] = sprintf(_("The name '%s' is used more than once."),$DevName);
224       }
226       /* Names should be written in lowercase
227        */
228       if(strtolower($DevName) != $DevName){
229         $message[] = sprintf(_("The host name '%s' should be written in lowercase."), $DevName);
230       }
232       /* Check records
233        */                 
234       $singleEntries = array("cNAMERecord","pTRRecord");
236       $tmp  = array();
237       $tmp2 = array();
238       foreach($device['RECORDS'] as $Num => $Rec){
240         /* Check for multiple use of unique record types
241          */
242         if(in_array($Rec['type'],$singleEntries)){
243           if(!isset($tmp[$Rec['type']])){
244             $tmp[$Rec['type']] = "";
245           }else{
246             $message[] = sprintf(_("The record type '%s' is a unique type and can't be defined twice."),$type);
247           }
248         }
250         /* Check for empty / duplicate entries in record array 
251          */
252         if(empty($Rec['value'])){
253           $message[] = sprintf(_("There is an empty '%s' for host '%s'."),$Rec['type'],$DevName);
254         }
256         /* Check for duplicate record entries 
257          */
258         if(!isset($tmp[$Rec['type']][$Rec['value']])){
259           $tmp[$Rec['type']][$Rec['value']] = "";
260         }else{
261           $message[] = sprintf(_("There is a duplicate entry in '%s' for '%s'."),$Rec['type'],$DevName); 
262         }
263       }
264     }
265     return ($message);
266   }
268   function save()
269   {
270     if($this->disableDialog) return;
272     $todo = array(); 
274     /* Create todolist
275      */
276     foreach($this->Devices as $name => $dev){
277       if(isset($dev['OrigCn'])){
278         if(count($dev['RECORDS'])){
279           $todo[] = getDNSHostEntriesDiff($this->config,$dev['OrigCn'],$dev,$name);
280         }else{
281           $dev['exists'] = false;
282           $todo[] = getDNSHostEntriesDiff($this->config,$dev['OrigCn'],$dev,$name);
283         }
284       }else{
285         if(count($dev['RECORDS'])){
286           $todo[] = getDNSHostEntriesDiff($this->config,"",$dev,$name);
287         }else{
288           $dev['exists'] = false;
289           $todo[] = getDNSHostEntriesDiff($this->config,"",$dev,$name);
290         }
291       }
292     }
294     $tmp = array();
295     $tmp['del']   = array();
296     $tmp['add']   = array();
297     $tmp['move']  = array();
298     foreach($todo as $to){
299       foreach($to as $type => $entries){
300         $tmp[$type] = array_merge($tmp[$type],$entries);
301       }
302     }
304     /* Get ldap link
305      */
306     $ldap = $this->config->get_ldap_link();
307     $ldap->cd ($this->config->current['BASE']);
309     /* move follwoing entries
310      */
311     foreach($tmp['move'] as $src => $dst){
312       $this->recursive_move($src,$dst);
313     }
315     /* Delete dns */
316     foreach($tmp['del'] as $dn => $del){
317       $ldap->cd($dn);
318       $ldap->rmdir_recursive($dn);
319     }
321     /* Add || Update new DNS entries
322      */
323     foreach($tmp['add'] as $dn => $attrs){
324       $ldap->cd($dn);
325       $ldap->cat($dn);
326       if(count($ldap->fetch())){
327         $ldap->cd($dn);
328         $ldap->modify ($attrs);
329       }else{
330         $ldap->cd($dn);
331         $ldap->add($attrs);
332       }
333     }
334   }
337   /* Create html table out of given entry 
338    */
339   function generateRecordConfigurationRow($objKey){
341     /* Get some basic informations 
342      */
343     $obj        = $this->Devices[$objKey];
344     $objectName = $objKey;
346     /* Abort if emtpy
347      */
348     if(count($obj['RECORDS']) == 0) return "";
350     /* Set title 
351      */
352     $str = "<h3>".sprintf(_("Settings for '%s'"),$objKey)." : </h3>";
354     $hostNameOnce = true;
356     /* Walk through all defined records 
357      */
358     $str.= "<table cellspacing='0' cellpadding='0'>";
359     foreach($obj['RECORDS'] as $id => $record){
361       /* Create unique post name
362        */
363       $name = base64_encode($objKey)."|".$id;
365       $str .= "<tr><td style='width:80px;'>\n";
367       /* Only first host entry name should be editable
368        */
369       if($hostNameOnce){
370         $hostNameOnce = false;  
371         $str .="<input type='text' name='RenameHost_".$name."' value='".$objectName."'>\n";
372       }else{
373         $str .=$objectName;
374       }  
376       /* Create rest. Selectbox, icons ...
377        */
378       $str .="
379         </td>
380         <td style='width:80px;'>
381           ".$this->createRecordTypeSelection($record['type'],$name)."
382         </td>
383         <td>
384           <input type='text'  value='".$record['value']."' name='ValueSelection_".$name."' style='width:250px;'>
385         </td>
386         <td style='width:50px;text-align:right;'>
387           <input type='image' name='AddRecord_".$name."'   src='images/crossref.png' alt='"._("Add")."' title='"._("Add")."'>
388           <input type='image' name='RemoveRecord_".$name."' src='images/edittrash.png'      alt='"._("Remove")."' title='"._("Remove")."'>
389         </td>
390       </tr>";
391     }
392     $str .="</table>";
393     return($str); 
394   }
397   /* Create selectbox with all available option types 
398    */
399   function createRecordTypeSelection($id,$refID){
401     $str = "\n<select name='RecordTypeSelection_".$refID."'>";
402     foreach($this->RecordTypes as $type => $atr) {
403       if($id == $type){
404         $str .="\n<option value='".$type."' selected >".strtoupper(preg_replace("/record/i","",$type))."</option>";
405       }else{
406         $str .="\n<option value='".$type."'>".strtoupper(preg_replace("/record/i","",$type))."</option>";
407       }
408     }
409     $str.= "\n</select>";
410     return($str);
411   }
414   function remove_from_parent()
415   {
416   }
420 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
421 ?>