Code

2a27325ddfbe8db08f79cd97a098851fa0d94549
[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   }
256   /* Remove dns service 
257    */
258   function remove_from_parent()
259   {
260     if(!$this->DNSinitially_was_account){
261       return;
262     }
263     print_red("Can't remove dns yet. returning without remove.");
264     return;
265     $ldap = $this->config->get_ldap_link();
266     $ldap->ls("(&(objectClass=dNSZone)(zoneName=*)(relativeDomainName=@))",$this->orig_dn,array("relativeDomainName","zoneName"));
267     while($attr = $ldap->fetch()){
268       $ldap->cd($attr['dn']);
269       $ldap->rmDir($attr['dn']);
270     }
271     show_ldap_error($ldap->get_error(), _("Removing DNS service failed"));
272   }
275   /* Save to LDAP */
276   function save()
277   {
278     $ldap = $this->config->get_ldap_link();
279     $ldap->cd($this->config->current['BASE']);  
280   
281     /* Get differences 
282      */
283     $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$this->orig_dn);
285     /* Updated zone entries if reverser or forward name has changed  
286      * Must be done before moving entries, else the given dn is invalid
287      */
288     if(isset($tmp['zoneUpdates'])){
289       foreach($tmp['zoneUpdates'] as $dn => $attrs){
290         $ldap->cd($dn);
291         $ldap->modify($attrs);
292         show_ldap_error("Zone:".$ldap->get_error(), _("Updating DNS service failed"));
293       }
294     }
296     /* Delete dns 
297      */
298     foreach($tmp['del'] as $dn => $del){
299       $ldap->cd($dn);
300       $ldap->rmdir_recursive($dn);
301       show_ldap_error($ldap->get_error(), _("Removing DNS entries failed"));
302     }
304     /* move follwoing entries
305      */
306     foreach($tmp['move'] as $src => $dst){
307       $this->recursive_move($src,$dst);
308     }
310     /* Add || Update new DNS entries
311      */
312     foreach($tmp['add'] as $dn => $attrs){
313       $ldap->cd($dn);
314       $ldap->cat($dn, array('dn'));
315       if(count($ldap->fetch())){
316         $ldap->cd($dn);
317         $ldap->modify ($attrs);
318       }else{
319         $ldap->cd($dn);
320         $ldap->add($attrs);
321       }
322       show_ldap_error($ldap->get_error(), _("Saving DNS entries failed"));
323     }
324   }
326 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
327 ?>