Code

Allow remove of dns extension
[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 $initially_was_account;
22   
23   /* ServerService tab vars */
24   var $conflicts        = array("servdns");
25   var $DisplayName      = "";
26   var $StatusFlag       = "";
28   function servdns ($config, $dn= NULL)
29   {
30     plugin::plugin ($config, $dn);
32     $this->DisplayName = _("Domain name system service");
34     $this->orig_dn = $dn;
36     /* Get record types for zones
37      */
38     $this->RecordTypes = getDnsRecordTypes(true);
40     /* Get all zone Informations
41      */
42     $this->Zones = getDNSZoneEntries($config,$dn);
43     
44     /* If there is at least one entry in this -> types, we have DNS enabled 
45      */
46     if(count($this->Zones) == 0){
47       $this->is_account = false;
48     }else{
49       $this->is_account = true;
50     }
51     $this->initially_was_account = $this->is_account;
52   }
55   function execute()
56   {
57     /* Call parent execute 
58      */
59     plugin::execute();
61     /* Fill templating stuff 
62      */
63     $smarty= get_smarty();
64     $display= "";
66     /* Do we need to flip is_account state? 
67      */
68     if (isset($_POST['modify_state'])){
69       $this->is_account= !$this->is_account;
70     }
72     if ($this->is_account){
73       $display= $this->show_header(_("Remove DNS service"),
74           _("This server has DNS features enabled. You can disable them by clicking below."));
75     } else {
76       $display= $this->show_header(_("Add DNS service"),
77           _("This server has DNS features disabled. You can enable them by clicking below."));
78       return ($display);
79     }
82     /* Edited or Added zone 
83      */
84     if(isset($_POST['SaveZoneChanges'])){
85       $this->dialog->save_object();
87       /* Check for errors  
88        */
89       if(count($this->dialog->check())){
90         foreach($this->dialog->check() as $msgs){
91           print_red($msgs); 
92         }
93       }else{
94         /* add new/edited zone 
95          */
96         $ret = $this->dialog->save();
97         if(!$this->dialog->isNew){
98           unset($this->Zones[$this->dialog->OldZoneName]);
99         }
100         $this->Zones[$ret['zoneName']] = $ret;
101         $this->dialog = NULL;
102       }
103     }
105     /* Cancel zone edit / new 
106      */
107     if(isset($_POST['CancelZoneChanges'])){
108       $this->dialog = NULL;
109     }
111     /* Add empty new zone 
112      */
113     if(isset($_POST['AddZone'])){
114       $this->dialog = new servdnseditZone($this->config,$this->dn);
115     }
117     /* Check for edit zone request 
118      */
119     $once = false;
120     foreach( $_POST as $name => $value){
121   
122       /* check all post for edit request 
123        */
124       if(preg_match("/^editZone_/",$name)&&!$once){
125         $once =true;
126         $tmp = preg_replace("/^editZone_/","",$name);
127         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
128         $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$tmp]);
129       }
131       /* check posts for delete zone 
132        */
133       if(preg_match("/^delZone_/",$name)&&!$once){
135         $once =true;
136         $tmp = preg_replace("/^delZone_/","",$name);
137         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
138      
139         /* Initiate deletion
140          */ 
141         $this->RemoveZone($tmp); 
142       }
143     }
145     /* Show dialog 
146      */
147     if($this->dialog!= NULL){
148       $this->dialog->save_object();
149       $this->dialog->parent = $this;
150       return($this->dialog->execute());
151     }
153     /* Create Listbox with existing Zones 
154      */
155     $ZoneList = new divSelectBox("dNSZones");
156     $ZoneList -> SetHeight(254);
158     /* Add entries to divlist
159      */
160     $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
161       <input type='image' src='images/edittrash.png' name='delZone_%s'>";
162     foreach($this->Zones as $zone => $values ){
163       $ZoneList->AddEntry(array(
164             array("string" => $zone),
165             array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
166             array("string" => _("TTL")." : ".$values['sOAttl']),
167             array("string" => _("Class")." : ".$values['dNSClass']),
168             array("string" =>str_replace("%s",base64_encode($zone),$editImg))
169             ));
170     }    
172     /* Display tempalte 
173      */
174     $smarty->assign("ZoneList",$ZoneList->DrawList());
175     $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
176     return($display);
177   }
180   /* Delete specified zone
181    */
182   function RemoveZone($id)
183   {
184     $zones =  $this->getUsedZoneNames();
186     if(isset($this->Zones[$id]['InitialReverseZone'])){
187       $rev = FlipIp($this->Zones[$id]['InitialReverseZone']);
188     }else{
189       $rev = FlipIp($this->Zones[$id]['ReverseZone']);
190     }
192     $zonename = "";
193     if(isset($this->Zones[$id]['InitialzoneName'])){
194       $zonename= $this->Zones[$id]['InitialzoneName'];
195     }
197     $used = array();
199     /* Add Records which use this zoneName
200      */
201     if(isset($zones[$zonename])){
202       $used = array_merge($used,$zones[$zonename]);
203     }
205     /* Add Records which uses this reverse zone
206      */
207     if(isset($zones[$rev.".in-addr.arpa"])){
208       $used = array_merge($used,$zones[$rev.".in-addr.arpa"]);
209     } 
211     /* There are still entries using this configuration
212      *  Abort deletion
213      */ 
214     if(count($used)){
215       $i = 2;
216       $str ="";
217       foreach($used as $dn){
218         if($i > 0 ){
219           $i --;
220           $str.=$dn." ";
221         }
222       }
224       /*  Only show 2 dns in the error message 
225        */
226       if(count($used)> 2) {
227         $str .=" ... ";
228       }
229       print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
231     }else{
232       unset($this->Zones[$id]);
233     }
234   } 
237   /* This funtion returns all used Zonenames 
238    */
239   function getUsedZoneNames()
240   {
241     $ret = array();
242     $ldap = $this->config->get_ldap_link();
243     $ldap->cd($this->config->current['BASE']);
244     $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
245     while($attr = $ldap->fetch()){
246       $ret[$attr['zoneName'][0]][] = $attr['dn'];
247     }
248     return($ret);
249   }
252   /* Remove dns service 
253    */
254   function remove_from_parent()
255   {
256     if($this->initially_was_account){
257       $ldap = $this->config->get_ldap_link();
258       $ldap->ls("(&(objectClass=dNSZone)(zoneName=*)(relativeDomainName=@))",$this->orig_dn,array("relativeDomainName","zoneName"));
259       while($attr = $ldap->fetch()){
260         $ldap->cd($attr['dn']);
261         $ldap->rmdir_recursive($attr['dn']);
262       }
263       show_ldap_error($ldap->get_error(), _("Removing DNS service failed"));
264     }
265   }
268   /* Save to LDAP */
269   function save()
270   {
271     $ldap = $this->config->get_ldap_link();
272     $ldap->cd($this->config->current['BASE']);  
273   
274     /* Get differences 
275      */
276     $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$this->orig_dn);
278     /* Updated zone entries if reverser or forward name has changed  
279      * Must be done before moving entries, else the given dn is invalid
280      */
281     if(isset($tmp['zoneUpdates'])){
282       foreach($tmp['zoneUpdates'] as $dn => $attrs){
283         $ldap->cd($dn);
284         $ldap->modify($attrs);
285         show_ldap_error("Zone:".$ldap->get_error(), _("Updating DNS service failed"));
286       }
287     }
289     /* Delete dns 
290      */
291     foreach($tmp['del'] as $dn => $del){
292       $ldap->cd($dn);
293       $ldap->rmdir_recursive($dn);
294       show_ldap_error($ldap->get_error(), _("Removing DNS entries failed"));
295     }
297     /* move follwoing entries
298      */
299     foreach($tmp['move'] as $src => $dst){
300       $this->recursive_move($src,$dst);
301     }
303     /* Add || Update new DNS entries
304      */
305     foreach($tmp['add'] as $dn => $attrs){
306       $ldap->cd($dn);
307       $ldap->cat($dn, array('dn'));
308       if(count($ldap->fetch())){
309         $ldap->cd($dn);
310         $ldap->modify ($attrs);
311       }else{
312         $ldap->cd($dn);
313         $ldap->add($attrs);
314       }
315       show_ldap_error($ldap->get_error(), _("Saving DNS entries failed"));
316     }
317   }
320   /* Directly save new status flag */
321   function setStatus($value)
322   {
323     if($value == "none") return;
324     if(!$this->initially_was_account) return;
325     $ldap = $this->config->get_ldap_link();
326     $ldap->cd($this->dn);
327     $ldap->cat($this->dn,array("objectClass"));
328     if($ldap->count()){
330       $tmp = $ldap->fetch();
331       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
332         $attrs['objectClass'][] = $tmp['objectClass'][$i];
333       }
334       $flag = $this->StatusFlag;
335       $attrs[$flag] = $value;
336       $this->$flag = $value;
337       $ldap->modify($attrs);
338       show_ldap_error($ldap->get_error());
339       $this->action_hook();
340     }
341   }
344   function getListEntry()
345   {
346     $this->updateStatusState();
347     $flag = $this->StatusFlag;
348     if(empty($flag)){
349       $fields['Status']      = "";
350     }else{
351       $fields['Status']      = $this->$flag;
352     }
353     $fields['Message']    = _("DNS service");
354     $fields['AllowStart'] = true;
355     $fields['AllowStop']  = true;
356     $fields['AllowRestart'] = true;
357     $fields['AllowRemove']= true;
358     $fields['AllowEdit']  = true;
359     return($fields);
360   }
363   /* Get updates for status flag */
364   function updateStatusState()
365   {
366     if(empty($this->StatusFlag)) return;
368     $attrs = array();
369     $flag = $this->StatusFlag;
370     $ldap = $this->config->get_ldap_link();
371     $ldap->cd($this->cn);
372     $ldap->cat($this->dn,array($flag));
373     if($ldap->count()){
374       $attrs = $ldap->fetch();
375     }
376     if(isset($attrs[$flag][0])){
377       $this->$flag = $attrs[$flag][0];
378     }
379   }
381 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
382 ?>