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