Code

Updated Post 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;
60       $this->OldZoneName        = $attrs['zoneName'];
61       $this->OldReverseZone     = $attrs['ReverseZone'];
63       $this->InitialzoneName    = $attrs['InitialzoneName'];
64       $this->InitialReverseZone = $attrs['InitialReverseZone'];
66       $this->isNew                  = false;
68       foreach($this->attributes as $value){
69         if(isset($attrs[$value])){
70           $this->$value = $attrs[$value];
71         }
72       }
74       $this->sOAmail            = preg_replace("/\./","@",$this->sOAmail,1);
75       $this->sOAmail            = preg_replace("/\.$/","",$this->sOAmail);
76       $this->sOAprimary         = preg_replace("/\.$/","",$this->sOAprimary);
77       $this->zoneName           = preg_replace("/\.$/","",$this->zoneName);
79       if(isset($attrs['RECORDS'])){
80         $this->Records = $attrs['RECORDS']; 
82         $tmp2 = array();
83         $usedPrio = array();
84         foreach($this->Records as $key => $rec){
85           if($rec['type'] == "mXRecord"){
86             $tmp = split(" ",$rec['value']);
87             $rec['value'] = $tmp[1];
88             $tmp2[$tmp[0]] = $rec;
89             unset($this->Records[$key]);
90           }
91           if($rec['type'] == "nSRecord"){
92             unset($this->Records[$key]);
93           }
94         }
95         if(count($tmp2) != 0){
96           reset($tmp2);
97           ksort($tmp2);
98         }
99         $this->mXRecords = $tmp2;
100       }else{
101         $this->mXRecords  = array();
102         $this->Records    = array();
103       }
105       $str = date("Ymd");
106       if(preg_match("/^".$str."/",$this->sOAserial)){
107         $this->sOAserial = $this->sOAserial + 1;
108       }else{
109         $this->sOAserial = date("Ymd")."01";
110       }
111     }
113     /* Detect Network class */
114     if(!empty($this->ReverseZone)){
116       $dots = count(split(".",$this->ReverseZone));
117       if($dots == 0){
118         $this->NetworkClass = "A";  
119         $this->ReverseZone .= ".0.0.0"; 
120       }elseif($dots == 1){
121         $this->NetworkClass = "B";  
122         $this->ReverseZone .= ".0.0"; 
123       }else{
124         $this->NetworkClass = "C";  
125         $this->ReverseZone .= ".0"; 
126       }
127     }
128   }
130   /* TRansports the geiven Arraykey one position up*/
131   function ArrayUp($atr,$attrs)
132   {
133     $ret = $attrs;
134     $pos = $atr ;
135     $cn = count($attrs);
136     if(!(($pos == -1)||($pos == 1)||($pos >$cn))){
137       $before = array_slice($attrs,0,($pos-2));
138       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
139       $unten  = array_slice($attrs,$pos);
140       $ret = array();
141       $ret = $this->combineArrays($before,$mitte,$unten);
142     }
143     return($ret);
144   }
147   /* TRansports the geiven Arraykey one position up*/
148   function ArrayDown($atr,$attrs)
149   {
150     $ret = $attrs;
151     $pos = $atr ;
152     $cn = count($attrs);
153     if(!(($pos == -1)||($pos == $cn))){
154       $before = array_slice($attrs,0,($pos-1));
155       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
156       $unten  = array_slice($attrs,($pos+1));
157       $ret = array();
158       $ret = $this->combineArrays($before,$mitte,$unten);
159     }
160     return($ret);
161   }
163   /* Combine new array */
164   function combineArrays($ar0,$ar1,$ar2)
165   {
166     $ret = array();
167     if(is_array($ar0))
168     foreach($ar0 as $ar => $a){
169         $ret[]=$a;
170     }
171     if(is_array($ar1))
172     foreach($ar1 as $ar => $a){
173         $ret[]=$a;
174     }
175     if(is_array($ar2))
176     foreach($ar2 as $ar => $a){
177         $ret[]=$a;
178     }
179     return($ret);
180   }
181   
182   function getpos($atr,$attrs)
183   {
184     $i = 0;
185     foreach($attrs as $attr => $name)    {
186       $i++;
187       if($attr == $atr){
188         return($i);
189       }
190     }
191     return(-1);
192   }
195   function execute()
196   {
197     /* Call parent execute */
198     plugin::execute();
201     /* Fill templating stuff */
202     $smarty= get_smarty();
203     $display= "";
205     /* Open Zone Entry Edit Dialog
206      */
207     if(!count($this->ZoneObject)){
208       $smarty->assign("AllowZoneEdit" , false);
209     }else{
210       $smarty->assign("AllowZoneEdit" , true);
211       if(isset($_POST['EditZoneEntries'])){
212         $this->dialog= new servDNSeditZoneEntries($this->config,$this->dn,$this->ZoneObject);
213         $this->dialog->parent = $this;
214       }
215     }
217     /* Save Zone Entry Edit Dialog
218      */
219     if(isset($_POST['SaveZoneEntryChanges'])){
220       $this->dialog->save_object();
221       if(count($this->dialog->check())){
222         $msgs = $this->dialog->check();
223         foreach($msgs as $msg){
224           print_red($msg);
225         }
226       }else{
227         $this->dialog->save();
228         $rev = FlipIp(getNameFromMix($this->InitialReverseZone)).".in-addr.arpa";
229         $for = getNameFromMix($this->InitialzoneName);
230        
231         $this->parent->handle_post_events("modify",array("dn" => $this->dn,"zoneName" => $rev));
232         $this->parent->handle_post_events("modify",array("dn" => $this->dn,"zoneName" => $for));
233         $this->dialog = false;
234       }
235     }
237     /* Cancel Zone Entrie Edit Dialog
238     */
239     if(isset($_POST['CancelZoneEntryChanges'])){
240       $this->dialog = false;
241     }
243     /* Display any type of open dialogs 
244      */
245     if($this->dialog){
246       $this->dialog->save_object();
247       return($this->dialog->execute());
248     }
250     $once =true;
251     foreach($_POST as $name => $value){
252       if((preg_match("/^MXup_/",$name)) && ($once)){
253         $once = false;
255         $id = preg_replace("/^MXup_/","",$name);
256         $id = preg_replace("/_.*$/","",$id);
257         $id = base64_decode($id);
258     
259         $this->mXRecords = $this->ArrayUp(($id+1),$this->mXRecords);
260       }
261       if((preg_match("/^MXdown_/",$name)) && ($once)){
262         $once = false;
263         
264         $id = preg_replace("/^MXdown_/","",$name);
265         $id = preg_replace("/_.*$/","",$id);
266         $id = base64_decode($id);
267   
268         $this->mXRecords = $this->ArrayDown(($id+1),$this->mXRecords);
269       }
270       if((preg_match("/^MXdel_/",$name)) && ($once)){
271         $once = false;
272         
273         $id = preg_replace("/^MXdel_/","",$name);
274         $id = preg_replace("/_.*$/","",$id);
275         $id = base64_decode($id);
276         
277         unset($this->mXRecords[$id]);
279         $tmp  =array();
280         foreach($this->mXRecords as $entry){
281           $tmp[] = $entry;
282         }
283  
284         $this->mXRecords = $tmp; 
285       }
286     }
288     if((isset($_POST['AddMXRecord'])) && (!empty($_POST['StrMXRecord']))){
289       $this->mXRecords[] = array("type"=>"mXRecord","value"=>trim($_POST['StrMXRecord']));      
290     }
292     /* Handle Post events */
293     $once = true;
294     foreach($_POST as $name => $value){
296       /* Delete record if requested */
297       if((preg_match("/RemoveRecord_/",$name))&&($once)){
298         $once = false;
299         $id= preg_replace("/RemoveRecord_/","",$name);
300         unset($this->Records[$id]);
301       }
302     }
304     /* Add new Zonerecord */
305     if(isset($_POST['AddNewRecord'])){
306       $this->Records[] = array("type"=>"aRecord","value"=>"");
307     }
309     /* Fill in values */
310     foreach($this->attributes as $name){
311       $smarty->assign($name,$this->$name);
312     }
314     /* Set zoneNames without server suffix */
315     foreach(array("zoneName","ReverseZone") as $attr){
316       $smarty->assign($attr,getNameFromMix($this->$attr));
317     }
319     $div = new DivSelectBox("MxRecords");
320     $div->setHeight(120);
321     $recs = $this->mXRecords;
323     $oneup    = "<input name='MXup_%s'    type='image' src='images/sort_up.png'    title='"._("Up")."'      class='center'>&nbsp;"; 
324     $onedown  = "<input name='MXdown_%s'  type='image' src='images/sort_down.png'  title='"._("Down")."'    class='center'>&nbsp;"; 
325     $onedel   = "<img src='images/empty.png' width='20' class='center'>
326                  <input name='MXdel_%s'   type='image' src='images/edittrash.png'  title='"._("Delete")."'  class='center'>"; 
328     foreach($recs as $key => $rec){
329       $div ->AddEntry(array(
330             array("string"=>$rec['value']),
331 /*            array("string"=>$key,
332                   "attach"=>"style='width:20px;'"),*/
333             array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
334                   "attach"=>"style='width:70px;border-right:0px;'")
335             ));
336     }
338     /* Assign records list */
339     $smarty->assign("NotNew", false);
340     $smarty->assign("Mxrecords",  $div->DrawList());
341     $smarty->assign("records"  ,  $this->generateRecordsList());
342     $smarty->assign("NetworkClass",  $this->NetworkClass);
343     $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)"));
345     /* Display tempalte */
346     $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
347     return($display);
348   }
350   function remove_from_parent()
351   {
352   }
354   /* Save data to object */
355   function save_object()
356   {
357     //plugin::save_object();
358     foreach($this->attributes as $attr){
359       if(isset($_POST[$attr])){
360         $this->$attr = $_POST[$attr];
361       }
362     }
364     if(isset($_POST['NetworkClass'])){
365       $this->NetworkClass = $_POST['NetworkClass'];
366     }
368     foreach(array("zoneName","ReverseZone") as $attr){
369       if(isset($_POST[$attr])){
370         $this->$attr = strtoupper($this->cn)."/".$_POST[$attr];
371       }
372     }
374     foreach($this->Records as $id => $value){  
375       if(isset($_POST['RecordTypeSelectedFor_'.$id])){
376         $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
377       }
378       if(isset($_POST['RecordValue_'.$id])){
379         $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
380       }
381     }
382   }
385   /* Check supplied data */
386   function check()
387   {
388     /* Call common method to give check the hook */
389     $message= plugin::check();
390         
391     /* Check if zoneName is already in use */
392     $usedZones = $this->getUsedZoneNames();
393     if(($this->isNew == true)||($this->zoneName  != $this->InitialzoneName)||($this->ReverseZone != $this->InitialReverseZone)){
394 /*      if((isset($usedZones[$this->zoneName]))&&($this->zoneName  != $this->InitialzoneName)){
395         $message[] =_("This zoneName is already in use");
396       }
397       if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitialReverseZone)){
398         $message[] =_("This reverse zone is already in use");
399       }*/
400     }
402     if(empty($this->zoneName)){
403       $message[] =sprintf(_("Please choose a valid zone name."));
404     }
406     if(empty($this->ReverseZone)){
407       $message[] =sprintf(_("Please choose a valid reverse zone name."));
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);
443   
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 way 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 way 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 way 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     $ret['sOAmail']            = preg_replace("/\@/",".",$this->sOAmail);
525     foreach(array("sOAprimary","zoneName","sOAmail") as $attr){
526       if(!preg_match("/\.$/",$ret[$attr])){
527         if(!is_ip($ret[$attr])){
528           $ret[$attr] = $ret[$attr].".";
529         }
530       }
531     }
533     $ret['RECORDS'][] = array("type" => "nSRecord","value" => $ret['sOAprimary']) ;
534     return($ret);
535   }
537   
538   /* This function generate a table row for each used record.
539      This table row displays the recordtype in a select box
540       and the specified value for the record, and a remove button.
541      The last element of the table also got an 'add' button.
542    */
543   function generateRecordsList($changeStateForRecords="")
544   {
545     $changeStateForRecords = "";
547     $str = "<table summary=''>";
548     foreach($this->Records as $key => $entry){
550       if($entry['type'] == "mXRecord") continue;
551       
552       $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
553       $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
554       $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
556       $str.=" <tr>".
557         "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
558         "   <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
559         "   <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
560         "</tr>";
561     }
563     $str.= "  <tr>".
564       "    <td colspan=2></td><td>".
565       "      <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
566       "    </td>".
567       "  </tr>".
568       "</table>";
569     return($str);
570   }
572   /* This function generates a select box out of $this->RecordTypes options.
573      The Parameter $selected is used to predefine an attribute.
574      $name is used to specify a post name
575    */
576   function generateRecordListBox($selected,$name)
577   {
578     $str = "<select name='".$name."' id='".$name."'>";
579     foreach($this->RecordTypes as $type => $value){
581       if(preg_match("/^mXRecord$/i",$value)) continue;
583       $use = "";
584       if($type == $selected){
585         $use = " selected ";
586       }
587       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
588     }
589     $str.="</select>";
590     return($str);
591   }
594 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
595 ?>