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