Code

Fixed a couple of static/non-static error messages
[gosa.git] / plugins / admin / systems / class_servDNS.inc
1 <?php
3 class servdns extends goService
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();
18   var $orig_dn          = "";
20   var $initially_was_account;
21   
22   /* ServerService tab vars */
23   var $conflicts        = array("servdns");
24   var $DisplayName      = "";
25   var $StatusFlag       = "";
26   var $view_logged      = FALSE;
28   var $dns_server_list   = array("ENTRIES"=> array(),"FOR_LIST"=> array());
29   var $take_over_id       = -1;
32   function servdns (&$config, $dn= NULL, $parent= NULL)
33   {
34     plugin::plugin ($config, $dn, $parent);
36     $this->DisplayName = _("DNS service");
38     $this->orig_dn = $dn;
40     /* Get record types for zones
41      */
42     $this->RecordTypes = getDnsRecordTypes(true);
44     /* Get all zone Informations
45      */
46     $this->Zones = getDNSZoneEntries($config,$dn);
47     
48     /* If there is at least one entry in this -> types, we have DNS enabled 
49      */
50     if(count($this->Zones) == 0){
51       $this->is_account = false;
52       $this->dns_server_list = $this->get_list_of_dns_servers();
53     }else{
54       $this->is_account = true;
55     }
56     $this->initially_was_account = $this->is_account;
57   }
60   function get_list_of_dns_servers()
61   {
62     $ret = array("ENTRIES"=> array(),"FOR_LIST"=> array());
63     $ldap = $this->config->get_ldap_link();
64     $ldap->cd($this->config->current['BASE']);
65     $ldap->search("(&(objectClass=dNSZone)(zoneName=*))",array("dn","zoneName"));
66     $dns = array();
67     while($attrs = $ldap->fetch()){
68       /* Skip own config */
69       if($this->dn != "new" && preg_match("/".normalizePreg($this->dn)."$/",$attrs['dn'])){
70         continue;
71       }
72       $dn = preg_replace("/^zoneName=[^,]+,/","",$attrs['dn']);
73       if(preg_match("/^cn=/",$dn) && !in_array($dn,$dns)){
74         $dns[] = $dn;
75       }
76     }
77     $i = 0;
78     foreach($dns as $dn){
79       $ldap->cat($dn,array('*'));
80       if($ldap->count()){
81         $i ++;
82         $attrs = $ldap->fetch();
83           $ret['ENTRIES'][$i]   = $attrs;
84           $ret['FOR_LIST'][$i] = $attrs['cn'][0];
85       }
86     }
87     return($ret);
88   }  
91   function get_dns_info_string($id)
92   {
93     $ret="";
94     $ldap = $this->config->get_ldap_link();
95     $ldap->cd($this->dns_server_list['ENTRIES'][$id]['dn']);
96     $ldap->search("(|(zoneName=*)(relativeDomainName=*))",array("dn"));
97     while($attrs = $ldap->fetch()){
98       $ret .= $attrs['dn']."\n";
99     }
100     return($ret);
101   }
104   function execute()
105   {
106     /* Call parent execute 
107      */
108     plugin::execute();
110     if($this->is_account && !$this->view_logged){
111       $this->view_logged = TRUE;
112       new log("view","server/".get_class($this),$this->dn);
113     }
115     /* Fill templating stuff 
116      */
117     $smarty= get_smarty();
118     $smarty->assign("dns_take_over",FALSE);
119     $smarty->assign("is_createable",$this->acl_is_createable());
120     $display= "";
123     $this->initially_was_account= $this->is_account;
124     /*****************/
125     /* Handle Take Over Actions
126     /*****************/
128     /* Give smarty the required informations */
129     $smarty->assign("dns_server_list", $this->dns_server_list['FOR_LIST']);
130     $smarty->assign("dns_server_list_cnt", count($this->dns_server_list['FOR_LIST']));
132     /* Take over requested, save id */
133     if(isset($_POST['take_over_src']) && isset($_POST['take_over'])){
134       $id = $_POST['take_over_src'];
135       if(isset($this->dns_server_list['ENTRIES'][$id])){
136         $this->take_over_id = $id;
137       }
138     }
140     /* Abort take over action */
141     if(isset($_POST['cancel_take_over'])){
142       $this->dialog =false;
143       $this->take_over_id = -1;
144       $this->dns_server_list = $this->get_list_of_dns_servers();
145     }
147     /* Display informartion about take over that will be started when saving this server
148      *  and hide default dns output
149      */
150     if($this->take_over_id != -1){
151       $this->dialog = FALSE;
152       $id = $this->take_over_id;
153       $info = $this->get_dns_info_string($id);
154       $smarty->assign("dns_take_over",TRUE);
155       $smarty->assign("info",$info);
156       $warning = sprintf(_("You are going to migrate the DNS setup from server '%s'."),$this->dns_server_list['ENTRIES'][$id]['cn'][0]);
157       $warning2 = _("The migration will be startet when you save this system. To cancel this action, use the cancel button below.");
158       $smarty->assign("warning",$warning);
159       $smarty->assign("warning2",$warning2);
160       return($smarty->fetch(get_template_path('servdns.tpl', TRUE)));
161     }
164     /* Do we need to flip is_account state? 
165      */
166     if (isset($_POST['modify_state'])){
167       $this->is_account= !$this->is_account;
168     }
170     if ($this->is_account){
171       $display= $this->show_disable_header(_("Remove DNS service"),
172           _("This server has DNS features enabled. You can disable them by clicking below."));
173     } else {
174       $display= $this->show_enable_header(_("Add DNS service"),
175           _("This server has DNS features disabled. You can enable them by clicking below."));
176       return ($display);
177     }
180     /* Edited or Added zone 
181      */
182     if(isset($_POST['SaveZoneChanges'])){
183       $this->dialog->save_object();
185       /* Check for errors  
186        */
187       if(count($this->dialog->check())){
188         foreach($this->dialog->check() as $msgs){
189           print_red($msgs); 
190         }
191       }else{
192         /* add new/edited zone 
193          */
194         $ret = $this->dialog->save();
195         if(!$this->dialog->isNew){
196           unset($this->Zones[$this->dialog->OldZoneName]);
197         }
198         $this->Zones[$ret['zoneName']] = $ret;
199         $this->dialog = FALSE;
200       }
201     }
203     /* Cancel zone edit / new 
204      */
205     if(isset($_POST['CancelZoneChanges'])){
206       $this->dialog = FALSE;
207     }
209     /* Add empty new zone 
210      */
211     if(isset($_POST['AddZone'])){
212       $this->dialog = new servdnseditZone($this->config,$this->dn);
213     }
215     /* Check for edit zone request 
216      */
217     $once = false;
218     foreach( $_POST as $name => $value){
219   
220       /* check all post for edit request 
221        */
222       if(preg_match("/^editZone_/",$name)&&!$once){
223         $once =true;
224         $tmp = preg_replace("/^editZone_/","",$name);
225         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
226         $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$tmp]);
227       }
229       /* check posts for delete zone 
230        */
231       if(preg_match("/^delZone_/",$name)&&!$once){
233         $once =true;
234         $tmp = preg_replace("/^delZone_/","",$name);
235         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
236      
237         /* Initiate deletion
238          */ 
239         $this->RemoveZone($tmp); 
240       }
241     }
243     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
244       $id = base64_decode($_GET['id']);
245       if(isset($this->Zones[$id])){
246          $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$id]);
247       }
248     }
250     if(isset($_GET['act']) && $_GET['act'] == "edit" && isset($_GET['id'])){
251       $id = base64_decode($_GET['id']);
252       if(isset($this->Zones[$id])){
253          $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$id]);
254       }
255     }
257     /* Show dialog 
258      */
259     if(is_object($this->dialog)){
260       $this->dialog->save_object();
261       $this->dialog->parent = $this;
262       return($this->dialog->execute());
263     }
265     /* Create Listbox with existing Zones 
266      */
267     $ZoneList = new divSelectBox("dNSZones");
268     $ZoneList -> SetHeight(254);
270     /* Add entries to divlist
271      */
272     $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>";
273     if($this->acl_is_removeable()){
274       $editImg.= "<input type='image' src='images/edittrash.png' name='delZone_%s'>";
275     }
277     $link = "<a href='?plug=".$_GET['plug']."&act=edit&id=%s'>%s</a>";
278     foreach($this->Zones as $zone => $values ){
279       $ZoneList->AddEntry(array(
280             array("string" => sprintf($link,base64_encode($zone),($zone))),
281             array("string" => sprintf($link,base64_encode($zone),_("Reverse zone")." : ".($values['ReverseZone']))),
282             array("string" => _("TTL")." : ".$values['sOAttl']),
283             array("string" => _("Class")." : ".$values['dNSClass']),
284             array("string" =>str_replace("%s",base64_encode($zone),$editImg))
285             ));
286     }    
288     /* Display tempalte 
289      */
290     $smarty->assign("ZoneList",$ZoneList->DrawList());
291     $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
292     return($display);
293   }
296   /* Delete specified zone
297    */
298   function RemoveZone($id)
299   {
300     $zones =  $this->getUsedZoneNames();
302     if(isset($this->Zones[$id]['InitialReverseZone'])){
303       $rev = FlipIp($this->Zones[$id]['InitialReverseZone']);
304     }else{
305       $rev = FlipIp($this->Zones[$id]['ReverseZone']);
306     }
308     $zonename = "";
309     if(isset($this->Zones[$id]['InitialzoneName'])){
310       $zonename= $this->Zones[$id]['InitialzoneName'];
311     }
313     $used = array();
315     /* Add Records which use this zoneName
316      */
317     if(isset($zones[$zonename])){
318       $used = array_merge($used,$zones[$zonename]);
319     }
321     /* Add Records which uses this reverse zone
322      */
323     if(isset($zones[$rev.".in-addr.arpa"])){
324       $used = array_merge($used,$zones[$rev.".in-addr.arpa"]);
325     } 
327     /* There are still entries using this configuration
328      *  Abort deletion
329      */
330     if(count($used)){
331       $i = 2;
332       $str ="";
333       foreach($used as $dn){
334         if($i > 0 && !preg_match("/,relativeDomainName=/",$dn)){
335           $i --;
336           $name = preg_replace("/^[^=]+=([^,]*),.*$/","\\1",$dn);
337           $zone = preg_replace("/^.*zoneName=([^,]*),.*$/","\\1",$dn);
338           $str.= $name.".".$zone." ";
339         }
340       }
342       /*  Only show 2 dns in the error message 
343        */
344       if(count($used)> 2) {
345         $str .=" ... ";
346       }
347       print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
348       return(false);
349     }else{
350       unset($this->Zones[$id]);
351       return(true);
352     }
353   } 
356   /* This funtion returns all used Zonenames 
357    */
358   function getUsedZoneNames()
359   {
360     $ret = array();
361     $ldap = $this->config->get_ldap_link();
362     $ldap->cd($this->config->current['BASE']);
363     $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
364     while($attr = $ldap->fetch()){
365       $ret[$attr['zoneName'][0]][] = $attr['dn'];
366     }
367     return($ret);
368   }
371   /* Remove dns service 
372    */
373   function remove_from_parent()
374   {
375     if($this->initially_was_account){
376       $bool = true;
377       $this->is_account = FALSE;
378       foreach($this->Zones as $key => $zone){
379         $bool= $bool & $this->RemoveZone($key);
380       }
382       if($bool){
383         $this->save();
384       }
385       return($bool);
386     }
387   }
390   /* Save to LDAP */
391   function save()
392   {
394     /* Take over handling
395      * - Create list of zones managed by source server 
396      * - Copy ldap entries to destination server 
397      * - Remove old zone entries from source
398      */
399     if($this->take_over_id != -1){
400       $del = array();
401       $id = $this->take_over_id;
402       $src = $this->dns_server_list['ENTRIES'][$id]['dn'];
403       $ldap = $this->config->get_ldap_link(); 
404       $ldap->ls("(objectClass=dnsZone)",$src,array('cn'));
405       while($attrs = $ldap->fetch()){
406         $src_zone = $attrs['dn'];
407         $dst_zone = preg_replace("/".normalizePreg($src)."$/",$this->dn,$src_zone);
408         $res = plugin::recursive_move($src_zone, $dst_zone);
410         if($res){
411           $del [] = $src_zone;
412         }
413       }
414       foreach($del as $src_zone){
415         $ldap->rmdir_recursive($src_zone);
416       }
417       return;
418     }
420     /* Save zone editor changes now */
421     foreach($this->Zones as $name => $zone){
422       if(isset($zone['zoneEditor'] ) && $zone['zoneEditor'] != NULL && is_object($zone['zoneEditor'])){
423         $zone['zoneEditor']->save();
424         unset($this->Zones[$name]['zoneEditor']);;
425       }
426     }
428     $ldap = $this->config->get_ldap_link();
429     $ldap->cd($this->config->current['BASE']);  
430   
431     /* Get differences 
432      */
433     $old_dn = $this->orig_dn;
434     if($old_dn == "new"){
435       $old_dn = $this->dn;
436     }
438     $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$old_dn);
440     /* Update dns to current object dn */ 
441     $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$old_dn);
442     $tmp2 = array();
443     foreach($tmp as $key1 => $data1){
444       $tmp2[$key1] = array();
445       foreach($data1 as $key2 => $data2){
446         $tmp2[$key1][preg_replace("/".normalizePreg($old_dn)."$/",$this->dn,$key2)] = $data2;
447       }
448     }
449     $tmp = $tmp2;
451     /* Updated zone entries if reverser or forward name has changed  
452      * Must be done before moving entries, else the given dn is invalid
453      */
454     if(isset($tmp['zoneUpdates'])){
455       foreach($tmp['zoneUpdates'] as $dn => $attrs){
456         $ldap->cd($dn);
457         $ldap->modify($attrs);
458         new log("modfiy","unknown/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
459         show_ldap_error($ldap->get_error(), sprintf(_("Updating of system server/dns with dn '%s' failed."),$this->dn));
460       }
461     }
463     /* Delete dns 
464      */
465     foreach($tmp['del'] as $dn => $del){
467       $for = $del['InitialzoneName'];
468       $rev = FlipIp($del['InitialReverseZone']).".in-addr.arpa";
470       $ldap->cd($dn);
471       $ldap->rmdir_recursive($dn);
472       new log("remove","unknown/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
473       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/dns with dn '%s' failed."),$this->dn));
475       /* Handle Post events */
476       if(preg_match("/^zoneName=/",$dn)){
477 #        $this->handle_post_events("remove",array("dn" => $dn,"zoneName" => $for));
478 #        $this->handle_post_events("remove",array("dn" => $dn,"zoneName" => $rev));
479       }
480     }
482     /* move follwoing entries
483      */
484     foreach($tmp['move'] as $src => $dst){
485       $this->recursive_move($src,$dst);
486     }
488     /* Add || Update new DNS entries
489      */
490     foreach($tmp['add'] as $dn => $attrs){
491       $ldap->cd($dn);
492       $ldap->cat($dn, array('dn'));
493       if($ldap->fetch()){
494         $ldap->cd($dn);
495         $ldap->modify ($attrs);
496         show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/dns with dn '%s' failed."),$this->dn));
498         /* Handle Post events */
499         if(preg_match("/^zoneName=/",$dn)){
500 #          $this->handle_post_events("modify",array("dn" => $dn,"zoneName" => $attrs['zoneName']));
501         }
502       }else{
503         $ldap->cd($dn);
504         $ldap->add($attrs);
505         show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/dns with dn '%s' failed."),$this->dn));
507         /* Handle Post events */
508         if(preg_match("/^zoneName=/",$dn)){
509 #          $this->handle_post_events("add",array("dn" => $dn,"zoneName" => $attrs['zoneName']));
510         }
511       }
512     }
513     $this->handle_post_events("modify");
514   }
517   /* Directly save new status flag */
518   function setStatus($value)
519   {
520     if($value == "none") return;
521     if(!$this->initially_was_account) return;
522     if(empty($this->StatusFlag)) return;
523     $ldap = $this->config->get_ldap_link();
524     $ldap->cd($this->dn);
525     $ldap->cat($this->dn,array("objectClass"));
526     if($ldap->count()){
528       $tmp = $ldap->fetch();
529       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
530         $attrs['objectClass'][] = $tmp['objectClass'][$i];
531       }
532       $flag = $this->StatusFlag;
533       $attrs[$flag] = $value;
534       $this->$flag = $value;
535       $ldap->modify($attrs);
536       show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for system server/dns with dn '%s' failed."),$this->dn));
537       $this->action_hook();
538     }
539   }
542   function getListEntry()
543   {
544     $fields               = goService::getListEntry(); 
545     $fields['Message']    = _("DNS service");
546     $fields['AllowEdit']  = true;
547     return($fields);
548   }
551   /* Get updates for status flag */
552   function updateStatusState()
553   {
554     if(empty($this->StatusFlag)) return;
556     $attrs = array();
557     $flag = $this->StatusFlag;
558     $ldap = $this->config->get_ldap_link();
559     $ldap->cd($this->cn);
560     $ldap->cat($this->dn,array($flag));
561     if($ldap->count()){
562       $attrs = $ldap->fetch();
563     }
564     if(isset($attrs[$flag][0])){
565       $this->$flag = $attrs[$flag][0];
566     }
567   }
570   /* Return plugin informations for acl handling */
571   static function plInfo()
572   {
573     return (array(
574           "plShortName"   => _("DNS service"),
575           "plDescription" => _("DNS service")." ("._("Services").")",
576           "plSelfModify"  => FALSE,
577           "plDepends"     => array(),
578           "plPriority"    => 83,
579           "plSection"     => array("administration"),
580           "plCategory"    => array("server"),
582           "plProvidedAcls"=> array(
583             "zoneName"      =>_("Zone name"),
584             "ReverseZone"   =>_("Reverse zone"),
585             "sOAprimary"    =>_("Primary dns server"),
586             "sOAmail"       =>_("Mail address"),
587             "sOAserial"     =>_("Serial"),
588             "sOArefresh"    =>_("Refresh"),
589             "sOAretry"      =>_("Retry"),
590             "sOAexpire"     =>_("Expire"),
591             "sOAttl"        =>_("TTL"),
592             "zoneRecords"   =>_("Zone records"))
593     ));
594   }
597 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
598 ?>