Code

Added "Force localboot"
[gosa.git] / plugins / admin / systems / class_servDNS.inc
1 <?php
3 require_once("class_goService.inc");
5 class servdns extends goService
6 {
7   /* CLI vars */
8   var $cli_summary= "Manage server basic objects";
9   var $cli_description= "Some longer text\nfor help";
10   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
12   /* attribute list for save action */
13   var $ignore_account   = FALSE;
14   var $attributes       = array(); 
15   var $objectclasses    = array("whatever");
17   var $RecordTypes      = array();
18   var $Zones            = array();
19   var $dialog           = NULL;
21   var $orig_dn          = "";
23   var $initially_was_account;
24   
25   /* ServerService tab vars */
26   var $conflicts        = array("servdns");
27   var $DisplayName      = "";
28   var $StatusFlag       = "";
30   function servdns ($config, $dn= NULL, $parent= NULL)
31   {
32     plugin::plugin ($config, $dn, $parent);
34     $this->DisplayName = _("DNS service");
36     $this->orig_dn = $dn;
38     /* Get record types for zones
39      */
40     $this->RecordTypes = getDnsRecordTypes(true);
42     /* Get all zone Informations
43      */
44     $this->Zones = getDNSZoneEntries($config,$dn);
45     
46     /* If there is at least one entry in this -> types, we have DNS enabled 
47      */
48     if(count($this->Zones) == 0){
49       $this->is_account = false;
50     }else{
51       $this->is_account = true;
52     }
53     $this->initially_was_account = $this->is_account;
54   }
57   function execute()
58   {
59     /* Call parent execute 
60      */
61     plugin::execute();
63     /* Fill templating stuff 
64      */
65     $smarty= get_smarty();
66     $display= "";
68     /* Do we need to flip is_account state? 
69      */
70     if (isset($_POST['modify_state'])){
71       $this->is_account= !$this->is_account;
72     }
74     if ($this->is_account){
75       $display= $this->show_disable_header(_("Remove DNS service"),
76           _("This server has DNS features enabled. You can disable them by clicking below."));
77     } else {
78       $display= $this->show_enable_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'])){
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){
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){
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     }    
174     /* Display tempalte 
175      */
176     $smarty->assign("ZoneList",$ZoneList->DrawList());
177     $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
178     return($display);
179   }
182   /* Delete specified zone
183    */
184   function RemoveZone($id)
185   {
186     $zones =  $this->getUsedZoneNames();
188     if(isset($this->Zones[$id]['InitialReverseZone'])){
189       $rev = FlipIp($this->Zones[$id]['InitialReverseZone']);
190     }else{
191       $rev = FlipIp($this->Zones[$id]['ReverseZone']);
192     }
194     $zonename = "";
195     if(isset($this->Zones[$id]['InitialzoneName'])){
196       $zonename= $this->Zones[$id]['InitialzoneName'];
197     }
199     $used = array();
201     /* Add Records which use this zoneName
202      */
203     if(isset($zones[$zonename])){
204       $used = array_merge($used,$zones[$zonename]);
205     }
207     /* Add Records which uses this reverse zone
208      */
209     if(isset($zones[$rev.".in-addr.arpa"])){
210       $used = array_merge($used,$zones[$rev.".in-addr.arpa"]);
211     } 
213     /* There are still entries using this configuration
214      *  Abort deletion
215      */
216     if(count($used)){
217       $i = 2;
218       $str ="";
219       foreach($used as $dn){
220         if($i > 0 && !preg_match("/,relativeDomainName=/",$dn)){
221           $i --;
222           $name = preg_replace("/^[^=]+=([^,]*),.*$/","\\1",$dn);
223           $zone = preg_replace("/^.*zoneName=([^,]*),.*$/","\\1",$dn);
224           $str.= $name.".".$zone." ";
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)));
234       return(false);
235     }else{
236       unset($this->Zones[$id]);
237       return(true);
238     }
239   } 
242   /* This funtion returns all used Zonenames 
243    */
244   function getUsedZoneNames()
245   {
246     $ret = array();
247     $ldap = $this->config->get_ldap_link();
248     $ldap->cd($this->config->current['BASE']);
249     $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
250     while($attr = $ldap->fetch()){
251       $ret[$attr['zoneName'][0]][] = $attr['dn'];
252     }
253     return($ret);
254   }
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   }
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     $old_dn = $this->orig_dn;
284     if($old_dn == "new"){
285       $old_dn = $this->dn;
286     }
288     $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$old_dn);
290     /* Updated zone entries if reverser or forward name has changed  
291      * Must be done before moving entries, else the given dn is invalid
292      */
293     if(isset($tmp['zoneUpdates'])){
294       foreach($tmp['zoneUpdates'] as $dn => $attrs){
295         $ldap->cd($dn);
296         $ldap->modify($attrs);
297         show_ldap_error($ldap->get_error(), sprintf(_("Updating of system server/dns with dn '%s' failed."),$this->dn));
298       }
299     }
301     /* Delete dns 
302      */
303     foreach($tmp['del'] as $dn => $del){
304       $ldap->cd($dn);
305       $ldap->rmdir_recursive($dn);
306       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/dns with dn '%s' failed."),$this->dn));
307     }
309     /* move follwoing entries
310      */
311     foreach($tmp['move'] as $src => $dst){
312       $this->recursive_move($src,$dst);
313     }
315     /* Add || Update new DNS entries
316      */
317     foreach($tmp['add'] as $dn => $attrs){
318       $ldap->cd($dn);
319       $ldap->cat($dn, array('dn'));
320       if(count($ldap->fetch())){
321         $ldap->cd($dn);
322         $ldap->modify ($attrs);
323       }else{
324         $ldap->cd($dn);
325         $ldap->add($attrs);
326       }
327       show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/dns with dn '%s' failed."),$this->dn));
328     }
329   }
332   /* Directly save new status flag */
333   function setStatus($value)
334   {
335     if($value == "none") return;
336     if(!$this->initially_was_account) return;
337     if(empty($this->StatusFlag)) return;
338     $ldap = $this->config->get_ldap_link();
339     $ldap->cd($this->dn);
340     $ldap->cat($this->dn,array("objectClass"));
341     if($ldap->count()){
343       $tmp = $ldap->fetch();
344       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
345         $attrs['objectClass'][] = $tmp['objectClass'][$i];
346       }
347       $flag = $this->StatusFlag;
348       $attrs[$flag] = $value;
349       $this->$flag = $value;
350       $ldap->modify($attrs);
351       show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for system server/dns with dn '%s' failed."),$this->dn));
352       $this->action_hook();
353     }
354   }
357   function getListEntry()
358   {
359     $fields               = goService::getListEntry(); 
360     $fields['Message']    = _("DNS service");
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   }
385   /* Return plugin informations for acl handling */
386   function plInfo()
387   {
388     return (array(
389           "plShortName"   => _("DNS service"),
390           "plDescription" => _("DNS service")." ("._("Services").")",
391           "plSelfModify"  => FALSE,
392           "plDepends"     => array(),
393           "plPriority"    => 83,
394           "plSection"     => array("administration"),
395           "plCategory"    => array("server"),
397           "plProvidedAcls"=> array(
398             "zoneName"      =>_("Zone name"),
399             "ReverseZone"   =>_("Reverse zone"),
400             "sOAprimary"    =>_("Primary dns server"),
401             "sOAmail"       =>_("Mail address"),
402             "sOAserial"     =>_("Serial"),
403             "sOArefresh"    =>_("Refresh"),
404             "sOAretry"      =>_("Retry"),
405             "sOAexpire"     =>_("Expire"),
406             "sOAttl"        =>_("TTL"),
407             "zoneRecords"   =>_("Zone records"))
408     ));
409   }
412 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
413 ?>