Code

Fixed DNs zone editor. Save failed for new entries.
[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("cn");
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 
23   var $cn;
25   function servDNSeditZoneEntries ($config,$dn, $zoneObject)
26   {
27     plugin::plugin ($config, $dn);
29     /* Initialise class
30      */
31     $this->RecordTypes  = getDnsRecordTypes();
32     $this->dn           = "zoneName=".$zoneObject['InitialzoneName'].",".$dn; 
33     $this->zoneName     = strtoupper($this->cn)."/".$zoneObject['InitialzoneName'];
34     $this->reverseName  = strtoupper($this->cn)."/".$zoneObject['InitialReverseZone'];
36     /* Get ldap connection 
37      */
38     $ldap = $this->config->get_ldap_link();
39     $ldap->cd($this->config->current['BASE']);
41     /* Get zone content
42      */
43     $ldap->ls("(&(objectClass=dNSZone)(!(relativeDomainName=@)))",$this->dn,array("relativeDomainName"));
44     
45     while($attrs = $ldap->fetch()){
46       $this->Devices[$attrs['relativeDomainName'][0]] = getDNSHostEntries($config,$attrs['relativeDomainName'][0],true);
47       $this->Devices[$attrs['relativeDomainName'][0]]['OrigCn'] = $attrs['relativeDomainName'][0];
48     }
50     $ldap->cat($this->dn,array("objectClass"));
52     $this->disableDialog = true;
53     if(count($this->Devices)|| $ldap->count()){
54       $this->disableDialog = false;
55     }
56   }
58   function execute()
59   {
60     plugin::execute();
63     /* Check posts for operations ...  
64      */
65     $once = true;
66     foreach($_POST as $name => $value){
68       /* Add a new Record in given object  
69        */
70       
71       $tmp    = preg_replace("/^.*_(.*)_.*$/","\\1",$name);
72       $tmp2   = split("\|",$tmp);
74       /* Add new host entry
75        */
76       if((preg_match("/^UserRecord_?/",$name)) && ($once)){
77         $once = false;
78         $entry = getDNSHostEntries($this->config,"",true);     
79         $entry['exists']    = true;
80         $entry['zoneName']  = $this->zoneName; 
81         $entry['RECORDS'][] = array("type" => "aRecord" , "value"=>"");
82         $this->Devices[_("New entry")] = $entry;
83       }
85       if(count($tmp2) != 2) continue;
87       $Name     = base64_decode($tmp2[0]);
88       $RecordID = $tmp2[1];
90       /* Add new REcord
91        */
92       if((preg_match("/^AddRecord_/",$name)) && ($once)){
93         $once = false;
94         $this->Devices[$Name]['RECORDS'][] = $this->Devices[$Name]['RECORDS'][$RecordID];
95       }
97       /* Remove record from given dn
98        */
99       if((preg_match("/^RemoveRecord_/",$name)) && ($once)){
100         $once   = false;
101         if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
102           unset($this->Devices[$Name]['RECORDS'][$RecordID]);
103         }
104       }
106     }
108     /* Fill templating stuff */
109     $smarty= get_smarty();
110     $display= "";
112     $table = "";
113     foreach($this->Devices as $key => $dev){
114       $table .= $this->generateRecordConfigurationRow($key);
115     }
117     $smarty->assign("disableDialog",$this->disableDialog);
118     $smarty->assign("table",$table);;
119     $display.= $smarty->fetch(get_template_path('servDNSeditZoneEntries.tpl', TRUE));
120     return($display);
121   }
124   function save_object()
125   {
126     /* Possible attributes posted 
127      */
128     foreach($_POST as $name => $value){
130       /* Extract informations out of post name 
131        */
132       $tmp    = preg_replace("/^.*_/","\\1",$name);
133       $tmp2   = split("\|",$tmp);      
134  
135       if(count($tmp2) != 2) continue;
137       $Name     = base64_decode($tmp2[0]);
138       $RecordID = $tmp2[1];
140       /* Check for value change 
141        */          
142       if(preg_match("/ValueSelection_/",$name)){
143         if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
144           $this->Devices[$Name]['RECORDS'][$RecordID]['value'] = $value;
145         }
146       }
148       /* record type changed
149        */        
150       if(preg_match("/^RecordTypeSelection_/",$name)){
151         if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
152           $this->Devices[$Name]['RECORDS'][$RecordID]['type'] = $value;
153         }  
154       }   
155     }
156     
157     /* check for renamed entries 
158      */ 
159     foreach($_POST as $name => $value){
161       /* Extract informations out of post name 
162        */
163       $tmp    = preg_replace("/^.*_/","\\1",$name);
164       $tmp2   = split("\|",$tmp);      
165  
166       if(count($tmp2) != 2) continue;
168       $Name     = base64_decode($tmp2[0]);
169       $RecordID = $tmp2[1];
171       /* Host renamed
172        */
173       if(preg_match("/RenameHost_/",$name)){
174         if((isset($this->Devices[$Name])) && ($Name != $value)){
175     
176           if(isset($this->Devices[$value])){
177             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));
178           }else{
179             $this->Devices[$value] = $this->Devices[$Name];
180             unset($this->Devices[$Name]);
181           }
182         }
183       }
184     }
185   }
188   /*  check something 
189    */
190   function check()
191   {
192     /* Call common method to give check the hook */
193     $message= plugin::check();
194     
195     $ldap = $this->config->get_ldap_link();
196     $ldap->cd($this->config->current['BASE']);
198     $names = array();
200     foreach($this->Devices as $DevName => $device){
202       /* skip checking empty attributes */
203       if(count($this->Devices[$DevName]['RECORDS']) == 0){
204         return;
205       }
207       /* Checking entry name
208        */
209       if((!is_uid($DevName)) | (empty($DevName))){
210         $message[] = sprintf(_("Entry name '%s' contains invalid characters."), $DevName);
211       }      
213       /* Renaming check for existing devices 
214        */
215       if(isset($device['OrigCn'])  && ($DevName != $device['OrigCn'] )){
216         $ldap->cd($this->config->current['BASE']);
217         $ldap->search("(relativeDomainName=".$DevName.")",array("relativeDomainName"));
218         if($ldap->count()){
219           $message[] = sprintf(_("Can not rename '%s' to '%s',the destination name already exists."),$device['OrigCn'],$DevName);
220         }
221       }elseif(!isset($device['OrigCn'])){
222         $ldap->cd($this->config->current['BASE']);
223         $ldap->search("(relativeDomainName=".$DevName.")",array("relativeDomainName"));
224         if($ldap->count()){
225           $message[] = sprintf(_("Can not create '%s',the destination name already exists."),$DevName);
226         }
227       }
229       /* Check names 
230        */
231       if(!isset($names[$DevName])){
232         $names[$DevName] = "";
233       }else{
234         $message[] = sprintf(_("The name '%s' is used more than once."),$DevName);
235       }
237       /* Names should be written in lowercase
238        */
241       if(strtolower($DevName) != $DevName){
242         $message[] = sprintf(_("The host name '%s' should be written in lowercase."), $DevName);
243       }
245       /* Check records
246        */                 
247       $singleEntries = array("cNAMERecord","pTRRecord");
249       $tmp  = array();
250       $tmp2 = array();
251       foreach($device['RECORDS'] as $Num => $Rec){
253         /* Check for multiple use of unique record types
254          */
255         if(in_array($Rec['type'],$singleEntries)){
256           if(!isset($tmp[$Rec['type']])){
257             $tmp[$Rec['type']] = "";
258           }else{
259             $message[] = sprintf(_("The record type '%s' is a unique type and can't be defined twice."),$type);
260           }
261         }
263         /* Check for empty / duplicate entries in record array 
264          */
265         if(empty($Rec['value'])){
266           $message[] = sprintf(_("There is an empty '%s' for host '%s'."),$Rec['type'],$DevName);
267         }
269         /* Check for duplicate record entries 
270          */
271         if(!isset($tmp[$Rec['type']][$Rec['value']])){
272           $tmp[$Rec['type']][$Rec['value']] = "";
273         }else{
274           $message[] = sprintf(_("There is a duplicate entry in '%s' for '%s'."),$Rec['type'],$DevName); 
275         }
276       }
277     }
278     return ($message);
279   }
281   function save()
282   {
283     if($this->disableDialog) return;
285     $todo = array(); 
287     print_a($this->Devices);
289     /* Create todolist
290      */
291     foreach($this->Devices as $name => $dev){
292       if(isset($dev['OrigCn'])){
293         if(count($dev['RECORDS'])){
294           $todo[] = getDNSHostEntriesDiff($this->config,$dev['OrigCn'],$dev,$name);
295         }else{
296           $dev['exists'] = false;
297           $todo[] = getDNSHostEntriesDiff($this->config,$dev['OrigCn'],$dev,$name);
298         }
299       }else{
300         if(count($dev['RECORDS'])){
301           $todo[] = getDNSHostEntriesDiff($this->config,"",$dev,$name);
302         }else{
303           $dev['exists'] = false;
304           $todo[] = getDNSHostEntriesDiff($this->config,"",$dev,$name);
305         }
306       }
307     }
309     $tmp = array();
310     $tmp['del']   = array();
311     $tmp['add']   = array();
312     $tmp['move']  = array();
313     foreach($todo as $to){
314       foreach($to as $type => $entries){
315         $tmp[$type] = array_merge($tmp[$type],$entries);
316       }
317     }
319     /* Get ldap link
320      */
321     $ldap = $this->config->get_ldap_link();
322     $ldap->cd ($this->config->current['BASE']);
324     /* move follwoing entries
325      */
326     foreach($tmp['move'] as $src => $dst){
327       $this->recursive_move($src,$dst);
328     }
330     /* Delete dns */
331     foreach($tmp['del'] as $dn => $del){
332       $ldap->cd($dn);
333       $ldap->rmdir_recursive($dn);
334     }
336     /* Add || Update new DNS entries
337      */
338     foreach($tmp['add'] as $dn => $attrs){
339       $ldap->cd($dn);
340       $ldap->cat($dn, array('dn'));
341       if(count($ldap->fetch())){
342         $ldap->cd($dn);
343         $ldap->modify ($attrs);
344       }else{
345         $ldap->cd($dn);
346         $ldap->add($attrs);
347       }
348     }
349   }
352   /* Create html table out of given entry 
353    */
354   function generateRecordConfigurationRow($objKey){
356     /* Get some basic informations 
357      */
358     $obj        = $this->Devices[$objKey];
359     $objectName = $objKey;
361     /* Abort if emtpy
362      */
363     if(count($obj['RECORDS']) == 0) return "";
365     /* Set title 
366      */
367     $str= "<br>";
369     $hostNameOnce = true;
371     /* Walk through all defined records 
372      */
373     $str.= "<table cellspacing='0' cellpadding='0'>";
374     foreach($obj['RECORDS'] as $id => $record){
376       /* Create unique post name
377        */
378       $name = base64_encode($objKey)."|".$id;
380       $str .= "<tr><td style='width:170px;'>\n";
382       /* Only first host entry name should be editable
383        */
384       if($hostNameOnce){
385         $hostNameOnce = false;  
386         $str .="<input type='text' name='RenameHost_".$name."' value='".$objectName."'>\n";
387       }
389       /* Create rest. Selectbox, icons ...
390        */
391       $str .="
392         </td>
393         <td style='width:90px;'>
394           ".$this->createRecordTypeSelection($record['type'],$name)."
395         </td>
396         <td>
397           <input type='text'  value='".$record['value']."' name='ValueSelection_".$name."' style='width:250px;'>
398         </td>
399         <td style='width:50px;text-align:right;'>
400           <input type='image' name='AddRecord_".$name."'   src='images/list_new.png' alt='"._("Add")."' title='"._("Add")."'>
401           <input type='image' name='RemoveRecord_".$name."' src='images/edittrash.png'      alt='"._("Remove")."' title='"._("Remove")."'>
402         </td>
403       </tr>";
404     }
405     $str .="</table>";
406     return($str); 
407   }
410   /* Create selectbox with all available option types 
411    */
412   function createRecordTypeSelection($id,$refID){
414     $str = "\n<select name='RecordTypeSelection_".$refID."'>";
415     foreach($this->RecordTypes as $type => $atr) {
416       if($id == $type){
417         $str .="\n<option value='".$type."' selected >".strtoupper(preg_replace("/record/i","",$type))."</option>";
418       }else{
419         $str .="\n<option value='".$type."'>".strtoupper(preg_replace("/record/i","",$type))."</option>";
420       }
421     }
422     $str.= "\n</select>";
423     return($str);
424   }
427   function remove_from_parent()
428   {
429   }
433 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
434 ?>