Code

e8bb14fe08d6408c7703ea6bb461f9f5aedacce0
[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          = "";
34   var $dialog                   = false;
36   var $isNew                    = true;
37   var $cn;
38   var $ZoneObject               = array();
40   function servdnseditZone ($config, $dn= NULL,$attrs = array())
41   {
42     plugin::plugin ($config, $dn);
44     /* All types with required attrs */
45     $this->RecordTypes = getDnsRecordTypes(true); 
47     if(!count($attrs)){
48       $this->OldZoneName        = "";
49       $this->OldReverseZone     = "";
50       $this->isNew              = true;
51       $this->sOAserial          = date("Ymd")."1";
52       
53       $this->InitialzoneName    = "";//$attrs['InitialzoneName'];
54       $this->InitialReverseZone = "";//$attrs['InitialReverseZone'];
55     }else{
56       $this->ZoneObject         = $attrs;
58       $this->OldZoneName        = $attrs['zoneName'];
59       $this->OldReverseZone     = $attrs['ReverseZone'];
61       $this->InitialzoneName    = $attrs['InitialzoneName'];
62       $this->InitialReverseZone = $attrs['InitialReverseZone'];
64       $this->isNew                  = false;
66       foreach($this->attributes as $value){
67         if(isset($attrs[$value])){
68           $this->$value = $attrs[$value];
69         }
70       }
71       if(isset($attrs['RECORDS'])){
72         $this->Records = $attrs['RECORDS']; 
74         $tmp2 = array();
75         $usedPrio = array();
76         foreach($this->Records as $key => $rec){
77           if($rec['type'] == "mXRecord"){
78             $tmp = split(" ",$rec['value']);
79             $rec['value'] = $tmp[1];
80             $tmp2[$tmp[0]] = $rec;
81             unset($this->Records[$key]);
82           }
83         }
84         if(count($tmp2) != 0){
85           reset($tmp2);
86           ksort($tmp2);
87         }
88         $this->mXRecords = $tmp2;
89       }else{
90         $this->mXRecords  = array();
91         $this->Records    = array();
92       }
94       $str = date("Ymd");
95       if(preg_match("/^".$str."/",$this->sOAserial)){
96         $this->sOAserial = $this->sOAserial + 1;
97       }else{
98         $this->sOAserial = date("Ymd")."01";
99       }
100     }
101   }
103   /* TRansports the geiven Arraykey one position up*/
104   function ArrayUp($atr,$attrs)
105   {
106     $ret = $attrs;
107     $pos = $atr ;
108     $cn = count($attrs);
109     if(!(($pos == -1)||($pos == 1)||($pos >$cn))){
110       $before = array_slice($attrs,0,($pos-2));
111       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
112       $unten  = array_slice($attrs,$pos);
113       $ret = array();
114       $ret = $this->combineArrays($before,$mitte,$unten);
115     }
116     return($ret);
117   }
120   /* TRansports the geiven Arraykey one position up*/
121   function ArrayDown($atr,$attrs)
122   {
123     $ret = $attrs;
124     $pos = $atr ;
125     $cn = count($attrs);
126     if(!(($pos == -1)||($pos == $cn))){
127       $before = array_slice($attrs,0,($pos-1));
128       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
129       $unten  = array_slice($attrs,($pos+1));
130       $ret = array();
131       $ret = $this->combineArrays($before,$mitte,$unten);
132     }
133     return($ret);
134   }
136   /* Combine new array */
137   function combineArrays($ar0,$ar1,$ar2)
138   {
139     $ret = array();
140     if(is_array($ar0))
141     foreach($ar0 as $ar => $a){
142         $ret[]=$a;
143     }
144     if(is_array($ar1))
145     foreach($ar1 as $ar => $a){
146         $ret[]=$a;
147     }
148     if(is_array($ar2))
149     foreach($ar2 as $ar => $a){
150         $ret[]=$a;
151     }
152     return($ret);
153   }
154   
155   function getpos($atr,$attrs)
156   {
157     $i = 0;
158     foreach($attrs as $attr => $name)    {
159       $i++;
160       if($attr == $atr){
161         return($i);
162       }
163     }
164     return(-1);
165   }
168   function execute()
169   {
170     /* Call parent execute */
171     plugin::execute();
174     /* Fill templating stuff */
175     $smarty= get_smarty();
176     $display= "";
178     /* Open Zone Entry Edit Dialog
179      */
180     if(!count($this->ZoneObject)){
181       $smarty->assign("AllowZoneEdit" , false);
182     }else{
183       $smarty->assign("AllowZoneEdit" , true);
184       if(isset($_POST['EditZoneEntries'])){
185         $this->dialog= new servDNSeditZoneEntries($this->config,$this->dn,$this->ZoneObject);
186       }
187     }
189     /* Save Zone Entry Edit Dialog
190      */
191     if(isset($_POST['SaveZoneEntryChanges'])){
192       $this->dialog->save_object();
193       if(count($this->dialog->check())){
194         $msgs = $this->dialog->check();
195         foreach($msgs as $msg){
196           print_red($msg);
197         }
198       }else{
199         $this->dialog->save();
200         $this->dialog = false;
201       }
202     }
204     /* Cancel Zone Entrie Edit Dialog
205     */
206     if(isset($_POST['CancelZoneEntryChanges'])){
207       $this->dialog = false;
208     }
210     /* Display any type of open dialogs 
211      */
212     if($this->dialog){
213       $this->dialog->save_object();
214       return($this->dialog->execute());
215     }
217     $once =true;
218     foreach($_POST as $name => $value){
219       if((preg_match("/^MXup_/",$name)) && ($once)){
220         $once = false;
222         $id = preg_replace("/^MXup_/","",$name);
223         $id = preg_replace("/_.*$/","",$id);
224         $id = base64_decode($id);
225     
226         $this->mXRecords = $this->ArrayUp(($id+1),$this->mXRecords);
227       }
228       if((preg_match("/^MXdown_/",$name)) && ($once)){
229         $once = false;
230         
231         $id = preg_replace("/^MXdown_/","",$name);
232         $id = preg_replace("/_.*$/","",$id);
233         $id = base64_decode($id);
234   
235         $this->mXRecords = $this->ArrayDown(($id+1),$this->mXRecords);
236       }
237       if((preg_match("/^MXdel_/",$name)) && ($once)){
238         $once = false;
239         
240         $id = preg_replace("/^MXdel_/","",$name);
241         $id = preg_replace("/_.*$/","",$id);
242         $id = base64_decode($id);
243         
244         unset($this->mXRecords[$id]);
246         $tmp  =array();
247         foreach($this->mXRecords as $entry){
248           $tmp[] = $entry;
249         }
250  
251         $this->mXRecords = $tmp; 
252       }
253     }
255     if((isset($_POST['AddMXRecord'])) && (!empty($_POST['StrMXRecord']))){
256       $this->mXRecords[] = array("type"=>"mXRecord","value"=>trim($_POST['StrMXRecord']));      
257     }
259     /* Handle Post events */
260     $once = true;
261     foreach($_POST as $name => $value){
263       /* Delete record if requested */
264       if((preg_match("/RemoveRecord_/",$name))&&($once)){
265         $once = false;
266         $id= preg_replace("/RemoveRecord_/","",$name);
267         unset($this->Records[$id]);
268       }
269     }
271     /* Add new Zonerecord */
272     if(isset($_POST['AddNewRecord'])){
273       $this->Records[] = array("type"=>"aRecord","value"=>"");
274     }
276     /* Fill in values */
277     foreach($this->attributes as $name){
278       $smarty->assign($name,$this->$name);
279     }
281     /* Set zoneNames without server suffix */
282     foreach(array("zoneName","ReverseZone") as $attr){
283       $smarty->assign($attr,getNameFromMix($this->$attr));
284     }
286     $div = new DivSelectBox("MxRecords");
287     $div->setHeight(120);
288     $recs = $this->mXRecords;
290     $oneup    = "<input name='MXup_%s'    type='image' src='images/sort_up.png'    title='"._("Up")."'      class='center'>&nbsp;"; 
291     $onedown  = "<input name='MXdown_%s'  type='image' src='images/sort_down.png'  title='"._("Down")."'    class='center'>&nbsp;"; 
292     $onedel   = "<img src='images/empty.png' width='20' class='center'>
293                  <input name='MXdel_%s'   type='image' src='images/edittrash.png'  title='"._("Delete")."'  class='center'>"; 
295     foreach($recs as $key => $rec){
296       $div ->AddEntry(array(
297             array("string"=>$rec['value']),
298 /*            array("string"=>$key,
299                   "attach"=>"style='width:20px;'"),*/
300             array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
301                   "attach"=>"style='width:70px;border-right:0px;'")
302             ));
303     }
305     /* Assign records list */
307       $smarty->assign("NotNew", false);
309     $smarty->assign("Mxrecords",  $div->DrawList());
310     $smarty->assign("records"  ,  $this->generateRecordsList());
312     /* Display tempalte */
313     $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
314     return($display);
315   }
317   function remove_from_parent()
318   {
319   }
321   /* Save data to object */
322   function save_object()
323   {
324     //plugin::save_object();
325     foreach($this->attributes as $attr){
326       if(isset($_POST[$attr])){
327         $this->$attr = $_POST[$attr];
328       }
329     }
331     foreach(array("zoneName","ReverseZone") as $attr){
332       if(isset($_POST[$attr])){
333         $this->$attr = strtoupper($this->cn)."/".$_POST[$attr];
334       }
335     }
337     foreach($this->Records as $id => $value){  
338       if(isset($_POST['RecordTypeSelectedFor_'.$id])){
339         $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
340       }
341       if(isset($_POST['RecordValue_'.$id])){
342         $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
343       }
344     }
345   }
348   /* Check supplied data */
349   function check()
350   {
351     /* Call common method to give check the hook */
352     $message= plugin::check();
353         
354     /* Check if zoneName is already in use */
355     $usedZones = $this->getUsedZoneNames();
356     if(($this->isNew == true)||($this->zoneName  != $this->InitialzoneName)||($this->ReverseZone != $this->InitialReverseZone)){
357 /*      if((isset($usedZones[$this->zoneName]))&&($this->zoneName  != $this->InitialzoneName)){
358         $message[] =_("This zoneName is already in use");
359       }
360       if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitialReverseZone)){
361         $message[] =_("This reverse zone is already in use");
362       }*/
363     }
365     if(empty($this->zoneName)){
366       $message[] =sprintf(_("Please choose a valid zone name."));
367     }
369     if(empty($this->ReverseZone)){
370       $message[] =sprintf(_("Please choose a valid reverse zone name."));
371     }
373     if(!preg_match("/\.$/",$this->sOAprimary)){
374       $message[] = _("Primary dns server must end with '.' to be a valid entry.");
375     }
377     if(!preg_match("/\.$/",$this->sOAmail)){
378       $message[] = _("Your specified mail address must end with '.' to be a valid record.");
379     }
381     if(preg_match("/@/",$this->sOAmail)){
382       $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
383     }
385     if(preg_match("/@/",$this->sOAmail)){
386       $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
387     }
389     if(getNameFromMix($this->zoneName) != strtolower(getNameFromMix($this->zoneName))){
390       $message[] = _("Only lowercase strings are allowed as zone name.");
391     }
393     if(!is_numeric($this->sOAserial)){
394       $message[] = _("Please specify a numeric value for serial number.");
395     }
397     if(!is_numeric($this->sOArefresh)){
398       $message[] = _("Please specify a numeric value for refresh.");
399     }
401     if(!is_numeric($this->sOAttl)){
402       $message[] = _("Please specify a numeric value for ttl.");
403     }
405     if(!is_numeric($this->sOAexpire)){
406       $message[] = _("Please specify a numeric value for expire.");
407     }
409     if(!is_numeric($this->sOAretry)){
410       $message[] = _("Please specify a numeric value for retry.");
411     }
413     foreach($this->Records as $name => $values){
414       /* only lower-case is allowed in record entries ... */
415       if($values['value'] != strtolower($values['value'])){
416         $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
417       }
418     }
419     return ($message);
420   }
422   /* This funtion returns all used Zonenames */
423   function getUsedZoneNames()
424   {
425     $ret = array();
426     $ldap = $this->config->get_ldap_link();
427     $ldap->cd($this->config->current['BASE']);
428     $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
429     while($attr = $ldap->fetch()){
430       if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
431         if(isset($attr['tXTRecord'][0])){
432           $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
433           $ret[$zn] =FlipIp(preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]));
434         }
435       }else{
436         $ret[$attr['zoneName'][0]]="";
437       }
438     }
439     return($ret);
440   }
442   /* Save to LDAP */
443   function save()
444   {
445     $ret =array();
446     foreach($this->attributes as $name){
447       $ret[$name] = $this->$name;
448     }
450     /* Create mx records 
451      */
452     foreach($this->mXRecords as $key => $rec){
453       $rec['value']= $key." ".$rec['value'];
454       $this->Records [] = $rec;
455     }
457     $ret['RECORDS'] = $this->Records; 
459     $ret['InitialReverseZone']=  $this->InitialReverseZone;
460     $ret['InitialzoneName']   =  $this->InitialzoneName;
462     return($ret);
463   }
465   
466   /* This function generate a table row for each used record.
467      This table row displays the recordtype in a select box
468       and the specified value for the record, and a remove button.
469      The last element of the table also got an 'add' button.
470    */
471   function generateRecordsList($changeStateForRecords="")
472   {
473     $changeStateForRecords = "";
475     $str = "<table summary=''>";
476     foreach($this->Records as $key => $entry){
478       if($entry['type'] == "mXRecord") continue;
479       
480       $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
481       $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
482       $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
484       $str.=" <tr>".
485         "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
486         "   <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
487         "   <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
488         "</tr>";
489     }
491     $str.= "  <tr>".
492       "    <td colspan=2></td><td>".
493       "      <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
494       "    </td>".
495       "  </tr>".
496       "</table>";
497     return($str);
498   }
500   /* This function generates a select box out of $this->RecordTypes options.
501      The Parameter $selected is used to predefine an attribute.
502      $name is used to specify a post name
503    */
504   function generateRecordListBox($selected,$name)
505   {
506     $str = "<select name='".$name."' id='".$name."'>";
507     foreach($this->RecordTypes as $type => $value){
509       if(preg_match("/^mXRecord$/i",$value)) continue;
511       $use = "";
512       if($type == $selected){
513         $use = " selected ";
514       }
515       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
516     }
517     $str.="</select>";
518     return($str);
519   }
522 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
523 ?>