Code

fixed minor bugs
[gosa.git] / plugins / admin / systems / class_termDNS.inc
1 <?php
3 class termDNS extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary      = "Manage server basic objects";
7   var $cli_description  = "Some longer text\nfor help";
8   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account = TRUE;
13   /* Basic informations 
14    */
15   var $attributes     = array("ipHostNumber","macAddress");
16   var $objectclasses  = array("whatever");
18   var $ipHostNumber   = "";    // IP address 
19   var $macAddress     = "";    // Mac address 
20   var $cn             = "";    // CN of currently edited device 
21   var $OrigCn         = "";    // Initial cn
22   var $IPisMust       = false;
23   var $MACisMust      = false;
25   /* DNS attributes  
26    */
27   var $DNSattributes            = array("dNSClass","zoneName","dNSTTL");
28   var $DNS_is_Account           = false;
29   var $DNSinitially_was_account = false;
30   var $dnsEntry                 = array();
31   var $DNSenabled               = false;
33   /*  Terminal dns 
34    */
35   function termDNS ($config, $dn,$objectClasses,$IPisMust = false)
36   {
37     /* We need to know which objectClasses are used, to store the ip/mac
38      * Because of different type of devices   
39      */
40     $this->objectclasses  =  $objectClasses;
41     $this->IPisMust       = $IPisMust;
43     plugin::plugin ($config, $dn);
45     $this->OrigCn = $this->attrs['cn'][0];
47  
48     /* Hide all dns specific code, if dns is not available 
49      */
50     $DNSenabled = false;
51     foreach($this->config->data['TABS']['SERVTABS'] as $tab){
52       if(preg_match("/^servdns$/",$tab['CLASS'])){
53         $this->DNSenabled = true;
54       }
55     }
56     if(!$this->DNSenabled){
57       $this->DNS_is_account = false;
58       return;
59     }
61     /* Get Zones  
62      */
63     $this->Zones        = getAvailableZones($config);
65     /* Get Entry 
66      */
67     $this->dnsEntry     = getDNSHostEntries($config,$this->OrigCn);
69     /* Remove A record which equals $this->ipHostNumber
70      */
71     foreach($this->dnsEntry['RECORDS'] as $key => $rec){
72       if(($rec['type'] == "aRecord") && ($rec['value'] == $this->ipHostNumber)){
73         unset($this->dnsEntry['RECORDS'][$key]);
74       }
75     }
77     /* Get Record types 
78      */
79     $this->RecordTypes  = getDnsRecordTypes();
81     /* If there is at least one entry in this -> types, we have DNS enabled 
82      */
83     if($this->dnsEntry['exists']){
84       $this->DNS_is_account = true;
85     }else{
86       $this->DNS_is_account = false;
87     }
88     
89     /* Store initally account settings 
90      */
91     $this->DNSinitially_was_account = $this->DNS_is_account;
92   }
95   function getVarsForSaving($attrs) 
96   {
97     foreach($this->attributes as $attr){
98       if(!empty($this->$attr)){
99         $attrs[$attr] = $this->$attr;
100       }
101     }
102     return($attrs); 
103   }
105   function execute()
106   {
107           /* Call parent execute */
108     $smarty= get_smarty();
109     $display= "";
111     $smarty->assign("staticAddress", ""); 
112  
113     /* There is no dns available 
114      */
115     if($this->DNSenabled == false){
116        
117       /* Is IP address must ? */ 
118       $smarty->assign("DNS_is_account",false);  
119       $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
120     
121       /* Assign smarty all non DNs attributes */
122       foreach($this->attributes as $attr){
123         $smarty->assign($attr,$this->$attr);
124       }
125       $smarty->assign("staticAddress", "");
126       $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
127       return($display);
128     }else{
129       $smarty->assign("DNS_is_account",true); 
130     }
131  
132     /* Add new empty array to our record list */
133     if(isset($_POST['AddNewRecord'])){
134       $this->dnsEntry['RECORDS'][]  =array("type"=>"aRecord","value"=>"");
135     }
136    
137     /* Handle all posts */ 
138     $only_once =true;
139     foreach($_POST as $name => $value){
141       /* Check if we have to delete a record entry */
142       if((preg_match("/RemoveRecord_/",$name))&&($only_once)) {
143       
144         /* Avoid performing this once again */
145         $only_once = false;
147         /* Extract id for specified entry */
148         $id = preg_replace("/RemoveRecord_/","",$name);
149         $id = preg_replace("/_.*$/","",$id);
150     
151         /* Delete this record, mark edited entries to be able to delete them */
152         if(isset($this->dnsEntry['RECORDS'][$id])){
153           unset($this->dnsEntry['RECORDS'][$id]);
154         }
155       }
156     }
158     /* Assign smarty all non DNs attributes */
159     foreach($this->attributes as $attr){
160       $smarty->assign($attr,$this->$attr);
161     }
163     /* Assign smarty all DNS attributes */
164     foreach($this->DNSattributes as $attr){
165       $smarty->assign($attr,$this->dnsEntry[$attr]);
166     }
167     
168     /* Assign all needed vars */
169     $smarty->assign("DNSAccount",$this->DNS_is_account);
170     $smarty->assign("Zones",$this->Zones);
171     $smarty->assign("ZoneKeys",($this->Zones));
172     $smarty->assign("IPisMust",(($this->IPisMust)||($this->DNS_is_account)));
173   
174     $tmp = $this->generateRecordsList();
175     
176     $changeStateForRecords = $tmp['changeStateForRecords'];
178     $smarty->assign("records",$tmp['str']);
179     $smarty->assign("changeStateForRecords",$changeStateForRecords);
180     $smarty->assign("staticAddress","<font class=\"must\">*</font>");
182     $display.= $smarty->fetch(get_template_path('network.tpl', TRUE));
183     return($display);
184   }
186   function remove_from_parent()
187   {
188     /*
189     $ldap = $this->config->get_ldap_link();
190     $ldap->cd($this->orig_dn);
191     $ldap->search("(&(objectClass=dNSZone)(zoneName=*)(!(relativeDomainName=@)))",array("relativeDomainName","zoneName"));
192     while($attr = $ldap->fetch()){  
193       $ldap->cd($attr['dn']);
194       $ldap->rmDir($attr['dn']);
195     }
196     */
197   }
199   /* Save data to object */
200   function save_object()
201   {
202     /* Save all posted vars */
203     plugin::save_object();
204     
205     /* Ge all non dns attributes (IP/MAC)*/
206     foreach($this->attributes as $attr){
207       if(isset($_POST[$attr])){
208         $this->$attr = $_POST[$attr];
209       }
210     }
212     /* Get dns attributes */
213     if(isset($_POST['network_tpl_posted'])){
215       /* Check for posted record changes */
216       foreach($this->dnsEntry['RECORDS'] as $key => $value){
217       
218         /* Check if type has changed */
219         if(isset($_POST['RecordTypeSelectedFor_'.$key])){
220           $this->dnsEntry['RECORDS'][$key]['type'] = $_POST['RecordTypeSelectedFor_'.$key];
221         }
222         /* Check if value has changed */
223         if(isset($_POST['RecordValue_'.$key])){
224           $this->dnsEntry['RECORDS'][$key]['value'] = $_POST['RecordValue_'.$key];
225         }
226       }
227       
228       /* Get all basic DNS attributes (TTL, Clas ..)*/
229       foreach($this->DNSattributes as $attr){
230         if(isset($_POST[$attr])){
231           $this->dnsEntry[$attr] = $_POST[$attr];
232         }
233       }
235       /* Enable diable DNS */
236       if(isset($_POST['enableDNS'])){
237         $this->DNS_is_account = true;
238       }else{
239         $this->DNS_is_account = false;
240       }
241     }
242   }
245   /* Check supplied data */
246   function check()
247   {
248     $message= array();
251     /* Check if ip must be given
252      */  
253     if(($this->IPisMust)||($this->DNS_is_account)){
254   
255       /* Check if ip is empty 
256        */
257       if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
258         $message[]= _("The required field 'IP-address' is not set.");
259       }
261       /* check if given ip is valid ipi
262        */
263       $num="(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
264       if (!preg_match("/^$num\\.$num\\.$num\\.$num$/", $this->ipHostNumber)){
265         $message[]= _("Wrong IP format in field IP-address.");
266       }
267     }
269     /* Check if mac is empty 
270      */
271     if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
272       $message[]= _("The required field 'MAC-address' is not set.");
273     }
275     /* Check if given mac is valid mac 
276      */
277     $tr = count(split(":",$this->macAddress));
278     if($tr!=6){
279       $message[]=(_("The given macaddress is invalid. There must be 6 1byte segments seperated by ':'."));
280     }
282     /* only perfrom this checks if this is a valid DNS account */
283     if($this->DNS_is_account){
285       $checkArray = array();
286       $onlyOnce   = array();
288       $onlyOnce['cNAMERecord'] = 0;
290       /* Walk through all entries and detect duplicates or mismatches
291        */  
292       foreach($this->dnsEntry['RECORDS'] as $name => $values){
294         /* Count record values, to detect duplicate entries for a specific record
295          */
296         if(!isset($checkArray[$values['type']][$values['value']])){
297           $checkArray[$values['type']][$values['value']] = 0;
298         }else{
299           $message[] = sprintf(_("Found duplicate value for record type '%s'."),$values['type']);
300         }
302         /* Check if given entries in $onlyOnce are used more than once
303          */
304         if(isset($onlyOnce[$values['type']])){
305           $onlyOnce[$values['type']] ++;
306           if($onlyOnce[$values['type']] > 1){
307             $message[] = sprintf(_("Found more than one entry for the uniqe record type '%s'."),$values['type']);
308           }
309         }
311         /* Skip txt record ... 
312          */
313         if($values['type'] == "tXTRecord") continue;
315         /* Check if there is an aRecord defined which uses the same IP as used in IPhostAddress 
316          */
317         if(($values['type'] == "aRecord")&&($values['value'] == $this->ipHostNumber)){
318           $message[]=sprintf(_("The device IP '%s' is added as 'A Record', this will be done automatically, please remove the record."), 
319                $this->ipHostNumber);
320         }
322         /* only lower-case is allowed in record entries ... 
323          */
324         if($values['value'] != strtolower($values['value'])){
325           $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
326         }
327       }
328     }
329     return ($message);
330   }
333   /* Save to LDAP */
334   function save($dn)
335   {
336     $ldap= $this->config->get_ldap_link();
337    
338     /*******************/ 
339     /* IP-MAC HANDLING */
340     /*******************/ 
342     /* $dn was posted as parameter */
343     $this->dn = $dn;
344     
345     /* Save DNS setting & ip/Mac*/
346     plugin::save();
348     /* Write back to ldap */
349     $ldap->cd($this->dn);
350     $this->cleanup();
351     $ldap->modify ($this->attrs); 
353     /****************/ 
354     /* DNS HANDLING */
355     /****************/ 
357     /* If isn't DNS account but initially was DNS account 
358        remove all DNS entries 
359      */ 
361     /* Add ipHostNumber to aRecords
362      */
363     $this->dnsEntry['RECORDS'][] = array("type"=>"aRecord","value"=>$this->ipHostNumber);
365     /* Create diff and follow instructions 
366      * If Account was disabled, remove account by setting exists to false
367      */
368     if((!$this->DNS_is_account)&&($this->DNSinitially_was_account)){  
369       $this->dnsEntry['exists'] = false;
370       $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
371     }else{
372       $this->dnsEntry['exists'] = $this->DNS_is_account;
373       $tmp = getDNSHostEntriesDiff($this->config,$this->OrigCn,$this->dnsEntry,$this->cn);
374     }   
376     /* move follwoing entries 
377      */
378     foreach($tmp['move'] as $src => $dst){
379       $this->recursive_move($src,$dst);
380     }
382     /* Delete dns */
383     foreach($tmp['del'] as $dn => $del){
384       $ldap->cd($dn);
385       $ldap->rmdir_recursive($dn);
386     }
388     /* Add || Update new DNS entries 
389      */
390     foreach($tmp['add'] as $dn => $attrs){
391       $ldap->cd($dn);
392       $ldap->cat($dn);
393       if(count($ldap->fetch())){
394         $ldap->cd($dn);
395         $ldap->modify ($attrs); 
396       }else{
397         $ldap->cd($dn);
398         $ldap->add($attrs);
399       }
400     }
401     
402     /* Display errors 
403      */
404     if($ldap->get_error() != "Success"){
405       show_ldap_error("Record:".$ldap->get_error()); 
406     }
407   }
409   /*  Create html table with all used record types
410    */
411   function generateRecordsList()
412   {
413     $changeStateForRecords = "";
414     
415     if(!$this->DNS_is_account) {
416       $str = "<input type='submit' value='"._("Add")."' name='AddNewRecord' id='AddNewRecord' disabled>";
417       return $str;
418     }
419  
420     $str = "<table summary='' width='100%'>";
421     foreach($this->dnsEntry['RECORDS'] as $key => $entry){
423         $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
424         $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
425         $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
427         $str.=" <tr>".
428           "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
429           "   <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
430           "   <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
431           "</tr>";
432     }
434     $str.= "  <tr>".
435            "    <td colspan=2 width='50%'></td><td>".
436            "      <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
437            "    </td>".
438            "  </tr>".
439            "</table>";
440      $ret =  array("str" => $str, "changeStateForRecords" => $changeStateForRecords);
441     return($ret);
442   }
445   /* Create a html select box which allows us to select different types of records 
446    */
447   function generateRecordListBox($selected,$name)
448   {
449     $str = "<select name='".$name."' id='".$name."'>";
450     foreach($this->RecordTypes as $type => $value){
451       $use = "";
452       if($type == $selected){
453         $use = " selected ";
454       }
455       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
456     }
457     $str.="</select>";
458     return($str); 
459   }
462 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
463 ?>