Code

d814d416e36da1e446d715b0f66a2de307c67950
[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","dNSTTL","dNSClass",
13       "sOAprimary","sOAmail","sOAserial","sOArefresh","sOAretry","sOAexpire","sOAttl"); 
14   var $objectclasses  = array("whatever");
16   var $RecordTypes = array();
18   var $ReverseZone              = "";
19   var $ReverseDN                = "";
20   var $zoneName                 = "";
21   var $dNSTTL                   = "7200";
22   var $dNSClass                 = "IN";
23   var $status                   = "new";
25   var $sOAprimary               = "";
26   var $sOAmail                  = "";
27   var $sOAserial                = "";
28   var $sOArefresh               = "3600";
29   var $sOAretry                 = "1800";
30   var $sOAexpire                = "720000";
31   var $sOAttl                   = "6400";
33   var $Records                  = array();
35   var $InitiallyZoneName        = "";
36   var $InitiallyReverseZone     = "";
37   var $isNew                    = true;
39   function servdnseditZone ($config, $dn= NULL,$recordtypes,$attrs = array())
40   {
41     plugin::plugin ($config, $dn);
43     /* All types with required attrs */
44     $this->RecordTypes = $recordtypes; 
46     if(!count($attrs)){
47       $this->InitiallyZoneName      = "";
48       $this->InitiallyReverseZone   = "";
49       $this->isNew                  = true;
50       $this->sOAserial = date("Ymd")."1";
51     }else{
52       $this->InitiallyZoneName      = $attrs['zoneName'];
53       $this->InitiallyReverseZone   = $attrs['ReverseZone'];
54       $this->isNew                  = false;
56       foreach($this->attributes as $value){
57         $this->$value = $attrs[$value];
58       }
59       if(isset($attrs['Records'])){
60         $this->Records = $attrs['Records']; 
61       }else{
62         $this->Records = array();
63       }
64       $str = date("Ymd");
65       if(preg_match("/^".$str."/",$this->sOAserial)){
66         $this->sOAserial = $this->sOAserial + 1;
67       }else{
68         $this->sOAserial = date("Ymd")."01";
69       }
70     }
71   }
73   function execute()
74   {
75     /* Call parent execute */
76     plugin::execute();
78     /* Fill templating stuff */
79     $smarty= get_smarty();
80     $display= "";
82     /* Handle Post events */
83     $once = true;
84     foreach($_POST as $name => $value){
86       /* Delete record if requested */
87       if((preg_match("/RemoveRecord_/",$name))&&($once)){
88         $once = false;
89         $id= preg_replace("/RemoveRecord_/","",$name);
90         if($this->Records[$id]['status']!= "new"){
91           $this->Records[$id]['status']= "deleted";
92         }else{
93           unset($this->Records[$id]);
94         }
95       }
96     }
98     /* Add new Zonerecord */
99     if(isset($_POST['AddNewRecord'])){
100       $this->Records[] = array("type"=>"aRecord","inittype"=>"","value"=>"","status"=>"new");
101     }
103     /* Fill in values */
104     foreach($this->attributes as $name){
105       $smarty->assign($name,$this->$name);
106     }
108     /* Assign records list */
109     $smarty->assign("records",$this->generateRecordsList());
111     /* Display tempalte */
112     $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
113     return($display);
114   }
116   function remove_from_parent()
117   {
118   }
120   /* Save data to object */
121   function save_object()
122   {
123     //plugin::save_object();
124     foreach($this->attributes as $attr){
125       if(isset($_POST[$attr])){
126         $this->$attr = $_POST[$attr];
127       }
128     }
130     foreach($this->Records as $id => $value){  
131       if(isset($_POST['RecordTypeSelectedFor_'.$id])){
132         $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
133       }
134       if(isset($_POST['RecordValue_'.$id])){
135         $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
136       }
137     }
138   }
141   /* Check supplied data */
142   function check()
143   {
144     $message= array();
145     /* Check if zoneName is already in use */
146     $usedZones = $this->getUsedZoneNames();
147     if(($this->isNew == true)||($this->zoneName  != $this->InitiallyZoneName)||($this->ReverseZone != $this->InitiallyReverseZone)){
148       if((isset($usedZones[$this->zoneName]))&&($this->zoneName  != $this->InitiallyZoneName)){
149         $message[] =_("This zoneName is already in use");
150       }
151       if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitiallyReverseZone)){
152         $message[] =_("This reverse zone is already in use");
153       }
154     }
156     if(!preg_match("/\.$/",$this->sOAprimary)){
157       $message[] = _("Primary dns server must end with '.' to be a valid entry.");
158     }
160     if(!preg_match("/\.$/",$this->sOAmail)){
161       $message[] = _("Your specified mail address must end with '.' to be a valid record.");
162     }
164     if(preg_match("/@/",$this->sOAmail)){
165       $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
166     }
168     if(preg_match("/@/",$this->sOAmail)){
169       $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
170     }
172     if($this->zoneName != strtolower($this->zoneName)){
173       $message[] = _("Only lowercase strings are allowed as zone name.");
174     }
176     if(!is_numeric($this->sOAserial)){
177       $message[] = _("Please specify a numeric value for serial number.");
178     }
180     if(!is_numeric($this->sOArefresh)){
181       $message[] = _("Please specify a numeric value for refresh.");
182     }
184     if(!is_numeric($this->sOAttl)){
185       $message[] = _("Please specify a numeric value for ttl.");
186     }
188     if(!is_numeric($this->sOAexpire)){
189       $message[] = _("Please specify a numeric value for expire.");
190     }
192     if(!is_numeric($this->sOAretry)){
193       $message[] = _("Please specify a numeric value for retry.");
194     }
196     foreach($this->Records as $name => $values){
197       /* only lower-case is allowed in record entries ... */
198       if($values['value'] != strtolower($values['value'])){
199         $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
200       }
201     }
202     return ($message);
203   }
205   /* This funtion returns all used Zonenames */
206   function getUsedZoneNames()
207   {
208     $ret = array();
209     $ldap = $this->config->get_ldap_link();
210     $ldap->cd($this->config->current['BASE']);
211     $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
212     while($attr = $ldap->fetch()){
213       if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
214         $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
215         $ret[$zn] = $this->FlipIp(preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]));
216       }else{
217         $ret[$attr['zoneName'][0]]="";
218       }
219     }
220     return($ret);
221   }
223   /* this is used to flip the ip address for example
224       12.3.45  ->  54.3.12
225      Because some entries (like zones) are store like that 54.3.12.in-addr.arpa
226       but we want to display 12.3.45.
227   */
228   function FlipIp($ip)
229   {
230     $tmp = array_reverse(split("\.",$ip));
231     $new = "";
232     foreach($tmp as $section){
233       $new .= $section.".";
234     }
235     return(preg_replace("/.$/","",$new));
236   }
239   /* Save to LDAP */
240   function save()
241   {
242     $ret =array();
243     foreach($this->attributes as $name){
244       $ret[$name] = $this->$name;
245     }
246     $ret['Records'] = $this->Records; 
247     return($ret);
248   }
250   
251   /* This function generate a table row for each used record.
252      This table row displays the recordtype in a select box
253       and the specified value for the record, and a remove button.
254      The last element of the table also got an 'add' button.
255    */
256   function generateRecordsList($changeStateForRecords="")
257   {
258     $changeStateForRecords = "";
260     $str = "<table summary=''>";
261     foreach($this->Records as $key => $entry){
262       if($entry['status'] == "deleted") continue;
264       $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
265       $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
266       $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
268       $str.=" <tr>".
269         "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
270         "   <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
271         "   <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
272         "</tr>";
273     }
275     $str.= "  <tr>".
276       "    <td colspan=2></td><td>".
277       "      <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
278       "    </td>".
279       "  </tr>".
280       "</table>";
281     return($str);
282   }
284   /* This function generates a select box out of $this->RecordTypes options.
285      The Parameter $selected is used to predefine an attribute.
286      $name is used to specify a post name
287    */
288   function generateRecordListBox($selected,$name)
289   {
290     $str = "<select name='".$name."' id='".$name."'>";
291     foreach($this->RecordTypes as $type => $value){
292       $use = "";
293       if($type == $selected){
294         $use = " selected ";
295       }
296       $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
297     }
298     $str.="</select>";
299     return($str);
300   }
303 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
304 ?>