Code

c63fd9b1c947872ad51b66c2d07da7cc8d663185
[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'])){
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" => $zone),
167             array("string" => _("Reverse zone")." : ".$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();
190     if(isset($this->Zones[$id]['InitialReverseZone'])){
191       $rev = FlipIp($this->Zones[$id]['InitialReverseZone']);
192     }else{
193       $rev = FlipIp($this->Zones[$id]['ReverseZone']);
194     }
196     $zonename = "";
197     if(isset($this->Zones[$id]['InitialzoneName'])){
198       $zonename= $this->Zones[$id]['InitialzoneName'];
199     }
201     $used = array();
203     /* Add Records which use this zoneName
204      */
205     if(isset($zones[$zonename])){
206       $used = array_merge($used,$zones[$zonename]);
207     }
209     /* Add Records which uses this reverse zone
210      */
211     if(isset($zones[$rev.".in-addr.arpa"])){
212       $used = array_merge($used,$zones[$rev.".in-addr.arpa"]);
213     } 
215     /* There are still entries using this configuration
216      *  Abort deletion
217      */ 
218     if(count($used)){
219       $i = 2;
220       $str ="";
221       foreach($used as $dn){
222         if($i > 0 ){
223           $i --;
224           $str.=$dn." ";
225         }
226       }
228       /*  Only show 2 dns in the error message 
229        */
230       if(count($used)> 2) {
231         $str .=" ... ";
232       }
233       print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
235     }else{
236       unset($this->Zones[$id]);
237       return(true);
238     }
239     return(false);
240   } 
243   /* This funtion returns all used Zonenames 
244    */
245   function getUsedZoneNames()
246   {
247     $ret = array();
248     $ldap = $this->config->get_ldap_link();
249     $ldap->cd($this->config->current['BASE']);
250     $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
251     while($attr = $ldap->fetch()){
252       $ret[$attr['zoneName'][0]][] = $attr['dn'];
253     }
254     return($ret);
255   }
259     /* Remove dns service
260    */
261   function remove_from_parent()
262   {
263     if($this->DNSinitially_was_account){
264       $bool = true;
265       foreach($this->Zones as $key => $zone){
266         $bool= $bool & $this->RemoveZone($key);
267       }
269       if($bool){
270         $this->save();
271       }
272       return($bool);
273     }
274   }
278   /* Save to LDAP */
279   function save()
280   {
281     $ldap = $this->config->get_ldap_link();
282     $ldap->cd($this->config->current['BASE']);  
283   
284     /* Get differences 
285      */
286     $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$this->orig_dn);
288     /* Updated zone entries if reverser or forward name has changed  
289      * Must be done before moving entries, else the given dn is invalid
290      */
291     if(isset($tmp['zoneUpdates'])){
292       foreach($tmp['zoneUpdates'] as $dn => $attrs){
293         $ldap->cd($dn);
294         $ldap->modify($attrs);
295         show_ldap_error("Zone:".$ldap->get_error(), _("Updating DNS service failed"));
296       }
297     }
299     /* Delete dns 
300      */
301     foreach($tmp['del'] as $dn => $del){
302       $ldap->cd($dn);
303       $ldap->rmdir_recursive($dn);
304       show_ldap_error($ldap->get_error(), _("Removing DNS entries failed"));
305     }
307     /* move follwoing entries
308      */
309     foreach($tmp['move'] as $src => $dst){
310       $this->recursive_move($src,$dst);
311     }
313     /* Add || Update new DNS entries
314      */
315     foreach($tmp['add'] as $dn => $attrs){
316       $ldap->cd($dn);
317       $ldap->cat($dn, array('dn'));
318       if(count($ldap->fetch())){
319         $ldap->cd($dn);
320         $ldap->modify ($attrs);
321       }else{
322         $ldap->cd($dn);
323         $ldap->add($attrs);
324       }
325       show_ldap_error($ldap->get_error(), _("Saving DNS entries failed"));
326     }
327   }
329 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
330 ?>