Code

Updated dhcp client side.
[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       if(!empty($this->ipHostNumber)){
214         $this->dialog->statements['fixed-address'] = $this->ipHostNumber; 
215       }
216     }
218     if(isset($_POST['cancel_dhcp'])){
219       $this->dialog = FALSE; 
220     }
222     if(isset($_POST['save_dhcp'])){
223       $this->dialog->save_object();
224       
225       $msgs = $this->dialog->check(array());
226       if(count($msgs)){
227         foreach($msgs as $msg){
228           print_red($msg);
229         }
230       }else{
231         $this->dhcpHostEntry = $this->dialog->save();
232         $this->dialog = FALSE; 
233       }
234     }
236     if(isset($this->dialog) && $this->dialog != FALSE){
237       $this->dialog->save_object();
238       return($this->dialog->execute());
239     }
240  
241     $smarty->assign("dhcpEnabled",    $this->dhcpEnabled);
242     $smarty->assign("dhcp_is_Account",$this->dhcp_is_Account);
243     $smarty->assign("dhcpParentNode", $this->dhcpParentNode);
244     $smarty->assign("dhcpParentNodes",$this->dhcpParentNodes);
251     /* There is no dns available 
252      */
253     if($this->DNSenabled == false){
254        
255       /* Is IP address must ? */ 
256       $smarty->assign("DNS_is_account",false);  
257       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
258     
259       /* Assign smarty all non DNs attributes */
260       foreach($this->attributes as $attr){
261         $smarty->assign($attr,$this->$attr);
262         $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
263       }
264       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
266       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
267     }else{
268       $smarty->assign("DNS_is_account",true); 
270       /* Add new empty array to our record list */
271       if(isset($_POST['AddNewRecord'])){
272         $this->dnsEntry['RECORDS'][]  =array("type"=>"aRecord","value"=>"");
273       }
275       /* Handle all posts */ 
276       $only_once =true;
277       foreach($_POST as $name => $value){
279         /* Check if we have to delete a record entry */
280         if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
282           /* Avoid performing this once again */
283           $only_once = false;
285           /* Extract id for specified entry */
286           $id = preg_replace("/RemoveRecord_/","",$name);
287           $id = preg_replace("/_.*$/","",$id);
289           /* Delete this record, mark edited entries to be able to delete them */
290           if(isset($this->dnsEntry['RECORDS'][$id])){
291             unset($this->dnsEntry['RECORDS'][$id]);
292           }
293         }
294       }
296       /* Assign smarty all non DNs attributes */
297       foreach($this->attributes as $attr){
298         $smarty->assign($attr,$this->$attr);
299         $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
300       }
302       /* Assign smarty all DNS attributes */
303       foreach($this->DNSattributes as $attr){
304         $smarty->assign($attr,$this->dnsEntry[$attr]);
305         $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
306       }
308       /* Assign all needed vars */
309       $smarty->assign("DNSaccountACL",chkacl($this->acl,"termDNS"));
311       $smarty->assign("DNSAccount",$this->DNS_is_account);
312       $smarty->assign("Zones",$this->Zones);
313       $smarty->assign("ZoneKeys",($this->Zones));
314       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
316       /* Create zone array */
317       $idZones = array();
318       foreach($this->Zones as $id => $zone){
319         if($this->netmaskIsCoherent($id)) {
320           $idZones[$id] = $zone;
321         }else{
322           $idZones[$id] = $zone."&nbsp;("._("Not matching").")";
323         }
324       }    
325       $smarty->assign("Zones",$idZones);
326       $smarty->assign("ZoneKeys", $this->Zones);
328       $tmp = $this->generateRecordsList();
330       $changeStateForRecords = $tmp['changeStateForRecords'];
332       $smarty->assign("records",$tmp['str']);
333       $smarty->assign("changeStateForRecords",$changeStateForRecords);
334       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
336       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
337     }
338     return($display);
339   }
341   function remove_from_parent()
342   {
343     /*
344     $ldap = $this->config->get_ldap_link();
345     $ldap->cd($this->orig_dn);
346     $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("relativeDomainName","zoneName"));
347     while($attr = $ldap->fetch()){  
348       $ldap->cd($attr['dn']);
349       $ldap->rmDir($attr['dn']);
350       show_ldap_error("Record:".$ldap->get_error(), _("Removing terminal from DNS object failed")); 
351     }
352     */
353   }
355   /* Save data to object */
356   function save_object()
357   {
358     /* Save all posted vars */
359     plugin::save_object();
360   
362     /* Handle DHCP Posts*/ 
363     if($this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
364       foreach($this->dhcpAttributes as $attr){
365         if(isset($_POST[$attr])){
366           $this->$attr = $_POST[$attr];
367         }
368       }
369       if(isset($_POST['dhcp_is_Account'])){
370         $this->dhcp_is_Account = TRUE;
371       }else{
372         $this->dhcp_is_Account = FALSE;
373       }
374     }
375  
376  
377     /* Get dns attributes */
378     if(($this->DNSenabled) && (isset($_POST['network_tpl_posted'])) && chkacl($this->acl,"termDNS") == ""){
380       /* Check for posted record changes */
381       if(is_array($this->dnsEntry['RECORDS'])){
382         foreach($this->dnsEntry['RECORDS'] as $key => $value){
384           /* Check if type has changed */
385           if(isset($_POST['RecordTypeSelectedFor_'.$key])){
386             $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
387           }
388           /* Check if value has changed */
389           if(isset($_POST['RecordValue_'.$key])){
390             $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
391           }
392         }
393       }
394       /* Get all basic DNS attributes (TTL, Clas ..)*/
395       foreach($this->DNSattributes as $attr){
396         if(isset($_POST[$attr])){
397           $this->dnsEntry[$attr] = $_POST[$attr];
398         }
399       }
401       /* Enable diable DNS */
402       if(isset($_POST['enableDNS'])){
403         $this->DNS_is_account = true;
404       }else{
405         $this->DNS_is_account = false;
406       }
407     }
408   }
411   /* Check supplied data */
412   function check()
413   {
414     /* Call common method to give check the hook */
415     $message= plugin::check();
417     if($this->dhcpEnabled && $this->dhcpParentNode != "" && count($this->dhcpHostEntry) == 0){
418       $message[] =_("You haven not configured your dhcp settings yet.");
419     }
421     /* Check if ip must be given
422      */  
423     if(($this->IPisMust)||($this->DNS_is_account)){
424   
425       /* Check if ip is empty 
426        */
427       if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
428         $message[]= _("The required field 'IP-address' is not set.");
429       }
431     }
433     /* check if given ip is valid ip */
434     if ($this->ipHostNumber != "" && !is_ip($this->ipHostNumber)){
435       $message[]= _("Wrong IP format in field IP-address.");
436     }
438     /* Check if mac is empty 
439      */
440     if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
441       $message[]= _("The required field 'MAC-address' is not set.");
442     }
444     /* Check if given mac is valid mac 
445      */
446     if(!is_mac($this->macAddress)){
447       $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
448     }
450     /* only perfrom this checks if this is a valid DNS account */
451     if($this->DNS_is_account){
453       $checkArray = array();
454       $onlyOnce   = array();
456       $tmp = array_flip($this->Zones);
457       $tmp2 = $tmp[$this->dnsEntry['zoneName']];
458       if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
459         $tmp2 = preg_replace("/^.*\//","",$tmp2);
460         $message[] =sprintf(_("The specified IP address '%s' is not matching the selected reverse zone entry '%s'."),$this->ipHostNumber,$tmp2);
461       }
463                         // There can be many CNAME records
464       //$onlyOnce['cNAMERecord'] = 0;
466       /* Walk through all entries and detect duplicates or mismatches
467        */  
468       foreach($this->dnsEntry['RECORDS'] as $name => $values){
470         /* Count record values, to detect duplicate entries for a specific record
471          */
472         if(!isset($checkArray[$values['type']][$values['value']])){
473           $checkArray[$values['type']][$values['value']] = 0;
474         }else{
475           $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
476         }
478         /* Check if given entries in $onlyOnce are used more than once
479          */
480         if(isset($onlyOnce[$values['type']])){
481           $onlyOnce[$values['type']] ++;
482           if($onlyOnce[$values['type']] > 1){
483             $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
484           }
485         }
487         /* Skip txt record ... 
488          */
489         if($values['type'] == "tXTRecord") continue;
491         /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress 
492          */
493         if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
494           $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."), 
495                $this->ipHostNumber);
496         }
498         /* only lower-case is allowed in record entries ... 
499          */
500         if($values['value'] != strtolower($values['value'])){
501           $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
502         }
503       }
504     }
505     return ($message);
506   }
509   /* Save to LDAP */
510   function save($dn)
511   {
512     $ldap= $this->config->get_ldap_link();
513    
514     /*******************/ 
515     /* IP-MAC HANDLING */
516     /*******************/ 
518     /* $dn was posted as parameter */
519     $this->dn = $dn;
520     
521     /* Save DNS setting & ip/Mac*/
522     plugin::save();
524     /* Write back to ldap */
525     $ldap->cd($this->dn);
526     $this->cleanup();
527     $ldap->modify ($this->attrs); 
529     /****************/ 
530     /* DHCP HANDLING */
531     /****************/ 
532   
533     /* New entry */
534     if($this->dhcpEnabled){
536       /* Unset dhcpStatements if this attribute is empty  */
537       if(isset($this->dhcpHostEntry['dhcpStatements']) && 
538           ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
539         unset($this->dhcpHostEntry['dhcpStatements']);
540       }
541   
542       /* DHCP removed */
543       if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
544         $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
545         show_ldap_error($ldap->get_error(),_("Removing dhcp entry for this object failed."));
546       }
548       /* DHCP Added */
549       if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
550         $attrs = $this->dhcpHostEntry;
551         unset($attrs['MODIFIED']);
552         unset($attrs['dn']);
553         $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
554         $res = $ldap->add($attrs);
555         show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
556       }
558       /* DHCP still activated */
559       if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
561         /* DHCP node changed */
562         if($this->initial_dhcpParentNode != $this->dhcpParentNode){
563           $attrs = $this->dhcpHostEntry;
564           unset($attrs['dn']);
565           unset($attrs['MODIFIED']);
566           $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
567           $res = $ldap->add($attrs);
568           show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
569           if($res){
570             $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
571             show_ldap_error($ldap->get_error(),_("Removing old dhcp entry failed."));
572           }
573         }
574          
575         /* SAME node but modified */ 
576         if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1  && 
577             $this->initial_dhcpParentNode == $this->dhcpParentNode){
578           $attrs = $this->dhcpHostEntry;
579           unset($attrs['dn']);
580           unset($attrs['MODIFIED']);
581           $ldap->cd($this->dhcpHostEntry['dn']);
582           $ldap->modify($attrs);
583           show_ldap_error($ldap->get_error(),_("Modifying dhcp entry failed."));
584         }    
585       }
586     }
587       
589     /****************/ 
590     /* DNS HANDLING */
591     /****************/ 
593     /* If isn't DNS account but initially was DNS account 
594        remove all DNS entries 
595      */ 
596     if((!$this->DNSenabled) || (!$this->DNS_is_account && !$this->DNSinitially_was_account)){
597       return;
598     }else{
600       /* Add ipHostNumber to aRecords
601        */
602       if(!empty($this->ipHostNumber)){
603         $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
604       }
606       /* Create diff and follow instructions 
607        * If Account was disabled, remove account by setting exists to false
608        */
609       if((!$this->DNS_is_account)&&($this->DNSinitially_was_account)){  
610         $this->dnsEntry['exists'] = false;
611         $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
612       }else{
613         $this->dnsEntry['exists'] = $this->DNS_is_account;
614         $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
615       }   
617       /* move follwoing entries 
618        */
619       foreach($tmp['move'] as $src => $dst){
620         $this->recursive_move($src,$dst);
621       }
623       /* Delete dns */
624       foreach($tmp['del'] as $dn => $del){
625         $ldap->cd($dn);
626         $ldap->rmdir_recursive($dn);
627       }
629       /* Add || Update new DNS entries 
630        */
631       foreach($tmp['add'] as $dn => $attrs){
632         $ldap->cd($dn);
633         $ldap->cat($dn, array('dn'));
634         if(count($ldap->fetch())){
635           $ldap->cd($dn);
636           $ldap->modify ($attrs); 
637         }else{
638           $ldap->cd($dn);
639           $ldap->add($attrs);
640         }
641       }
643       /* Display errors 
644        */
645       if($ldap->get_error() != "Success"){
646         show_ldap_error("Record:".$ldap->get_error(), _("Saving terminal to DNS object failed")); 
647       }
648     }
649   }
651   /*  Create html table with all used record types
652    */
653   function generateRecordsList()
654   {
655     $changeStateForRecords = "";
656     
657     if(!$this->DNS_is_account) {
658       $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
659       return $str;
660     }
661  
662     $str = "<table summary='' width='100%'>";
663     foreach($this->dnsEntry['RECORDS'] as $key => $entry){
665         $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
666         $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
667         $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
669         $str.=" <tr>".
670           "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
671           "   <td><input ".chkacl($this->acl,"termDNS")." type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
672           "   <td><input ".chkacl($this->acl,"termDNS")." type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
673           "</tr>";
674     }
676     $str.= "  <tr>".
677            "    <td colspan=2 width='50%'></td><td>".
678            "      <input type='submit' value='"._("Add")."' name='AddNewRecord' ".chkacl($this->acl,"termDNS")." >".
679            "    </td>".
680            "  </tr>".
681            "</table>";
682      $ret =  array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
683     return($ret);
684   }
687   /* Create a html select box which allows us to select different types of records 
688    */
689   function generateRecordListBox($selected,$name)
690   {
691     $str = "<select ".chkacl($this->acl,"termDNS")."  name='".$name."' id='".$name."'>";
692     foreach($this->RecordTypes as $type => $value){
693       $use = "";
694       if($type == $selected){
695         $use = " selected ";
696       }
697       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
698     }
699     $str.="</select>";
700     return($str); 
701   }
703   
704   function get_dhcp_host_entry()
705   {
706     $attrs = array();
707     $dn = $this->get_dhcp_host_entry_dn();
708     if($dn){
709       $ldap = $this->config->get_ldap_link();
710       $ldap->cd($this->config->current['BASE']);
711       $ldap->cat($dn,array("*"));
712       if($ldap->count()){
713         $attrs = $ldap->fetch();
714         foreach($attrs as $key => $value){
715           if(is_numeric($key) || ($key == "count")){
716             unset($attrs[$key]);
717           }
718           if(is_array($value) && isset($value['count'])){
719             unset($attrs[$key]['count']);
720           }
721         }
722       }
723     }
724     return($attrs);
725   }
728   function get_dhcp_host_entry_dn()
729   {
730     $ldap = $this->config->get_ldap_link();
731     $ldap->cd($this->config->current['BASE']);
732     $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
734     if($ldap->count()){
735       $attr = $ldap->fetch();
736       return($attr['dn']);
737     }else{
738       return("");
739     }
740   }  
743   function get_dhcp_parent_node()
744   {
745     return(preg_replace("/^cn=".normalizePreg($this->cn).",/","",$this->get_dhcp_host_entry_dn()));
746   }
749   function get_dhcp_parent_nodes()
750   {
751     $ldap = $this->config->get_ldap_link();
752     $ldap->cd($this->config->current['BASE']);
753     $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
754     
755     $dhcp_dns = array();
756     while($attr = $ldap->fetch()){
757       $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
758     }
759   
760     foreach($dhcp_dns as $key => $pri_dns){
761       $ldap->cat($pri_dns,array("cn"));
762       $tmp = $ldap->fetch();
763       $dhcp_dns[$key] = $tmp['cn'][0];
764     }
765  
766     foreach($dhcp_dns as $dn => $cn){
767       $ldap->cd($dn);
768       $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
769                     "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
770       $tmp = array();
771       while($attr = $ldap->fetch()){
772         $tmp[$attr['dn']] = $attr['cn'][0];
773       }
774       $tmp2 = $this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.")&nbsp;");
775     }
776     return($tmp2);
777   }
779   
780   function create_tree($arr,$base,$current = "")
781   {
782     $ret = array();
783     foreach($arr as $r => $name){
784       $base_part = str_replace($base,"",$r);
785       if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
786         $ret[$r] = $current.$name;
787         $tmp = $this->create_tree($arr,$r,$current.".&nbsp;");
788         foreach($tmp as $sub_key => $sub_name){
789           $ret[$sub_key] = $sub_name;
790         }
791       } 
792     }
793     return($ret);
794   }
797 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
798 ?>