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($this->zoneName != strtolower($this->zoneName)){
157 $message[] = _("Only lowercase strings are allowed as zone name.");
158 }
160 if(!is_numeric($this->sOAserial)){
161 $message[] = _("Please specify a numeric value for serial number.");
162 }
164 if(!is_numeric($this->sOArefresh)){
165 $message[] = _("Please specify a numeric value for refresh.");
166 }
168 if(!is_numeric($this->sOAttl)){
169 $message[] = _("Please specify a numeric value for ttl.");
170 }
172 if(!is_numeric($this->sOAexpire)){
173 $message[] = _("Please specify a numeric value for expire.");
174 }
176 if(!is_numeric($this->sOAretry)){
177 $message[] = _("Please specify a numeric value for retry.");
178 }
180 foreach($this->Records as $name => $values){
181 /* only lower-case is allowed in record entries ... */
182 if($values['value'] != strtolower($values['value'])){
183 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
184 }
185 }
186 return ($message);
187 }
189 /* This funtion returns all used Zonenames */
190 function getUsedZoneNames()
191 {
192 $ret = array();
193 $ldap = $this->config->get_ldap_link();
194 $ldap->cd($this->config->current['BASE']);
195 $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
196 while($attr = $ldap->fetch()){
197 if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
198 $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
199 $ret[$zn] = $this->FlipIp(preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]));
200 }else{
201 $ret[$attr['zoneName'][0]]="";
202 }
203 }
204 return($ret);
205 }
207 /* this is used to flip the ip address for example
208 12.3.45 -> 54.3.12
209 Because some entries (like zones) are store like that 54.3.12.in-addr.arpa
210 but we want to display 12.3.45.
211 */
212 function FlipIp($ip)
213 {
214 $tmp = array_reverse(split("\.",$ip));
215 $new = "";
216 foreach($tmp as $section){
217 $new .= $section.".";
218 }
219 return(preg_replace("/.$/","",$new));
220 }
223 /* Save to LDAP */
224 function save()
225 {
226 $ret =array();
227 foreach($this->attributes as $name){
228 $ret[$name] = $this->$name;
229 }
230 $ret['Records'] = $this->Records;
231 return($ret);
232 }
235 /* This function generate a table row for each used record.
236 This table row displays the recordtype in a select box
237 and the specified value for the record, and a remove button.
238 The last element of the table also got an 'add' button.
239 */
240 function generateRecordsList($changeStateForRecords="")
241 {
242 $changeStateForRecords = "";
244 $str = "<table summary=''>";
245 foreach($this->Records as $key => $entry){
246 if($entry['status'] == "deleted") continue;
248 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
249 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
250 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
252 $str.=" <tr>".
253 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
254 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
255 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
256 "</tr>";
257 }
259 $str.= " <tr>".
260 " <td colspan=2></td><td>".
261 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
262 " </td>".
263 " </tr>".
264 "</table>";
265 return($str);
266 }
268 /* This function generates a select box out of $this->RecordTypes options.
269 The Parameter $selected is used to predefine an attribute.
270 $name is used to specify a post name
271 */
272 function generateRecordListBox($selected,$name)
273 {
274 $str = "<select name='".$name."' id='".$name."'>";
275 foreach($this->RecordTypes as $type => $value){
276 $use = "";
277 if($type == $selected){
278 $use = " selected ";
279 }
280 $str.="\n <option value='".$type."' ".$use.">".$type."</option>";
281 }
282 $str.="</select>";
283 return($str);
284 }
285 }
287 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
288 ?>