Code

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