Code

Added class selector
[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)){
108       $addr = preg_replace("/^[^\/]+\//","",$this->ReverseZone);
109       if(preg_match("/^[0-9]*\.0\.0\.0$/",$addr)){
110         $this->NetworkClass = "A";
111       }
112       if(preg_match("/^[0-9]*\.[0-9]*\.0\.0$/",$addr)){
113         $this->NetworkClass = "B";
114       }
115       if(preg_match("/^[0-9]*\.[0-9]*\.[0-9]*\.0$/",$addr)){
116         $this->NetworkClass = "C";
117       }
118     }
119   }
121   /* TRansports the geiven Arraykey one position up*/
122   function ArrayUp($atr,$attrs)
123   {
124     $ret = $attrs;
125     $pos = $atr ;
126     $cn = count($attrs);
127     if(!(($pos == -1)||($pos == 1)||($pos >$cn))){
128       $before = array_slice($attrs,0,($pos-2));
129       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
130       $unten  = array_slice($attrs,$pos);
131       $ret = array();
132       $ret = $this->combineArrays($before,$mitte,$unten);
133     }
134     return($ret);
135   }
138   /* TRansports the geiven Arraykey one position up*/
139   function ArrayDown($atr,$attrs)
140   {
141     $ret = $attrs;
142     $pos = $atr ;
143     $cn = count($attrs);
144     if(!(($pos == -1)||($pos == $cn))){
145       $before = array_slice($attrs,0,($pos-1));
146       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
147       $unten  = array_slice($attrs,($pos+1));
148       $ret = array();
149       $ret = $this->combineArrays($before,$mitte,$unten);
150     }
151     return($ret);
152   }
154   /* Combine new array */
155   function combineArrays($ar0,$ar1,$ar2)
156   {
157     $ret = array();
158     if(is_array($ar0))
159     foreach($ar0 as $ar => $a){
160         $ret[]=$a;
161     }
162     if(is_array($ar1))
163     foreach($ar1 as $ar => $a){
164         $ret[]=$a;
165     }
166     if(is_array($ar2))
167     foreach($ar2 as $ar => $a){
168         $ret[]=$a;
169     }
170     return($ret);
171   }
172   
173   function getpos($atr,$attrs)
174   {
175     $i = 0;
176     foreach($attrs as $attr => $name)    {
177       $i++;
178       if($attr == $atr){
179         return($i);
180       }
181     }
182     return(-1);
183   }
186   function execute()
187   {
188     /* Call parent execute */
189     plugin::execute();
192     /* Fill templating stuff */
193     $smarty= get_smarty();
194     $display= "";
196     /* Open Zone Entry Edit Dialog
197      */
198     if(!count($this->ZoneObject)){
199       $smarty->assign("AllowZoneEdit" , false);
200     }else{
201       $smarty->assign("AllowZoneEdit" , true);
202       if(isset($_POST['EditZoneEntries'])){
203         $this->dialog= new servDNSeditZoneEntries($this->config,$this->dn,$this->ZoneObject);
204       }
205     }
207     /* Save Zone Entry Edit Dialog
208      */
209     if(isset($_POST['SaveZoneEntryChanges'])){
210       $this->dialog->save_object();
211       if(count($this->dialog->check())){
212         $msgs = $this->dialog->check();
213         foreach($msgs as $msg){
214           print_red($msg);
215         }
216       }else{
217         $this->dialog->save();
218         $this->dialog = false;
219       }
220     }
222     /* Cancel Zone Entrie Edit Dialog
223     */
224     if(isset($_POST['CancelZoneEntryChanges'])){
225       $this->dialog = false;
226     }
228     /* Display any type of open dialogs 
229      */
230     if($this->dialog){
231       $this->dialog->save_object();
232       return($this->dialog->execute());
233     }
235     $once =true;
236     foreach($_POST as $name => $value){
237       if((preg_match("/^MXup_/",$name)) && ($once)){
238         $once = false;
240         $id = preg_replace("/^MXup_/","",$name);
241         $id = preg_replace("/_.*$/","",$id);
242         $id = base64_decode($id);
243     
244         $this->mXRecords = $this->ArrayUp(($id+1),$this->mXRecords);
245       }
246       if((preg_match("/^MXdown_/",$name)) && ($once)){
247         $once = false;
248         
249         $id = preg_replace("/^MXdown_/","",$name);
250         $id = preg_replace("/_.*$/","",$id);
251         $id = base64_decode($id);
252   
253         $this->mXRecords = $this->ArrayDown(($id+1),$this->mXRecords);
254       }
255       if((preg_match("/^MXdel_/",$name)) && ($once)){
256         $once = false;
257         
258         $id = preg_replace("/^MXdel_/","",$name);
259         $id = preg_replace("/_.*$/","",$id);
260         $id = base64_decode($id);
261         
262         unset($this->mXRecords[$id]);
264         $tmp  =array();
265         foreach($this->mXRecords as $entry){
266           $tmp[] = $entry;
267         }
268  
269         $this->mXRecords = $tmp; 
270       }
271     }
273     if((isset($_POST['AddMXRecord'])) && (!empty($_POST['StrMXRecord']))){
274       $this->mXRecords[] = array("type"=>"mXRecord","value"=>trim($_POST['StrMXRecord']));      
275     }
277     /* Handle Post events */
278     $once = true;
279     foreach($_POST as $name => $value){
281       /* Delete record if requested */
282       if((preg_match("/RemoveRecord_/",$name))&&($once)){
283         $once = false;
284         $id= preg_replace("/RemoveRecord_/","",$name);
285         unset($this->Records[$id]);
286       }
287     }
289     /* Add new Zonerecord */
290     if(isset($_POST['AddNewRecord'])){
291       $this->Records[] = array("type"=>"aRecord","value"=>"");
292     }
294     /* Fill in values */
295     foreach($this->attributes as $name){
296       $smarty->assign($name,$this->$name);
297     }
300     $div = new DivSelectBox("MxRecords");
301     $div->setHeight(120);
302     $recs = $this->mXRecords;
304     $oneup    = "<input name='MXup_%s'    type='image' src='images/sort_up.png'    title='"._("Up")."'      class='center'>&nbsp;"; 
305     $onedown  = "<input name='MXdown_%s'  type='image' src='images/sort_down.png'  title='"._("Down")."'    class='center'>&nbsp;"; 
306     $onedel   = "<img src='images/empty.png' width='20' class='center'>
307                  <input name='MXdel_%s'   type='image' src='images/edittrash.png'  title='"._("Delete")."'  class='center'>"; 
309     foreach($recs as $key => $rec){
310       $div ->AddEntry(array(
311             array("string"=>$rec['value']),
312 /*            array("string"=>$key,
313                   "attach"=>"style='width:20px;'"),*/
314             array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
315                   "attach"=>"style='width:70px;border-right:0px;'")
316             ));
317     }
319     /* Assign records list */
320     $smarty->assign("NotNew", false);
321     $smarty->assign("Mxrecords",  $div->DrawList());
322     $smarty->assign("records"  ,  $this->generateRecordsList());
323     $smarty->assign("NetworkClass",  $this->NetworkClass);
324     $smarty->assign("NetworkClasses",  array("A"=>"A","B"=>"B","C"=>"C"));
326     /* Display tempalte */
327     $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
328     return($display);
329   }
331   function remove_from_parent()
332   {
333   }
335   /* Save data to object */
336   function save_object()
337   {
338     //plugin::save_object();
339     foreach($this->attributes as $attr){
340       if(isset($_POST[$attr])){
341         $this->$attr = $_POST[$attr];
342       }
343     }
345     foreach($this->Records as $id => $value){  
346       if(isset($_POST['RecordTypeSelectedFor_'.$id])){
347         $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
348       }
349       if(isset($_POST['RecordValue_'.$id])){
350         $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
351       }
352     }
354      if(isset($_POST['NetworkClass'])){
355        $this->NetworkClass = $_POST['NetworkClass'];
356      }
358   }
361   /* Check supplied data */
362   function check()
363   {
364     /* Call common method to give check the hook */
365     $message= plugin::check();
366         
367     /* Check if zoneName is already in use */
368     $usedZones = $this->getUsedZoneNames();
369     if(($this->isNew == true)||($this->zoneName  != $this->InitialzoneName)||($this->ReverseZone != $this->InitialReverseZone)){
370     /*  if((isset($usedZones[$this->zoneName]))&&($this->zoneName  != $this->InitialzoneName)){
371         $message[] =_("This zoneName is already in use");
372       }
373       if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitialReverseZone)){
374         $message[] =_("This reverse zone is already in use");
375       }*/
376     }
378     if(empty($this->zoneName)){
379       $message[] =sprintf(_("Please choose a valid zone name."));
380     }
382     if(empty($this->ReverseZone)){
383       $message[] =sprintf(_("Please choose a valid reverse zone name."));
384     }
386     if(!preg_match("/\.$/",$this->sOAprimary)){
387       $message[] = _("Primary dns server must end with '.' to be a valid entry.");
388     }
390     if(!preg_match("/\.$/",$this->sOAmail)){
391       $message[] = _("Your specified mail address must end with '.' to be a valid record.");
392     }
394     if(preg_match("/@/",$this->sOAmail)){
395       $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
396     }
398     if(preg_match("/@/",$this->sOAmail)){
399       $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
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);
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 was 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 was 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 was 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     $ret['RECORDS'] = $this->Records; 
504     $ret['InitialReverseZone']=  $this->InitialReverseZone;
505     $ret['InitialzoneName']   =  $this->InitialzoneName;
507     return($ret);
508   }
510   
511   /* This function generate a table row for each used record.
512      This table row displays the recordtype in a select box
513       and the specified value for the record, and a remove button.
514      The last element of the table also got an 'add' button.
515    */
516   function generateRecordsList($changeStateForRecords="")
517   {
518     $changeStateForRecords = "";
520     $str = "<table summary=''>";
521     foreach($this->Records as $key => $entry){
523       if($entry['type'] == "mXRecord") continue;
524       
525       $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
526       $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
527       $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
529       $str.=" <tr>".
530         "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
531         "   <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
532         "   <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
533         "</tr>";
534     }
536     $str.= "  <tr>".
537       "    <td colspan=2></td><td>".
538       "      <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
539       "    </td>".
540       "  </tr>".
541       "</table>";
542     return($str);
543   }
545   /* This function generates a select box out of $this->RecordTypes options.
546      The Parameter $selected is used to predefine an attribute.
547      $name is used to specify a post name
548    */
549   function generateRecordListBox($selected,$name)
550   {
551     $str = "<select name='".$name."' id='".$name."'>";
552     foreach($this->RecordTypes as $type => $value){
554       if(preg_match("/^mXRecord$/i",$value)) continue;
556       $use = "";
557       if($type == $selected){
558         $use = " selected ";
559       }
560       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
561     }
562     $str.="</select>";
563     return($str);
564   }
567 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
568 ?>