Code

a500ce84b9df35f64b12e6bc54d4af218ea0dde6
[gosa.git] / plugins / admin / systems / class_servDNS.inc
1 <?php
3 class servdns 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   = FALSE;
12   var $attributes       = array(); 
13   var $objectclasses    = array("whatever");
15   var $RecordTypes      = array();
16   var $Zones            = array();
17   var $dialog           = NULL;
19   var $orig_dn          = "";
21   var $DNSinitially_was_account;
24   function servdns ($config, $dn= NULL, $parent= NULL)
25   {
26     plugin::plugin ($config, $dn, $parent);
28     $this->orig_dn = $dn;
30     /* Get record types for zones
31      */
32     $this->RecordTypes = getDnsRecordTypes(true);
34     /* Get all zone Informations
35      */
36     $this->Zones = getDNSZoneEntries($config,$dn);
37   
38     /* If there is at least one entry in this -> types, we have DNS enabled 
39      */
40     if(count($this->Zones) == 0){
41       $this->is_account = false;
42     }else{
43       $this->is_account = true;
44     }
45     $this->DNSinitially_was_account = $this->is_account;
46   }
49   function execute()
50   {
51     /* Call parent execute 
52      */
53     plugin::execute();
55     /* Fill templating stuff 
56      */
57     $smarty= get_smarty();
58     $display= "";
60     /* Do we need to flip is_account state? 
61      */
62     if (isset($_POST['modify_state'])){
64       /* Only change account state if allowed */
65       if($this->is_account && $this->acl == "#all#"){
66         $this->is_account= !$this->is_account;
67         $this->is_modified = true;
68       }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
69         $this->is_account= !$this->is_account;
70         $this->is_modified = true;
71       }
72     }
74     if ($this->is_account){
75       $display= $this->show_header(_("Remove DNS service"),
76           _("This server has DNS features enabled. You can disable them by clicking below."));
77     } else {
78       $display= $this->show_header(_("Add DNS service"),
79           _("This server has DNS features disabled. You can enable them by clicking below."));
80       return ($display);
81     }
84     /* Edited or Added zone 
85      */
86     if((isset($_POST['SaveZoneChanges'])) && is_object($this->dialog)){
87       $this->dialog->save_object();
89       /* Check for errors  
90        */
91       if(count($this->dialog->check())){
92         foreach($this->dialog->check() as $msgs){
93           print_red($msgs); 
94         }
95       }else{
96         /* add new/edited zone 
97          */
98         $ret = $this->dialog->save();
99         if(!$this->dialog->isNew){
100           unset($this->Zones[$this->dialog->OldZoneName]);
101         }
102         $this->Zones[$ret['zoneName']] = $ret;
103         $this->dialog = NULL;
104       }
105     }
107     /* Cancel zone edit / new 
108      */
109     if(isset($_POST['CancelZoneChanges'])){
110       $this->dialog = NULL;
111     }
113     /* Add empty new zone 
114      */
115     if(isset($_POST['AddZone']) && chkacl($this->acl,"servdns") == ""){
116       $this->dialog = new servdnseditZone($this->config,$this->dn);
117     }
119     /* Check for edit zone request 
120      */
121     $once = false;
122     foreach( $_POST as $name => $value){
123   
124       /* check all post for edit request 
125        */
126       if(preg_match("/^editZone_/",$name)&&!$once && chkacl($this->acl,"servdns") == ""){
127         $once =true;
128         $tmp = preg_replace("/^editZone_/","",$name);
129         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
130         $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$tmp]);
131       }
133       /* check posts for delete zone 
134        */
135       if(preg_match("/^delZone_/",$name)&&!$once && chkacl($this->acl,"servdns") == ""){
137         $once =true;
138         $tmp = preg_replace("/^delZone_/","",$name);
139         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
140      
141         /* Initiate deletion
142          */ 
143         $this->RemoveZone($tmp); 
144       }
145     }
147     /* Show dialog 
148      */
149     if($this->dialog!= NULL){
150       $this->dialog->save_object();
151       $this->dialog->parent = $this;
152       return($this->dialog->execute());
153     }
155     /* Create Listbox with existing Zones 
156      */
157     $ZoneList = new divSelectBox("dNSZones");
158     $ZoneList -> SetHeight(254);
160     /* Add entries to divlist
161      */
162     $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
163       <input type='image' src='images/edittrash.png' name='delZone_%s'>";
164     foreach($this->Zones as $zone => $values ){
165       $ZoneList->AddEntry(array(
166             array("string" => getNameFromMix($zone)),
167             array("string" => _("Reverse zone")." : ".getNameFromMix($values['ReverseZone'])),
168             array("string" => _("TTL")." : ".$values['sOAttl']),
169             array("string" => _("Class")." : ".$values['dNSClass']),
170             array("string" => str_replace("%s",base64_encode($zone),$editImg))
171             ));
172     }    
173   
174     $smarty->assign("servdnsACL",chkacl($this->acl,"servdns"));
175   
176     /* Display tempalte 
177      */
178     $smarty->assign("ZoneList",$ZoneList->DrawList());
179     $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
180     return($display);
181   }
184   /* Delete specified zone
185    */
186   function RemoveZone($id)
187   {
188     $zones =  $this->getUsedZoneNames();
189     $rev  ="";
190   
191     if(isset($this->Zones[$id]['InitialReverseZone'])){
192       $rev = FlipIp(getNameFromMix($this->Zones[$id]['InitialReverseZone']));
193     }elseif(isset($this->Zones[$id]['ReverseZone'])){
194       $rev = FlipIp(getNameFromMix($this->Zones[$id]['ReverseZone']));
195     }
197     $zonename = "";
198     if(isset($this->Zones[$id]['InitialzoneName'])){
199       $zonename= getNameFromMix($this->Zones[$id]['InitialzoneName']);
200     }
202     $used = array();
204     /* Add Records which use this zoneName
205      */
206     if(isset($zones[$zonename])){
207       $used = array_merge($used,$zones[$zonename]);
208     }
210     /* Add Records which uses this reverse zone
211      */
212     if(isset($zones[$rev.".in-addr.arpa"])){
213       $used = array_merge($used,$zones[$rev.".in-addr.arpa"]);
214     } 
216     /* There are still entries using this configuration
217      *  Abort deletion
218      */ 
219     if(count($used)){
220       $i = 2;
221       $str ="";
222       foreach($used as $dn){
223         if($i > 0 ){
224           $i --;
225           $str.=$dn." ";
226         }
227       }
229       /*  Only show 2 dns in the error message 
230        */
231       if(count($used)> 2) {
232         $str .=" ... ";
233       }
234       print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
236     }else{
237       unset($this->Zones[$id]);
238       return(true);
239     }
240     return(false);
241   } 
244   /* This funtion returns all used Zonenames 
245    */
246   function getUsedZoneNames()
247   {
248     $ret = array();
249     $ldap = $this->config->get_ldap_link();
250     $ldap->cd($this->config->current['BASE']);
251     $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
252     while($attr = $ldap->fetch()){
253       $ret[$attr['zoneName'][0]][] = $attr['dn'];
254     }
255     return($ret);
256   }
260     /* Remove dns service
261    */
262   function remove_from_parent()
263   {
264     if($this->DNSinitially_was_account){
265       $bool = true;
266       foreach($this->Zones as $key => $zone){
267         $bool= $bool & $this->RemoveZone($key);
268       }
270       if($bool){
271         $this->save();
272       }
273       return($bool);
274     }
275   }
279   /* Save to LDAP */
280   function save()
281   {
282     $ldap = $this->config->get_ldap_link();
283     $ldap->cd($this->config->current['BASE']);  
284   
285     /* Get differences 
286      */
288     $old_dn =  $this->orig_dn;
289     if($old_dn == "new"){
290       $old_dn = $this->dn;
291     }
292   
293     $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$old_dn);
295     /* Updated zone entries if reverser or forward name has changed  
296      * Must be done before moving entries, else the given dn is invalid
297      */
298     if(isset($tmp['zoneUpdates'])){
299       foreach($tmp['zoneUpdates'] as $dn => $attrs){
300         $ldap->cd($dn);
301         $ldap->modify($attrs);
302         show_ldap_error("Zone:".$ldap->get_error(), _("Updating DNS service failed"));
303       }
304     }
306     /* Delete dns 
307      */
308     foreach($tmp['del'] as $dn => $del){
309       $ldap->cd($dn);
310       $ldap->rmdir_recursive($dn);
311       show_ldap_error($ldap->get_error(), _("Removing DNS entries failed"));
312     }
314     /* move follwoing entries
315      */
316     foreach($tmp['move'] as $src => $dst){
317       $this->recursive_move($src,$dst);
318     }
320     /* Add || Update new DNS entries
321      */
322     foreach($tmp['add'] as $dn => $attrs){
323       $ldap->cd($dn);
324       $ldap->cat($dn, array('dn'));
325       if(count($ldap->fetch())){
326         $ldap->cd($dn);
327         $ldap->modify ($attrs);
328       }else{
329         $ldap->cd($dn);
330         $ldap->add($attrs);
331       }
332       show_ldap_error($ldap->get_error(), _("Saving DNS entries failed"));
333     }
334   }
336 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
337 ?>