Code

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