Code

485eede2bd72ca312814574787f729ac4ad70cef
[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       = "";
29   var $view_logged      = FALSE;
31   function servdns ($config, $dn= NULL, $parent= NULL)
32   {
33     plugin::plugin ($config, $dn, $parent);
35     $this->DisplayName = _("DNS service");
37     $this->orig_dn = $dn;
39     /* Get record types for zones
40      */
41     $this->RecordTypes = getDnsRecordTypes(true);
43     /* Get all zone Informations
44      */
45     $this->Zones = getDNSZoneEntries($config,$dn);
46     
47     /* If there is at least one entry in this -> types, we have DNS enabled 
48      */
49     if(count($this->Zones) == 0){
50       $this->is_account = false;
51     }else{
52       $this->is_account = true;
53     }
54     $this->initially_was_account = $this->is_account;
55   }
58   function execute()
59   {
60     /* Call parent execute 
61      */
62     plugin::execute();
64     if($this->is_account && !$this->view_logged){
65       $this->view_logged = TRUE;
66       @log::log("view","server/".get_class($this),$this->dn);
67     }
69     /* Fill templating stuff 
70      */
71     $smarty= get_smarty();
72     $display= "";
74     /* Do we need to flip is_account state? 
75      */
76     if (isset($_POST['modify_state'])){
77       $this->is_account= !$this->is_account;
78     }
80     if ($this->is_account){
81       $display= $this->show_disable_header(_("Remove DNS service"),
82           _("This server has DNS features enabled. You can disable them by clicking below."));
83     } else {
84       $display= $this->show_enable_header(_("Add DNS service"),
85           _("This server has DNS features disabled. You can enable them by clicking below."));
86       return ($display);
87     }
90     /* Edited or Added zone 
91      */
92     if(isset($_POST['SaveZoneChanges'])){
93       $this->dialog->save_object();
95       /* Check for errors  
96        */
97       if(count($this->dialog->check())){
98         foreach($this->dialog->check() as $msgs){
99           print_red($msgs); 
100         }
101       }else{
102         /* add new/edited zone 
103          */
104         $ret = $this->dialog->save();
105         if(!$this->dialog->isNew){
106           unset($this->Zones[$this->dialog->OldZoneName]);
107         }
108         $this->Zones[$ret['zoneName']] = $ret;
109         $this->dialog = NULL;
110       }
111     }
113     /* Cancel zone edit / new 
114      */
115     if(isset($_POST['CancelZoneChanges'])){
116       $this->dialog = NULL;
117     }
119     /* Add empty new zone 
120      */
121     if(isset($_POST['AddZone'])){
122       $this->dialog = new servdnseditZone($this->config,$this->dn);
123     }
125     /* Check for edit zone request 
126      */
127     $once = false;
128     foreach( $_POST as $name => $value){
129   
130       /* check all post for edit request 
131        */
132       if(preg_match("/^editZone_/",$name)&&!$once){
133         $once =true;
134         $tmp = preg_replace("/^editZone_/","",$name);
135         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
136         $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$tmp]);
137       }
139       /* check posts for delete zone 
140        */
141       if(preg_match("/^delZone_/",$name)&&!$once){
143         $once =true;
144         $tmp = preg_replace("/^delZone_/","",$name);
145         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
146      
147         /* Initiate deletion
148          */ 
149         $this->RemoveZone($tmp); 
150       }
151     }
153     /* Show dialog 
154      */
155     if($this->dialog!= NULL){
156       $this->dialog->save_object();
157       $this->dialog->parent = $this;
158       return($this->dialog->execute());
159     }
161     /* Create Listbox with existing Zones 
162      */
163     $ZoneList = new divSelectBox("dNSZones");
164     $ZoneList -> SetHeight(254);
166     /* Add entries to divlist
167      */
168     $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
169       <input type='image' src='images/edittrash.png' name='delZone_%s'>";
170     foreach($this->Zones as $zone => $values ){
171       $ZoneList->AddEntry(array(
172             array("string" => $zone),
173             array("string" => _("Reverse zone")." : ".$values['ReverseZone']),
174             array("string" => _("TTL")." : ".$values['sOAttl']),
175             array("string" => _("Class")." : ".$values['dNSClass']),
176             array("string" =>str_replace("%s",base64_encode($zone),$editImg))
177             ));
178     }    
180     /* Display tempalte 
181      */
182     $smarty->assign("ZoneList",$ZoneList->DrawList());
183     $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
184     return($display);
185   }
188   /* Delete specified zone
189    */
190   function RemoveZone($id)
191   {
192     $zones =  $this->getUsedZoneNames();
194     if(isset($this->Zones[$id]['InitialReverseZone'])){
195       $rev = FlipIp($this->Zones[$id]['InitialReverseZone']);
196     }else{
197       $rev = FlipIp($this->Zones[$id]['ReverseZone']);
198     }
200     $zonename = "";
201     if(isset($this->Zones[$id]['InitialzoneName'])){
202       $zonename= $this->Zones[$id]['InitialzoneName'];
203     }
205     $used = array();
207     /* Add Records which use this zoneName
208      */
209     if(isset($zones[$zonename])){
210       $used = array_merge($used,$zones[$zonename]);
211     }
213     /* Add Records which uses this reverse zone
214      */
215     if(isset($zones[$rev.".in-addr.arpa"])){
216       $used = array_merge($used,$zones[$rev.".in-addr.arpa"]);
217     } 
219     /* There are still entries using this configuration
220      *  Abort deletion
221      */
222     if(count($used)){
223       $i = 2;
224       $str ="";
225       foreach($used as $dn){
226         if($i > 0 && !preg_match("/,relativeDomainName=/",$dn)){
227           $i --;
228           $name = preg_replace("/^[^=]+=([^,]*),.*$/","\\1",$dn);
229           $zone = preg_replace("/^.*zoneName=([^,]*),.*$/","\\1",$dn);
230           $str.= $name.".".$zone." ";
231         }
232       }
234       /*  Only show 2 dns in the error message 
235        */
236       if(count($used)> 2) {
237         $str .=" ... ";
238       }
239       print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
240       return(false);
241     }else{
242       unset($this->Zones[$id]);
243       return(true);
244     }
245   } 
248   /* This funtion returns all used Zonenames 
249    */
250   function getUsedZoneNames()
251   {
252     $ret = array();
253     $ldap = $this->config->get_ldap_link();
254     $ldap->cd($this->config->current['BASE']);
255     $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
256     while($attr = $ldap->fetch()){
257       $ret[$attr['zoneName'][0]][] = $attr['dn'];
258     }
259     return($ret);
260   }
263   /* Remove dns service 
264    */
265   function remove_from_parent()
266   {
267     if($this->initially_was_account){
268       $bool = true;
269       foreach($this->Zones as $key => $zone){
270         $bool= $bool & $this->RemoveZone($key);
271       }
273       if($bool){
274         $this->save();
275       }
276       return($bool);
277     }
278   }
281   /* Save to LDAP */
282   function save()
283   {
284     $ldap = $this->config->get_ldap_link();
285     $ldap->cd($this->config->current['BASE']);  
286   
287     /* Get differences 
288      */
289     $old_dn = $this->orig_dn;
290     if($old_dn == "new"){
291       $old_dn = $this->dn;
292     }
294     $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$old_dn);
296     /* Updated zone entries if reverser or forward name has changed  
297      * Must be done before moving entries, else the given dn is invalid
298      */
299     if(isset($tmp['zoneUpdates'])){
300       foreach($tmp['zoneUpdates'] as $dn => $attrs){
301         $ldap->cd($dn);
302         $ldap->modify($attrs);
303         @log::log("modfiy","unknown/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
304         show_ldap_error($ldap->get_error(), sprintf(_("Updating of system server/dns with dn '%s' failed."),$this->dn));
305       }
306     }
308     /* Delete dns 
309      */
310     foreach($tmp['del'] as $dn => $del){
311       $ldap->cd($dn);
312       $ldap->rmdir_recursive($dn);
313       @log::log("remove","unknown/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
314       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/dns with dn '%s' failed."),$this->dn));
315     }
317     /* move follwoing entries
318      */
319     foreach($tmp['move'] as $src => $dst){
320       $this->recursive_move($src,$dst);
321     }
323     /* Add || Update new DNS entries
324      */
325     foreach($tmp['add'] as $dn => $attrs){
326       $ldap->cd($dn);
327       $ldap->cat($dn, array('dn'));
328       if(count($ldap->fetch())){
329         $ldap->cd($dn);
330         $ldap->modify ($attrs);
331       }else{
332         $ldap->cd($dn);
333         $ldap->add($attrs);
334       }
335       show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/dns with dn '%s' failed."),$this->dn));
336     }
337   }
340   /* Directly save new status flag */
341   function setStatus($value)
342   {
343     if($value == "none") return;
344     if(!$this->initially_was_account) return;
345     if(empty($this->StatusFlag)) return;
346     $ldap = $this->config->get_ldap_link();
347     $ldap->cd($this->dn);
348     $ldap->cat($this->dn,array("objectClass"));
349     if($ldap->count()){
351       $tmp = $ldap->fetch();
352       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
353         $attrs['objectClass'][] = $tmp['objectClass'][$i];
354       }
355       $flag = $this->StatusFlag;
356       $attrs[$flag] = $value;
357       $this->$flag = $value;
358       $ldap->modify($attrs);
359       show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for system server/dns with dn '%s' failed."),$this->dn));
360       $this->action_hook();
361     }
362   }
365   function getListEntry()
366   {
367     $fields               = goService::getListEntry(); 
368     $fields['Message']    = _("DNS service");
369     $fields['AllowEdit']  = true;
370     return($fields);
371   }
374   /* Get updates for status flag */
375   function updateStatusState()
376   {
377     if(empty($this->StatusFlag)) return;
379     $attrs = array();
380     $flag = $this->StatusFlag;
381     $ldap = $this->config->get_ldap_link();
382     $ldap->cd($this->cn);
383     $ldap->cat($this->dn,array($flag));
384     if($ldap->count()){
385       $attrs = $ldap->fetch();
386     }
387     if(isset($attrs[$flag][0])){
388       $this->$flag = $attrs[$flag][0];
389     }
390   }
393   /* Return plugin informations for acl handling */
394   function plInfo()
395   {
396     return (array(
397           "plShortName"   => _("DNS service"),
398           "plDescription" => _("DNS service")." ("._("Services").")",
399           "plSelfModify"  => FALSE,
400           "plDepends"     => array(),
401           "plPriority"    => 83,
402           "plSection"     => array("administration"),
403           "plCategory"    => array("server"),
405           "plProvidedAcls"=> array(
406             "zoneName"      =>_("Zone name"),
407             "ReverseZone"   =>_("Reverse zone"),
408             "sOAprimary"    =>_("Primary dns server"),
409             "sOAmail"       =>_("Mail address"),
410             "sOAserial"     =>_("Serial"),
411             "sOArefresh"    =>_("Refresh"),
412             "sOAretry"      =>_("Retry"),
413             "sOAexpire"     =>_("Expire"),
414             "sOAttl"        =>_("TTL"),
415             "zoneRecords"   =>_("Zone records"))
416     ));
417   }
420 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
421 ?>