Code

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