Code

Updated printer ppd saving
[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 $initially_was_account = false;
45   var $dnsEntry                 = array();
46   var $DNSenabled               = false;
48   /*  Terminal dns 
49    */
50   function termDNS (&$config, $parent,$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->parent         = $parent;
56     $this->objectclasses  =  $objectClasses;
57     $this->IPisMust       = $IPisMust;
59     plugin::plugin ($config, $parent->dn);
61     if(isset($this->attrs['cn'][0])){
62       $this->OrigCn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
63       $this->cn = preg_replace("/\\\$\$/","",$this->attrs['cn'][0]);
64     }
66  
67     /************
68      * DHCP 
69      ************/
71     /* Hide all dhcp specific code, if dhcp plugin is not present in config */
72     $dhcpEnabled = FALSE;
73     if($this->config->search("servdhcp","class",array("tabs"))){
74       $this->dhcpEnabled = TRUE;
75     }
76     if($this->dhcpEnabled){
77       $this->dhcpParentNodes = $this->get_dhcp_parent_nodes();
78       $this->dhcpParentNode  = $this->get_dhcp_parent_node();
79       if($this->dhcpParentNode){
80         $this->dhcp_is_Account = TRUE;
81         $this->initial_dhcp_is_Account = TRUE;
82         $this->dhcpHostEntry  = $this->get_dhcp_host_entry();    
83       }
84       $this->initial_dhcpHostEntry = $this->dhcpHostEntry;
85       $this->initial_dhcpParentNode= $this->dhcpParentNode;
86     }
89     /************
90      * Autonetwork hook 
91      ************/
92  
93     /* Do we have autonet support? */
94     if (isset($this->config->data['MAIN']['AUTO_NETWORK_HOOK'])){
95       $this->autonet= true;
96     }
99     /************
100      * DNS
101      ************/
102  
103     /* Hide all dns specific code, if dns is not available 
104      */
105     $DNSenabled = false;
106     foreach($this->config->data['TABS']['SERVERSERVICE'] as $tab){
107       if(preg_match("/^servdns$/",$tab['CLASS'])){
108         $this->DNSenabled = true;
109       }
110     }
111     if(!$this->DNSenabled){
112       $this->DNS_is_account = false;
113       return;
114     }
116     if($this->DNSenabled){
118       /* Get Zones  
119        */
120       $this->Zones        = getAvailableZones($config);
122       /* Get Entry 
123        */
124       $this->dnsEntry     = getDNSHostEntries($config,$this->OrigCn);
126       /* Remove A record which equals $this->ipHostNumber
127        */
128       $ptr = $this->get_pTRRecord();
129       foreach($this->dnsEntry['RECORDS'] as $key => $rec){
130         if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
131           unset($this->dnsEntry['RECORDS'][$key]);
132         }
133         if(($rec['type'] == "pTRRecord") && ($rec['value'] == $ptr)){
134           unset($this->dnsEntry['RECORDS'][$key]);
135         }
136       }
138       /* Get Record types 
139        */
140       $this->RecordTypes  = getDnsRecordTypes();
142       /* If there is at least one entry in this -> types, we have DNS enabled 
143        */
144       if($this->dnsEntry['exists']){
145         $this->DNS_is_account = true;
146       }else{
147         $this->DNS_is_account = false;
148       }
149     }
151     /* Create a list of used mac and ip addresses */
152     $ldap = $this->config->get_ldap_link();
153     $ldap->cd($this->config->current['BASE']);
154     $ldap->search("(|(macAddress=*)(ipHostNumber=*))",array("macAddress","ipHostNumber"));
155     while($attrs = $ldap->fetch()){
156       if(isset($attrs['ipHostNumber'][0])){
157         $this->used_ip_mac["ip:".$attrs['ipHostNumber'][0]] = "ip:".$attrs['ipHostNumber'][0];
158       }
159       if(isset($attrs['macAddress'][0])){
160         $this->used_ip_mac["mac:".$attrs['macAddress'][0]] = "mac:".$attrs['macAddress'][0];
161       }
162     } 
164     /* Save initial ip and mac values, to be able 
165         check if the used values are already in use */ 
166     $this->orig_ipHostNumber   = $this->ipHostNumber;  
167     $this->orig_macAddress     = $this->macAddress;
168  
169     /* Store initally account settings 
170      */
171     $this->initially_was_account = $this->DNS_is_account;
173     if($this->DNS_is_account){
174       new log("view","unknown/".get_class($this),$this->dn);
175     }
176   }
179   function netmaskIsCoherent($idZone) 
180   {
181     $netmask = FlipIp(str_replace(".in-addr.arpa","",getNameFromMix($idZone)));
182     if(!strstr($this->ipHostNumber, $netmask)){
183       return false;
184     }else{
185       return true;
186     }
187   }
190   function getVarsForSaving($attrs) 
191   {
192     foreach($this->attributes as $attr){
193       if(!empty($this->$attr)){
194         $attrs[$attr] = $this->$attr;
195       }
196     }
197     return($attrs); 
198   }
200   function execute()
201   {
202           /* Call parent execute */
203     $smarty= get_smarty();
205     $tmp = $this->plInfo();
206     foreach($tmp['plProvidedAcls'] as $name => $translation){
207       $smarty->assign($name."ACL",$this->getacl($name));
208     }
210     $display= "";
212     $smarty->assign("staticAddress", ""); 
213     $smarty->assign("autonet", "");
214  
215     /* Check for autonet button */
216     if ($this->autonet && isset($_POST['autonet'])){
217       $cmd= $this->config->data['MAIN']['AUTO_NETWORK_HOOK'];
218       if(!empty($cmd) && $this->cn != ""){
219         $res = shell_exec($cmd." ".$this->cn);
220         if(!$res){
221           print_red(sprintf(_("Can't execute specified AUTO_NETWORK_HOOK '%s'. Please check your gosa.conf."),$cmd));
222         } else {
223           $res= split(';', trim($res));
224           if (isset($res[0]) && $res[0] != ""){
225             $this->ipHostNumber= $res[0];
226           }
227           if (isset($res[1]) && $res[1] != ""){
228             $this->macAddress= $res[1];
229           }
230         }
231       }
232     }
234     
235   
236     /**********
237      * DHCP Handling
238      **********/
239  
240     if(isset($_POST['dhcpEditOptions'])){
242       if(count($this->dhcpHostEntry) == 0){
243         $this->dialog = new dhcpHost($this->dhcpParentNode,TRUE);
244       }else{
245         $this->dialog = new dhcpHost($this->dhcpHostEntry,TRUE);
246       }
247       $this->dialog->cn = $this->cn; 
248       $this->dialog->dhcpHWAddress = "ethernet ".$this->macAddress; 
249       if(!empty($this->ipHostNumber)){
250         $this->dialog->statements['fixed-address'] = $this->ipHostNumber; 
251       }
252     }
254     if(isset($_POST['cancel_dhcp'])){
255       $this->dialog = FALSE; 
256     }
258     if(isset($_POST['save_dhcp'])){
259       $this->dialog->save_object();
260       
261       $msgs = $this->dialog->check(array());
262       if(count($msgs)){
263         foreach($msgs as $msg){
264           print_red($msg);
265         }
266       }else{
267         $this->dhcpHostEntry = $this->dialog->save();
268         $this->dialog = FALSE; 
269       }
270     }
272     if(is_object($this->dialog)){
273       $this->dialog->save_object();
274       return($this->dialog->execute());
275     }
276  
277     $smarty->assign("dhcpEnabled",    $this->dhcpEnabled);
278     $smarty->assign("dhcp_is_Account",$this->dhcp_is_Account);
279     $smarty->assign("dhcpParentNode", $this->dhcpParentNode);
280     $smarty->assign("dhcpParentNodes",$this->dhcpParentNodes);
281     $smarty->assign("dhcpParentNodeCnt",count($this->dhcpParentNodes));
284     /* There is no dns available
285      */
286     if($this->DNSenabled == false){
288       /* Is IP address must ? */
289       $smarty->assign("DNS_is_account",false);
290       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
292       /* Assign smarty all non DNs attributes */
293       foreach($this->attributes as $attr){
294         $smarty->assign($attr,$this->$attr);
295       }
296       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
298       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
299     }else{
300       $smarty->assign("DNS_is_account",true);
302       /* Add new empty array to our record list */
303       if(isset($_POST['AddNewRecord'])){
304         $this->dnsEntry['RECORDS'][]  =array("type"=>"aRecord","value"=>"");
305       }
307       /* Handle all posts */
308       $only_once =true;
309       foreach($_POST as $name => $value){
311         /* Check if we have to delete a record entry */
312         if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
314           /* Avoid performing this once again */
315           $only_once = false;
317           /* Extract id for specified entry */
318           $id = preg_replace("/RemoveRecord_/","",$name);
319           $id = preg_replace("/_.*$/","",$id);
321           /* Delete this record, mark edited entries to be able to delete them */
322           if(isset($this->dnsEntry['RECORDS'][$id])){
323             unset($this->dnsEntry['RECORDS'][$id]);
324           }
325         }
326       }
327       /* Assign smarty all non DNs attributes */
328       foreach($this->attributes as $attr){
329         $smarty->assign($attr,$this->$attr);
330       }
332       /* Assign smarty all DNS attributes */
333       foreach($this->DNSattributes as $attr){
334         $smarty->assign($attr,$this->dnsEntry[$attr]);
335       }
337       /* Assign all needed vars */
338       $smarty->assign("DNSAccount",$this->DNS_is_account);
339       $smarty->assign("Zones",$this->Zones);
340       $smarty->assign("ZoneCnt",count($this->Zones));
341       $smarty->assign("ZoneKeys",($this->Zones));
342       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
344       /* Create zone array */
345       $idZones = array();
346       foreach($this->Zones as $id => $zone){
347         if($this->netmaskIsCoherent($id)) {
348           $idZones[$id] = $zone;
349         }else{
350           $idZones[$id] = $zone."&nbsp;("._("Not matching").")";
351         }
352       }
353       $smarty->assign("Zones",$idZones);
354       $smarty->assign("ZoneKeys", $this->Zones);
356       $tmp = $this->generateRecordsList();
358       $changeStateForRecords = $tmp['changeStateForRecords'];
360       $smarty->assign("records",$tmp['str']);
361       $smarty->assign("changeStateForRecords",$changeStateForRecords);
362       $smarty->assign("staticAddress","<font class=\"must\">*</font>");
364       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
365     }
366     return($display);
367   }
370   function remove_from_parent()
371   {
372     if($this->initially_was_account){
374       $ldap = $this->config->get_ldap_link();
376       $tmp = array();
377       $this->dnsEntry['exists'] = false;
378       $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
380       /* Delete dns */
381       foreach($tmp['del'] as $dn => $del){
382         $ldap->cd($dn);
383         $ldap->rmdir_recursive($dn);
384         new log("remove","unknown/".get_class($this),$dn);
385       }
386     }
387   }
390   /* Save data to object */
391   function save_object()
392   {
394     if(isset($_POST['network_tpl_posted'])){
396       /* Save all posted vars */
397       plugin::save_object();
399       /* Handle DHCP Posts*/
400       if($this->dhcpEnabled && isset($_POST['network_tpl_posted'])){
401         foreach($this->dhcpAttributes as $attr){
402           if(isset($_POST[$attr])){
403             $this->$attr = $_POST[$attr];
404           }
405         }
406         if(isset($_POST['dhcp_is_Account'])){
407           $this->dhcp_is_Account = TRUE;
408         }else{
409           $this->dhcp_is_Account = FALSE;
410         }
411       }
413       /* Ge all non dns attributes (IP/MAC)*/
414       foreach($this->attributes as $attr){
415         if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
416           $this->$attr = $_POST[$attr];
417         }
418       }
420       /* Check if DNS should be enabled / disabled */
421       if($this->DNS_is_account && $this->acl_is_removeable() && !isset($_POST['DNS_is_account'])){
422         $this->DNS_is_account = false;
423       }elseif(!$this->DNS_is_account && $this->acl_is_createable() && isset($_POST['DNS_is_account'])){
424         $this->DNS_is_account = true;
425       }
427       /* Get dns attributes */
428       if(($this->DNSenabled) && (isset($_POST['network_tpl_posted']))){
430         /* Check for posted record changes */
431         if(is_array($this->dnsEntry['RECORDS']) && $this->acl_is_writeable("Records")){
432           foreach($this->dnsEntry['RECORDS'] as $key => $value){
434             /* Check if type has changed */
435             if(isset($_POST['RecordTypeSelectedFor_'.$key])){
436               $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
437             }
438             /* Check if value has changed */
439             if(isset($_POST['RecordValue_'.$key])){
440               $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
441             }
442           }
443         }
444         /* Get all basic DNS attributes (TTL, Clas ..)*/
445         foreach($this->DNSattributes as $attr){
446           if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
447             $this->dnsEntry[$attr] = $_POST[$attr];
448           }
449         }
452       }
453     }
454   }
457   /* Check supplied data */
458   function check()
459   {
460     /* Call common method to give check the hook */
461     $message= plugin::check();
463     if($this->dhcpEnabled && $this->dhcp_is_Account && $this->dhcpParentNode != "" && count($this->dhcpHostEntry) == 0){
464       $message[] =_("You have not configured your dhcp settings yet.");
465     }
466     
467     /* Check if mac and ip are already used */
468     if(!empty($this->ipHostNumber) && $this->DNS_is_account && 
469         $this->ipHostNumber != $this->orig_ipHostNumber && 
470         in_array("ip:".$this->ipHostNumber,$this->used_ip_mac)){
471       $message[] =_("The specified IP address is already in use.");
472     }
473     if(!empty($this->macAddress) && $this->dhcp_is_Account && 
474         $this->macAddress != $this->orig_macAddress && 
475         in_array("mac:".$this->macAddress,$this->used_ip_mac)){
476       $message[] =_("The specified MAC address is already in use.");
477     }
479     /* Check if ip must be given
480      */  
481     if(($this->IPisMust)||($this->DNS_is_account)){
482       if (empty($this->ipHostNumber)){
483         $message[]= _("The required field 'IP-address' is not set.");
484       }
486       if (!is_ip($this->ipHostNumber)){
487         $message[]= _("Wrong IP format in field IP-address.");
488       }
489     }
491     /* Check if mac is empty 
492      */
493     if ($this->macAddress == "" ){
494       $message[]= _("The required field 'MAC-address' is not set.");
495     }
496     if(!is_mac($this->macAddress)){
497       $message[]=(_("The given macaddress is invalid. There must be 6 2byte segments seperated by ':'."));
498     }
500     /* only perfrom this checks if this is a valid DNS account */
501     if($this->DNS_is_account){
503       $checkArray = array();
504       $onlyOnce   = array();
506       //  $onlyOnce['cNAMERecord'] = 0;
507        $tmp = array_flip($this->Zones);
508        $tmp2 = $tmp[$this->dnsEntry['zoneName']];
509        if(!$this->netmaskIsCoherent($tmp2)){ //this->dnsEntry['zoneName'])){
510          $tmp2 = preg_replace("/^.*\//","",$tmp2);
511          $message[] =sprintf(_("The specified IP address '%s' is not matching the selected reverse zone entry '%s'."),$this->ipHostNumber,$tmp2);
512        }
514       /* Walk through all entries and detect duplicates or mismatches
515        */  
516       foreach($this->dnsEntry['RECORDS'] as $name => $values){
518         /* Count record values, to detect duplicate entries for a specific record
519          */
520         if(!isset($checkArray[$values['type']][$values['value']])){
521           $checkArray[$values['type']][$values['value']] = 0;
522         }else{
523           $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
524         }
526         /* Check if given entries in $onlyOnce are used more than once
527          */
528         if(isset($onlyOnce[$values['type']])){
529           $onlyOnce[$values['type']] ++;
530           if($onlyOnce[$values['type']] > 1){
531             $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
532           }
533         }
535         /* Skip txt record ... 
536          */
537         if($values['type'] == "tXTRecord") continue;
539         /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress 
540          */
541         if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
542           $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."), 
543                $this->ipHostNumber);
544         }
546         /* only lower-case is allowed in record entries ... 
547          */
548         if($values['value'] != strtolower($values['value'])){
549           $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
550         }
551       }
552     }
553     return ($message);
554   }
557   /* Save to LDAP */
558   function save()
559   {
560     $ldap= $this->config->get_ldap_link();
561   
562     $dn = $this->parent->dn;
563  
564     /*******************/ 
565     /* IP-MAC HANDLING */
566     /*******************/ 
568     /* $dn was posted as parameter */
569     $this->dn = $dn;
570     
571     /* Save DNS setting & ip/Mac*/
572     plugin::save();
574     /* Write back to ldap */
575     $ldap->cd($this->dn);
576     $this->cleanup();
577     $ldap->modify ($this->attrs); 
579     /****************/ 
580     /* DHCP HANDLING */
581     /****************/ 
582   
583     /* New entry */
584     if($this->dhcpEnabled){
586       /* Write mac address to dhcp settings */
587       if($this->dhcp_is_Account){
588         if(!isset($this->dhcpHostEntry['dhcpHWAddress'][0]) || 
589             !preg_match("/ethernet ".$this->macAddress."/",$this->dhcpHostEntry['dhcpHWAddress'][0])){
590           $this->dhcpHostEntry['dhcpHWAddress'] = array("ethernet ".$this->macAddress);
591           $this->dhcpHostEntry['MODIFIED'] = TRUE;
592         }
593       }
594   
596       /* Unset dhcpStatements if this attribute is empty  */
597       if(isset($this->dhcpHostEntry['dhcpStatements']) && 
598           ($this->dhcpHostEntry['dhcpStatements'] == "" || count($this->dhcpHostEntry['dhcpStatements']) == 0) ){
599         unset($this->dhcpHostEntry['dhcpStatements']);
600       }
601   
602       /* DHCP removed */
603       if($this->initial_dhcp_is_Account && !$this->dhcp_is_Account){
604         $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
605         show_ldap_error($ldap->get_error(),_("Removing dhcp entry for this object failed."));
606       }
608       /* DHCP Added */
609       if(!$this->initial_dhcp_is_Account && $this->dhcp_is_Account){
610         $attrs = $this->dhcpHostEntry;
611         unset($attrs['MODIFIED']);
612         unset($attrs['dn']);
613         $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
614         $res = $ldap->add($attrs);
615 #        print_a($attrs);
616 #        print("cn=".$this->cn.",".$this->dhcpParentNode);
617         show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
618       }
620       /* DHCP still activated */
621       if($this->initial_dhcp_is_Account && $this->dhcp_is_Account){
623         /* DHCP node changed */
624         if(($this->initial_dhcpParentNode != $this->dhcpParentNode) || 
625            ($this->cn != $this->OrigCn)){
626           $attrs = $this->dhcpHostEntry;
627           $attrs['cn'] = $this->cn;
628           unset($attrs['dn']);
629           unset($attrs['MODIFIED']);
630           $ldap->cd("cn=".$this->cn.",".$this->dhcpParentNode);
631           $res = $ldap->add($attrs);
632           show_ldap_error($ldap->get_error(),_("Tried to add new dhcp entry failed."));
633           if($res){
634             $ldap->rmdir_recursive($this->dhcpHostEntry['dn']);
635             show_ldap_error($ldap->get_error(),_("Removing old dhcp entry failed."));
636           }
637         }
638          
639         /* SAME node but modified */ 
640         if(isset($this->dhcpHostEntry['MODIFIED']) && $this->dhcpHostEntry['MODIFIED'] == 1  && 
641             $this->initial_dhcpParentNode == $this->dhcpParentNode){
642           $attrs = $this->dhcpHostEntry;
643           unset($attrs['dn']);
644           unset($attrs['MODIFIED']);
645           $ldap->cd($this->dhcpHostEntry['dn']);
646           $ldap->modify($attrs);
647           show_ldap_error($ldap->get_error(),_("Modifying dhcp entry failed."));
648         }    
649       }
650     }
651       
653     /****************/ 
654     /* DNS HANDLING */
655     /****************/ 
657     /* If isn't DNS account but initially was DNS account 
658        remove all DNS entries 
659      */ 
660     if((!$this->DNSenabled) || ((!$this->DNS_is_account)&&(!$this->initially_was_account))){
661       return;
662     }else{
664       /* Add ipHostNumber to aRecords
665        */
666       $backup_dnsEntry = $this->dnsEntry;
667       if(!empty($this->ipHostNumber)){
668         $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
669         $ptr = $this->get_pTRRecord();
670         if(!empty($ptr)){
671           $this->dnsEntry['RECORDS'][] = array("type"=>"pTRRecord","value"=>$ptr);
672         } 
673       }
675       /* Create diff and follow instructions 
676        * If Account was disabled, remove account by setting exists to false
677        */
678       if((!$this->DNS_is_account)&&($this->initially_was_account)){  
679         $this->dnsEntry['exists'] = false;
680         $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
681       }else{
682         $this->dnsEntry['exists'] = $this->DNS_is_account;
683         $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
684       }   
686       /* move follwoing entries 
687        */
688       foreach($tmp['move'] as $src => $dst){
689         $this->recursive_move($src,$dst);
690       }
692       /* Delete dns */
693       foreach($tmp['del'] as $dn => $del){
694         $ldap->cd($dn);
695         $ldap->rmdir_recursive($dn);
696         new log("modify","unknown/".get_class($this),$dn,array("*"),$ldap->get_error());
697       }
699       /* Add || Update new DNS entries 
700        */
701       foreach($tmp['add'] as $dn => $attrs){
702         $ldap->cd($dn);
703         $ldap->cat($dn, array('dn'));
704         if(count($ldap->fetch())){
705           $ldap->cd($dn);
706           $ldap->modify ($attrs); 
707         }else{
708           $ldap->cd($dn);
709           $ldap->add($attrs);
710         }
711         new log("modify","unknown/".get_class($this),$dn,array_keys($attrs),$ldap->get_error());
712       }
715       /* Display errors 
716        */
717       if($ldap->get_error() != "Success"){
718         show_ldap_error($ldap->get_error(), sprintf(_("Saving of terminal/dns account with dn '%s' failed."),$this->dn));
719       }
721       $this->dnsEntry =  $backup_dnsEntry;
722     }
723   }
725   /*  Create html table with all used record types
726    */
727   function generateRecordsList()
728   {
729     $changeStateForRecords = "";
730     
731     if(!$this->DNS_is_account) {
732       $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled='disabled'>";
733       return(array("str" => $str, "changeStateForRecords"=> ""));
734     }
735  
736     $str = "<table summary='' width='100%'>";
737     foreach($this->dnsEntry['RECORDS'] as $key => $entry){
739         $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
740         $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
741         $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
743         $str.=" <tr>".
744           "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
745           "   <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
746           "   <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
747           "</tr>";
748     }
750     $str.= "  <tr>".
751            "    <td colspan=2 width='50%'></td><td>".
752            "      <input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord'>".
753            "    </td>".
754            "  </tr>".
755            "</table>";
756      $ret =  array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
757     return($ret);
758   }
761   /* Create a html select box which allows us to select different types of records 
762    */
763   function generateRecordListBox($selected,$name)
764   {
765     $str = "<select name='".$name."' id='".$name."'>";
766     foreach($this->RecordTypes as $type => $value){
767       $use = "";
768       if($type == $selected){
769         $use = " selected ";
770       }
771       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
772     }
773     $str.="</select>";
774     return($str); 
775   }
778   /* Return plugin informations for acl handling  */ 
779   function plInfo()
780   {
781     $tmp =  array(
782         "plShortName"   => _("DNS"),
783         "plDescription" => _("DNS settings"),
784         "plSelfModify"  => FALSE,
785         "plDepends"     => array(),
786         "plPriority"    => 5,
787         "plSection"     => array("administration"),
788         "plCategory"    => array("workstation","terminal","phone","server","component","printer","winworkstation"),
790         "plProvidedAcls"=> array(
791           "ipHostNumber"  => _("IP address"),
792           "macAddress"    => _("MAC address"))
793         );
795     /* Hide all dns specific code, if dns is not available
796      */
797     foreach($_SESSION['config']->data['TABS']['SERVERSERVICE'] as $tab){
798       if(preg_match("/^servdns$/",$tab['CLASS'])){
799         $tmp['plProvidedAcls']["Records"]        = _("DNS records");
800         $tmp['plProvidedAcls']["zoneName"]       = _("Zone name");
801         $tmp['plProvidedAcls']["dNSTTL"]         = _("TTL");
802       }
803     }
804     return($tmp);
805   }
807   
808   function get_dhcp_host_entry()
809   {
810     $attrs = array();
811     $dn = $this->get_dhcp_host_entry_dn();
812     if($dn){
813       $ldap = $this->config->get_ldap_link();
814       $ldap->cd($this->config->current['BASE']);
815       $ldap->cat($dn,array("*"));
816       if($ldap->count()){
817         $attrs = $ldap->fetch();
818         foreach($attrs as $key => $value){
819           if(is_numeric($key) || ($key == "count")){
820             unset($attrs[$key]);
821           }
822           if(is_array($value) && isset($value['count'])){
823             unset($attrs[$key]['count']);
824           }
825         }
826       }
827     }
828     return($attrs);
829   }
832   function get_dhcp_host_entry_dn()
833   {
834     $ldap = $this->config->get_ldap_link();
835     $ldap->cd($this->config->current['BASE']);
836     $ldap->search ("(&(objectClass=dhcpHost)(cn=".$this->cn."))",array("cn","dn"));
838     if($ldap->count()){
839       $attr = $ldap->fetch();
840       return($attr['dn']);
841     }else{
842       return("");
843     }
844   }  
847   function get_dhcp_parent_node()
848   {
849     return(preg_replace("/^cn=".normalizePreg($this->cn).",/","",$this->get_dhcp_host_entry_dn()));
850   }
853   function get_dhcp_parent_nodes()
854   {
855     $ldap = $this->config->get_ldap_link();
856     $ldap->cd($this->config->current['BASE']);
857     $ldap->search("(objectClass=dhcpService)",array("dhcpPrimaryDN"));
858     
859     $dhcp_dns = array();
860     while($attr = $ldap->fetch()){
861       $dhcp_dns[$attr['dn']] = $attr['dhcpPrimaryDN'][0];
862     }
863  
864     foreach($dhcp_dns as $key => $pri_dns){
865       $ldap->cat($pri_dns,array("cn"));
866       $tmp = $ldap->fetch();
867       if(isset($tmp['cn'][0])){
868         $dhcp_dns[$key] = $tmp['cn'][0];
869       }else{
870         unset($dhcp_dns[$key]);
871       }
872     }
874     $tmp = $tmp2 = array();
875     foreach($dhcp_dns as $dn => $cn){
876       $ldap->cd($dn);
877       $ldap->search("(|(objectClass=dhcpService)(objectClass=dhcpGroup)".
878                     "(objectClass=dhcpSubnet)(objectClass=dhcpSharedNetwork))",array("cn"));
879       while($attr = $ldap->fetch()){
880         $tmp[$attr['dn']] = $attr['cn'][0];
881       }
882       $tmp2 = array_merge($tmp2,$this->create_tree($tmp,preg_replace("/^[^,]+,/i","",$dn),"(".$cn.")&nbsp;"));
883     }
884     return($tmp2);
885   }
887   
888   /* this function returns the default ptr record entry */
889   function get_pTRRecord()
890   {
891     if(!empty($this->ipHostNumber) && isset($this->dnsEntry['zoneName']) && !empty($this->dnsEntry['zoneName'])){
892       $ldap = $this->config->get_ldap_link();
893       $ldap->cat(getDNSZoneDN($this->config,$this->dnsEntry['zoneName']));
894       $attrs = $ldap->fetch();
895       $tmp = array_flip($this->Zones);
896       $tmp = preg_replace("/^[^\/]*+\//","",$tmp[$this->dnsEntry['zoneName']]);
897       $tmp = trim(preg_replace("/\.in-addr.arpa$/","",$tmp));
898       $ptr = preg_replace("/^".normalizePreg(FlipIp($tmp))."\./","",$this->ipHostNumber);
899       return($ptr);
900     }else{
901       return(FALSE);
902     }
903   }
905   
906   function create_tree($arr,$base,$current = "")
907   {
908     $ret = array();
909     foreach($arr as $r => $name){
910       $base_part = str_replace($base,"",$r);
911       if(preg_match("/^[a-z]*=".normalizePreg($name)."(|,)$/i",$base_part)){
912         $ret[$r] = $current.$name;
913         $tmp = $this->create_tree($arr,$r,$current.".&nbsp;");
914         foreach($tmp as $sub_key => $sub_name){
915           $ret[$sub_key] = $sub_name;
916         }
917       } 
918     }
919     return($ret);
920   }
923 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
924 ?>