Code

ce3b7ca42f2549e030a8ff2581bb1df6d8abcc76
[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   
569       /* Unset dhcpStatements if this attribute is empty  */
570       if(isset($this->dhcpHostEntry['dhcpStatements']) && 
571           ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
572         unset($this->dhcpHostEntry['dhcpStatements']);
573       }
574   
575       /* DHCP removed */
576       if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
577         $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
578         show_ldap_error($ldap->get_error(),_("Removing dhcp entry for this object failed."));
580         $tmp = new servdhcp($this->config,$this->dhcpParentNode);
581         $tmp->handle_post_events("remove");
582       }
584       /* DHCP Added */
585       if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
587         $dn = "cn=".$this->cn.",".$this->dhcpParentNode;
589         $attrs = $this->dhcpHostEntry;
590         $attrs['cn'] = $this->cn;
591         
592         unset($attrs['MODIFIED']);
593         unset($attrs['dn']);
594         $ldap->cd($dn);
595         $res = $ldap->add($attrs);
597         $tmp = new servdhcp($this->config,$this->dhcpParentNode);
598         $tmp->handle_post_events("add");
600 #        print_a($attrs);
601 #        print("cn=".$this->cn.",".$this->dhcpParentNode);
602         show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
603       }
605       /* DHCP still activated */
606       if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
608         /* DHCP node changed */
609         if(($this->initial_dhcpParentNode != $this->dhcpParentNode) || 
610            ($this->cn != $this->OrigCn)){
611           $attrs = $this->dhcpHostEntry;
612           $attrs['cn'] = $this->cn;
613           unset($attrs['dn']);
614           unset($attrs['MODIFIED']);
615           $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
616           $res = $ldap->add($attrs);
618           $tmp = new servdhcp($this->config,$this->dhcpParentNode);
619           $tmp->handle_post_events("modify");
621           show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
622           if($res){
623             $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
624             show_ldap_error($ldap->get_error(),_("Removing old dhcp entry failed."));
625           }
626         }
627          
628         /* SAME node but modified */ 
629         if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1  && 
630             $this->initial_dhcpParentNode == $this->dhcpParentNode){
631           $attrs = $this->dhcpHostEntry;
632           unset($attrs['dn']);
633           unset($attrs['MODIFIED']);
634           $ldap->cd($this->dhcpHostEntry['dn']);
635           $ldap->modify($attrs);
636           
637           $tmp = new servdhcp($this->config,$this->dhcpParentNode);
638           $tmp->handle_post_events("modify");
640           show_ldap_error($ldap->get_error(),_("Modifying dhcp entry failed."));
641         }    
642       }
643     }
644       
646     /****************/ 
647     /* DNS HANDLING */
648     /****************/ 
650     /* If isn't DNS account but initially was DNS account 
651        remove all DNS entries 
652      */ 
653     if((!$this->DNSenabled) || (!$this->DNS_is_account && !$this->DNSinitially_was_account)){
654       return;
655     }else{
657       /* Add ipHostNumber to aRecords
658        */
659       if(!empty($this->ipHostNumber)){
660         $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
661         $ptr = $this->get_pTRRecord();
662         if(!empty($ptr)){
663           $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
664         }
665       }
667       /* Create diff and follow instructions 
668        * If Account was disabled, remove account by setting exists to false
669        */
670       if((!$this->DNS_is_account)&&($this->DNSinitially_was_account)){  
671         $this->dnsEntry['exists'] = false;
672         $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
673       }else{
674         $this->dnsEntry['exists'] = $this->DNS_is_account;
675         $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
676       }   
678       /* move follwoing entries 
679        */
680       foreach($tmp['move'] as $src => $dst){
681         $this->recursive_move($src,$dst);
682       }
684       /* Delete dns */
685       foreach($tmp['del'] as $dn => $del){
686         $ldap->cd($dn);
687         $ldap->rmdir_recursive($dn);
688       }
690       /* Add || Update new DNS entries 
691        */
692       foreach($tmp['add'] as $dn => $attrs){
693         $ldap->cd($dn);
694         $ldap->cat($dn, array('dn'));
695         if(count($ldap->fetch())){
696           $ldap->cd($dn);
697           $ldap->modify ($attrs); 
698         }else{
699           $ldap->cd($dn);
700           $ldap->add($attrs);
701         }
702       }
704       /* Display errors 
705        */
706       if($ldap->get_error() != "Success"){
707         show_ldap_error("Record:".$ldap->get_error(), _("Saving terminal to DNS object failed")); 
708       }
709     }
710   }
712   /*  Create html table with all used record types
713    */
714   function generateRecordsList()
715   {
716     $changeStateForRecords = "";
717     
718     if(!$this->DNS_is_account) {
719       $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
720       return $str;
721     }
722  
723     $str = "<table summary='' width='100%'>";
724     foreach($this->dnsEntry['RECORDS'] as $key => $entry){
726         $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
727         $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
728         $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
730         $str.=" <tr>".
731           "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
732           "   <td><input ".chkacl($this->acl,"termDNS")." type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
733           "   <td><input ".chkacl($this->acl,"termDNS")." type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
734           "</tr>";
735     }
737     $str.= "  <tr>".
738            "    <td colspan=2 width='50%'></td><td>".
739            "      <input type='submit' value='"._("Add")."' name='AddNewRecord' ".chkacl($this->acl,"termDNS")." >".
740            "    </td>".
741            "  </tr>".
742            "</table>";
743      $ret =  array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
744     return($ret);
745   }
748   /* Create a html select box which allows us to select different types of records 
749    */
750   function generateRecordListBox($selected,$name)
751   {
752     $str = "<select ".chkacl($this->acl,"termDNS")."  name='".$name."' id='".$name."'>";
753     foreach($this->RecordTypes as $type => $value){
754       $use = "";
755       if($type == $selected){
756         $use = " selected ";
757       }
758       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
759     }
760     $str.="</select>";
761     return($str); 
762   }
764   
765   function get_dhcp_host_entry()
766   {
767     $attrs = array();
768     $dn = $this->get_dhcp_host_entry_dn();
769     if($dn){
770       $ldap = $this->config->get_ldap_link();
771       $ldap->cd($this->config->current['BASE']);
772       $ldap->cat($dn,array("*"));
773       if($ldap->count()){
774         $attrs = $ldap->fetch();
775         foreach($attrs as $key => $value){
776           if(is_numeric($key) || ($key == "count")){
777             unset($attrs[$key]);
778           }
779           if(is_array($value) && isset($value['count'])){
780             unset($attrs[$key]['count']);
781           }
782         }
783       }
784     }
785     return($attrs);
786   }
789   function get_dhcp_host_entry_dn()
790   {
791     $ldap = $this->config->get_ldap_link();
792     $ldap->cd($this->config->current['BASE']);
793     $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
795     if($ldap->count()){
796       $attr = $ldap->fetch();
797       return($attr['dn']);
798     }else{
799       return("");
800     }
801   }  
804   function get_dhcp_parent_node()
805   {
806     return(preg_replace("/^cn=".normalizePreg($this->cn).",/","",$this->get_dhcp_host_entry_dn()));
807   }
810   function get_dhcp_parent_nodes()
811   {
812     $ldap = $this->config->get_ldap_link();
813     $ldap->cd($this->config->current['BASE']);
814     $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
815     
816     $dhcp_dns = array();
817     while($attr = $ldap->fetch()){
818       $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
819     }
820  
821     foreach($dhcp_dns as $key => $pri_dns){
822       $ldap->cat($pri_dns,array("cn"));
823       $tmp = $ldap->fetch();
824       if(isset($tmp['cn'][0])){
825         $dhcp_dns[$key] = $tmp['cn'][0];
826       }else{
827         unset($dhcp_dns[$key]);
828       }
829     }
831     $tmp = $tmp2 = array();
832     foreach($dhcp_dns as $dn => $cn){
833       $ldap->cd($dn);
834       $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
835                     "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
836       while($attr = $ldap->fetch()){
837         $tmp[$attr['dn']] = $attr['cn'][0];
838       }
839       $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.")&nbsp;"));
840     }
841     return($tmp2);
842   }
844   
845   /* this function returns the default ptr record entry */
846   function get_pTRRecord()
847   {
848     if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
849       $ldap = $this->config->get_ldap_link();
850       $ldap->cat(getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
851       $attrs = $ldap->fetch();
852       $tmp = array_flip($this->Zones);
853       $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
854       $tmp = trim(preg_replace("/\.in-addr.arpa$/","",$tmp));
855       $ptr = preg_replace("/^".normalizePreg(FlipIp($tmp))."\./","",$this->ipHostNumber);
856       return($ptr);
857     }else{
858       return(FALSE);
859     }
860   }
862   
863   function create_tree($arr,$base,$current = "")
864   {
865     $ret = array();
866     foreach($arr as $r => $name){
867       $base_part = str_replace($base,"",$r);
868       if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
869         $ret[$r] = $current.$name;
870         $tmp = $this->create_tree($arr,$r,$current.".&nbsp;");
871         foreach($tmp as $sub_key => $sub_name){
872           $ret[$sub_key] = $sub_name;
873         }
874       } 
875     }
876     return($ret);
877   }
879   function force_dns()
880   {
881     if($this->DNSenabled){
882       $this->DNS_is_account  = TRUE;
883       $this->hide_dns_check_box = TRUE;
884     }
885   }
888 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
889 ?>