Code

We need to place the hardware address, too
[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);
252     /* There is no dns available 
253      */
254     if($this->DNSenabled == false){
255        
256       /* Is IP address must ? */ 
257       $smarty->assign("DNS_is_account",false);  
258       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
259     
260       /* Assign smarty all non DNs attributes */
261       foreach($this->attributes as $attr){
262         $smarty->assign($attr,$this->$attr);
263         $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
264       }
265       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
267       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
268     }else{
269       $smarty->assign("DNS_is_account",true); 
271       /* Add new empty array to our record list */
272       if(isset($_POST['AddNewRecord'])){
273         $this->dnsEntry['RECORDS'][]  =array("type"=>"aRecord","value"=>"");
274       }
276       /* Handle all posts */ 
277       $only_once =true;
278       foreach($_POST as $name => $value){
280         /* Check if we have to delete a record entry */
281         if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
283           /* Avoid performing this once again */
284           $only_once = false;
286           /* Extract id for specified entry */
287           $id = preg_replace("/RemoveRecord_/","",$name);
288           $id = preg_replace("/_.*$/","",$id);
290           /* Delete this record, mark edited entries to be able to delete them */
291           if(isset($this->dnsEntry['RECORDS'][$id])){
292             unset($this->dnsEntry['RECORDS'][$id]);
293           }
294         }
295       }
297       /* Assign smarty all non DNs attributes */
298       foreach($this->attributes as $attr){
299         $smarty->assign($attr,$this->$attr);
300         $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
301       }
303       /* Assign smarty all DNS attributes */
304       foreach($this->DNSattributes as $attr){
305         $smarty->assign($attr,$this->dnsEntry[$attr]);
306         $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
307       }
309       /* Assign all needed vars */
310       $smarty->assign("DNSaccountACL",chkacl($this->acl,"termDNS"));
312       $smarty->assign("DNSAccount",$this->DNS_is_account);
313       $smarty->assign("Zones",$this->Zones);
314       $smarty->assign("ZoneKeys",($this->Zones));
315       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
317       /* Create zone array */
318       $idZones = array();
319       foreach($this->Zones as $id => $zone){
320         if($this->netmaskIsCoherent($id)) {
321           $idZones[$id] = $zone;
322         }else{
323           $idZones[$id] = $zone."&nbsp;("._("Not matching").")";
324         }
325       }    
326       $smarty->assign("Zones",$idZones);
327       $smarty->assign("ZoneKeys", $this->Zones);
329       $tmp = $this->generateRecordsList();
331       $changeStateForRecords = $tmp['changeStateForRecords'];
333       $smarty->assign("records",$tmp['str']);
334       $smarty->assign("changeStateForRecords",$changeStateForRecords);
335       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
337       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
338     }
339     return($display);
340   }
342   function remove_from_parent()
343   {
344     /*
345     $ldap = $this->config->get_ldap_link();
346     $ldap->cd($this->orig_dn);
347     $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("relativeDomainName","zoneName"));
348     while($attr = $ldap->fetch()){  
349       $ldap->cd($attr['dn']);
350       $ldap->rmDir($attr['dn']);
351       show_ldap_error("Record:".$ldap->get_error(), _("Removing terminal from DNS object failed")); 
352     }
353     */
354   }
356   /* Save data to object */
357   function save_object()
358   {
359     /* Save all posted vars */
360     plugin::save_object();
361   
363     /* Handle DHCP Posts*/ 
364     if($this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
365       foreach($this->dhcpAttributes as $attr){
366         if(isset($_POST[$attr])){
367           $this->$attr = $_POST[$attr];
368         }
369       }
370       if(isset($_POST['dhcp_is_Account'])){
371         $this->dhcp_is_Account = TRUE;
372       }else{
373         $this->dhcp_is_Account = FALSE;
374       }
375     }
376  
377  
378     /* Get dns attributes */
379     if(($this->DNSenabled) && (isset($_POST['network_tpl_posted'])) && chkacl($this->acl,"termDNS") == ""){
381       /* Check for posted record changes */
382       if(is_array($this->dnsEntry['RECORDS'])){
383         foreach($this->dnsEntry['RECORDS'] as $key => $value){
385           /* Check if type has changed */
386           if(isset($_POST['RecordTypeSelectedFor_'.$key])){
387             $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
388           }
389           /* Check if value has changed */
390           if(isset($_POST['RecordValue_'.$key])){
391             $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
392           }
393         }
394       }
395       /* Get all basic DNS attributes (TTL, Clas ..)*/
396       foreach($this->DNSattributes as $attr){
397         if(isset($_POST[$attr])){
398           $this->dnsEntry[$attr] = $_POST[$attr];
399         }
400       }
402       /* Enable diable DNS */
403       if(isset($_POST['enableDNS'])){
404         $this->DNS_is_account = true;
405       }else{
406         $this->DNS_is_account = false;
407       }
408     }
409   }
412   /* Check supplied data */
413   function check()
414   {
415     /* Call common method to give check the hook */
416     $message= plugin::check();
418     if($this->dhcpEnabled && $this->dhcpParentNode != "" && count($this->dhcpHostEntry) == 0){
419       $message[] =_("You haven not configured your dhcp settings yet.");
420     }
422     /* Check if ip must be given
423      */  
424     if(($this->IPisMust)||($this->DNS_is_account)){
425   
426       /* Check if ip is empty 
427        */
428       if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
429         $message[]= _("The required field 'IP-address' is not set.");
430       }
432     }
434     /* check if given ip is valid ip */
435     if ($this->ipHostNumber != "" && !is_ip($this->ipHostNumber)){
436       $message[]= _("Wrong IP format in field IP-address.");
437     }
439     /* Check if mac is empty 
440      */
441     if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
442       $message[]= _("The required field 'MAC-address' is not set.");
443     }
445     /* Check if given mac is valid mac 
446      */
447     if(!is_mac($this->macAddress)){
448       $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
449     }
451     /* only perfrom this checks if this is a valid DNS account */
452     if($this->DNS_is_account){
454       $checkArray = array();
455       $onlyOnce   = array();
457       $tmp = array_flip($this->Zones);
458       $tmp2 = $tmp[$this->dnsEntry['zoneName']];
459       if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
460         $tmp2 = preg_replace("/^.*\//","",$tmp2);
461         $message[] =sprintf(_("The specified IP address '%s' is not matching the selected reverse zone entry '%s'."),$this->ipHostNumber,$tmp2);
462       }
464                         // There can be many CNAME records
465       //$onlyOnce['cNAMERecord'] = 0;
467       /* Walk through all entries and detect duplicates or mismatches
468        */  
469       foreach($this->dnsEntry['RECORDS'] as $name => $values){
471         /* Count record values, to detect duplicate entries for a specific record
472          */
473         if(!isset($checkArray[$values['type']][$values['value']])){
474           $checkArray[$values['type']][$values['value']] = 0;
475         }else{
476           $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
477         }
479         /* Check if given entries in $onlyOnce are used more than once
480          */
481         if(isset($onlyOnce[$values['type']])){
482           $onlyOnce[$values['type']] ++;
483           if($onlyOnce[$values['type']] > 1){
484             $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
485           }
486         }
488         /* Skip txt record ... 
489          */
490         if($values['type'] == "tXTRecord") continue;
492         /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress 
493          */
494         if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
495           $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."), 
496                $this->ipHostNumber);
497         }
499         /* only lower-case is allowed in record entries ... 
500          */
501         if($values['value'] != strtolower($values['value'])){
502           $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
503         }
504       }
505     }
506     return ($message);
507   }
510   /* Save to LDAP */
511   function save($dn)
512   {
513     $ldap= $this->config->get_ldap_link();
514    
515     /*******************/ 
516     /* IP-MAC HANDLING */
517     /*******************/ 
519     /* $dn was posted as parameter */
520     $this->dn = $dn;
521     
522     /* Save DNS setting & ip/Mac*/
523     plugin::save();
525     /* Write back to ldap */
526     $ldap->cd($this->dn);
527     $this->cleanup();
528     $ldap->modify ($this->attrs); 
530     /****************/ 
531     /* DHCP HANDLING */
532     /****************/ 
533   
534     /* New entry */
535     if($this->dhcpEnabled){
537       /* Unset dhcpStatements if this attribute is empty  */
538       if(isset($this->dhcpHostEntry['dhcpStatements']) && 
539           ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
540         unset($this->dhcpHostEntry['dhcpStatements']);
541       }
542   
543       /* DHCP removed */
544       if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
545         $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
546         show_ldap_error($ldap->get_error(),_("Removing dhcp entry for this object failed."));
547       }
549       /* DHCP Added */
550       if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
551         $attrs = $this->dhcpHostEntry;
552         unset($attrs['MODIFIED']);
553         unset($attrs['dn']);
554         $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
555         $res = $ldap->add($attrs);
556         show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
557       }
559       /* DHCP still activated */
560       if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
562         /* DHCP node changed */
563         if($this->initial_dhcpParentNode != $this->dhcpParentNode){
564           $attrs = $this->dhcpHostEntry;
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 ?>