Code

eda11ea1986ed819198d95cae97abbbf107a9678
[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     }
155     return ($message);
156   }
158   /* This funtion returns all used Zonenames */
159   function getUsedZoneNames()
160   {
161     $ret = array();
162     $ldap = $this->config->get_ldap_link();
163     $ldap->cd($this->config->current['BASE']);
164     $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
165     while($attr = $ldap->fetch()){
166       if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
167         $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
168         $ret[$zn] = $this->FlipIp(preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]));
169       }else{
170         $ret[$attr['zoneName'][0]]="";
171       }
172     }
173     return($ret);
174   }
176   /* this is used to flip the ip address for example
177       12.3.45  ->  54.3.12
178      Because some entries (like zones) are store like that 54.3.12.in-addr.arpa
179       but we want to display 12.3.45.
180   */
181   function FlipIp($ip)
182   {
183     $tmp = array_reverse(split("\.",$ip));
184     $new = "";
185     foreach($tmp as $section){
186       $new .= $section.".";
187     }
188     return(preg_replace("/.$/","",$new));
189   }
192   /* Save to LDAP */
193   function save()
194   {
195     $ret =array();
196     foreach($this->attributes as $name){
197       $ret[$name] = $this->$name;
198     }
199     $ret['Records'] = $this->Records; 
200     return($ret);
201   }
203   
204   /* This function generate a table row for each used record.
205      This table row displays the recordtype in a select box
206       and the specified value for the record, and a remove button.
207      The last element of the table also got an 'add' button.
208    */
209   function generateRecordsList($changeStateForRecords="")
210   {
211     $changeStateForRecords = "";
213     $str = "<table summary=''>";
214     foreach($this->Records as $key => $entry){
215       if($entry['status'] == "deleted") continue;
217       $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
218       $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
219       $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
221       $str.=" <tr>".
222         "   <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
223         "   <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
224         "   <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
225         "</tr>";
226     }
228     $str.= "  <tr>".
229       "    <td colspan=2></td><td>".
230       "      <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
231       "    </td>".
232       "  </tr>".
233       "</table>";
234     return($str);
235   }
237   /* This function generates a select box out of $this->RecordTypes options.
238      The Parameter $selected is used to predefine an attribute.
239      $name is used to specify a post name
240    */
241   function generateRecordListBox($selected,$name)
242   {
243     $str = "<select name='".$name."' id='".$name."'>";
244     foreach($this->RecordTypes as $type => $value){
245       $use = "";
246       if($type == $selected){
247         $use = " selected ";
248       }
249       $str.="\n <option value='".$type."' ".$use.">".$type."</option>";
250     }
251     $str.="</select>";
252     return($str);
253   }
256 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
257 ?>