Code

Updated dns handling
[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;
64       $this->OldZoneName        = $attrs['zoneName'];
65       $this->OldReverseZone     = $attrs['ReverseZone'];
67       $this->InitialzoneName    = $attrs['InitialzoneName'];
68       $this->InitialReverseZone = $attrs['InitialReverseZone'];
70       $this->isNew                  = false;
72       foreach($this->attributes as $value){
73         $this->$value = $attrs[$value];
74       }
75       if(isset($attrs['RECORDS'])){
76         $this->Records = $attrs['RECORDS']; 
78         $tmp2 = array();
79         $usedPrio = array();
80         foreach($this->Records as $key => $rec){
81           if($rec['type'] == "mXRecord"){
82             $tmp = split(" ",$rec['value']);
83             $rec['value'] = $tmp[1];
84             $tmp2[$tmp[0]] = $rec;
85             unset($this->Records[$key]);
86           }
87         }
88         if(count($tmp2) != 0){
89           reset($tmp2);
90           ksort($tmp2);
91         }
92         $this->mXRecords = $tmp2;
93       }else{
94         $this->mXRecords  = array();
95         $this->Records    = array();
96       }
98       $str = date("Ymd");
99       if(preg_match("/^".$str."/",$this->sOAserial)){
100         $this->sOAserial = $this->sOAserial + 1;
101       }else{
102         $this->sOAserial = date("Ymd")."01";
103       }
104     }
106     /* Detect Network class */
107     if(!empty($this->ReverseZone)){
109       $dots = count(split(".",$this->ReverseZone));
110       if($dots == 0){
111         $this->NetworkClass = "A";  
112         $this->ReverseZone .= ".0.0.0"; 
113       }elseif($dots == 1){
114         $this->NetworkClass = "B";  
115         $this->ReverseZone .= ".0.0"; 
116       }else{
117         $this->NetworkClass = "C";  
118         $this->ReverseZone .= ".0"; 
119       }
120     }
121   }
123   /* TRansports the geiven Arraykey one position up*/
124   function ArrayUp($atr,$attrs)
125   {
126     $ret = $attrs;
127     $pos = $atr ;
128     $cn = count($attrs);
129     if(!(($pos == -1)||($pos == 1)||($pos >$cn))){
130       $before = array_slice($attrs,0,($pos-2));
131       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
132       $unten  = array_slice($attrs,$pos);
133       $ret = array();
134       $ret = $this->combineArrays($before,$mitte,$unten);
135     }
136     return($ret);
137   }
140   /* TRansports the geiven Arraykey one position up*/
141   function ArrayDown($atr,$attrs)
142   {
143     $ret = $attrs;
144     $pos = $atr ;
145     $cn = count($attrs);
146     if(!(($pos == -1)||($pos == $cn))){
147       $before = array_slice($attrs,0,($pos-1));
148       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
149       $unten  = array_slice($attrs,($pos+1));
150       $ret = array();
151       $ret = $this->combineArrays($before,$mitte,$unten);
152     }
153     return($ret);
154   }
156   /* Combine new array */
157   function combineArrays($ar0,$ar1,$ar2)
158   {
159     $ret = array();
160     if(is_array($ar0))
161     foreach($ar0 as $ar => $a){
162         $ret[]=$a;
163     }
164     if(is_array($ar1))
165     foreach($ar1 as $ar => $a){
166         $ret[]=$a;
167     }
168     if(is_array($ar2))
169     foreach($ar2 as $ar => $a){
170         $ret[]=$a;
171     }
172     return($ret);
173   }
174   
175   function getpos($atr,$attrs)
176   {
177     $i = 0;
178     foreach($attrs as $attr => $name)    {
179       $i++;
180       if($attr == $atr){
181         return($i);
182       }
183     }
184     return(-1);
185   }
188   function execute()
189   {
190     /* Call parent execute */
191     plugin::execute();
194     /* Fill templating stuff */
195     $smarty= get_smarty();
196     $display= "";
198     /* Open Zone Entry Edit Dialog
199      */
200     if(!count($this->ZoneObject)){
201       $smarty->assign("AllowZoneEdit" , false);
202     }else{
203       $smarty->assign("AllowZoneEdit" , true);
204       if(isset($_POST['EditZoneEntries'])){
205         $this->dialog= new servDNSeditZoneEntries($this->config,$this->dn,$this->ZoneObject);
206       }
207     }
209     /* Save Zone Entry Edit Dialog
210      */
211     if(isset($_POST['SaveZoneEntryChanges'])){
212       $this->dialog->save_object();
213       if(count($this->dialog->check())){
214         $msgs = $this->dialog->check();
215         foreach($msgs as $msg){
216           print_red($msg);
217         }
218       }else{
219         $this->dialog->save();
220         $this->dialog = false;
221       }
222     }
224     /* Cancel Zone Entrie Edit Dialog
225     */
226     if(isset($_POST['CancelZoneEntryChanges'])){
227       $this->dialog = false;
228     }
230     /* Display any type of open dialogs 
231      */
232     if($this->dialog){
233       $this->dialog->save_object();
234       return($this->dialog->execute());
235     }
237     $once =true;
238     foreach($_POST as $name => $value){
239       if((preg_match("/^MXup_/",$name)) && ($once)){
240         $once = false;
242         $id = preg_replace("/^MXup_/","",$name);
243         $id = preg_replace("/_.*$/","",$id);
244         $id = base64_decode($id);
245     
246         $this->mXRecords = $this->ArrayUp(($id+1),$this->mXRecords);
247       }
248       if((preg_match("/^MXdown_/",$name)) && ($once)){
249         $once = false;
250         
251         $id = preg_replace("/^MXdown_/","",$name);
252         $id = preg_replace("/_.*$/","",$id);
253         $id = base64_decode($id);
254   
255         $this->mXRecords = $this->ArrayDown(($id+1),$this->mXRecords);
256       }
257       if((preg_match("/^MXdel_/",$name)) && ($once)){
258         $once = false;
259         
260         $id = preg_replace("/^MXdel_/","",$name);
261         $id = preg_replace("/_.*$/","",$id);
262         $id = base64_decode($id);
263         
264         unset($this->mXRecords[$id]);
266         $tmp  =array();
267         foreach($this->mXRecords as $entry){
268           $tmp[] = $entry;
269         }
270  
271         $this->mXRecords = $tmp; 
272       }
273     }
275     if((isset($_POST['AddMXRecord'])) && (!empty($_POST['StrMXRecord']))){
276       $this->mXRecords[] = array("type"=>"mXRecord","value"=>trim($_POST['StrMXRecord']));      
277     }
279     /* Handle Post events */
280     $once = true;
281     foreach($_POST as $name => $value){
283       /* Delete record if requested */
284       if((preg_match("/RemoveRecord_/",$name))&&($once)){
285         $once = false;
286         $id= preg_replace("/RemoveRecord_/","",$name);
287         unset($this->Records[$id]);
288       }
289     }
291     /* Add new Zonerecord */
292     if(isset($_POST['AddNewRecord'])){
293       $this->Records[] = array("type"=>"aRecord","value"=>"");
294     }
296     /* Fill in values */
297     foreach($this->attributes as $name){
298       $smarty->assign($name,$this->$name);
299     }
302     $div = new DivSelectBox("MxRecords");
303     $div->setHeight(120);
304     $recs = $this->mXRecords;
306     $oneup    = "<input name='MXup_%s'    type='image' src='images/sort_up.png'    title='"._("Up")."'      class='center'>&nbsp;"; 
307     $onedown  = "<input name='MXdown_%s'  type='image' src='images/sort_down.png'  title='"._("Down")."'    class='center'>&nbsp;"; 
308     $onedel   = "<img src='images/empty.png' width='20' class='center'>
309                  <input name='MXdel_%s'   type='image' src='images/edittrash.png'  title='"._("Delete")."'  class='center'>"; 
311     foreach($recs as $key => $rec){
312       $div ->AddEntry(array(
313             array("string"=>$rec['value']),
314 /*            array("string"=>$key,
315                   "attach"=>"style='width:20px;'"),*/
316             array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
317                   "attach"=>"style='width:70px;border-right:0px;'")
318             ));
319     }
321     /* Assign records list */
322     $smarty->assign("NotNew", false);
323     $smarty->assign("Mxrecords",  $div->DrawList());
324     $smarty->assign("records"  ,  $this->generateRecordsList());
325     $smarty->assign("NetworkClass",  $this->NetworkClass);
326     $smarty->assign("NetworkClasses",  array("A"=>"A","B"=>"B","C"=>"C"));
328     /* Display tempalte */
329     $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
330     return($display);
331   }
333   function remove_from_parent()
334   {
335   }
337   /* Save data to object */
338   function save_object()
339   {
340     //plugin::save_object();
341     foreach($this->attributes as $attr){
342       if(isset($_POST[$attr])){
343         $this->$attr = $_POST[$attr];
344       }
345     }
347     foreach($this->Records as $id => $value){  
348       if(isset($_POST['RecordTypeSelectedFor_'.$id])){
349         $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
350       }
351       if(isset($_POST['RecordValue_'.$id])){
352         $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
353       }
354     }
356      if(isset($_POST['NetworkClass'])){
357        $this->NetworkClass = $_POST['NetworkClass'];
358      }
360   }
363   /* Check supplied data */
364   function check()
365   {
366     /* Call common method to give check the hook */
367     $message= plugin::check();
368         
369     /* Check if zoneName is already in use */
370     $usedZones = $this->getUsedZoneNames();
371     if(($this->isNew == true)||($this->zoneName  != $this->InitialzoneName)||($this->ReverseZone != $this->InitialReverseZone)){
372     /*  if((isset($usedZones[$this->zoneName]))&&($this->zoneName  != $this->InitialzoneName)){
373         $message[] =_("This zoneName is already in use");
374       }
375       if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitialReverseZone)){
376         $message[] =_("This reverse zone is already in use");
377       }*/
378     }
380     if(empty($this->zoneName)){
381       $message[] =sprintf(_("Please choose a valid zone name."));
382     }
384     if(empty($this->ReverseZone)){
385       $message[] =sprintf(_("Please choose a valid reverse zone name."));
386     }
388     if(!preg_match("/\.$/",$this->sOAprimary)){
389       $message[] = _("Primary dns server must end with '.' to be a valid entry.");
390     }
392     if(!preg_match("/\.$/",$this->sOAmail)){
393       $message[] = _("Your specified mail address must end with '.' to be a valid record.");
394     }
396     if(preg_match("/@/",$this->sOAmail)){
397       $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
398     }
400     if(preg_match("/@/",$this->sOAmail)){
401       $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
402     }
404     if($this->zoneName != strtolower($this->zoneName)){
405       $message[] = _("Only lowercase strings are allowed as zone name.");
406     }
408     if(!is_numeric($this->sOAserial)){
409       $message[] = _("Please specify a numeric value for serial number.");
410     }
412     if(!is_numeric($this->sOArefresh)){
413       $message[] = _("Please specify a numeric value for refresh.");
414     }
416     if(!is_numeric($this->sOAttl)){
417       $message[] = _("Please specify a numeric value for ttl.");
418     }
420     if(!is_numeric($this->sOAexpire)){
421       $message[] = _("Please specify a numeric value for expire.");
422     }
424     if(!is_numeric($this->sOAretry)){
425       $message[] = _("Please specify a numeric value for retry.");
426     }
428     foreach($this->Records as $name => $values){
429       /* only lower-case is allowed in record entries ... */
430       if($values['value'] != strtolower($values['value'])){
431         $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
432       }
433     }
435     /* Check class for given Zone Address */
436     $addr = preg_replace("/^[^\/]+\//","",$this->ReverseZone);
438     /* Check for valid&complete IP address */
439     if(!is_ip($addr)){
440       $message[] = _("The given network address is not a valid, please specify a valid IP address.");
441     }
442   
443     /* Check if given address matches selected network class */
444     switch($this->NetworkClass){
445       case 'A': { 
446                   if(!preg_match("/^[0-9]*\.0\.0\.0$/",$addr)){
447                     $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this was x.0.0.0"));
448                   }
449                 }
450                 break;
451       case 'B': {
452                   if(!preg_match("/^[0-9]*\.[0-9]*\.0\.0$/",$addr)){
453                     $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this was x.x.0.0"));
454                   }
455                 }
456                 break;
457       case 'C': {
458                   if(!preg_match("/^[0-9]*\.[0-9]*\.[0-9]*\.0$/",$addr)){
459                     $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this was x.x.x.0"));
460                   }
461                 }
462                 break;
463       default : $message[] =sprintf(_("The given network class '%s' is not valid."),$this->NetworkClass);
464     }
466     return ($message);
467   }
469   /* This funtion returns all used Zonenames */
470   function getUsedZoneNames()
471   {
472     $ret = array();
473     $ldap = $this->config->get_ldap_link();
474     $ldap->cd($this->config->current['BASE']);
475     $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
476     while($attr = $ldap->fetch()){
477       if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
478         if(isset($attr['tXTRecord'][0])){
479           $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
480           $ret[$zn] =FlipIp(preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]));
481         }
482       }else{
483         $ret[$attr['zoneName'][0]]="";
484       }
485     }
486     return($ret);
487   }
489   /* Save to LDAP */
490   function save()
491   {
492     $ret =array();
493     foreach($this->attributes as $name){
494       $ret[$name] = $this->$name;
495     }
497     /* Create mx records 
498      */
499     foreach($this->mXRecords as $key => $rec){
500       $rec['value']= $key." ".$rec['value'];
501       $this->Records [] = $rec;
502     }
504   
505     $ret['RECORDS'] = $this->Records; 
506  
507     switch($this->NetworkClass){
508       case 'C' : $ret['ReverseZone']= preg_replace("/\.[0-9]*$/","",$this->ReverseZone);break;
509       case 'B' : $ret['ReverseZone']= preg_replace("/\.[0-9]*\.[0-9]*$/","",$this->ReverseZone);break;
510       case 'A' : $ret['ReverseZone']= preg_replace("/\.[0-9]*\.[0-9]*\.[0-9]*$/","",$this->ReverseZone);break;
511       default : trigger_error("Invalid network class given '".$this->NetworkClass."'");
512     }
514     $ret['InitialReverseZone']=  $this->InitialReverseZone;
515     $ret['InitialzoneName']   =  $this->InitialzoneName;
517     return($ret);
518   }
520   
521   /* This function generate a table row for each used record.
522      This table row displays the recordtype in a select box
523       and the specified value for the record, and a remove button.
524      The last element of the table also got an 'add' button.
525    */
526   function generateRecordsList($changeStateForRecords="")
527   {
528     $changeStateForRecords = "";
530     $str = "<table summary=''>";
531     foreach($this->Records as $key => $entry){
533       if($entry['type'] == "mXRecord") continue;
534       
535       $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
536       $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
537       $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
539       $str.=" <tr>".
540         "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
541         "   <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
542         "   <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
543         "</tr>";
544     }
546     $str.= "  <tr>".
547       "    <td colspan=2></td><td>".
548       "      <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
549       "    </td>".
550       "  </tr>".
551       "</table>";
552     return($str);
553   }
555   /* This function generates a select box out of $this->RecordTypes options.
556      The Parameter $selected is used to predefine an attribute.
557      $name is used to specify a post name
558    */
559   function generateRecordListBox($selected,$name)
560   {
561     $str = "<select name='".$name."' id='".$name."'>";
562     foreach($this->RecordTypes as $type => $value){
564       if(preg_match("/^mXRecord$/i",$value)) continue;
566       $use = "";
567       if($type == $selected){
568         $use = " selected ";
569       }
570       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
571     }
572     $str.="</select>";
573     return($str);
574   }
577 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
578 ?>