Code

Updated remove from parent.
[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     }
238   } 
241   /* This funtion returns all used Zonenames 
242    */
243   function getUsedZoneNames()
244   {
245     $ret = array();
246     $ldap = $this->config->get_ldap_link();
247     $ldap->cd($this->config->current['BASE']);
248     $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
249     while($attr = $ldap->fetch()){
250       $ret[$attr['zoneName'][0]][] = $attr['dn'];
251     }
252     return($ret);
253   }
257     /* Remove dns service
258    */
259   function remove_from_parent()
260   {
261     if($this->initially_was_account){
262       $bool = true;
263       foreach($this->Zones as $key => $zone){
264         $bool= $bool & $this->RemoveZone($key);
265       }
267       if($bool){
268         $this->save();
269       }
270       return($bool);
271     }
272   }
276   /* Save to LDAP */
277   function save()
278   {
279     $ldap = $this->config->get_ldap_link();
280     $ldap->cd($this->config->current['BASE']);  
281   
282     /* Get differences 
283      */
284     $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$this->orig_dn);
286     /* Updated zone entries if reverser or forward name has changed  
287      * Must be done before moving entries, else the given dn is invalid
288      */
289     if(isset($tmp['zoneUpdates'])){
290       foreach($tmp['zoneUpdates'] as $dn => $attrs){
291         $ldap->cd($dn);
292         $ldap->modify($attrs);
293         show_ldap_error("Zone:".$ldap->get_error(), _("Updating DNS service failed"));
294       }
295     }
297     /* Delete dns 
298      */
299     foreach($tmp['del'] as $dn => $del){
300       $ldap->cd($dn);
301       $ldap->rmdir_recursive($dn);
302       show_ldap_error($ldap->get_error(), _("Removing DNS entries failed"));
303     }
305     /* move follwoing entries
306      */
307     foreach($tmp['move'] as $src => $dst){
308       $this->recursive_move($src,$dst);
309     }
311     /* Add || Update new DNS entries
312      */
313     foreach($tmp['add'] as $dn => $attrs){
314       $ldap->cd($dn);
315       $ldap->cat($dn, array('dn'));
316       if(count($ldap->fetch())){
317         $ldap->cd($dn);
318         $ldap->modify ($attrs);
319       }else{
320         $ldap->cd($dn);
321         $ldap->add($attrs);
322       }
323       show_ldap_error($ldap->get_error(), _("Saving DNS entries failed"));
324     }
325   }
327 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
328 ?>