Code

Fixed ip change problem
[gosa.git] / plugins / admin / systems / class_termDNS.inc
1 <?php
3 class termDNS extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account = true;
7   var $autonet        = false;
9   /* Basic informations 
10    */
11   var $attributes     = array("ipHostNumber","macAddress");
12   var $objectclasses  = array("whatever");
14   var $ipHostNumber   = "";    // IP address 
15   var $macAddress     = "";    // Mac address 
16   var $cn             = "";    // CN of currently edited device 
17   var $OrigCn         = "";    // Initial cn
18   var $IPisMust       = false;
19   var $MACisMust      = false;
20   var $dialog;
22   /* DCHP Attributes 
23    */
24   var $dhcpAttributes           = array("dhcpParentNode");
25   var $dhcpEnabled              = FALSE;
26   var $dhcp_is_Account          = FALSE;
27   var $dhcpParentNodes          = array();
28   var $dhcpParentNode           = "";
29   var $dhcpHostEntry            = array();
30   var $initial_dhcpParentNode   = "";
31   var $initial_dhcpHostEntry    = array();
32   var $initial_dhcp_is_Account  = FALSE;
33   
35   /* DNS attributes  
36    */
37   var $DNSattributes            = array("dNSClass","zoneName","dNSTTL");
38   var $DNS_is_Account           = false;
39   var $DNSinitially_was_account = false;
40   var $dnsEntry                 = array();
41   var $DNSenabled               = false;
42   var $hide_dns_check_box       = FALSE;
44   /*  Terminal dns 
45    */
46   function termDNS ($config, $dn,$objectClasses,$IPisMust = false)
47   {
48     /* We need to know which objectClasses are used, to store the ip/mac
49      * Because of different type of devices   
50      */
51     $this->objectclasses  =  $objectClasses;
52     $this->IPisMust       = $IPisMust;
54     plugin::plugin ($config, $dn);
56     if(isset($this->attrs['cn'][0])){
57       $this->OrigCn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
58       $this->cn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
59     }
61  
62     /************
63      * DHCP 
64      ************/
66     /* Hide all dhcp specific code, if dhcp plugin is not present in config */
67     $dhcpEnabled = FALSE;
68     foreach($this->config->data['TABS']['SERVTABS'] as $tab){
69       if(preg_match("/^servdhcp$/",$tab['CLASS'])){
70         $this->dhcpEnabled = TRUE;
71       }
72     }
73     if($this->dhcpEnabled){
74       $this->dhcpParentNodes = $this->get_dhcp_parent_nodes();
75       $this->dhcpParentNode  = $this->get_dhcp_parent_node();
76       if($this->dhcpParentNode){
77         $this->dhcp_is_Account = TRUE;
78         $this->initial_dhcp_is_Account = TRUE;
79         $this->dhcpHostEntry  = $this->get_dhcp_host_entry();    
80       }
81       $this->initial_dhcpHostEntry = $this->dhcpHostEntry;
82       $this->initial_dhcpParentNode= $this->dhcpParentNode;
83     }
86     /************
87      * Autonetwork hook 
88      ************/
89  
90     /* Do we have autonet support? */
91     if (isset($this->config->data['MAIN']['AUTO_NETWORK_HOOK'])){
92       $this->autonet= true;
93     }
96     /************
97      * DNS
98      ************/
99  
100     /* Hide all dns specific code, if dns is not available 
101      */
102     $DNSenabled = false;
103     foreach($this->config->data['TABS']['SERVTABS'] as $tab){
104       if(preg_match("/^servdns$/",$tab['CLASS'])){
105         $this->DNSenabled = true;
106       }
107     }
108     if(!$this->DNSenabled){
109       $this->DNS_is_account = false;
110       return;
111     }
113     if($this->DNSenabled){
115       /* Get Zones  
116        */
117       $this->Zones        = getAvailableZones($config);
119       /* Get Entry 
120        */
121       $this->dnsEntry     = getDNSHostEntries($config,$this->OrigCn);
123       /* Remove A record which equals $this->ipHostNumber
124        */
125       $ptr = $this->get_pTRRecord();
126       foreach($this->dnsEntry['RECORDS'] as $key => $rec){
127         if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
128           unset($this->dnsEntry['RECORDS'][$key]);
129         }
130         if(($rec['type'] == "pTRRecord") && ($rec['value'] == $ptr)){
131           unset($this->dnsEntry['RECORDS'][$key]);
132         }
133       }
135       /* Get Record types 
136        */
137       $this->RecordTypes  = getDnsRecordTypes();
139       /* If there is at least one entry in this -> types, we have DNS enabled 
140        */
141       if($this->dnsEntry['exists']){
142         $this->DNS_is_account = true;
143       }else{
144         $this->DNS_is_account = false;
145       }
146    
147     }
148  
149     /* Store initally account settings 
150      */
151     $this->DNSinitially_was_account = $this->DNS_is_account;
152   }
155   function netmaskIsCoherent($idZone) 
156   {
157     $netmask = FlipIp(str_replace(".in-addr.arpa","",getNameFromMix($idZone)));
158     if(!strstr($this->ipHostNumber, $netmask)){
159       return false;
160     }else{
161       return true;
162     }
163   }
166   function getVarsForSaving($attrs) 
167   {
168     foreach($this->attributes as $attr){
169       if(!empty($this->$attr)){
170         $attrs[$attr] = $this->$attr;
171       }
172     }
173     return($attrs); 
174   }
176   function execute()
177   {
178           /* Call parent execute */
179     $smarty= get_smarty();
180     $display= "";
182     $smarty->assign("staticAddress", ""); 
183     $smarty->assign("autonet", $this->autonet);
184  
185     /* Check for autonet button */
186     if ($this->autonet && isset($_POST['autonet'])){
187       $cmd= $this->config->data['MAIN']['AUTO_NETWORK_HOOK'];
188       if(!empty($cmd) && $this->cn != ""){
189         $res = shell_exec($cmd." ".$this->cn);
190         if(!$res){
191           print_red(sprintf(_("Can't execute specified AUTO_NETWORK_HOOK '%s'. Please check your gosa.conf."),$cmd));
192         } else {
193           $res= split(';', trim($res));
194           if (isset($res[0]) && $res[0] != ""){
195             $this->ipHostNumber= $res[0];
196           }
197           if (isset($res[1]) && $res[1] != ""){
198             $this->macAddress= $res[1];
199           }
200         }
201       }
202     }
204     
205   
206     /**********
207      * DHCP Handling
208      **********/
209  
210     if(isset($_POST['dhcpEditOptions'])){
212       if(count($this->dhcpHostEntry) == 0){
213         $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
214       }else{
215         $this->dialog = new dhcpHost($this->dhcpHostEntry,TRUE);
216       }
217       $this->dialog->cn = $this->cn; 
218       $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress; 
219       if(!empty($this->ipHostNumber)){
220         $this->dialog->statements['fixed-address'] = $this->ipHostNumber; 
221       }
222     }
224     if(isset($_POST['cancel_dhcp'])){
225       $this->dialog = FALSE; 
226     }
228     if(isset($_POST['save_dhcp'])){
229       $this->dialog->save_object();
230       
231       $msgs = $this->dialog->check(array());
232       if(count($msgs)){
233         foreach($msgs as $msg){
234           print_red($msg);
235         }
236       }else{
237         $this->dhcpHostEntry = $this->dialog->save();
238         $this->dialog = FALSE; 
239       }
240     }
242     if(isset($this->dialog) && $this->dialog != FALSE){
243       $this->dialog->save_object();
244       return($this->dialog->execute());
245     }
246  
247     $smarty->assign("dhcpEnabled",    $this->dhcpEnabled);
248     $smarty->assign("dhcp_is_Account",$this->dhcp_is_Account);
249     $smarty->assign("dhcpParentNode", $this->dhcpParentNode);
250     $smarty->assign("dhcpParentNodes",$this->dhcpParentNodes);
251     $smarty->assign("dhcpParentNodeCnt",count($this->dhcpParentNodes));
253     /* There is no dns available 
254      */
255     if($this->DNSenabled == false){
256        
257       /* Is IP address must ? */ 
258       $smarty->assign("DNS_is_account",false);  
259       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
260     
261       /* Assign smarty all non DNs attributes */
262       foreach($this->attributes as $attr){
263         $smarty->assign($attr,$this->$attr);
264         $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
265       }
266       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
268       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
269     }else{
270       $smarty->assign("DNS_is_account",true); 
272       /* Add new empty array to our record list */
273       if(isset($_POST['AddNewRecord'])){
274         $this->dnsEntry['RECORDS'][]  =array("type"=>"aRecord","value"=>"");
275       }
277       /* Handle all posts */ 
278       $only_once =true;
279       foreach($_POST as $name => $value){
281         /* Check if we have to delete a record entry */
282         if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
284           /* Avoid performing this once again */
285           $only_once = false;
287           /* Extract id for specified entry */
288           $id = preg_replace("/RemoveRecord_/","",$name);
289           $id = preg_replace("/_.*$/","",$id);
291           /* Delete this record, mark edited entries to be able to delete them */
292           if(isset($this->dnsEntry['RECORDS'][$id])){
293             unset($this->dnsEntry['RECORDS'][$id]);
294           }
295         }
296       }
298       /* Assign smarty all non DNs attributes */
299       foreach($this->attributes as $attr){
300         $smarty->assign($attr,$this->$attr);
301         $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
302       }
304       /* Assign smarty all DNS attributes */
305       foreach($this->DNSattributes as $attr){
306         $smarty->assign($attr,$this->dnsEntry[$attr]);
307         $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
308       }
310       /* Assign all needed vars */
311       $smarty->assign("DNSaccountACL",chkacl($this->acl,"termDNS"));
313       $smarty->assign("DNSAccount",$this->DNS_is_account);
314       $smarty->assign("hide_dns_check_box",$this->hide_dns_check_box);
315   
316       $smarty->assign("Zones",$this->Zones);
317       $smarty->assign("ZoneCnt",count($this->Zones));
318       $smarty->assign("ZoneKeys",($this->Zones));
319       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
321       /* Create zone array */
322       $idZones = array();
323       foreach($this->Zones as $id => $zone){
324         if($this->netmaskIsCoherent($id)) {
325           $idZones[$id] = $zone;
326         }else{
327           $idZones[$id] = $zone."&nbsp;("._("Not matching").")";
328         }
329       }    
330       $smarty->assign("Zones",$idZones);
331       $smarty->assign("ZoneKeys", $this->Zones);
333       $tmp = $this->generateRecordsList();
335       $changeStateForRecords = $tmp['changeStateForRecords'];
337       $smarty->assign("records",$tmp['str']);
338       $smarty->assign("changeStateForRecords",$changeStateForRecords);
339       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
341       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
342     }
343     return($display);
344   }
346   function remove_from_parent()
347   {
348     /*
349     $ldap = $this->config->get_ldap_link();
350     $ldap->cd($this->orig_dn);
351     $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("relativeDomainName","zoneName"));
352     while($attr = $ldap->fetch()){  
353       $ldap->cd($attr['dn']);
354       $ldap->rmDir($attr['dn']);
355       show_ldap_error("Record:".$ldap->get_error(), _("Removing terminal from DNS object failed")); 
356     }
357     */
358   }
360   /* Save data to object */
361   function save_object()
362   {
363     /* Save all posted vars */
364     plugin::save_object();
365   
367     /* Handle DHCP Posts*/ 
368     if($this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
369       foreach($this->dhcpAttributes as $attr){
370         if(isset($_POST[$attr])){
371           $this->$attr = $_POST[$attr];
372         }
373       }
374       if(isset($_POST['dhcp_is_Account'])){
375         $this->dhcp_is_Account = TRUE;
376       }else{
377         $this->dhcp_is_Account = FALSE;
378       }
379     }
380  
381  
382     /* Get dns attributes */
383     if(($this->DNSenabled) && (isset($_POST['network_tpl_posted'])) && chkacl($this->acl,"termDNS") == ""){
385       /* Check for posted record changes */
386       if(is_array($this->dnsEntry['RECORDS'])){
387         foreach($this->dnsEntry['RECORDS'] as $key => $value){
389           /* Check if type has changed */
390           if(isset($_POST['RecordTypeSelectedFor_'.$key])){
391             $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
392           }
393           /* Check if value has changed */
394           if(isset($_POST['RecordValue_'.$key])){
395             $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
396           }
397         }
398       }
399       /* Get all basic DNS attributes (TTL, Clas ..)*/
400       foreach($this->DNSattributes as $attr){
401         if(isset($_POST[$attr])){
402           $this->dnsEntry[$attr] = $_POST[$attr];
403         }
404       }
406       /* Enable diable DNS */
407       if(isset($_POST['enableDNS'])){
408         $this->DNS_is_account = true;
409       }else{
410         $this->DNS_is_account = false;
411       }
412       if($this->hide_dns_check_box){
413         $this->DNS_is_account = true;
414       }
415     }
416   }
419   /* Check supplied data */
420   function check()
421   {
422     /* Call common method to give check the hook */
423     $message= plugin::check();
425     if($this->dhcpEnabled && $this->dhcp_is_Account && $this->dhcpParentNode != "" && count($this->dhcpHostEntry) == 0){
426 #      $message[] =_("You have not configured your dhcp settings yet.");
427     }
429     /* Check if ip must be given
430      */  
431     if(($this->IPisMust)||($this->DNS_is_account)){
432   
433       /* Check if ip is empty 
434        */
435       if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
436         $message[]= _("The required field 'IP-address' is not set.");
437       }
439     }
441     /* check if given ip is valid ip */
442     if ($this->ipHostNumber != "" && !is_ip($this->ipHostNumber)){
443       $message[]= _("Wrong IP format in field IP-address.");
444     }
446     /* Check if mac is empty 
447      */
448     if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
449       $message[]= _("The required field 'MAC-address' is not set.");
450     }
452     /* Check if given mac is valid mac 
453      */
454     if(!is_mac($this->macAddress)){
455       $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
456     }
458     /* only perfrom this checks if this is a valid DNS account */
459     if($this->DNS_is_account){
461       $checkArray = array();
462       $onlyOnce   = array();
464       $tmp = array_flip($this->Zones);
465       $tmp2 = $tmp[$this->dnsEntry['zoneName']];
466       if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
467         $tmp2 = preg_replace("/^.*\//","",$tmp2);
468         $message[] =sprintf(_("The specified IP address '%s' is not matching the selected reverse zone entry '%s'."),$this->ipHostNumber,$tmp2);
469       }
471                         // There can be many CNAME records
472       //$onlyOnce['cNAMERecord'] = 0;
474       /* Walk through all entries and detect duplicates or mismatches
475        */  
476       foreach($this->dnsEntry['RECORDS'] as $name => $values){
478         /* Count record values, to detect duplicate entries for a specific record
479          */
480         if(!isset($checkArray[$values['type']][$values['value']])){
481           $checkArray[$values['type']][$values['value']] = 0;
482         }else{
483           $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
484         }
486         /* Check if given entries in $onlyOnce are used more than once
487          */
488         if(isset($onlyOnce[$values['type']])){
489           $onlyOnce[$values['type']] ++;
490           if($onlyOnce[$values['type']] > 1){
491             $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
492           }
493         }
495         /* Skip txt record ... 
496          */
497         if($values['type'] == "tXTRecord") continue;
499         /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress 
500          */
501         if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
502           $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."), 
503                $this->ipHostNumber);
504         }
506         /* only lower-case is allowed in record entries ... 
507          */
508         if($values['value'] != strtolower($values['value'])){
509           $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
510         }
511       }
512     }
513     return ($message);
514   }
517   /* Save to LDAP */
518   function save($dn)
519   {
520     $ldap= $this->config->get_ldap_link();
521    
522     /*******************/ 
523     /* IP-MAC HANDLING */
524     /*******************/ 
526     /* $dn was posted as parameter */
527     $this->dn = $dn;
528     
529     /* Save DNS setting & ip/Mac*/
530     plugin::save();
532     /* Write back to ldap */
533     $ldap->cd($this->dn);
534     $this->cleanup();
535     $ldap->modify ($this->attrs); 
537     /****************/ 
538     /* DHCP HANDLING */
539     /****************/ 
540   
541     /* New entry */
542     if($this->dhcpEnabled){
544       if(count($this->dhcpHostEntry) == 0){
545         $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
546         $this->dialog->cn = $this->cn;
547         $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
548         if(!empty($this->ipHostNumber)){
549           $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
550         }
551         $this->dialog->execute();
552         $this->dialog->save_object(); 
553         $this->dhcpHostEntry = $this->dialog->save();
554         if(count($this->dhcpHostEntry['dhcpOption']) == 0){
555           $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
556         }
557       }
559       /* Write mac address to dhcp settings */
560       if($this->dhcp_is_Account){
561         if(!isset($this->dhcpHostEntry['dhcpHWAddress'][0]) || 
562             !preg_match("/ethernet ".$this->macAddress."/",$this->dhcpHostEntry['dhcpHWAddress'][0])){
563           $this->dhcpHostEntry['dhcpHWAddress'] = array("ethernet ".$this->macAddress);
564           $this->dhcpHostEntry['MODIFIED'] = TRUE;
565         }
566       }
567   
568       /* Updated IP host number */
569       if($this->dhcp_is_Account){
570         foreach($this->dhcpHostEntry['dhcpStatements'] as $id => $value){
571           if(preg_match("/^fixed-address/",$value)){
572             $this->dhcpHostEntry['dhcpStatements'][$id] = "fixed-address ".$this->ipHostNumber;
573             $this->dhcpHostEntry['MODIFIED'] = TRUE;
574           }
575         }
576       }
578       /* Unset dhcpStatements if this attribute is empty  */
579       if(isset($this->dhcpHostEntry['dhcpStatements']) && 
580           ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
581         unset($this->dhcpHostEntry['dhcpStatements']);
582       }
583   
584       /* DHCP removed */
585       if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
586         $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
587         show_ldap_error($ldap->get_error(),_("Removing dhcp entry for this object failed."));
589         $tmp = new servdhcp($this->config,$this->dhcpParentNode);
590         $tmp->handle_post_events("remove");
591       }
593       /* DHCP Added */
594       if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
596         $dn = "cn=".$this->cn.",".$this->dhcpParentNode;
598         $attrs = $this->dhcpHostEntry;
599         $attrs['cn'] = $this->cn;
600         
601         unset($attrs['MODIFIED']);
602         unset($attrs['dn']);
603         $ldap->cd($dn);
604         $res = $ldap->add($attrs);
606         $tmp = new servdhcp($this->config,$this->dhcpParentNode);
607         $tmp->handle_post_events("add");
609 #        print_a($attrs);
610 #        print("cn=".$this->cn.",".$this->dhcpParentNode);
611         show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
612       }
614       /* DHCP still activated */
615       if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
617         /* DHCP node changed */
618         if(($this->initial_dhcpParentNode != $this->dhcpParentNode) || 
619            ($this->cn != $this->OrigCn)){
620           $attrs = $this->dhcpHostEntry;
621           $attrs['cn'] = $this->cn;
622           unset($attrs['dn']);
623           unset($attrs['MODIFIED']);
624           $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
625           $res = $ldap->add($attrs);
627           $tmp = new servdhcp($this->config,$this->dhcpParentNode);
628           $tmp->handle_post_events("modify");
630           show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
631           if($res){
632             $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
633             show_ldap_error($ldap->get_error(),_("Removing old dhcp entry failed."));
634           }
635         }
636          
637         /* SAME node but modified */ 
638         if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1  && 
639             $this->initial_dhcpParentNode == $this->dhcpParentNode){
640           $attrs = $this->dhcpHostEntry;
641           unset($attrs['dn']);
642           unset($attrs['MODIFIED']);
643           $ldap->cd($this->dhcpHostEntry['dn']);
644           $ldap->modify($attrs);
645           
646           $tmp = new servdhcp($this->config,$this->dhcpParentNode);
647           $tmp->handle_post_events("modify");
649           show_ldap_error($ldap->get_error(),_("Modifying dhcp entry failed."));
650         }    
651       }
652     }
653       
655     /****************/ 
656     /* DNS HANDLING */
657     /****************/ 
659     /* If isn't DNS account but initially was DNS account 
660        remove all DNS entries 
661      */ 
662     if((!$this->DNSenabled) || (!$this->DNS_is_account && !$this->DNSinitially_was_account)){
663       return;
664     }else{
666       /* Add ipHostNumber to aRecords
667        */
668       if(!empty($this->ipHostNumber)){
669         $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
670         $ptr = $this->get_pTRRecord();
671         if(!empty($ptr)){
672           $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
673         }
674       }
676       /* Create diff and follow instructions 
677        * If Account was disabled, remove account by setting exists to false
678        */
679       if((!$this->DNS_is_account)&&($this->DNSinitially_was_account)){  
680         $this->dnsEntry['exists'] = false;
681         $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
682       }else{
683         $this->dnsEntry['exists'] = $this->DNS_is_account;
684         $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
685       }   
687       /* move follwoing entries 
688        */
689       foreach($tmp['move'] as $src => $dst){
690         $this->recursive_move($src,$dst);
691       }
693       /* Delete dns */
694       foreach($tmp['del'] as $dn => $del){
695         $ldap->cd($dn);
696         $ldap->rmdir_recursive($dn);
697       }
699       /* Add || Update new DNS entries 
700        */
701       foreach($tmp['add'] as $dn => $attrs){
702         $ldap->cd($dn);
703         $ldap->cat($dn, array('dn'));
704         if(count($ldap->fetch())){
705           $ldap->cd($dn);
706           $ldap->modify ($attrs); 
707         }else{
708           $ldap->cd($dn);
709           $ldap->add($attrs);
710         }
711       }
713       /* Display errors 
714        */
715       if($ldap->get_error() != "Success"){
716         show_ldap_error("Record:".$ldap->get_error(), _("Saving terminal to DNS object failed")); 
717       }
719       /* Handle post event */
720       $tmp = new servdns($this->config,$this->dn);
721       $tmp->handle_post_events("modify");
722     }
723   }
725   /*  Create html table with all used record types
726    */
727   function generateRecordsList()
728   {
729     $changeStateForRecords = "";
730     
731     if(!$this->DNS_is_account) {
732       $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
733       return $str;
734     }
735  
736     $str = "<table summary='' width='100%'>";
737     foreach($this->dnsEntry['RECORDS'] as $key => $entry){
739         $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
740         $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
741         $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
743         $str.=" <tr>".
744           "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
745           "   <td><input ".chkacl($this->acl,"termDNS")." type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
746           "   <td><input ".chkacl($this->acl,"termDNS")." type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
747           "</tr>";
748     }
750     $str.= "  <tr>".
751            "    <td colspan=2 width='50%'></td><td>".
752            "      <input type='submit' value='"._("Add")."' name='AddNewRecord' ".chkacl($this->acl,"termDNS")." >".
753            "    </td>".
754            "  </tr>".
755            "</table>";
756      $ret =  array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
757     return($ret);
758   }
761   /* Create a html select box which allows us to select different types of records 
762    */
763   function generateRecordListBox($selected,$name)
764   {
765     $str = "<select ".chkacl($this->acl,"termDNS")."  name='".$name."' id='".$name."'>";
766     foreach($this->RecordTypes as $type => $value){
767       $use = "";
768       if($type == $selected){
769         $use = " selected ";
770       }
771       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
772     }
773     $str.="</select>";
774     return($str); 
775   }
777   
778   function get_dhcp_host_entry()
779   {
780     $attrs = array();
781     $dn = $this->get_dhcp_host_entry_dn();
782     if($dn){
783       $ldap = $this->config->get_ldap_link();
784       $ldap->cd($this->config->current['BASE']);
785       $ldap->cat($dn,array("*"));
786       if($ldap->count()){
787         $attrs = $ldap->fetch();
788         foreach($attrs as $key => $value){
789           if(is_numeric($key) || ($key == "count")){
790             unset($attrs[$key]);
791           }
792           if(is_array($value) && isset($value['count'])){
793             unset($attrs[$key]['count']);
794           }
795         }
796       }
797     }
798     return($attrs);
799   }
802   function get_dhcp_host_entry_dn()
803   {
804     $ldap = $this->config->get_ldap_link();
805     $ldap->cd($this->config->current['BASE']);
806     $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
808     if($ldap->count()){
809       $attr = $ldap->fetch();
810       return($attr['dn']);
811     }else{
812       return("");
813     }
814   }  
817   function get_dhcp_parent_node()
818   {
819     return(preg_replace("/^cn=".normalizePreg($this->cn).",/","",$this->get_dhcp_host_entry_dn()));
820   }
823   function get_dhcp_parent_nodes()
824   {
825     $ldap = $this->config->get_ldap_link();
826     $ldap->cd($this->config->current['BASE']);
827     $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
828     
829     $dhcp_dns = array();
830     while($attr = $ldap->fetch()){
831       $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
832     }
833  
834     foreach($dhcp_dns as $key => $pri_dns){
835       $ldap->cat($pri_dns,array("cn"));
836       $tmp = $ldap->fetch();
837       if(isset($tmp['cn'][0])){
838         $dhcp_dns[$key] = $tmp['cn'][0];
839       }else{
840         unset($dhcp_dns[$key]);
841       }
842     }
844     $tmp = $tmp2 = array();
845     foreach($dhcp_dns as $dn => $cn){
846       $ldap->cd($dn);
847       $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
848                     "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
849       while($attr = $ldap->fetch()){
850         $tmp[$attr['dn']] = $attr['cn'][0];
851       }
852       $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.")&nbsp;"));
853     }
854     return($tmp2);
855   }
857   
858   /* this function returns the default ptr record entry */
859   function get_pTRRecord()
860   {
861     if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
862       $ldap = $this->config->get_ldap_link();
863       $ldap->cat(getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
864       $attrs = $ldap->fetch();
865       $tmp = array_flip($this->Zones);
866       $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
867       $tmp = trim(preg_replace("/\.in-addr.arpa$/","",$tmp));
868       $ptr = preg_replace("/^".normalizePreg(FlipIp($tmp))."\./","",$this->ipHostNumber);
869       return($ptr);
870     }else{
871       return(FALSE);
872     }
873   }
875   
876   function create_tree($arr,$base,$current = "")
877   {
878     $ret = array();
879     foreach($arr as $r => $name){
880       $base_part = str_replace($base,"",$r);
881       if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
882         $ret[$r] = $current.$name;
883         $tmp = $this->create_tree($arr,$r,$current.".&nbsp;");
884         foreach($tmp as $sub_key => $sub_name){
885           $ret[$sub_key] = $sub_name;
886         }
887       } 
888     }
889     return($ret);
890   }
892   function force_dns()
893   {
894     if($this->DNSenabled){
895       $this->DNS_is_account  = TRUE;
896       $this->hide_dns_check_box = TRUE;
897     }
898   }
901 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
902 ?>