Code

Fixed acls for system management
[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     $ldap->cat($this->dn,array("objectClass"));
51     $this->disableDialog = true;
52     if(count($this->Devices)|| $ldap->count()){
53       $this->disableDialog = false;
54     }
55   }
57   function execute()
58   {
59     plugin::execute();
62     /* Check posts for operations ...  
63      */
64     $once = true;
65     foreach($_POST as $name => $value){
67       /* Add a new Record in given object  
68        */
69       
70       $tmp    = preg_replace("/^.*_(.*)_.*$/","\\1",$name);
71       $tmp2   = split("\|",$tmp);
73       /* Add new host entry
74        */
75       if((preg_match("/^UserRecord_?/",$name)) && ($once)){
76         $once = false;
77         $entry = getDNSHostEntries($this->config,"",true);     
78         $entry['exists']    = true;
79         $entry['zoneName']  = $this->zoneName; 
80         $entry['RECORDS'][] = array("type" => "aRecord" , "value"=>"");
81         $this->Devices[_("New entry")] = $entry;
82       }
84       if(count($tmp2) != 2) continue;
86       $Name     = base64_decode($tmp2[0]);
87       $RecordID = $tmp2[1];
89       /* Add new REcord
90        */
91       if((preg_match("/^AddRecord_/",$name)) && ($once)){
92         $once = false;
93         $this->Devices[$Name]['RECORDS'][] = $this->Devices[$Name]['RECORDS'][$RecordID];
94       }
96       /* Remove record from given dn
97        */
98       if((preg_match("/^RemoveRecord_/",$name)) && ($once)){
99         $once   = false;
100         if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
101           unset($this->Devices[$Name]['RECORDS'][$RecordID]);
102         }
103       }
105     }
107     /* Fill templating stuff */
108     $smarty= get_smarty();
109     $display= "";
111     $table = "";
112     foreach($this->Devices as $key => $dev){
113       $table .= $this->generateRecordConfigurationRow($key);
114     }
116     $smarty->assign("disableDialog",$this->disableDialog);
117     $smarty->assign("table",$table);;
118     $display.= $smarty->fetch(get_template_path('servDNSeditZoneEntries.tpl', TRUE));
119     return($display);
120   }
123   function save_object()
124   {
125     /* Possible attributes posted 
126      */
127     foreach($_POST as $name => $value){
129       /* Extract informations out of post name 
130        */
131       $tmp    = preg_replace("/^.*_/","\\1",$name);
132       $tmp2   = split("\|",$tmp);      
133  
134       if(count($tmp2) != 2) continue;
136       $Name     = base64_decode($tmp2[0]);
137       $RecordID = $tmp2[1];
139       /* Check for value change 
140        */          
141       if(preg_match("/ValueSelection_/",$name)){
142         if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
143           $this->Devices[$Name]['RECORDS'][$RecordID]['value'] = $value;
144         }
145       }
147       /* record type changed
148        */        
149       if(preg_match("/^RecordTypeSelection_/",$name)){
150         if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
151           $this->Devices[$Name]['RECORDS'][$RecordID]['type'] = $value;
152         }  
153       }   
154     }
155     
156     /* check for renamed entries 
157      */ 
158     foreach($_POST as $name => $value){
160       /* Extract informations out of post name 
161        */
162       $tmp    = preg_replace("/^.*_/","\\1",$name);
163       $tmp2   = split("\|",$tmp);      
164  
165       if(count($tmp2) != 2) continue;
167       $Name     = base64_decode($tmp2[0]);
168       $RecordID = $tmp2[1];
170       /* Host renamed
171        */
172       if(preg_match("/RenameHost_/",$name)){
173         if((isset($this->Devices[$Name])) && ($Name != $value)){
174     
175           if(isset($this->Devices[$value])){
176             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));
177           }else{
178             $this->Devices[$value] = $this->Devices[$Name];
179             unset($this->Devices[$Name]);
180           }
181         }
182       }
183     }
184   }
187   /*  check something 
188    */
189   function check()
190   {
191     /* Call common method to give check the hook */
192     $message= plugin::check();
193     
194     $ldap = $this->config->get_ldap_link();
195     $ldap->cd($this->config->current['BASE']);
197     $names = array();
198     foreach($this->Devices as $DevName => $device){
200       /* Checking entry name
201        */
202       if((!is_uid($DevName)) | (empty($DevName))){
203         $message[] = sprintf(_("Entry name '%s' contains invalid characters."), $DevName);
204       }      
206       /* Renaming check for existing devices 
207        */
208       if(isset($device['OrigCn'])  && ($DevName != $device['OrigCn'] )){
209         $ldap->cd($this->config->current['BASE']);
210         $ldap->search("(relativeDomainName=".$DevName.")",array("relativeDomainName"));
211         if($ldap->count()){
212           $message[] = sprintf(_("Can not rename '%s' to '%s',the destination name already exists."),$device['OrigCn'],$DevName);
213         }
214       }elseif(!isset($device['OrigCn'])){
215         $ldap->cd($this->config->current['BASE']);
216         $ldap->search("(relativeDomainName=".$DevName.")",array("relativeDomainName"));
217         if($ldap->count()){
218           $message[] = sprintf(_("Can not create '%s',the destination name already exists."),$DevName);
219         }
220       }
222       /* Check names 
223        */
224       if(!isset($names[$DevName])){
225         $names[$DevName] = "";
226       }else{
227         $message[] = sprintf(_("The name '%s' is used more than once."),$DevName);
228       }
230       /* Names should be written in lowercase
231        */
232       if(strtolower($DevName) != $DevName){
233         $message[] = sprintf(_("The host name '%s' should be written in lowercase."), $DevName);
234       }
236       /* Check records
237        */                 
238       $singleEntries = array("cNAMERecord","pTRRecord");
240       $tmp  = array();
241       $tmp2 = array();
242       foreach($device['RECORDS'] as $Num => $Rec){
244         /* Check for multiple use of unique record types
245          */
246         if(in_array($Rec['type'],$singleEntries)){
247           if(!isset($tmp[$Rec['type']])){
248             $tmp[$Rec['type']] = "";
249           }else{
250             $message[] = sprintf(_("The record type '%s' is a unique type and can't be defined twice."),$type);
251           }
252         }
254         /* Check for empty / duplicate entries in record array 
255          */
256         if(empty($Rec['value'])){
257           $message[] = sprintf(_("There is an empty '%s' for host '%s'."),$Rec['type'],$DevName);
258         }
260         /* Check for duplicate record entries 
261          */
262         if(!isset($tmp[$Rec['type']][$Rec['value']])){
263           $tmp[$Rec['type']][$Rec['value']] = "";
264         }else{
265           $message[] = sprintf(_("There is a duplicate entry in '%s' for '%s'."),$Rec['type'],$DevName); 
266         }
267       }
268     }
269     return ($message);
270   }
272   function save()
273   {
274     if($this->disableDialog) return;
276     $todo = array(); 
278     /* Create todolist
279      */
280     foreach($this->Devices as $name => $dev){
281       if(isset($dev['OrigCn'])){
282         if(count($dev['RECORDS'])){
283           $todo[] = getDNSHostEntriesDiff($this->config,$dev['OrigCn'],$dev,$name);
284         }else{
285           $dev['exists'] = false;
286           $todo[] = getDNSHostEntriesDiff($this->config,$dev['OrigCn'],$dev,$name);
287         }
288       }else{
289         if(count($dev['RECORDS'])){
290           $todo[] = getDNSHostEntriesDiff($this->config,"",$dev,$name);
291         }else{
292           $dev['exists'] = false;
293           $todo[] = getDNSHostEntriesDiff($this->config,"",$dev,$name);
294         }
295       }
296     }
298     $tmp = array();
299     $tmp['del']   = array();
300     $tmp['add']   = array();
301     $tmp['move']  = array();
302     foreach($todo as $to){
303       foreach($to as $type => $entries){
304         $tmp[$type] = array_merge($tmp[$type],$entries);
305       }
306     }
308     /* Get ldap link
309      */
310     $ldap = $this->config->get_ldap_link();
311     $ldap->cd ($this->config->current['BASE']);
313     /* move follwoing entries
314      */
315     foreach($tmp['move'] as $src => $dst){
316       $this->recursive_move($src,$dst);
317     }
319     /* Delete dns */
320     foreach($tmp['del'] as $dn => $del){
321       $ldap->cd($dn);
322       $ldap->rmdir_recursive($dn);
323     }
325     /* Add || Update new DNS entries
326      */
327     foreach($tmp['add'] as $dn => $attrs){
328       $ldap->cd($dn);
329       $ldap->cat($dn, array('dn'));
330       if(count($ldap->fetch())){
331         $ldap->cd($dn);
332         $ldap->modify ($attrs);
333       }else{
334         $ldap->cd($dn);
335         $ldap->add($attrs);
336       }
337     }
338   }
341   /* Create html table out of given entry 
342    */
343   function generateRecordConfigurationRow($objKey){
345     /* Get some basic informations 
346      */
347     $obj        = $this->Devices[$objKey];
348     $objectName = $objKey;
350     /* Abort if emtpy
351      */
352     if(count($obj['RECORDS']) == 0) return "";
354     /* Set title 
355      */
356     $str= "<br>";
358     $hostNameOnce = true;
360     /* Walk through all defined records 
361      */
362     $str.= "<table cellspacing='0' cellpadding='0'>";
363     foreach($obj['RECORDS'] as $id => $record){
365       /* Create unique post name
366        */
367       $name = base64_encode($objKey)."|".$id;
369       $str .= "<tr><td style='width:170px;'>\n";
371       /* Only first host entry name should be editable
372        */
373       if($hostNameOnce){
374         $hostNameOnce = false;  
375         $str .="<input type='text' name='RenameHost_".$name."' value='".$objectName."'>\n";
376       }
378       /* Create rest. Selectbox, icons ...
379        */
380       $str .="
381         </td>
382         <td style='width:90px;'>
383           ".$this->createRecordTypeSelection($record['type'],$name)."
384         </td>
385         <td>
386           <input type='text'  value='".$record['value']."' name='ValueSelection_".$name."' style='width:250px;'>
387         </td>
388         <td style='width:50px;text-align:right;'>
389           <input type='image' name='AddRecord_".$name."'   src='images/list_new.png' alt='"._("Add")."' title='"._("Add")."'>
390           <input type='image' name='RemoveRecord_".$name."' src='images/edittrash.png'      alt='"._("Remove")."' title='"._("Remove")."'>
391         </td>
392       </tr>";
393     }
394     $str .="</table>";
395     return($str); 
396   }
399   /* Create selectbox with all available option types 
400    */
401   function createRecordTypeSelection($id,$refID){
403     $str = "\n<select name='RecordTypeSelection_".$refID."'>";
404     foreach($this->RecordTypes as $type => $atr) {
405       if($id == $type){
406         $str .="\n<option value='".$type."' selected >".strtoupper(preg_replace("/record/i","",$type))."</option>";
407       }else{
408         $str .="\n<option value='".$type."'>".strtoupper(preg_replace("/record/i","",$type))."</option>";
409       }
410     }
411     $str.= "\n</select>";
412     return($str);
413   }
416   function remove_from_parent()
417   {
418   }
422 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
423 ?>