Code

Updated termDNS check.
[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$/i",$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   }
347   function remove_from_parent()
348   {
349     if($this->DNSinitially_was_account){
350       $ldap = $this->config->get_ldap_link();
351       $tmp = array();
352       $this->dnsEntry['exists'] = false;
353       $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
355       /* Delete dns */
356       foreach($tmp['del'] as $dn => $del){
357         $ldap->cd($dn);
358         $ldap->rmdir_recursive($dn);
359         show_ldap_error("Record:".$ldap->get_error(), _("Removing terminal from DNS object failed")); 
360       }
361     }
362   }
365   /* Save data to object */
366   function save_object()
367   {
368     /* Save all posted vars */
369     plugin::save_object();
370   
372     /* Handle DHCP Posts*/ 
373     if($this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
374       foreach($this->dhcpAttributes as $attr){
375         if(isset($_POST[$attr])){
376           $this->$attr = $_POST[$attr];
377         }
378       }
379       if(isset($_POST['dhcp_is_Account'])){
380         $this->dhcp_is_Account = TRUE;
381       }else{
382         $this->dhcp_is_Account = FALSE;
383       }
384     }
385  
386  
387     /* Get dns attributes */
388     if(($this->DNSenabled) && (isset($_POST['network_tpl_posted'])) && chkacl($this->acl,"termDNS") == ""){
390       /* Check for posted record changes */
391       if(is_array($this->dnsEntry['RECORDS'])){
392         foreach($this->dnsEntry['RECORDS'] as $key => $value){
394           /* Check if type has changed */
395           if(isset($_POST['RecordTypeSelectedFor_'.$key])){
396             $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
397           }
398           /* Check if value has changed */
399           if(isset($_POST['RecordValue_'.$key])){
400             $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
401           }
402         }
403       }
404       /* Get all basic DNS attributes (TTL, Clas ..)*/
405       foreach($this->DNSattributes as $attr){
406         if(isset($_POST[$attr])){
407           $this->dnsEntry[$attr] = $_POST[$attr];
408         }
409       }
411       /* Enable diable DNS */
412       if(isset($_POST['enableDNS'])){
413         $this->DNS_is_account = true;
414       }else{
415         $this->DNS_is_account = false;
416       }
417       if($this->hide_dns_check_box){
418         $this->DNS_is_account = true;
419       }
420     }
421   }
424   /* Check supplied data */
425   function check()
426   {
427     /* Call common method to give check the hook */
428     $message= plugin::check();
430     if($this->dhcpEnabled && $this->dhcp_is_Account && $this->dhcpParentNode != "" && count($this->dhcpHostEntry) == 0){
431 #      $message[] =_("You have not configured your dhcp settings yet.");
432     }
434     /* Check if ip must be given
435      */  
436     if(($this->IPisMust)||($this->DNS_is_account)){
437   
438       /* Check if ip is empty 
439        */
440       if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
441         $message[]= _("The required field 'IP-address' is not set.");
442       }
444     }
446     /* check if given ip is valid ip */
447     if ($this->ipHostNumber != "" && !is_ip($this->ipHostNumber)){
448       $message[]= _("Wrong IP format in field IP-address.");
449     }
451     if($this->MACisMust || $this->dhcp_is_Account){
452       /* Check if mac is empty 
453        */
454       if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
455         $message[]= _("The required field 'MAC-address' is not set.");
456       }
457     }
459     /* Check if given mac is valid mac 
460      */
461     if(!empty($this->macAddress) && !is_mac($this->macAddress)){
462       $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
463     }
465     /* only perfrom this checks if this is a valid DNS account */
466     if($this->DNS_is_account){
468       $checkArray = array();
469       $onlyOnce   = array();
471       $tmp = array_flip($this->Zones);
472       $tmp2 = $tmp[$this->dnsEntry['zoneName']];
473       if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
474         $tmp2 = preg_replace("/^.*\//","",$tmp2);
475         $message[] =sprintf(_("The specified IP address '%s' is not matching the selected reverse zone entry '%s'."),$this->ipHostNumber,$tmp2);
476       }
478                         // There can be many CNAME records
479       //$onlyOnce['cNAMERecord'] = 0;
481       /* Walk through all entries and detect duplicates or mismatches
482        */  
483       foreach($this->dnsEntry['RECORDS'] as $name => $values){
485         /* Count record values, to detect duplicate entries for a specific record
486          */
487         if(!isset($checkArray[$values['type']][$values['value']])){
488           $checkArray[$values['type']][$values['value']] = 0;
489         }else{
490           $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
491         }
493         /* Check if given entries in $onlyOnce are used more than once
494          */
495         if(isset($onlyOnce[$values['type']])){
496           $onlyOnce[$values['type']] ++;
497           if($onlyOnce[$values['type']] > 1){
498             $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
499           }
500         }
502         /* Skip txt record ... 
503          */
504         if($values['type'] == "tXTRecord") continue;
506         /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress 
507          */
508         if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
509           $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."), 
510                $this->ipHostNumber);
511         }
513         /* only lower-case is allowed in record entries ... 
514          */
515         if($values['value'] != strtolower($values['value'])){
516           $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
517         }
518       }
519     }
520     return ($message);
521   }
524   /* Save to LDAP */
525   function save($dn)
526   {
527     $ldap= $this->config->get_ldap_link();
528    
529     /*******************/ 
530     /* IP-MAC HANDLING */
531     /*******************/ 
533     /* $dn was posted as parameter */
534     $this->dn = $dn;
535     
536     /* Save DNS setting & ip/Mac*/
537     plugin::save();
539     /* Write back to ldap */
540     $ldap->cd($this->dn);
541     $this->cleanup();
542     $ldap->modify ($this->attrs); 
544     /****************/ 
545     /* DHCP HANDLING */
546     /****************/ 
547   
548     /* New entry */
549     if($this->dhcpEnabled){
551       if(count($this->dhcpHostEntry) == 0){
552         $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
553         $this->dialog->cn = $this->cn;
554         $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress;
555         if(!empty($this->ipHostNumber)){
556           $this->dialog->statements['fixed-address'] = $this->ipHostNumber;
557         }
558         $this->dialog->execute();
559         $this->dialog->save_object(); 
560         $this->dhcpHostEntry = $this->dialog->save();
561         if(count($this->dhcpHostEntry['dhcpOption']) == 0){
562           $this->dhcpHostEntry['dhcpOption']= array("host-name ".$this->cn);
563         }
564       }
566       /* Write mac address to dhcp settings */
567       if($this->dhcp_is_Account){
568         if(!isset($this->dhcpHostEntry['dhcpHWAddress'][0]) || 
569             !preg_match("/ethernet ".$this->macAddress."/",$this->dhcpHostEntry['dhcpHWAddress'][0])){
570           $this->dhcpHostEntry['dhcpHWAddress'] = array("ethernet ".$this->macAddress);
571           $this->dhcpHostEntry['MODIFIED'] = TRUE;
572         }
573       }
574   
575       /* Updated IP host number */
576       if($this->dhcp_is_Account){
577         foreach($this->dhcpHostEntry['dhcpStatements'] as $id => $value){
578           if(preg_match("/^fixed-address/",$value)){
579             $this->dhcpHostEntry['dhcpStatements'][$id] = "fixed-address ".$this->ipHostNumber;
580             $this->dhcpHostEntry['MODIFIED'] = TRUE;
581           }
582         }
583       }
585       /* Unset dhcpStatements if this attribute is empty  */
586       if(isset($this->dhcpHostEntry['dhcpStatements']) && 
587           ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
588         unset($this->dhcpHostEntry['dhcpStatements']);
589       }
590   
591       /* DHCP removed */
592       if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
593         $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
594         show_ldap_error($ldap->get_error(),_("Removing dhcp entry for this object failed."));
596         $tmp = new servdhcp($this->config,$this->dhcpParentNode);
597         $tmp->handle_post_events("remove");
598       }
600       /* DHCP Added */
601       if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
603         $dn = "cn=".$this->cn.",".$this->dhcpParentNode;
605         $attrs = $this->dhcpHostEntry;
606         $attrs['cn'] = $this->cn;
607         
608         unset($attrs['MODIFIED']);
609         unset($attrs['dn']);
610         $ldap->cd($dn);
611         $res = $ldap->add($attrs);
613         $tmp = new servdhcp($this->config,$this->dhcpParentNode);
614         $tmp->handle_post_events("add");
616 #        print_a($attrs);
617 #        print("cn=".$this->cn.",".$this->dhcpParentNode);
618         show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
619       }
621       /* DHCP still activated */
622       if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
624         /* DHCP node changed */
625         if(($this->initial_dhcpParentNode != $this->dhcpParentNode) || 
626            ($this->cn != $this->OrigCn)){
627           $attrs = $this->dhcpHostEntry;
628           $attrs['cn'] = $this->cn;
629           unset($attrs['dn']);
630           unset($attrs['MODIFIED']);
631           $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
632           $res = $ldap->add($attrs);
634           $tmp = new servdhcp($this->config,$this->dhcpParentNode);
635           $tmp->handle_post_events("modify");
637           show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
638           if($res){
639             $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
640             show_ldap_error($ldap->get_error(),_("Removing old dhcp entry failed."));
641           }
642         }
643          
644         /* SAME node but modified */ 
645         if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1  && 
646             $this->initial_dhcpParentNode == $this->dhcpParentNode){
647           $attrs = $this->dhcpHostEntry;
648           unset($attrs['dn']);
649           unset($attrs['MODIFIED']);
650           $ldap->cd($this->dhcpHostEntry['dn']);
651           $ldap->modify($attrs);
652           
653           $tmp = new servdhcp($this->config,$this->dhcpParentNode);
654           $tmp->handle_post_events("modify");
656           show_ldap_error($ldap->get_error(),_("Modifying dhcp entry failed."));
657         }    
658       }
659     }
660       
662     /****************/ 
663     /* DNS HANDLING */
664     /****************/ 
666     /* If isn't DNS account but initially was DNS account 
667        remove all DNS entries 
668      */ 
669     if((!$this->DNSenabled) || (!$this->DNS_is_account && !$this->DNSinitially_was_account)){
670       return;
671     }else{
673       /* Add ipHostNumber to aRecords
674        */
675       if(!empty($this->ipHostNumber)){
676         $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
677         $ptr = $this->get_pTRRecord();
678         if(!empty($ptr)){
679           $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
680         }
681       }
683       /* Create diff and follow instructions 
684        * If Account was disabled, remove account by setting exists to false
685        */
686       if((!$this->DNS_is_account)&&($this->DNSinitially_was_account)){  
687         $this->dnsEntry['exists'] = false;
688         $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
689       }else{
690         $this->dnsEntry['exists'] = $this->DNS_is_account;
691         $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
692       }   
694       /* move follwoing entries 
695        */
696       foreach($tmp['move'] as $src => $dst){
697         $this->recursive_move($src,$dst);
698       }
700       /* Delete dns */
701       foreach($tmp['del'] as $dn => $del){
702         $ldap->cd($dn);
703         $ldap->rmdir_recursive($dn);
704       }
706       /* Add || Update new DNS entries 
707        */
708       foreach($tmp['add'] as $dn => $attrs){
709         $ldap->cd($dn);
710         $ldap->cat($dn, array('dn'));
711         if(count($ldap->fetch())){
712           $ldap->cd($dn);
713           $ldap->modify ($attrs); 
714         }else{
715           $ldap->cd($dn);
716           $ldap->add($attrs);
717         }
718       }
720       /* Display errors 
721        */
722       if($ldap->get_error() != "Success"){
723         show_ldap_error("Record:".$ldap->get_error(), _("Saving terminal to DNS object failed")); 
724       }
726       /* Handle post event */
727       $tmp = new servdns($this->config,$this->dn);
728       $tmp->handle_post_events("modify");
729     }
730   }
732   /*  Create html table with all used record types
733    */
734   function generateRecordsList()
735   {
736     $changeStateForRecords = "";
737     
738     if(!$this->DNS_is_account) {
739       $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
740       return $str;
741     }
742  
743     $str = "<table summary='' width='100%'>";
744     foreach($this->dnsEntry['RECORDS'] as $key => $entry){
746         $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
747         $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
748         $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
750         $str.=" <tr>".
751           "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
752           "   <td><input ".chkacl($this->acl,"termDNS")." type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
753           "   <td><input ".chkacl($this->acl,"termDNS")." type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
754           "</tr>";
755     }
757     $str.= "  <tr>".
758            "    <td colspan=2 width='50%'></td><td>".
759            "      <input type='submit' value='"._("Add")."' name='AddNewRecord' ".chkacl($this->acl,"termDNS")." >".
760            "    </td>".
761            "  </tr>".
762            "</table>";
763      $ret =  array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
764     return($ret);
765   }
768   /* Create a html select box which allows us to select different types of records 
769    */
770   function generateRecordListBox($selected,$name)
771   {
772     $str = "<select ".chkacl($this->acl,"termDNS")."  name='".$name."' id='".$name."'>";
773     foreach($this->RecordTypes as $type => $value){
774       $use = "";
775       if($type == $selected){
776         $use = " selected ";
777       }
778       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
779     }
780     $str.="</select>";
781     return($str); 
782   }
784   
785   function get_dhcp_host_entry()
786   {
787     $attrs = array();
788     $dn = $this->get_dhcp_host_entry_dn();
789     if($dn){
790       $ldap = $this->config->get_ldap_link();
791       $ldap->cd($this->config->current['BASE']);
792       $ldap->cat($dn,array("*"));
793       if($ldap->count()){
794         $attrs = $ldap->fetch();
795         foreach($attrs as $key => $value){
796           if(is_numeric($key) || ($key == "count")){
797             unset($attrs[$key]);
798           }
799           if(is_array($value) && isset($value['count'])){
800             unset($attrs[$key]['count']);
801           }
802         }
803       }
804     }
805     return($attrs);
806   }
809   function get_dhcp_host_entry_dn()
810   {
811     $ldap = $this->config->get_ldap_link();
812     $ldap->cd($this->config->current['BASE']);
813     $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
815     if($ldap->count()){
816       $attr = $ldap->fetch();
817       return($attr['dn']);
818     }else{
819       return("");
820     }
821   }  
824   function get_dhcp_parent_node()
825   {
826     return(preg_replace("/^cn=".normalizePreg($this->cn).",/","",$this->get_dhcp_host_entry_dn()));
827   }
830   function get_dhcp_parent_nodes()
831   {
832     $ldap = $this->config->get_ldap_link();
833     $ldap->cd($this->config->current['BASE']);
834     $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
835     
836     $dhcp_dns = array();
837     while($attr = $ldap->fetch()){
838       $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
839     }
840  
841     foreach($dhcp_dns as $key => $pri_dns){
842       $ldap->cat($pri_dns,array("cn"));
843       $tmp = $ldap->fetch();
844       if(isset($tmp['cn'][0])){
845         $dhcp_dns[$key] = $tmp['cn'][0];
846       }else{
847         unset($dhcp_dns[$key]);
848       }
849     }
851     $tmp = $tmp2 = array();
852     foreach($dhcp_dns as $dn => $cn){
853       $ldap->cd($dn);
854       $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
855                     "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
856       while($attr = $ldap->fetch()){
857         $tmp[$attr['dn']] = $attr['cn'][0];
858       }
859       $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.")&nbsp;"));
860     }
861     return($tmp2);
862   }
864   
865   /* this function returns the default ptr record entry */
866   function get_pTRRecord()
867   {
868     if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
869       $ldap = $this->config->get_ldap_link();
870       $ldap->cat(getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
871       $attrs = $ldap->fetch();
872       $tmp = array_flip($this->Zones);
873       $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
874       $tmp = trim(preg_replace("/\.in-addr.arpa$/","",$tmp));
875       $ptr = preg_replace("/^".normalizePreg(FlipIp($tmp))."\./","",$this->ipHostNumber);
876       return($ptr);
877     }else{
878       return(FALSE);
879     }
880   }
882   
883   function create_tree($arr,$base,$current = "")
884   {
885     $ret = array();
886     foreach($arr as $r => $name){
887       $base_part = str_replace($base,"",$r);
888       if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
889         $ret[$r] = $current.$name;
890         $tmp = $this->create_tree($arr,$r,$current.".&nbsp;");
891         foreach($tmp as $sub_key => $sub_name){
892           $ret[$sub_key] = $sub_name;
893         }
894       } 
895     }
896     return($ret);
897   }
899   function force_dns()
900   {
901     if($this->DNSenabled){
902       $this->DNS_is_account  = TRUE;
903       $this->hide_dns_check_box = TRUE;
904     }
905   }
908 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
909 ?>