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