Code

Updated gotomasses to support dns and dhcp options for initial_install action
[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;
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;
43   /*  Terminal dns 
44    */
45   function termDNS ($config, $dn,$objectClasses,$IPisMust = false)
46   {
47     /* We need to know which objectClasses are used, to store the ip/mac
48      * Because of different type of devices   
49      */
50     $this->objectclasses  =  $objectClasses;
51     $this->IPisMust       = $IPisMust;
53     plugin::plugin ($config, $dn);
55     if(isset($this->attrs['cn'][0])){
56       $this->OrigCn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
57       $this->cn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
58     }
60  
61     /************
62      * DHCP 
63      ************/
65     /* Hide all dhcp specific code, if dhcp plugin is not present in config */
66     $dhcpEnabled = FALSE;
67     foreach($this->config->data['TABS']['SERVTABS'] as $tab){
68       if(preg_match("/^servdhcp$/",$tab['CLASS'])){
69         $this->dhcpEnabled = TRUE;
70       }
71     }
72     if($this->dhcpEnabled){
73       $this->dhcpParentNodes = $this->get_dhcp_parent_nodes();
74       $this->dhcpParentNode  = $this->get_dhcp_parent_node();
75       if($this->dhcpParentNode){
76         $this->dhcp_is_Account = TRUE;
77         $this->initial_dhcp_is_Account = TRUE;
78         $this->dhcpHostEntry  = $this->get_dhcp_host_entry();    
79       }
80       $this->initial_dhcpHostEntry = $this->dhcpHostEntry;
81       $this->initial_dhcpParentNode= $this->dhcpParentNode;
82     }
85     /************
86      * Autonetwork hook 
87      ************/
88  
89     /* Do we have autonet support? */
90     if (isset($this->config->data['MAIN']['AUTO_NETWORK_HOOK'])){
91       $this->autonet= true;
92     }
95     /************
96      * DNS
97      ************/
98  
99     /* Hide all dns specific code, if dns is not available 
100      */
101     $DNSenabled = false;
102     foreach($this->config->data['TABS']['SERVTABS'] as $tab){
103       if(preg_match("/^servdns$/",$tab['CLASS'])){
104         $this->DNSenabled = true;
105       }
106     }
107     if(!$this->DNSenabled){
108       $this->DNS_is_account = false;
109       return;
110     }
112     if($this->DNSenabled){
114       /* Get Zones  
115        */
116       $this->Zones        = getAvailableZones($config);
118       /* Get Entry 
119        */
120       $this->dnsEntry     = getDNSHostEntries($config,$this->OrigCn);
122       /* Remove A record which equals $this->ipHostNumber
123        */
124       foreach($this->dnsEntry['RECORDS'] as $key => $rec){
125         if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
126           unset($this->dnsEntry['RECORDS'][$key]);
127         }
128       }
130       /* Get Record types 
131        */
132       $this->RecordTypes  = getDnsRecordTypes();
134       /* If there is at least one entry in this -> types, we have DNS enabled 
135        */
136       if($this->dnsEntry['exists']){
137         $this->DNS_is_account = true;
138       }else{
139         $this->DNS_is_account = false;
140       }
141    
142     }
143  
144     /* Store initally account settings 
145      */
146     $this->DNSinitially_was_account = $this->DNS_is_account;
147   }
150   function netmaskIsCoherent($idZone) 
151   {
152     $netmask = FlipIp(str_replace(".in-addr.arpa","",getNameFromMix($idZone)));
153     if(!strstr($this->ipHostNumber, $netmask)){
154       return false;
155     }else{
156       return true;
157     }
158   }
161   function getVarsForSaving($attrs) 
162   {
163     foreach($this->attributes as $attr){
164       if(!empty($this->$attr)){
165         $attrs[$attr] = $this->$attr;
166       }
167     }
168     return($attrs); 
169   }
171   function execute()
172   {
173           /* Call parent execute */
174     $smarty= get_smarty();
175     $display= "";
177     $smarty->assign("staticAddress", ""); 
178     $smarty->assign("autonet", $this->autonet);
179  
180     /* Check for autonet button */
181     if ($this->autonet && isset($_POST['autonet'])){
182       $cmd= $this->config->data['MAIN']['AUTO_NETWORK_HOOK'];
183       if(!empty($cmd) && $this->cn != ""){
184         $res = shell_exec($cmd." ".$this->cn);
185         if(!$res){
186           print_red(sprintf(_("Can't execute specified AUTO_NETWORK_HOOK '%s'. Please check your gosa.conf."),$cmd));
187         } else {
188           $res= split(';', trim($res));
189           if (isset($res[0]) && $res[0] != ""){
190             $this->ipHostNumber= $res[0];
191           }
192           if (isset($res[1]) && $res[1] != ""){
193             $this->macAddress= $res[1];
194           }
195         }
196       }
197     }
199     
200   
201     /**********
202      * DHCP Handling
203      **********/
204  
205     if(isset($_POST['dhcpEditOptions'])){
207       if(count($this->dhcpHostEntry) == 0){
208         $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
209       }else{
210         $this->dialog = new dhcpHost($this->dhcpHostEntry,TRUE);
211       }
212       $this->dialog->cn = $this->cn; 
213       $this->dialog->dhcpHWAddress = $this->macAddress; 
214       if(!empty($this->ipHostNumber)){
215         $this->dialog->statements['fixed-address'] = $this->ipHostNumber; 
216       }
217     }
219     if(isset($_POST['cancel_dhcp'])){
220       $this->dialog = FALSE; 
221     }
223     if(isset($_POST['save_dhcp'])){
224       $this->dialog->save_object();
225       
226       $msgs = $this->dialog->check(array());
227       if(count($msgs)){
228         foreach($msgs as $msg){
229           print_red($msg);
230         }
231       }else{
232         $this->dhcpHostEntry = $this->dialog->save();
233         $this->dialog = FALSE; 
234       }
235     }
237     if(isset($this->dialog) && $this->dialog != FALSE){
238       $this->dialog->save_object();
239       return($this->dialog->execute());
240     }
241  
242     $smarty->assign("dhcpEnabled",    $this->dhcpEnabled);
243     $smarty->assign("dhcp_is_Account",$this->dhcp_is_Account);
244     $smarty->assign("dhcpParentNode", $this->dhcpParentNode);
245     $smarty->assign("dhcpParentNodes",$this->dhcpParentNodes);
248     /* There is no dns available 
249      */
250     if($this->DNSenabled == false){
251        
252       /* Is IP address must ? */ 
253       $smarty->assign("DNS_is_account",false);  
254       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
255     
256       /* Assign smarty all non DNs attributes */
257       foreach($this->attributes as $attr){
258         $smarty->assign($attr,$this->$attr);
259         $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
260       }
261       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
263       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
264     }else{
265       $smarty->assign("DNS_is_account",true); 
267       /* Add new empty array to our record list */
268       if(isset($_POST['AddNewRecord'])){
269         $this->dnsEntry['RECORDS'][]  =array("type"=>"aRecord","value"=>"");
270       }
272       /* Handle all posts */ 
273       $only_once =true;
274       foreach($_POST as $name => $value){
276         /* Check if we have to delete a record entry */
277         if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
279           /* Avoid performing this once again */
280           $only_once = false;
282           /* Extract id for specified entry */
283           $id = preg_replace("/RemoveRecord_/","",$name);
284           $id = preg_replace("/_.*$/","",$id);
286           /* Delete this record, mark edited entries to be able to delete them */
287           if(isset($this->dnsEntry['RECORDS'][$id])){
288             unset($this->dnsEntry['RECORDS'][$id]);
289           }
290         }
291       }
293       /* Assign smarty all non DNs attributes */
294       foreach($this->attributes as $attr){
295         $smarty->assign($attr,$this->$attr);
296         $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
297       }
299       /* Assign smarty all DNS attributes */
300       foreach($this->DNSattributes as $attr){
301         $smarty->assign($attr,$this->dnsEntry[$attr]);
302         $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
303       }
305       /* Assign all needed vars */
306       $smarty->assign("DNSaccountACL",chkacl($this->acl,"termDNS"));
308       $smarty->assign("DNSAccount",$this->DNS_is_account);
309       $smarty->assign("Zones",$this->Zones);
310       $smarty->assign("ZoneKeys",($this->Zones));
311       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
313       /* Create zone array */
314       $idZones = array();
315       foreach($this->Zones as $id => $zone){
316         if($this->netmaskIsCoherent($id)) {
317           $idZones[$id] = $zone;
318         }else{
319           $idZones[$id] = $zone."&nbsp;("._("Not matching").")";
320         }
321       }    
322       $smarty->assign("Zones",$idZones);
323       $smarty->assign("ZoneKeys", $this->Zones);
325       $tmp = $this->generateRecordsList();
327       $changeStateForRecords = $tmp['changeStateForRecords'];
329       $smarty->assign("records",$tmp['str']);
330       $smarty->assign("changeStateForRecords",$changeStateForRecords);
331       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
333       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
334     }
335     return($display);
336   }
338   function remove_from_parent()
339   {
340     /*
341     $ldap = $this->config->get_ldap_link();
342     $ldap->cd($this->orig_dn);
343     $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("relativeDomainName","zoneName"));
344     while($attr = $ldap->fetch()){  
345       $ldap->cd($attr['dn']);
346       $ldap->rmDir($attr['dn']);
347       show_ldap_error("Record:".$ldap->get_error(), _("Removing terminal from DNS object failed")); 
348     }
349     */
350   }
352   /* Save data to object */
353   function save_object()
354   {
355     /* Save all posted vars */
356     plugin::save_object();
357   
359     /* Handle DHCP Posts*/ 
360     if($this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
361       foreach($this->dhcpAttributes as $attr){
362         if(isset($_POST[$attr])){
363           $this->$attr = $_POST[$attr];
364         }
365       }
366       if(isset($_POST['dhcp_is_Account'])){
367         $this->dhcp_is_Account = TRUE;
368       }else{
369         $this->dhcp_is_Account = FALSE;
370       }
371     }
372  
373  
374     /* Get dns attributes */
375     if(($this->DNSenabled) && (isset($_POST['network_tpl_posted'])) && chkacl($this->acl,"termDNS") == ""){
377       /* Check for posted record changes */
378       if(is_array($this->dnsEntry['RECORDS'])){
379         foreach($this->dnsEntry['RECORDS'] as $key => $value){
381           /* Check if type has changed */
382           if(isset($_POST['RecordTypeSelectedFor_'.$key])){
383             $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
384           }
385           /* Check if value has changed */
386           if(isset($_POST['RecordValue_'.$key])){
387             $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
388           }
389         }
390       }
391       /* Get all basic DNS attributes (TTL, Clas ..)*/
392       foreach($this->DNSattributes as $attr){
393         if(isset($_POST[$attr])){
394           $this->dnsEntry[$attr] = $_POST[$attr];
395         }
396       }
398       /* Enable diable DNS */
399       if(isset($_POST['enableDNS'])){
400         $this->DNS_is_account = true;
401       }else{
402         $this->DNS_is_account = false;
403       }
404     }
405   }
408   /* Check supplied data */
409   function check()
410   {
411     /* Call common method to give check the hook */
412     $message= plugin::check();
414     if($this->dhcpEnabled && $this->dhcpParentNode != "" && count($this->dhcpHostEntry) == 0){
415       $message[] =_("You haven not configured your dhcp settings yet.");
416     }
418     /* Check if ip must be given
419      */  
420     if(($this->IPisMust)||($this->DNS_is_account)){
421   
422       /* Check if ip is empty 
423        */
424       if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
425         $message[]= _("The required field 'IP-address' is not set.");
426       }
428     }
430     /* check if given ip is valid ip */
431     if ($this->ipHostNumber != "" && !is_ip($this->ipHostNumber)){
432       $message[]= _("Wrong IP format in field IP-address.");
433     }
435     /* Check if mac is empty 
436      */
437     if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
438       $message[]= _("The required field 'MAC-address' is not set.");
439     }
441     /* Check if given mac is valid mac 
442      */
443     if(!is_mac($this->macAddress)){
444       $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
445     }
447     /* only perfrom this checks if this is a valid DNS account */
448     if($this->DNS_is_account){
450       $checkArray = array();
451       $onlyOnce   = array();
453       $tmp = array_flip($this->Zones);
454       $tmp2 = $tmp[$this->dnsEntry['zoneName']];
455       if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
456         $tmp2 = preg_replace("/^.*\//","",$tmp2);
457         $message[] =sprintf(_("The specified IP address '%s' is not matching the selected reverse zone entry '%s'."),$this->ipHostNumber,$tmp2);
458       }
460                         // There can be many CNAME records
461       //$onlyOnce['cNAMERecord'] = 0;
463       /* Walk through all entries and detect duplicates or mismatches
464        */  
465       foreach($this->dnsEntry['RECORDS'] as $name => $values){
467         /* Count record values, to detect duplicate entries for a specific record
468          */
469         if(!isset($checkArray[$values['type']][$values['value']])){
470           $checkArray[$values['type']][$values['value']] = 0;
471         }else{
472           $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
473         }
475         /* Check if given entries in $onlyOnce are used more than once
476          */
477         if(isset($onlyOnce[$values['type']])){
478           $onlyOnce[$values['type']] ++;
479           if($onlyOnce[$values['type']] > 1){
480             $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
481           }
482         }
484         /* Skip txt record ... 
485          */
486         if($values['type'] == "tXTRecord") continue;
488         /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress 
489          */
490         if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
491           $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."), 
492                $this->ipHostNumber);
493         }
495         /* only lower-case is allowed in record entries ... 
496          */
497         if($values['value'] != strtolower($values['value'])){
498           $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
499         }
500       }
501     }
502     return ($message);
503   }
506   /* Save to LDAP */
507   function save($dn)
508   {
509     $ldap= $this->config->get_ldap_link();
510    
511     /*******************/ 
512     /* IP-MAC HANDLING */
513     /*******************/ 
515     /* $dn was posted as parameter */
516     $this->dn = $dn;
517     
518     /* Save DNS setting & ip/Mac*/
519     plugin::save();
521     /* Write back to ldap */
522     $ldap->cd($this->dn);
523     $this->cleanup();
524     $ldap->modify ($this->attrs); 
526     /****************/ 
527     /* DHCP HANDLING */
528     /****************/ 
529   
530     /* New entry */
531     if($this->dhcpEnabled){
533       /* Unset dhcpStatements if this attribute is empty  */
534       if(isset($this->dhcpHostEntry['dhcpStatements']) && 
535           ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
536         unset($this->dhcpHostEntry['dhcpStatements']);
537       }
538   
539       /* DHCP removed */
540       if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
541         $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
542         show_ldap_error($ldap->get_error(),_("Removing dhcp entry for this object failed."));
543       }
545       /* DHCP Added */
546       if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
547         $attrs = $this->dhcpHostEntry;
548         unset($attrs['MODIFIED']);
549         unset($attrs['dn']);
550         $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
551         $res = $ldap->add($attrs);
552 #        print_a($attrs);
553 #        print("cn=".$this->cn.",".$this->dhcpParentNode);
554         show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
555       }
557       /* DHCP still activated */
558       if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
560         /* DHCP node changed */
561         if(($this->initial_dhcpParentNode != $this->dhcpParentNode) || 
562            ($this->cn != $this->OrigCn)){
563           $attrs = $this->dhcpHostEntry;
564           $attrs['cn'] = $this->cn;
565           unset($attrs['dn']);
566           unset($attrs['MODIFIED']);
567           $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
568           $res = $ldap->add($attrs);
569           show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
570           if($res){
571             $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
572             show_ldap_error($ldap->get_error(),_("Removing old dhcp entry failed."));
573           }
574         }
575          
576         /* SAME node but modified */ 
577         if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1  && 
578             $this->initial_dhcpParentNode == $this->dhcpParentNode){
579           $attrs = $this->dhcpHostEntry;
580           unset($attrs['dn']);
581           unset($attrs['MODIFIED']);
582           $ldap->cd($this->dhcpHostEntry['dn']);
583           $ldap->modify($attrs);
584           show_ldap_error($ldap->get_error(),_("Modifying dhcp entry failed."));
585         }    
586       }
587     }
588       
590     /****************/ 
591     /* DNS HANDLING */
592     /****************/ 
594     /* If isn't DNS account but initially was DNS account 
595        remove all DNS entries 
596      */ 
597     if((!$this->DNSenabled) || (!$this->DNS_is_account && !$this->DNSinitially_was_account)){
598       return;
599     }else{
601       /* Add ipHostNumber to aRecords
602        */
603       if(!empty($this->ipHostNumber)){
604         $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
605       }
607       /* Create diff and follow instructions 
608        * If Account was disabled, remove account by setting exists to false
609        */
610       if((!$this->DNS_is_account)&&($this->DNSinitially_was_account)){  
611         $this->dnsEntry['exists'] = false;
612         $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
613       }else{
614         $this->dnsEntry['exists'] = $this->DNS_is_account;
615         $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
616       }   
618       /* move follwoing entries 
619        */
620       foreach($tmp['move'] as $src => $dst){
621         $this->recursive_move($src,$dst);
622       }
624       /* Delete dns */
625       foreach($tmp['del'] as $dn => $del){
626         $ldap->cd($dn);
627         $ldap->rmdir_recursive($dn);
628       }
630       /* Add || Update new DNS entries 
631        */
632       foreach($tmp['add'] as $dn => $attrs){
633         $ldap->cd($dn);
634         $ldap->cat($dn, array('dn'));
635         if(count($ldap->fetch())){
636           $ldap->cd($dn);
637           $ldap->modify ($attrs); 
638         }else{
639           $ldap->cd($dn);
640           $ldap->add($attrs);
641         }
642       }
644       /* Display errors 
645        */
646       if($ldap->get_error() != "Success"){
647         show_ldap_error("Record:".$ldap->get_error(), _("Saving terminal to DNS object failed")); 
648       }
649     }
650   }
652   /*  Create html table with all used record types
653    */
654   function generateRecordsList()
655   {
656     $changeStateForRecords = "";
657     
658     if(!$this->DNS_is_account) {
659       $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
660       return $str;
661     }
662  
663     $str = "<table summary='' width='100%'>";
664     foreach($this->dnsEntry['RECORDS'] as $key => $entry){
666         $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
667         $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
668         $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
670         $str.=" <tr>".
671           "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
672           "   <td><input ".chkacl($this->acl,"termDNS")." type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
673           "   <td><input ".chkacl($this->acl,"termDNS")." type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
674           "</tr>";
675     }
677     $str.= "  <tr>".
678            "    <td colspan=2 width='50%'></td><td>".
679            "      <input type='submit' value='"._("Add")."' name='AddNewRecord' ".chkacl($this->acl,"termDNS")." >".
680            "    </td>".
681            "  </tr>".
682            "</table>";
683      $ret =  array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
684     return($ret);
685   }
688   /* Create a html select box which allows us to select different types of records 
689    */
690   function generateRecordListBox($selected,$name)
691   {
692     $str = "<select ".chkacl($this->acl,"termDNS")."  name='".$name."' id='".$name."'>";
693     foreach($this->RecordTypes as $type => $value){
694       $use = "";
695       if($type == $selected){
696         $use = " selected ";
697       }
698       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
699     }
700     $str.="</select>";
701     return($str); 
702   }
704   
705   function get_dhcp_host_entry()
706   {
707     $attrs = array();
708     $dn = $this->get_dhcp_host_entry_dn();
709     if($dn){
710       $ldap = $this->config->get_ldap_link();
711       $ldap->cd($this->config->current['BASE']);
712       $ldap->cat($dn,array("*"));
713       if($ldap->count()){
714         $attrs = $ldap->fetch();
715         foreach($attrs as $key => $value){
716           if(is_numeric($key) || ($key == "count")){
717             unset($attrs[$key]);
718           }
719           if(is_array($value) && isset($value['count'])){
720             unset($attrs[$key]['count']);
721           }
722         }
723       }
724     }
725     return($attrs);
726   }
729   function get_dhcp_host_entry_dn()
730   {
731     $ldap = $this->config->get_ldap_link();
732     $ldap->cd($this->config->current['BASE']);
733     $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
735     if($ldap->count()){
736       $attr = $ldap->fetch();
737       return($attr['dn']);
738     }else{
739       return("");
740     }
741   }  
744   function get_dhcp_parent_node()
745   {
746     return(preg_replace("/^cn=".normalizePreg($this->cn).",/","",$this->get_dhcp_host_entry_dn()));
747   }
750   function get_dhcp_parent_nodes()
751   {
752     $ldap = $this->config->get_ldap_link();
753     $ldap->cd($this->config->current['BASE']);
754     $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
755     
756     $dhcp_dns = array();
757     while($attr = $ldap->fetch()){
758       $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
759     }
760   
761     foreach($dhcp_dns as $key => $pri_dns){
762       $ldap->cat($pri_dns,array("cn"));
763       $tmp = $ldap->fetch();
764       $dhcp_dns[$key] = $tmp['cn'][0];
765     }
766  
767     foreach($dhcp_dns as $dn => $cn){
768       $ldap->cd($dn);
769       $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
770                     "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
771       $tmp = array();
772       while($attr = $ldap->fetch()){
773         $tmp[$attr['dn']] = $attr['cn'][0];
774       }
775       $tmp2 = $this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.")&nbsp;");
776     }
777     return($tmp2);
778   }
780   
781   function create_tree($arr,$base,$current = "")
782   {
783     $ret = array();
784     foreach($arr as $r => $name){
785       $base_part = str_replace($base,"",$r);
786       if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
787         $ret[$r] = $current.$name;
788         $tmp = $this->create_tree($arr,$r,$current.".&nbsp;");
789         foreach($tmp as $sub_key => $sub_name){
790           $ret[$sub_key] = $sub_name;
791         }
792       } 
793     }
794     return($ret);
795   }
798 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
799 ?>