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 $isNew = true;
38 var $cn;
39 var $ZoneObject = array();
41 function servdnseditZone ($config, $dn= NULL,$attrs = array())
42 {
43 plugin::plugin ($config, $dn);
45 /* All types with required attrs */
46 $this->RecordTypes = getDnsRecordTypes(true);
48 if(!count($attrs)){
49 $this->OldZoneName = "";
50 $this->OldReverseZone = "";
51 $this->isNew = true;
52 $this->sOAserial = date("Ymd")."1";
54 $this->InitialzoneName = "";//$attrs['InitialzoneName'];
55 $this->InitialReverseZone = "";//$attrs['InitialReverseZone'];
56 }else{
57 $this->ZoneObject = $attrs;
60 $this->OldZoneName = $attrs['zoneName'];
61 $this->OldReverseZone = $attrs['ReverseZone'];
63 $this->InitialzoneName = $attrs['InitialzoneName'];
64 $this->InitialReverseZone = $attrs['InitialReverseZone'];
66 $this->isNew = false;
68 foreach($this->attributes as $value){
69 if(isset($attrs[$value])){
70 $this->$value = $attrs[$value];
71 }
72 }
74 $this->sOAmail = preg_replace("/\./","@",$this->sOAmail,1);
75 $this->sOAmail = preg_replace("/\.$/","",$this->sOAmail);
76 $this->sOAprimary = preg_replace("/\.$/","",$this->sOAprimary);
77 $this->zoneName = preg_replace("/\.$/","",$this->zoneName);
79 if(isset($attrs['RECORDS'])){
80 $this->Records = $attrs['RECORDS'];
82 $tmp2 = array();
83 $usedPrio = array();
84 foreach($this->Records as $key => $rec){
85 if($rec['type'] == "mXRecord"){
86 $tmp = split(" ",$rec['value']);
87 $rec['value'] = $tmp[1];
88 $tmp2[$tmp[0]] = $rec;
89 unset($this->Records[$key]);
90 }
91 if($rec['type'] == "nSRecord"){
92 unset($this->Records[$key]);
93 }
94 }
95 if(count($tmp2) != 0){
96 reset($tmp2);
97 ksort($tmp2);
98 }
99 $this->mXRecords = $tmp2;
100 }else{
101 $this->mXRecords = array();
102 $this->Records = array();
103 }
105 $str = date("Ymd");
106 if(preg_match("/^".$str."/",$this->sOAserial)){
107 $this->sOAserial = $this->sOAserial + 1;
108 }else{
109 $this->sOAserial = date("Ymd")."01";
110 }
111 }
113 /* Detect Network class */
114 if(!empty($this->ReverseZone)){
116 $dots = count(split(".",$this->ReverseZone));
117 if($dots == 0){
118 $this->NetworkClass = "A";
119 $this->ReverseZone .= ".0.0.0";
120 }elseif($dots == 1){
121 $this->NetworkClass = "B";
122 $this->ReverseZone .= ".0.0";
123 }else{
124 $this->NetworkClass = "C";
125 $this->ReverseZone .= ".0";
126 }
127 }
128 }
130 /* TRansports the geiven Arraykey one position up*/
131 function ArrayUp($atr,$attrs)
132 {
133 $ret = $attrs;
134 $pos = $atr ;
135 $cn = count($attrs);
136 if(!(($pos == -1)||($pos == 1)||($pos >$cn))){
137 $before = array_slice($attrs,0,($pos-2));
138 $mitte = array_reverse(array_slice($attrs,($pos-2),2));
139 $unten = array_slice($attrs,$pos);
140 $ret = array();
141 $ret = $this->combineArrays($before,$mitte,$unten);
142 }
143 return($ret);
144 }
147 /* TRansports the geiven Arraykey one position up*/
148 function ArrayDown($atr,$attrs)
149 {
150 $ret = $attrs;
151 $pos = $atr ;
152 $cn = count($attrs);
153 if(!(($pos == -1)||($pos == $cn))){
154 $before = array_slice($attrs,0,($pos-1));
155 $mitte = array_reverse(array_slice($attrs,($pos-1),2));
156 $unten = array_slice($attrs,($pos+1));
157 $ret = array();
158 $ret = $this->combineArrays($before,$mitte,$unten);
159 }
160 return($ret);
161 }
163 /* Combine new array */
164 function combineArrays($ar0,$ar1,$ar2)
165 {
166 $ret = array();
167 if(is_array($ar0))
168 foreach($ar0 as $ar => $a){
169 $ret[]=$a;
170 }
171 if(is_array($ar1))
172 foreach($ar1 as $ar => $a){
173 $ret[]=$a;
174 }
175 if(is_array($ar2))
176 foreach($ar2 as $ar => $a){
177 $ret[]=$a;
178 }
179 return($ret);
180 }
182 function getpos($atr,$attrs)
183 {
184 $i = 0;
185 foreach($attrs as $attr => $name) {
186 $i++;
187 if($attr == $atr){
188 return($i);
189 }
190 }
191 return(-1);
192 }
195 function execute()
196 {
197 /* Call parent execute */
198 plugin::execute();
201 /* Fill templating stuff */
202 $smarty= get_smarty();
203 $display= "";
205 /* Open Zone Entry Edit Dialog
206 */
207 if(!count($this->ZoneObject)){
208 $smarty->assign("AllowZoneEdit" , false);
209 }else{
210 $smarty->assign("AllowZoneEdit" , true);
211 if(isset($_POST['EditZoneEntries'])){
212 $this->dialog= new servDNSeditZoneEntries($this->config,$this->dn,$this->ZoneObject);
213 $this->dialog->parent = $this;
214 }
215 }
217 /* Save Zone Entry Edit Dialog
218 */
219 if(isset($_POST['SaveZoneEntryChanges'])){
220 $this->dialog->save_object();
221 if(count($this->dialog->check())){
222 $msgs = $this->dialog->check();
223 foreach($msgs as $msg){
224 print_red($msg);
225 }
226 }else{
227 $this->dialog->save();
228 $this->dialog = false;
229 }
230 }
232 /* Cancel Zone Entrie Edit Dialog
233 */
234 if(isset($_POST['CancelZoneEntryChanges'])){
235 $this->dialog = false;
236 }
238 /* Display any type of open dialogs
239 */
240 if($this->dialog){
241 $this->dialog->save_object();
242 return($this->dialog->execute());
243 }
245 $once =true;
246 foreach($_POST as $name => $value){
247 if((preg_match("/^MXup_/",$name)) && ($once)){
248 $once = false;
250 $id = preg_replace("/^MXup_/","",$name);
251 $id = preg_replace("/_.*$/","",$id);
252 $id = base64_decode($id);
254 $this->mXRecords = $this->ArrayUp(($id+1),$this->mXRecords);
255 }
256 if((preg_match("/^MXdown_/",$name)) && ($once)){
257 $once = false;
259 $id = preg_replace("/^MXdown_/","",$name);
260 $id = preg_replace("/_.*$/","",$id);
261 $id = base64_decode($id);
263 $this->mXRecords = $this->ArrayDown(($id+1),$this->mXRecords);
264 }
265 if((preg_match("/^MXdel_/",$name)) && ($once)){
266 $once = false;
268 $id = preg_replace("/^MXdel_/","",$name);
269 $id = preg_replace("/_.*$/","",$id);
270 $id = base64_decode($id);
272 unset($this->mXRecords[$id]);
274 $tmp =array();
275 foreach($this->mXRecords as $entry){
276 $tmp[] = $entry;
277 }
279 $this->mXRecords = $tmp;
280 }
281 }
283 if((isset($_POST['AddMXRecord'])) && (!empty($_POST['StrMXRecord']))){
284 $this->mXRecords[] = array("type"=>"mXRecord","value"=>trim($_POST['StrMXRecord']));
285 }
287 /* Handle Post events */
288 $once = true;
289 foreach($_POST as $name => $value){
291 /* Delete record if requested */
292 if((preg_match("/RemoveRecord_/",$name))&&($once)){
293 $once = false;
294 $id= preg_replace("/RemoveRecord_/","",$name);
295 unset($this->Records[$id]);
296 }
297 }
299 /* Add new Zonerecord */
300 if(isset($_POST['AddNewRecord'])){
301 $this->Records[] = array("type"=>"aRecord","value"=>"");
302 }
304 /* Fill in values */
305 foreach($this->attributes as $name){
306 $smarty->assign($name,$this->$name);
307 }
309 /* Set zoneNames without server suffix */
310 foreach(array("zoneName","ReverseZone") as $attr){
311 $smarty->assign($attr,getNameFromMix($this->$attr));
312 }
314 $div = new DivSelectBox("MxRecords");
315 $div->setHeight(120);
316 $recs = $this->mXRecords;
318 $oneup = "<input name='MXup_%s' type='image' src='images/sort_up.png' title='"._("Up")."' class='center'> ";
319 $onedown = "<input name='MXdown_%s' type='image' src='images/sort_down.png' title='"._("Down")."' class='center'> ";
320 $onedel = "<img src='images/empty.png' width='20' class='center'>
321 <input name='MXdel_%s' type='image' src='images/edittrash.png' title='"._("Delete")."' class='center'>";
323 foreach($recs as $key => $rec){
324 $div ->AddEntry(array(
325 array("string"=>$rec['value']),
326 /* array("string"=>$key,
327 "attach"=>"style='width:20px;'"),*/
328 array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
329 "attach"=>"style='width:70px;border-right:0px;'")
330 ));
331 }
333 /* Assign records list */
334 $smarty->assign("NotNew", false);
335 $smarty->assign("Mxrecords", $div->DrawList());
336 $smarty->assign("records" , $this->generateRecordsList());
337 $smarty->assign("NetworkClass", $this->NetworkClass);
338 $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)"));
340 /* Display tempalte */
341 $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
342 return($display);
343 }
345 function remove_from_parent()
346 {
347 }
349 /* Save data to object */
350 function save_object()
351 {
352 //plugin::save_object();
353 foreach($this->attributes as $attr){
354 if(isset($_POST[$attr])){
355 $this->$attr = $_POST[$attr];
356 }
357 }
359 if(isset($_POST['NetworkClass'])){
360 $this->NetworkClass = $_POST['NetworkClass'];
361 }
363 foreach(array("zoneName","ReverseZone") as $attr){
364 if(isset($_POST[$attr])){
365 $this->$attr = strtoupper($this->cn)."/".$_POST[$attr];
366 }
367 }
369 foreach($this->Records as $id => $value){
370 if(isset($_POST['RecordTypeSelectedFor_'.$id])){
371 $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
372 }
373 if(isset($_POST['RecordValue_'.$id])){
374 $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
375 }
376 }
377 }
380 /* Check supplied data */
381 function check()
382 {
383 /* Call common method to give check the hook */
384 $message= plugin::check();
386 /* Check if zoneName is already in use */
387 $usedZones = $this->getUsedZoneNames();
388 if(($this->isNew == true)||($this->zoneName != $this->InitialzoneName)||($this->ReverseZone != $this->InitialReverseZone)){
389 /* if((isset($usedZones[$this->zoneName]))&&($this->zoneName != $this->InitialzoneName)){
390 $message[] =_("This zoneName is already in use");
391 }
392 if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitialReverseZone)){
393 $message[] =_("This reverse zone is already in use");
394 }*/
395 }
397 if(empty($this->zoneName)){
398 $message[] =sprintf(_("Please choose a valid zone name."));
399 }
401 if(empty($this->ReverseZone)){
402 $message[] =sprintf(_("Please choose a valid reverse zone name."));
403 }
405 if(getNameFromMix($this->zoneName) != strtolower(getNameFromMix($this->zoneName))){
406 $message[] = _("Only lowercase strings are allowed as zone name.");
407 }
409 if(!is_numeric($this->sOAserial)){
410 $message[] = _("Please specify a numeric value for serial number.");
411 }
413 if(!is_numeric($this->sOArefresh)){
414 $message[] = _("Please specify a numeric value for refresh.");
415 }
417 if(!is_numeric($this->sOAttl)){
418 $message[] = _("Please specify a numeric value for ttl.");
419 }
421 if(!is_numeric($this->sOAexpire)){
422 $message[] = _("Please specify a numeric value for expire.");
423 }
425 if(!is_numeric($this->sOAretry)){
426 $message[] = _("Please specify a numeric value for retry.");
427 }
429 foreach($this->Records as $name => $values){
430 /* only lower-case is allowed in record entries ... */
431 if($values['value'] != strtolower($values['value'])){
432 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
433 }
434 }
436 /* Check class for given Zone Address */
437 $addr = preg_replace("/^[^\/]*+\//","",$this->ReverseZone);
439 /* Check for valid&complete IP address */
440 if(!is_ip($addr)){
441 $message[] = _("The given network address is not a valid, please specify a valid IP address.");
442 }
444 /* Check if given address matches selected network class */
445 switch($this->NetworkClass){
446 case 'A': {
447 if(!preg_match("/^[0-9]*\.0\.0\.0$/",$addr)){
448 $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.0.0.0"));
449 }
450 }
451 break;
452 case 'B': {
453 if(!preg_match("/^[0-9]*\.[0-9]*\.0\.0$/",$addr)){
454 $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.x.0.0"));
455 }
456 }
457 break;
458 case 'C': {
459 if(!preg_match("/^[0-9]*\.[0-9]*\.[0-9]*\.0$/",$addr)){
460 $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.x.x.0"));
461 }
462 }
463 break;
464 default : $message[] =sprintf(_("The given network class '%s' is not valid."),$this->NetworkClass);
465 }
467 return ($message);
468 }
470 /* This funtion returns all used Zonenames */
471 function getUsedZoneNames()
472 {
473 $ret = array();
474 $ldap = $this->config->get_ldap_link();
475 $ldap->cd($this->config->current['BASE']);
476 $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
477 while($attr = $ldap->fetch()){
478 if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
479 if(isset($attr['tXTRecord'][0])){
480 $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
481 $ret[$zn] =FlipIp(preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]));
482 }
483 }else{
484 $ret[$attr['zoneName'][0]]="";
485 }
486 }
487 return($ret);
488 }
490 /* Save to LDAP */
491 function save()
492 {
493 $ret =array();
494 foreach($this->attributes as $name){
495 $ret[$name] = $this->$name;
496 }
498 /* Create mx records
499 */
500 foreach($this->mXRecords as $key => $rec){
501 $rec['value']= $key." ".$rec['value'];
502 $this->Records [] = $rec;
503 }
506 $ret['RECORDS'] = $this->Records;
508 switch($this->NetworkClass){
509 case 'C' : $ret['ReverseZone']= preg_replace("/\.[0-9]*$/","",$this->ReverseZone);break;
510 case 'B' : $ret['ReverseZone']= preg_replace("/\.[0-9]*\.[0-9]*$/","",$this->ReverseZone);break;
511 case 'A' : $ret['ReverseZone']= preg_replace("/\.[0-9]*\.[0-9]*\.[0-9]*$/","",$this->ReverseZone);break;
512 default : trigger_error("Invalid network class given '".$this->NetworkClass."'");
513 }
515 $ret['InitialReverseZone']= $this->InitialReverseZone;
516 $ret['InitialzoneName'] = $this->InitialzoneName;
518 $ret['sOAmail'] = preg_replace("/\@/",".",$this->sOAmail);
520 foreach(array("sOAprimary","zoneName","sOAmail") as $attr){
521 if(!preg_match("/\.$/",$ret[$attr])){
522 if(!is_ip($ret[$attr])){
523 $ret[$attr] = $ret[$attr].".";
524 }
525 }
526 }
528 $ret['RECORDS'][] = array("type" => "nSRecord","value" => $ret['sOAprimary']) ;
529 return($ret);
530 }
533 /* This function generate a table row for each used record.
534 This table row displays the recordtype in a select box
535 and the specified value for the record, and a remove button.
536 The last element of the table also got an 'add' button.
537 */
538 function generateRecordsList($changeStateForRecords="")
539 {
540 $changeStateForRecords = "";
542 $str = "<table summary=''>";
543 foreach($this->Records as $key => $entry){
545 if($entry['type'] == "mXRecord") continue;
547 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
548 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
549 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
551 $str.=" <tr>".
552 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
553 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
554 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
555 "</tr>";
556 }
558 $str.= " <tr>".
559 " <td colspan=2></td><td>".
560 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
561 " </td>".
562 " </tr>".
563 "</table>";
564 return($str);
565 }
567 /* This function generates a select box out of $this->RecordTypes options.
568 The Parameter $selected is used to predefine an attribute.
569 $name is used to specify a post name
570 */
571 function generateRecordListBox($selected,$name)
572 {
573 $str = "<select name='".$name."' id='".$name."'>";
574 foreach($this->RecordTypes as $type => $value){
576 if(preg_match("/^mXRecord$/i",$value)) continue;
578 $use = "";
579 if($type == $selected){
580 $use = " selected ";
581 }
582 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
583 }
584 $str.="</select>";
585 return($str);
586 }
587 }
589 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
590 ?>