Code

155966473306d485bf01ae25fd62d5d0b3ddf073
[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       return;
258     }
259     print_red("Can't remove dns yet. returning without remove.");
260     return;
261     $ldap = $this->config->get_ldap_link();
262     $ldap->ls("(&(objectClass=dNSZone)(zoneName=*)(relativeDomainName=@))",$this->orig_dn,array("relativeDomainName","zoneName"));
263     while($attr = $ldap->fetch()){
264       $ldap->cd($attr['dn']);
265       $ldap->rmDir($attr['dn']);
266     }
267     show_ldap_error($ldap->get_error(), _("Removing DNS service failed"));
268   }
271   /* Save to LDAP */
272   function save()
273   {
274     $ldap = $this->config->get_ldap_link();
275     $ldap->cd($this->config->current['BASE']);  
276   
277     /* Get differences 
278      */
279     $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$this->orig_dn);
281     /* Updated zone entries if reverser or forward name has changed  
282      * Must be done before moving entries, else the given dn is invalid
283      */
284     if(isset($tmp['zoneUpdates'])){
285       foreach($tmp['zoneUpdates'] as $dn => $attrs){
286         $ldap->cd($dn);
287         $ldap->modify($attrs);
288         show_ldap_error("Zone:".$ldap->get_error(), _("Updating DNS service failed"));
289       }
290     }
292     /* Delete dns 
293      */
294     foreach($tmp['del'] as $dn => $del){
295       $ldap->cd($dn);
296       $ldap->rmdir_recursive($dn);
297       show_ldap_error($ldap->get_error(), _("Removing DNS entries failed"));
298     }
300     /* move follwoing entries
301      */
302     foreach($tmp['move'] as $src => $dst){
303       $this->recursive_move($src,$dst);
304     }
306     /* Add || Update new DNS entries
307      */
308     foreach($tmp['add'] as $dn => $attrs){
309       $ldap->cd($dn);
310       $ldap->cat($dn, array('dn'));
311       if(count($ldap->fetch())){
312         $ldap->cd($dn);
313         $ldap->modify ($attrs);
314       }else{
315         $ldap->cd($dn);
316         $ldap->add($attrs);
317       }
318       show_ldap_error($ldap->get_error(), _("Saving DNS entries failed"));
319     }
320   }
323   /* Directly save new status flag */
324   function setStatus($value)
325   {
326     if($value == "none") return;
327     if(!$this->initially_was_account) return;
328     $ldap = $this->config->get_ldap_link();
329     $ldap->cd($this->dn);
330     $ldap->cat($this->dn,array("objectClass"));
331     if($ldap->count()){
333       $tmp = $ldap->fetch();
334       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
335         $attrs['objectClass'][] = $tmp['objectClass'][$i];
336       }
337       $flag = $this->StatusFlag;
338       $attrs[$flag] = $value;
339       $this->$flag = $value;
340       $ldap->modify($attrs);
341       show_ldap_error($ldap->get_error());
342       $this->action_hook();
343     }
344   }
347   function getListEntry()
348   {
349     $this->updateStatusState();
350     $flag = $this->StatusFlag;
351     if(empty($flag)){
352       $fields['Status']      = "";
353     }else{
354       $fields['Status']      = $this->$flag;
355     }
356     $fields['Message']    = _("DNS service");
357     $fields['AllowStart'] = true;
358     $fields['AllowStop']  = true;
359     $fields['AllowRestart'] = true;
360     $fields['AllowRemove']= true;
361     $fields['AllowEdit']  = true;
362     return($fields);
363   }
366   /* Get updates for status flag */
367   function updateStatusState()
368   {
369     if(empty($this->StatusFlag)) return;
371     $attrs = array();
372     $flag = $this->StatusFlag;
373     $ldap = $this->config->get_ldap_link();
374     $ldap->cd($this->cn);
375     $ldap->cat($this->dn,array($flag));
376     if($ldap->count()){
377       $attrs = $ldap->fetch();
378     }
379     if(isset($attrs[$flag][0])){
380       $this->$flag = $attrs[$flag][0];
381     }
382   }
384 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
385 ?>