Code

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