e3770a5e402715afe7365d995e6967530e8d3724
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","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 $dNSClass = "IN";
22 var $status = "new";
24 var $sOAprimary = "";
25 var $sOAmail = "";
26 var $sOAserial = "";
27 var $sOArefresh = "3600";
28 var $sOAretry = "1800";
29 var $sOAexpire = "720000";
30 var $sOAttl = "6400";
32 var $Records = array();
33 var $mXRecords = array();
35 var $InitiallyZoneName = "";
36 var $InitiallyReverseZone = "";
37 var $isNew = true;
39 var $dialog = false;
41 function servdnseditZone ($config, $dn= NULL,$recordtypes,$attrs = array())
42 {
43 plugin::plugin ($config, $dn);
45 /* All types with required attrs */
46 $this->RecordTypes = $recordtypes;
48 if(!count($attrs)){
49 $this->InitiallyZoneName = "";
50 $this->InitiallyReverseZone = "";
51 $this->isNew = true;
52 $this->sOAserial = date("Ymd")."1";
53 }else{
54 $this->InitiallyZoneName = $attrs['zoneName'];
55 $this->InitiallyReverseZone = $attrs['ReverseZone'];
56 $this->isNew = false;
58 foreach($this->attributes as $value){
59 $this->$value = $attrs[$value];
60 }
61 if(isset($attrs['Records'])){
62 $this->Records = $attrs['Records'];
64 $tmp2 = array();
65 $usedPrio = array();
66 foreach($this->Records as $key => $rec){
67 if($rec['type'] == "mXRecord"){
68 $tmp = split(" ",$rec['value']);
69 $rec['value'] = $tmp[0];
70 $tmp2[$tmp[1]] = $rec;
71 unset($this->Records[$key]);
72 }
73 }
74 if(count($tmp2) != 0){
75 reset($tmp2);
76 ksort($tmp2);
77 }
78 $this->mXRecords = $tmp2;
79 }else{
80 $this->mXRecords = array();
81 $this->Records = array();
82 }
83 $str = date("Ymd");
84 if(preg_match("/^".$str."/",$this->sOAserial)){
85 $this->sOAserial = $this->sOAserial + 1;
86 }else{
87 $this->sOAserial = date("Ymd")."01";
88 }
89 }
90 }
92 /* TRansports the geiven Arraykey one position up*/
93 function ArrayUp($atr,$attrs)
94 {
95 $ret = $attrs;
96 $pos = $atr ;
97 $cn = count($attrs);
98 if(!(($pos == -1)||($pos == 1)||($pos >$cn))){
99 $before = array_slice($attrs,0,($pos-2));
100 $mitte = array_reverse(array_slice($attrs,($pos-2),2));
101 $unten = array_slice($attrs,$pos);
102 $ret = array();
103 $ret = $this->combineArrays($before,$mitte,$unten);
104 }
105 return($ret);
106 }
109 /* TRansports the geiven Arraykey one position up*/
110 function ArrayDown($atr,$attrs)
111 {
112 $ret = $attrs;
113 $pos = $atr ;
114 $cn = count($attrs);
115 if(!(($pos == -1)||($pos == $cn))){
116 $before = array_slice($attrs,0,($pos-1));
117 $mitte = array_reverse(array_slice($attrs,($pos-1),2));
118 $unten = array_slice($attrs,($pos+1));
119 $ret = array();
120 $ret = $this->combineArrays($before,$mitte,$unten);
121 }
122 return($ret);
123 }
125 /* Combine new array */
126 function combineArrays($ar0,$ar1,$ar2)
127 {
128 $ret = array();
129 if(is_array($ar0))
130 foreach($ar0 as $ar => $a){
131 $ret[]=$a;
132 }
133 if(is_array($ar1))
134 foreach($ar1 as $ar => $a){
135 $ret[]=$a;
136 }
137 if(is_array($ar2))
138 foreach($ar2 as $ar => $a){
139 $ret[]=$a;
140 }
141 return($ret);
142 }
144 function getpos($atr,$attrs)
145 {
146 $i = 0;
147 foreach($attrs as $attr => $name) {
148 $i++;
149 if($attr == $atr){
150 return($i);
151 }
152 }
153 return(-1);
154 }
157 function execute()
158 {
159 /* Call parent execute */
160 plugin::execute();
163 /* Fill templating stuff */
164 $smarty= get_smarty();
165 $display= "";
168 /* Open Zone Entry Edit Dialog
169 */
170 if(isset($_POST['EditZoneEntries'])){
171 $this->dialog= new servDNSeditZoneEntries($this->config,$this->dn);
172 }
174 /* Save Zone Entry Edit Dialog
175 */
176 if(isset($_POST['SaveZoneEntryChanges'])){
177 $this->dialog->save_object();
178 if(count($this->dialog->check())){
179 $msgs = $this->dialog->check();
180 foreach($msgs as $msg){
181 print_red($msg);
182 }
183 }else{
184 $this->dialog->save();
185 $this->dialog = false;
186 }
187 }
189 /* Cancel Zone Entrie Edit Dialog
190 */
191 if(isset($_POST['CancelZoneEntryChanges'])){
192 $this->dialog = false;
193 }
195 /* Display any type of open dialogs
196 */
197 if($this->dialog){
198 $this->dialog->save_object();
199 return($this->dialog->execute());
200 }
202 $once =true;
203 foreach($_POST as $name => $value){
204 if((preg_match("/^MXup_/",$name)) && ($once)){
205 $once = false;
207 $id = preg_replace("/^MXup_/","",$name);
208 $id = preg_replace("/_.*$/","",$id);
209 $id = base64_decode($id);
211 $this->mXRecords = $this->ArrayUp(($id+1),$this->mXRecords);
212 }
213 if((preg_match("/^MXdown_/",$name)) && ($once)){
214 $once = false;
216 $id = preg_replace("/^MXdown_/","",$name);
217 $id = preg_replace("/_.*$/","",$id);
218 $id = base64_decode($id);
220 $this->mXRecords = $this->ArrayDown(($id+1),$this->mXRecords);
221 }
222 if((preg_match("/^MXdel_/",$name)) && ($once)){
223 $once = false;
225 $id = preg_replace("/^MXdel_/","",$name);
226 $id = preg_replace("/_.*$/","",$id);
227 $id = base64_decode($id);
229 unset($this->mXRecords[$id]);
231 $tmp =array();
232 foreach($this->mXRecords as $entry){
233 $tmp[] = $entry;
234 }
236 $this->mXRecords = $tmp;
237 }
238 }
240 if((isset($_POST['AddMXRecord'])) && (!empty($_POST['StrMXRecord']))){
241 $this->mXRecords[] = array("type"=>"mXRecord","inittype"=>"","value"=>trim($_POST['StrMXRecord']),"status"=>"new");
242 }
244 /* Handle Post events */
245 $once = true;
246 foreach($_POST as $name => $value){
248 /* Delete record if requested */
249 if((preg_match("/RemoveRecord_/",$name))&&($once)){
250 $once = false;
251 $id= preg_replace("/RemoveRecord_/","",$name);
252 if($this->Records[$id]['status']!= "new"){
253 $this->Records[$id]['status']= "deleted";
254 }else{
255 unset($this->Records[$id]);
256 }
257 }
258 }
260 /* Add new Zonerecord */
261 if(isset($_POST['AddNewRecord'])){
262 $this->Records[] = array("type"=>"aRecord","inittype"=>"","value"=>"","status"=>"new");
263 }
265 /* Fill in values */
266 foreach($this->attributes as $name){
267 $smarty->assign($name,$this->$name);
268 }
271 $div = new DivSelectBox("MxRecords");
272 $div->setHeight(120);
273 $recs = $this->mXRecords;
275 $oneup = "<input name='MXup_%s' type='image' src='images/sort_up.png' title='"._("Up")."' class='center'> ";
276 $onedown = "<input name='MXdown_%s' type='image' src='images/sort_down.png' title='"._("Down")."' class='center'> ";
277 $onedel = "<img src='images/empty.png' width='20' class='center'>
278 <input name='MXdel_%s' type='image' src='images/edittrash.png' title='"._("Delete")."' class='center'>";
280 foreach($recs as $key => $rec){
281 $div ->AddEntry(array(
282 array("string"=>$rec['value']),
283 /* array("string"=>$key,
284 "attach"=>"style='width:20px;'"),*/
285 array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
286 "attach"=>"style='width:70px;border-right:0px;'")
287 ));
288 }
290 /* Assign records list */
292 $smarty->assign("Mxrecords", $div->DrawList());
293 $smarty->assign("records" , $this->generateRecordsList());
295 /* Display tempalte */
296 $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
297 return($display);
298 }
300 function remove_from_parent()
301 {
302 }
304 /* Save data to object */
305 function save_object()
306 {
307 //plugin::save_object();
308 foreach($this->attributes as $attr){
309 if(isset($_POST[$attr])){
310 $this->$attr = $_POST[$attr];
311 }
312 }
314 foreach($this->Records as $id => $value){
315 if(isset($_POST['RecordTypeSelectedFor_'.$id])){
316 $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
317 }
318 if(isset($_POST['RecordValue_'.$id])){
319 $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
320 }
321 }
322 }
325 /* Check supplied data */
326 function check()
327 {
328 $message= array();
329 /* Check if zoneName is already in use */
330 $usedZones = $this->getUsedZoneNames();
331 if(($this->isNew == true)||($this->zoneName != $this->InitiallyZoneName)||($this->ReverseZone != $this->InitiallyReverseZone)){
332 if((isset($usedZones[$this->zoneName]))&&($this->zoneName != $this->InitiallyZoneName)){
333 $message[] =_("This zoneName is already in use");
334 }
335 if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitiallyReverseZone)){
336 $message[] =_("This reverse zone is already in use");
337 }
338 }
340 if(!preg_match("/\.$/",$this->sOAprimary)){
341 $message[] = _("Primary dns server must end with '.' to be a valid entry.");
342 }
344 if(!preg_match("/\.$/",$this->sOAmail)){
345 $message[] = _("Your specified mail address must end with '.' to be a valid record.");
346 }
348 if(preg_match("/@/",$this->sOAmail)){
349 $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
350 }
352 if(preg_match("/@/",$this->sOAmail)){
353 $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
354 }
356 if($this->zoneName != strtolower($this->zoneName)){
357 $message[] = _("Only lowercase strings are allowed as zone name.");
358 }
360 if(!is_numeric($this->sOAserial)){
361 $message[] = _("Please specify a numeric value for serial number.");
362 }
364 if(!is_numeric($this->sOArefresh)){
365 $message[] = _("Please specify a numeric value for refresh.");
366 }
368 if(!is_numeric($this->sOAttl)){
369 $message[] = _("Please specify a numeric value for ttl.");
370 }
372 if(!is_numeric($this->sOAexpire)){
373 $message[] = _("Please specify a numeric value for expire.");
374 }
376 if(!is_numeric($this->sOAretry)){
377 $message[] = _("Please specify a numeric value for retry.");
378 }
380 foreach($this->Records as $name => $values){
381 /* only lower-case is allowed in record entries ... */
382 if($values['value'] != strtolower($values['value'])){
383 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
384 }
385 }
386 return ($message);
387 }
389 /* This funtion returns all used Zonenames */
390 function getUsedZoneNames()
391 {
392 $ret = array();
393 $ldap = $this->config->get_ldap_link();
394 $ldap->cd($this->config->current['BASE']);
395 $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
396 while($attr = $ldap->fetch()){
397 if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
398 if(isset($attr['tXTRecord'][0])){
399 $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
400 $ret[$zn] = $this->FlipIp(preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]));
401 }
402 }else{
403 $ret[$attr['zoneName'][0]]="";
404 }
405 }
406 return($ret);
407 }
409 /* this is used to flip the ip address for example
410 12.3.45 -> 54.3.12
411 Because some entries (like zones) are store like that 54.3.12.in-addr.arpa
412 but we want to display 12.3.45.
413 */
414 function FlipIp($ip)
415 {
416 $tmp = array_reverse(split("\.",$ip));
417 $new = "";
418 foreach($tmp as $section){
419 $new .= $section.".";
420 }
421 return(preg_replace("/.$/","",$new));
422 }
425 /* Save to LDAP */
426 function save()
427 {
428 $ret =array();
429 foreach($this->attributes as $name){
430 $ret[$name] = $this->$name;
431 }
433 foreach($this->mXRecords as $key => $rec){
434 $rec['value'].= " ".$key;
435 $this->Records [] = $rec;
436 }
438 $ret['Records'] = $this->Records;
440 return($ret);
441 }
444 /* This function generate a table row for each used record.
445 This table row displays the recordtype in a select box
446 and the specified value for the record, and a remove button.
447 The last element of the table also got an 'add' button.
448 */
449 function generateRecordsList($changeStateForRecords="")
450 {
451 $changeStateForRecords = "";
453 $str = "<table summary=''>";
454 foreach($this->Records as $key => $entry){
456 if($entry['type'] == "mXRecord") continue;
458 if($entry['status'] == "deleted") continue;
460 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
461 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
462 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
464 $str.=" <tr>".
465 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
466 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
467 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
468 "</tr>";
469 }
471 $str.= " <tr>".
472 " <td colspan=2></td><td>".
473 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
474 " </td>".
475 " </tr>".
476 "</table>";
477 return($str);
478 }
480 /* This function generates a select box out of $this->RecordTypes options.
481 The Parameter $selected is used to predefine an attribute.
482 $name is used to specify a post name
483 */
484 function generateRecordListBox($selected,$name)
485 {
486 $str = "<select name='".$name."' id='".$name."'>";
487 foreach($this->RecordTypes as $type => $value){
489 if(preg_match("/^mXRecord$/i",$value)) continue;
491 $use = "";
492 if($type == $selected){
493 $use = " selected ";
494 }
495 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
496 }
497 $str.="</select>";
498 return($str);
499 }
500 }
502 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
503 ?>