1 <?php
3 class servdnseditZone extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account= TRUE;
7 var $attributes = array("zoneName","ReverseZone","dNSClass","cn",
8 "sOAprimary","sOAmail","sOAserial","sOArefresh","sOAretry","sOAexpire","sOAttl");
9 var $objectclasses = array("whatever");
11 var $RecordTypes = array();
13 var $ReverseZone = "";
14 var $zoneName = "";
15 var $dNSClass = "IN";
17 var $sOAprimary = "";
18 var $sOAmail = "";
19 var $sOAserial = "";
20 var $sOArefresh = "3600";
21 var $sOAretry = "1800";
22 var $sOAexpire = "720000";
23 var $sOAttl = "6400";
25 var $Records = array();
26 var $mXRecords = array();
28 var $OldZoneName = ""; // To detect changes made with this edit
29 var $OldReverseZone = "";
31 var $InitialReverseZone = "";
32 var $InitialzoneName = "";
33 var $NetworkClass = "A" ; // One out of A,B,C
35 var $dialog = false;
37 var $zoneEditor = NULL;
39 var $isNew = true;
40 var $cn;
41 var $ZoneObject = array();
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->ZoneObject = $attrs;
61 if(isset($attrs['zoneEditor'])){
62 $this->zoneEditor = $attrs['zoneEditor'];
63 }
64 $this->OldZoneName = $attrs['zoneName'];
65 $this->OldReverseZone = $attrs['ReverseZone'];
67 $this->InitialzoneName = $attrs['InitialzoneName'];
68 $this->InitialReverseZone = $attrs['InitialReverseZone'];
70 $this->isNew = false;
72 foreach($this->attributes as $value){
73 if(isset($attrs[$value])){
74 $this->$value = $attrs[$value];
75 }
76 }
78 $this->sOAmail = preg_replace("/\./","@",$this->sOAmail,1);
79 $this->sOAmail = preg_replace("/\.$/","",$this->sOAmail);
80 $this->sOAprimary = preg_replace("/\.$/","",$this->sOAprimary);
81 $this->zoneName = preg_replace("/\.$/","",$this->zoneName);
83 if(isset($attrs['RECORDS'])){
84 $this->Records = $attrs['RECORDS'];
86 $tmp2 = array();
87 $usedPrio = array();
88 foreach($this->Records as $key => $rec){
89 if($rec['type'] == "mXRecord"){
90 $tmp = split(" ",$rec['value']);
91 $rec['value'] = $tmp[1];
92 $tmp2[$tmp[0]] = $rec;
93 unset($this->Records[$key]);
94 }
95 if($rec['type'] == "nSRecord"){
96 unset($this->Records[$key]);
97 }
98 }
99 if(count($tmp2) != 0){
100 reset($tmp2);
101 ksort($tmp2);
102 }
103 $this->mXRecords = $tmp2;
104 }else{
105 $this->mXRecords = array();
106 $this->Records = array();
107 }
109 $str = date("Ymd");
110 if(preg_match("/^".$str."/",$this->sOAserial)){
111 $this->sOAserial = $this->sOAserial + 1;
112 }else{
113 $this->sOAserial = date("Ymd")."01";
114 }
115 }
117 /* Detect Network class */
118 if(!empty($this->ReverseZone)){
120 $dots = count(split("\.",preg_replace("/^[^\/]*+\//","",$this->ReverseZone)));
121 if($dots == 1){
122 $this->NetworkClass = "A";
123 $this->ReverseZone .= ".0.0.0";
124 }elseif($dots == 2){
125 $this->NetworkClass = "B";
126 $this->ReverseZone .= ".0.0";
127 }else{
128 $this->NetworkClass = "C";
129 $this->ReverseZone .= ".0";
130 }
131 }
132 }
134 /* TRansports the geiven Arraykey one position up*/
135 function ArrayUp($atr,$attrs)
136 {
137 $ret = $attrs;
138 $pos = $atr ;
139 $cn = count($attrs);
140 if(!(($pos == -1)||($pos == 1)||($pos >$cn))){
141 $before = array_slice($attrs,0,($pos-2));
142 $mitte = array_reverse(array_slice($attrs,($pos-2),2));
143 $unten = array_slice($attrs,$pos);
144 $ret = array();
145 $ret = $this->combineArrays($before,$mitte,$unten);
146 }
147 return($ret);
148 }
151 /* TRansports the geiven Arraykey one position up*/
152 function ArrayDown($atr,$attrs)
153 {
154 $ret = $attrs;
155 $pos = $atr ;
156 $cn = count($attrs);
157 if(!(($pos == -1)||($pos == $cn))){
158 $before = array_slice($attrs,0,($pos-1));
159 $mitte = array_reverse(array_slice($attrs,($pos-1),2));
160 $unten = array_slice($attrs,($pos+1));
161 $ret = array();
162 $ret = $this->combineArrays($before,$mitte,$unten);
163 }
164 return($ret);
165 }
167 /* Combine new array */
168 function combineArrays($ar0,$ar1,$ar2)
169 {
170 $ret = array();
171 if(is_array($ar0))
172 foreach($ar0 as $ar => $a){
173 $ret[]=$a;
174 }
175 if(is_array($ar1))
176 foreach($ar1 as $ar => $a){
177 $ret[]=$a;
178 }
179 if(is_array($ar2))
180 foreach($ar2 as $ar => $a){
181 $ret[]=$a;
182 }
183 return($ret);
184 }
186 function getpos($atr,$attrs)
187 {
188 $i = 0;
189 foreach($attrs as $attr => $name) {
190 $i++;
191 if($attr == $atr){
192 return($i);
193 }
194 }
195 return(-1);
196 }
199 function execute()
200 {
201 /* Call parent execute */
202 plugin::execute();
205 /* Fill templating stuff */
206 $smarty= get_smarty();
207 $display= "";
209 /* Open Zone Entry Edit Dialog
210 */
211 if(!count($this->ZoneObject)){
212 $smarty->assign("AllowZoneEdit" , false);
213 }else{
214 $smarty->assign("AllowZoneEdit" , true);
215 if(isset($_POST['EditZoneEntries'])){
216 if($this->zoneEditor == NULL){
217 $this->zoneEditor= new servDNSeditZoneEntries($this->config,$this->dn,$this->ZoneObject);
218 $this->zoneEditor->parent = $this;
219 }
220 $this->dialog = $this->zoneEditor;
221 }
222 }
224 /* Save Zone Entry Edit Dialog
225 */
226 if(isset($_POST['SaveZoneEntryChanges']) && is_object($this->dialog)){
227 $this->dialog->save_object();
228 if(count($this->dialog->check())){
229 $msgs = $this->dialog->check();
230 foreach($msgs as $msg){
231 print_red($msg);
232 }
233 }else{
234 if (version_compare(phpversion(), '5.0') < 0) {
235 $this->zoneEditor = $this->dialog;
236 }else{
237 $this->zoneEditor = clone($this->dialog);
238 }
239 $this->dialog = FALSE;
240 # $rev = FlipIp(getNameFromMix($this->InitialReverseZone)).".in-addr.arpa";
241 # $for = getNameFromMix($this->InitialzoneName);
242 #
243 # $this->parent->handle_post_events("modify",array("dn" => $this->dn,"zoneName" => $rev));
244 # $this->parent->handle_post_events("modify",array("dn" => $this->dn,"zoneName" => $for));
245 # $this->dialog = false;
246 }
247 }
249 /* Cancel Zone Entrie Edit Dialog
250 */
251 if(isset($_POST['CancelZoneEntryChanges'])){
252 $this->dialog = false;
253 }
255 /* Display any type of open dialogs
256 */
257 if($this->dialog){
258 $this->dialog->save_object();
259 return($this->dialog->execute());
260 }
262 $once =true;
263 foreach($_POST as $name => $value){
264 if((preg_match("/^MXup_/",$name)) && ($once)){
265 $once = false;
267 $id = preg_replace("/^MXup_/","",$name);
268 $id = preg_replace("/_.*$/","",$id);
269 $id = base64_decode($id);
271 $this->mXRecords = $this->ArrayUp(($id+1),$this->mXRecords);
272 }
273 if((preg_match("/^MXdown_/",$name)) && ($once)){
274 $once = false;
276 $id = preg_replace("/^MXdown_/","",$name);
277 $id = preg_replace("/_.*$/","",$id);
278 $id = base64_decode($id);
280 $this->mXRecords = $this->ArrayDown(($id+1),$this->mXRecords);
281 }
282 if((preg_match("/^MXdel_/",$name)) && ($once)){
283 $once = false;
285 $id = preg_replace("/^MXdel_/","",$name);
286 $id = preg_replace("/_.*$/","",$id);
287 $id = base64_decode($id);
289 unset($this->mXRecords[$id]);
291 $tmp =array();
292 foreach($this->mXRecords as $entry){
293 $tmp[] = $entry;
294 }
296 $this->mXRecords = $tmp;
297 }
298 }
300 if((isset($_POST['AddMXRecord'])) && (!empty($_POST['StrMXRecord']))){
301 $this->mXRecords[] = array("type"=>"mXRecord","value"=>trim($_POST['StrMXRecord']));
302 }
304 /* Handle Post events */
305 $once = true;
306 foreach($_POST as $name => $value){
308 /* Delete record if requested */
309 if((preg_match("/RemoveRecord_/",$name))&&($once)){
310 $once = false;
311 $id= preg_replace("/RemoveRecord_/","",$name);
312 unset($this->Records[$id]);
313 }
314 }
316 /* Add new Zonerecord */
317 if(isset($_POST['AddNewRecord'])){
318 $this->Records[] = array("type"=>"aRecord","value"=>"");
319 }
321 /* Fill in values */
322 foreach($this->attributes as $name){
323 $smarty->assign($name,$this->$name);
324 }
326 /* Set zoneNames without server suffix */
327 foreach(array("zoneName","ReverseZone") as $attr){
328 $smarty->assign($attr,getNameFromMix($this->$attr));
329 }
331 $div = new DivSelectBox("MxRecords");
332 $div->setHeight(120);
333 $recs = $this->mXRecords;
335 $oneup = "<input name='MXup_%s' type='image' src='images/sort_up.png' title='"._("Up")."' class='center'> ";
336 $onedown = "<input name='MXdown_%s' type='image' src='images/sort_down.png' title='"._("Down")."' class='center'> ";
337 $onedel = "<img src='images/empty.png' width='20' class='center'>
338 <input name='MXdel_%s' type='image' src='images/edittrash.png' title='"._("Delete")."' class='center'>";
340 foreach($recs as $key => $rec){
341 $div ->AddEntry(array(
342 array("string"=>$rec['value']),
343 /* array("string"=>$key,
344 "attach"=>"style='width:20px;'"),*/
345 array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
346 "attach"=>"style='width:70px;border-right:0px;'")
347 ));
348 }
350 /* Assign records list */
351 $smarty->assign("NotNew", false);
352 $smarty->assign("Mxrecords", $div->DrawList());
353 $smarty->assign("records" , $this->generateRecordsList());
354 $smarty->assign("NetworkClass", $this->NetworkClass);
355 $smarty->assign("NetworkClasses", array("A"=>"255.0.0.0 (Class A)","B"=>"255.255.0.0 (Class B)","C"=>"255.255.255.0 (Class C)"));
357 /* Display tempalte */
358 $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
359 return($display);
360 }
362 function remove_from_parent()
363 {
364 }
366 /* Save data to object */
367 function save_object()
368 {
369 //plugin::save_object();
370 foreach($this->attributes as $attr){
371 if(isset($_POST[$attr])){
372 $this->$attr = $_POST[$attr];
373 }
374 }
376 if(isset($_POST['NetworkClass'])){
377 $this->NetworkClass = $_POST['NetworkClass'];
378 }
380 foreach(array("zoneName","ReverseZone") as $attr){
381 if(isset($_POST[$attr])){
382 $this->$attr = strtoupper($this->cn)."/".$_POST[$attr];
383 }
384 }
386 foreach($this->Records as $id => $value){
387 if(isset($_POST['RecordTypeSelectedFor_'.$id])){
388 $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
389 }
390 if(isset($_POST['RecordValue_'.$id])){
391 $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
392 }
393 }
394 }
397 /* Check supplied data */
398 function check()
399 {
400 /* Call common method to give check the hook */
401 $message= plugin::check();
403 /* Check if zoneName is already in use */
404 $usedZones = $this->getUsedZoneNames();
405 if(($this->isNew == true)||($this->zoneName != $this->InitialzoneName)||($this->ReverseZone != $this->InitialReverseZone)){
406 /* if((isset($usedZones[$this->zoneName]))&&($this->zoneName != $this->InitialzoneName)){
407 $message[] =_("This zoneName is already in use");
408 }
409 if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitialReverseZone)){
410 $message[] =_("This reverse zone is already in use");
411 }*/
412 }
414 if(empty($this->zoneName)){
415 $message[] =sprintf(_("Please choose a valid zone name."));
416 }
418 if(empty($this->ReverseZone)){
419 $message[] =sprintf(_("Please choose a valid reverse zone name."));
420 }
422 if(getNameFromMix($this->zoneName) != strtolower(getNameFromMix($this->zoneName))){
423 $message[] = _("Only lowercase strings are allowed as zone name.");
424 }
426 if(!is_numeric($this->sOAserial)){
427 $message[] = _("Please specify a numeric value for serial number.");
428 }
430 if(!is_numeric($this->sOArefresh)){
431 $message[] = _("Please specify a numeric value for refresh.");
432 }
434 if(!is_numeric($this->sOAttl)){
435 $message[] = _("Please specify a numeric value for ttl.");
436 }
438 if(!is_numeric($this->sOAexpire)){
439 $message[] = _("Please specify a numeric value for expire.");
440 }
442 if(!is_numeric($this->sOAretry)){
443 $message[] = _("Please specify a numeric value for retry.");
444 }
446 foreach($this->Records as $name => $values){
447 /* only lower-case is allowed in record entries ... */
448 if($values['value'] != strtolower($values['value'])){
449 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
450 }
451 }
453 /* Check class for given Zone Address */
454 $addr = preg_replace("/^[^\/]*+\//","",$this->ReverseZone);
456 /* Check for valid&complete IP address */
457 if(!is_ip($addr)){
458 $message[] = _("The given network address is not a valid, please specify a valid IP address.");
459 }
461 /* Check if given address matches selected network class */
462 switch($this->NetworkClass){
463 case 'A': {
464 if(!preg_match("/^[0-9]*\.0\.0\.0$/",$addr)){
465 $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.0.0.0"));
466 }
467 }
468 break;
469 case 'B': {
470 if(!preg_match("/^[0-9]*\.[0-9]*\.0\.0$/",$addr)){
471 $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.x.0.0"));
472 }
473 }
474 break;
475 case 'C': {
476 if(!preg_match("/^[0-9]*\.[0-9]*\.[0-9]*\.0$/",$addr)){
477 $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.x.x.0"));
478 }
479 }
480 break;
481 default : $message[] =sprintf(_("The given network class '%s' is not valid."),$this->NetworkClass);
482 }
484 return ($message);
485 }
487 /* This funtion returns all used Zonenames */
488 function getUsedZoneNames()
489 {
490 $ret = array();
491 $ldap = $this->config->get_ldap_link();
492 $ldap->cd($this->config->current['BASE']);
493 $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
494 while($attr = $ldap->fetch()){
495 if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
496 if(isset($attr['tXTRecord'][0])){
497 $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
498 $ret[$zn] =FlipIp(preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]));
499 }
500 }else{
501 $ret[$attr['zoneName'][0]]="";
502 }
503 }
504 return($ret);
505 }
507 /* Save to LDAP */
508 function save()
509 {
510 $ret =array();
511 foreach($this->attributes as $name){
512 $ret[$name] = $this->$name;
513 }
515 /* Create mx records
516 */
517 foreach($this->mXRecords as $key => $rec){
518 $rec['value']= $key." ".$rec['value'];
519 $this->Records [] = $rec;
520 }
523 $ret['RECORDS'] = $this->Records;
525 switch($this->NetworkClass){
526 case 'C' : $ret['ReverseZone']= preg_replace("/\.[0-9]*$/","",$this->ReverseZone);break;
527 case 'B' : $ret['ReverseZone']= preg_replace("/\.[0-9]*\.[0-9]*$/","",$this->ReverseZone);break;
528 case 'A' : $ret['ReverseZone']= preg_replace("/\.[0-9]*\.[0-9]*\.[0-9]*$/","",$this->ReverseZone);break;
529 default : trigger_error("Invalid network class given '".$this->NetworkClass."'");
530 }
532 $ret['InitialReverseZone']= $this->InitialReverseZone;
533 $ret['InitialzoneName'] = $this->InitialzoneName;
535 $ret['sOAmail'] = preg_replace("/\@/",".",$this->sOAmail);
537 foreach(array("sOAprimary","zoneName","sOAmail") as $attr){
538 if(!preg_match("/\.$/",$ret[$attr])){
539 if(!is_ip($ret[$attr])){
540 $ret[$attr] = $ret[$attr].".";
541 }
542 }
543 }
545 $ret['RECORDS'][] = array("type" => "nSRecord","value" => $ret['sOAprimary']) ;
547 $ret['zoneEditor'] = $this->zoneEditor;
548 return($ret);
549 }
552 /* This function generate a table row for each used record.
553 This table row displays the recordtype in a select box
554 and the specified value for the record, and a remove button.
555 The last element of the table also got an 'add' button.
556 */
557 function generateRecordsList($changeStateForRecords="")
558 {
559 $changeStateForRecords = "";
561 $str = "<table summary=''>";
562 foreach($this->Records as $key => $entry){
564 if($entry['type'] == "mXRecord") continue;
566 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
567 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
568 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
570 $str.=" <tr>".
571 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
572 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
573 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
574 "</tr>";
575 }
577 $str.= " <tr>".
578 " <td colspan=2></td><td>".
579 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
580 " </td>".
581 " </tr>".
582 "</table>";
583 return($str);
584 }
586 /* This function generates a select box out of $this->RecordTypes options.
587 The Parameter $selected is used to predefine an attribute.
588 $name is used to specify a post name
589 */
590 function generateRecordListBox($selected,$name)
591 {
592 $str = "<select name='".$name."' id='".$name."'>";
593 foreach($this->RecordTypes as $type => $value){
595 if(preg_match("/^mXRecord$/i",$value)) continue;
597 $use = "";
598 if($type == $selected){
599 $use = " selected ";
600 }
601 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
602 }
603 $str.="</select>";
604 return($str);
605 }
606 }
608 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
609 ?>