Code

Updated acl handling for dns attributes
[gosa.git] / plugins / admin / systems / class_servDNSeditZone.inc
1 <?php
3 class servdnseditZone 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;
12   var $attributes     = array("zoneName","ReverseZone","dNSClass",
13       "sOAprimary","sOAmail","sOAserial","sOArefresh","sOAretry","sOAexpire","sOAttl"); 
14   var $objectclasses  = array("whatever");
16   var $RecordTypes              = array();
18   var $ReverseZone              = "";
19   var $zoneName                 = "";
20   var $dNSClass                 = "IN";
22   var $sOAprimary               = "";
23   var $sOAmail                  = "";
24   var $sOAserial                = "";
25   var $sOArefresh               = "3600";
26   var $sOAretry                 = "1800";
27   var $sOAexpire                = "720000";
28   var $sOAttl                   = "6400";
30   var $Records                  = array();
31   var $mXRecords                = array();
33   var $OldZoneName              = ""; // To detect changes made with this edit
34   var $OldReverseZone           = "";
36   var $InitialReverseZone       = "";
37   var $InitialzoneName          = "";
38   var $NetworkClass                = "A" ; // One out of A,B,C
40   var $dialog                   = false;
42   var $isNew                    = true;
44   var $ZoneObject               = array();
46   function servdnseditZone ($config, $dn= NULL,$attrs = array())
47   {
48     plugin::plugin ($config, $dn);
50     /* All types with required attrs */
51     $this->RecordTypes = getDnsRecordTypes(true); 
53     if(!count($attrs)){
54       $this->OldZoneName        = "";
55       $this->OldReverseZone     = "";
56       $this->isNew              = true;
57       $this->sOAserial          = date("Ymd")."1";
58       
59       $this->InitialzoneName    = "";//$attrs['InitialzoneName'];
60       $this->InitialReverseZone = "";//$attrs['InitialReverseZone'];
61     }else{
62       $this->ZoneObject         = $attrs;
65       $this->OldZoneName        = $attrs['zoneName'];
66       $this->OldReverseZone     = $attrs['ReverseZone'];
68       $this->InitialzoneName    = $attrs['InitialzoneName'];
69       $this->InitialReverseZone = $attrs['InitialReverseZone'];
71       $this->isNew                  = false;
73       foreach($this->attributes as $value){
74         $this->$value = $attrs[$value];
75       }
77       $this->sOAmail            = preg_replace("/\./","@",$this->sOAmail,1);
78       $this->sOAmail            = preg_replace("/\.$/","",$this->sOAmail);
79       $this->sOAprimary         = preg_replace("/\.$/","",$this->sOAprimary);
80       $this->zoneName           = preg_replace("/\.$/","",$this->zoneName);
82       if(isset($attrs['RECORDS'])){
83         $this->Records = $attrs['RECORDS']; 
85         $tmp2 = array();
86         $usedPrio = array();
87         foreach($this->Records as $key => $rec){
88           if($rec['type'] == "mXRecord"){
89             $tmp = split(" ",$rec['value']);
90             $rec['value'] = $tmp[1];
91             $tmp2[$tmp[0]] = $rec;
92             unset($this->Records[$key]);
93           }
94           if($rec['type'] == "nSRecord"){
95             unset($this->Records[$key]);
96           }
97         }
98         if(count($tmp2) != 0){
99           reset($tmp2);
100           ksort($tmp2);
101         }
102         $this->mXRecords = $tmp2;
103       }else{
104         $this->mXRecords  = array();
105         $this->Records    = array();
106       }
108       $str = date("Ymd");
109       if(preg_match("/^".$str."/",$this->sOAserial)){
110         $this->sOAserial = $this->sOAserial + 1;
111       }else{
112         $this->sOAserial = date("Ymd")."01";
113       }
114     }
116     /* Detect Network class */
117     if(!empty($this->ReverseZone)){
119       $dots = count(split(".",$this->ReverseZone));
120       if($dots == 0){
121         $this->NetworkClass = "A";  
122         $this->ReverseZone .= ".0.0.0"; 
123       }elseif($dots == 1){
124         $this->NetworkClass = "B";  
125         $this->ReverseZone .= ".0.0"; 
126       }else{
127         $this->NetworkClass = "C";  
128         $this->ReverseZone .= ".0"; 
129       }
130     }
131   }
133   /* TRansports the geiven Arraykey one position up*/
134   function ArrayUp($atr,$attrs)
135   {
136     $ret = $attrs;
137     $pos = $atr ;
138     $cn = count($attrs);
139     if(!(($pos == -1)||($pos == 1)||($pos >$cn))){
140       $before = array_slice($attrs,0,($pos-2));
141       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
142       $unten  = array_slice($attrs,$pos);
143       $ret = array();
144       $ret = $this->combineArrays($before,$mitte,$unten);
145     }
146     return($ret);
147   }
150   /* TRansports the geiven Arraykey one position up*/
151   function ArrayDown($atr,$attrs)
152   {
153     $ret = $attrs;
154     $pos = $atr ;
155     $cn = count($attrs);
156     if(!(($pos == -1)||($pos == $cn))){
157       $before = array_slice($attrs,0,($pos-1));
158       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
159       $unten  = array_slice($attrs,($pos+1));
160       $ret = array();
161       $ret = $this->combineArrays($before,$mitte,$unten);
162     }
163     return($ret);
164   }
166   /* Combine new array */
167   function combineArrays($ar0,$ar1,$ar2)
168   {
169     $ret = array();
170     if(is_array($ar0))
171     foreach($ar0 as $ar => $a){
172         $ret[]=$a;
173     }
174     if(is_array($ar1))
175     foreach($ar1 as $ar => $a){
176         $ret[]=$a;
177     }
178     if(is_array($ar2))
179     foreach($ar2 as $ar => $a){
180         $ret[]=$a;
181     }
182     return($ret);
183   }
184   
185   function getpos($atr,$attrs)
186   {
187     $i = 0;
188     foreach($attrs as $attr => $name)    {
189       $i++;
190       if($attr == $atr){
191         return($i);
192       }
193     }
194     return(-1);
195   }
198   function execute()
199   {
200     /* Call parent execute */
201     plugin::execute();
205     /* Fill templating stuff */
206     $smarty= get_smarty();
207     $ui = get_userinfo();
209     $smarty->assign("ACLs",$this->parent->getacl(""));
210     $display= "";
212     /* Open Zone Entry Edit Dialog
213      */
214     if(!count($this->ZoneObject)){
215       $smarty->assign("AllowZoneEdit" , false);
216     }else{
217       $smarty->assign("AllowZoneEdit" , true);
218       if(isset($_POST['EditZoneEntries'])){
219         $this->dialog= new servDNSeditZoneEntries($this->config,$this->dn,$this->ZoneObject);
220       }
221     }
223     /* Save Zone Entry Edit Dialog
224      */
225     if(isset($_POST['SaveZoneEntryChanges'])){
226       $this->dialog->save_object();
227       if(count($this->dialog->check())){
228         $msgs = $this->dialog->check();
229         foreach($msgs as $msg){
230           print_red($msg);
231         }
232       }else{
233         $this->dialog->save();
234         $this->dialog = false;
235       }
236     }
238     /* Cancel Zone Entrie Edit Dialog
239     */
240     if(isset($_POST['CancelZoneEntryChanges'])){
241       $this->dialog = false;
242     }
244     /* Display any type of open dialogs 
245      */
246     if($this->dialog){
247       $this->dialog->save_object();
248       return($this->dialog->execute());
249     }
251     $once =true;
252     foreach($_POST as $name => $value){
253       if((preg_match("/^MXup_/",$name)) && ($once)){
254         $once = false;
256         $id = preg_replace("/^MXup_/","",$name);
257         $id = preg_replace("/_.*$/","",$id);
258         $id = base64_decode($id);
259     
260         $this->mXRecords = $this->ArrayUp(($id+1),$this->mXRecords);
261       }
262       if((preg_match("/^MXdown_/",$name)) && ($once)){
263         $once = false;
264         
265         $id = preg_replace("/^MXdown_/","",$name);
266         $id = preg_replace("/_.*$/","",$id);
267         $id = base64_decode($id);
268   
269         $this->mXRecords = $this->ArrayDown(($id+1),$this->mXRecords);
270       }
271       if((preg_match("/^MXdel_/",$name)) && ($once)){
272         $once = false;
273         
274         $id = preg_replace("/^MXdel_/","",$name);
275         $id = preg_replace("/_.*$/","",$id);
276         $id = base64_decode($id);
277         
278         unset($this->mXRecords[$id]);
280         $tmp  =array();
281         foreach($this->mXRecords as $entry){
282           $tmp[] = $entry;
283         }
284  
285         $this->mXRecords = $tmp; 
286       }
287     }
289     if((isset($_POST['AddMXRecord'])) && (!empty($_POST['StrMXRecord']))){
290       $this->mXRecords[] = array("type"=>"mXRecord","value"=>trim($_POST['StrMXRecord']));      
291     }
293     /* Handle Post events */
294     $once = true;
295     foreach($_POST as $name => $value){
297       /* Delete record if requested */
298       if((preg_match("/RemoveRecord_/",$name))&&($once)){
299         $once = false;
300         $id= preg_replace("/RemoveRecord_/","",$name);
301         unset($this->Records[$id]);
302       }
303     }
305     /* Add new Zonerecord */
306     if(isset($_POST['AddNewRecord'])){
307       $this->Records[] = array("type"=>"aRecord","value"=>"");
308     }
310     /* Fill in values */
311     foreach($this->attributes as $name){
312       $smarty->assign($name,$this->$name);
313     }
316     $div = new DivSelectBox("MxRecords");
317     $div->setHeight(120);
318     $recs = $this->mXRecords;
320     $oneup    = "<input name='MXup_%s'    type='image' src='images/sort_up.png'    title='"._("Up")."'      class='center'>&nbsp;"; 
321     $onedown  = "<input name='MXdown_%s'  type='image' src='images/sort_down.png'  title='"._("Down")."'    class='center'>&nbsp;"; 
322     $onedel   = "<img src='images/empty.png' width='20' class='center'>
323                  <input name='MXdel_%s'   type='image' src='images/edittrash.png'  title='"._("Delete")."'  class='center'>"; 
325     foreach($recs as $key => $rec){
326       $div ->AddEntry(array(
327             array("string"=>$rec['value']),
328 /*            array("string"=>$key,
329                   "attach"=>"style='width:20px;'"),*/
330             array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
331                   "attach"=>"style='width:70px;border-right:0px;'")
332             ));
333     }
335     /* Assign records list */
336     $smarty->assign("NotNew", false);
337     $smarty->assign("Mxrecords",  $div->DrawList());
338     $smarty->assign("records"  ,  $this->generateRecordsList());
339     $smarty->assign("NetworkClass",  $this->NetworkClass);
340     $smarty->assign("NetworkClasses",  array("A"=>"255.0.0.0 (Class A)","B"=>"255.255.0.0 (Class B)","C"=>"255.255.255.0 (Class C)"));
342     /* Display tempalte */
343     $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
344     return($display);
345   }
347   function remove_from_parent()
348   {
349   }
351   /* Save data to object */
352   function save_object()
353   {
354     //plugin::save_object();
355     foreach($this->attributes as $attr){
356       if(isset($_POST[$attr])){
357         $this->$attr = $_POST[$attr];
358       }
359     }
361     foreach($this->Records as $id => $value){  
362       if(isset($_POST['RecordTypeSelectedFor_'.$id])){
363         $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
364       }
365       if(isset($_POST['RecordValue_'.$id])){
366         $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
367       }
368     }
370      if(isset($_POST['NetworkClass'])){
371        $this->NetworkClass = $_POST['NetworkClass'];
372      }
374   }
377   /* Check supplied data */
378   function check()
379   {
380     /* Call common method to give check the hook */
381     $message= plugin::check();
382         
383     /* Check if zoneName is already in use */
384     $usedZones = $this->getUsedZoneNames();
385     if(($this->isNew == true)||($this->zoneName  != $this->InitialzoneName)||($this->ReverseZone != $this->InitialReverseZone)){
386     /*  if((isset($usedZones[$this->zoneName]))&&($this->zoneName  != $this->InitialzoneName)){
387         $message[] =_("This zoneName is already in use");
388       }
389       if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitialReverseZone)){
390         $message[] =_("This reverse zone is already in use");
391       }*/
392     }
394     if(empty($this->zoneName)){
395       $message[] =sprintf(_("Please choose a valid zone name."));
396     }
398     if(empty($this->ReverseZone)){
399       $message[] =sprintf(_("Please choose a valid reverse zone name."));
400     }
402     if($this->zoneName != strtolower($this->zoneName)){
403       $message[] = _("Only lowercase strings are allowed as zone name.");
404     }
406     if(!is_numeric($this->sOAserial)){
407       $message[] = _("Please specify a numeric value for serial number.");
408     }
410     if(!is_numeric($this->sOArefresh)){
411       $message[] = _("Please specify a numeric value for refresh.");
412     }
414     if(!is_numeric($this->sOAttl)){
415       $message[] = _("Please specify a numeric value for ttl.");
416     }
418     if(!is_numeric($this->sOAexpire)){
419       $message[] = _("Please specify a numeric value for expire.");
420     }
422     if(!is_numeric($this->sOAretry)){
423       $message[] = _("Please specify a numeric value for retry.");
424     }
426     foreach($this->Records as $name => $values){
427       /* only lower-case is allowed in record entries ... */
428       if($values['value'] != strtolower($values['value'])){
429         $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
430       }
431     }
433     /* Check class for given Zone Address */
434     $addr = preg_replace("/^[^\/]*+\//","",$this->ReverseZone);
435   
436     /* Check for valid&complete IP address */
437     if(!is_ip($addr)){
438       $message[] = _("The given network address is not a valid, please specify a valid IP address.");
439     }
440  
441     /* Check if given address matches selected network class */
442     switch($this->NetworkClass){
443       case 'A': { 
444                   if(!preg_match("/^[0-9]*\.0\.0\.0$/",$addr)){
445                     $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.0.0.0"));
446                   }
447                 }
448                 break;
449       case 'B': {
450                   if(!preg_match("/^[0-9]*\.[0-9]*\.0\.0$/",$addr)){
451                     $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.x.0.0"));
452                   }
453                 }
454                 break;
455       case 'C': {
456                   if(!preg_match("/^[0-9]*\.[0-9]*\.[0-9]*\.0$/",$addr)){
457                     $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.x.x.0"));
458                   }
459                 }
460                 break;
461       default : $message[] =sprintf(_("The given network class '%s' is not valid."),$this->NetworkClass);
462     }
464     return ($message);
465   }
467   /* This funtion returns all used Zonenames */
468   function getUsedZoneNames()
469   {
470     $ret = array();
471     $ldap = $this->config->get_ldap_link();
472     $ldap->cd($this->config->current['BASE']);
473     $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
474     while($attr = $ldap->fetch()){
475       if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
476         if(isset($attr['tXTRecord'][0])){
477           $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
478           $ret[$zn] =FlipIp(preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]));
479         }
480       }else{
481         $ret[$attr['zoneName'][0]]="";
482       }
483     }
484     return($ret);
485   }
487   /* Save to LDAP */
488   function save()
489   {
490     $ret =array();
491     foreach($this->attributes as $name){
492       $ret[$name] = $this->$name;
493     }
495     /* Create mx records 
496      */
497     foreach($this->mXRecords as $key => $rec){
498       $rec['value']= $key." ".$rec['value'];
499       $this->Records [] = $rec;
500     }
502   
503     $ret['RECORDS'] = $this->Records; 
504  
505     switch($this->NetworkClass){
506       case 'C' : $ret['ReverseZone']= preg_replace("/\.[0-9]*$/","",$this->ReverseZone);break;
507       case 'B' : $ret['ReverseZone']= preg_replace("/\.[0-9]*\.[0-9]*$/","",$this->ReverseZone);break;
508       case 'A' : $ret['ReverseZone']= preg_replace("/\.[0-9]*\.[0-9]*\.[0-9]*$/","",$this->ReverseZone);break;
509       default : trigger_error("Invalid network class given '".$this->NetworkClass."'");
510     }
512     $ret['InitialReverseZone']=  $this->InitialReverseZone;
513     $ret['InitialzoneName']   =  $this->InitialzoneName;
515     $ret['sOAmail']            = preg_replace("/\@/",".",$this->sOAmail);
517     foreach(array("sOAprimary","zoneName","sOAmail") as $attr){
518       if(!preg_match("/\.$/",$ret[$attr])){
519         if(!is_ip($ret[$attr])){
520           $ret[$attr] = $ret[$attr].".";
521         }
522       }
523     }
525     $ret['RECORDS'][] = array("type" => "nSRecord","value" => $ret['sOAprimary']) ;
526     return($ret);
527   }
529   
530   /* This function generate a table row for each used record.
531      This table row displays the recordtype in a select box
532       and the specified value for the record, and a remove button.
533      The last element of the table also got an 'add' button.
534    */
535   function generateRecordsList($changeStateForRecords="")
536   {
537     $changeStateForRecords = "";
539     $str = "<table summary=''>";
540     foreach($this->Records as $key => $entry){
542       if($entry['type'] == "mXRecord") continue;
543       
544       $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
545       $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
546       $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
548       $str.=" <tr>".
549         "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
550         "   <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
551         "   <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
552         "</tr>";
553     }
555     $str.= "  <tr>".
556       "    <td colspan=2></td><td>".
557       "      <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
558       "    </td>".
559       "  </tr>".
560       "</table>";
561     return($str);
562   }
564   /* This function generates a select box out of $this->RecordTypes options.
565      The Parameter $selected is used to predefine an attribute.
566      $name is used to specify a post name
567    */
568   function generateRecordListBox($selected,$name)
569   {
570     $str = "<select name='".$name."' id='".$name."'>";
571     foreach($this->RecordTypes as $type => $value){
573       if(preg_match("/^mXRecord$/i",$value)) continue;
575       $use = "";
576       if($type == $selected){
577         $use = " selected ";
578       }
579       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
580     }
581     $str.="</select>";
582     return($str);
583   }
586 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
587 ?>