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();
208 $smarty->assign("ACLs",$ui->get_permissions($this->dn,"server/servdns"));
209 $display= "";
211 /* Open Zone Entry Edit Dialog
212 */
213 if(!count($this->ZoneObject)){
214 $smarty->assign("AllowZoneEdit" , false);
215 }else{
216 $smarty->assign("AllowZoneEdit" , true);
217 if(isset($_POST['EditZoneEntries'])){
218 $this->dialog= new servDNSeditZoneEntries($this->config,$this->dn,$this->ZoneObject);
219 }
220 }
222 /* Save Zone Entry Edit Dialog
223 */
224 if(isset($_POST['SaveZoneEntryChanges'])){
225 $this->dialog->save_object();
226 if(count($this->dialog->check())){
227 $msgs = $this->dialog->check();
228 foreach($msgs as $msg){
229 print_red($msg);
230 }
231 }else{
232 $this->dialog->save();
233 $this->dialog = false;
234 }
235 }
237 /* Cancel Zone Entrie Edit Dialog
238 */
239 if(isset($_POST['CancelZoneEntryChanges'])){
240 $this->dialog = false;
241 }
243 /* Display any type of open dialogs
244 */
245 if($this->dialog){
246 $this->dialog->save_object();
247 return($this->dialog->execute());
248 }
250 $once =true;
251 foreach($_POST as $name => $value){
252 if((preg_match("/^MXup_/",$name)) && ($once)){
253 $once = false;
255 $id = preg_replace("/^MXup_/","",$name);
256 $id = preg_replace("/_.*$/","",$id);
257 $id = base64_decode($id);
259 $this->mXRecords = $this->ArrayUp(($id+1),$this->mXRecords);
260 }
261 if((preg_match("/^MXdown_/",$name)) && ($once)){
262 $once = false;
264 $id = preg_replace("/^MXdown_/","",$name);
265 $id = preg_replace("/_.*$/","",$id);
266 $id = base64_decode($id);
268 $this->mXRecords = $this->ArrayDown(($id+1),$this->mXRecords);
269 }
270 if((preg_match("/^MXdel_/",$name)) && ($once)){
271 $once = false;
273 $id = preg_replace("/^MXdel_/","",$name);
274 $id = preg_replace("/_.*$/","",$id);
275 $id = base64_decode($id);
277 unset($this->mXRecords[$id]);
279 $tmp =array();
280 foreach($this->mXRecords as $entry){
281 $tmp[] = $entry;
282 }
284 $this->mXRecords = $tmp;
285 }
286 }
288 if((isset($_POST['AddMXRecord'])) && (!empty($_POST['StrMXRecord']))){
289 $this->mXRecords[] = array("type"=>"mXRecord","value"=>trim($_POST['StrMXRecord']));
290 }
292 /* Handle Post events */
293 $once = true;
294 foreach($_POST as $name => $value){
296 /* Delete record if requested */
297 if((preg_match("/RemoveRecord_/",$name))&&($once)){
298 $once = false;
299 $id= preg_replace("/RemoveRecord_/","",$name);
300 unset($this->Records[$id]);
301 }
302 }
304 /* Add new Zonerecord */
305 if(isset($_POST['AddNewRecord'])){
306 $this->Records[] = array("type"=>"aRecord","value"=>"");
307 }
309 /* Fill in values */
310 foreach($this->attributes as $name){
311 $smarty->assign($name,$this->$name);
312 }
315 $div = new DivSelectBox("MxRecords");
316 $div->setHeight(120);
317 $recs = $this->mXRecords;
319 $oneup = "<input name='MXup_%s' type='image' src='images/sort_up.png' title='"._("Up")."' class='center'> ";
320 $onedown = "<input name='MXdown_%s' type='image' src='images/sort_down.png' title='"._("Down")."' class='center'> ";
321 $onedel = "<img src='images/empty.png' width='20' class='center'>
322 <input name='MXdel_%s' type='image' src='images/edittrash.png' title='"._("Delete")."' class='center'>";
324 foreach($recs as $key => $rec){
325 $div ->AddEntry(array(
326 array("string"=>$rec['value']),
327 /* array("string"=>$key,
328 "attach"=>"style='width:20px;'"),*/
329 array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
330 "attach"=>"style='width:70px;border-right:0px;'")
331 ));
332 }
334 /* Assign records list */
335 $smarty->assign("NotNew", false);
336 $smarty->assign("Mxrecords", $div->DrawList());
337 $smarty->assign("records" , $this->generateRecordsList());
338 $smarty->assign("NetworkClass", $this->NetworkClass);
339 $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)"));
341 /* Display tempalte */
342 $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
343 return($display);
344 }
346 function remove_from_parent()
347 {
348 }
350 /* Save data to object */
351 function save_object()
352 {
353 //plugin::save_object();
354 foreach($this->attributes as $attr){
355 if(isset($_POST[$attr])){
356 $this->$attr = $_POST[$attr];
357 }
358 }
360 foreach($this->Records as $id => $value){
361 if(isset($_POST['RecordTypeSelectedFor_'.$id])){
362 $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
363 }
364 if(isset($_POST['RecordValue_'.$id])){
365 $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
366 }
367 }
369 if(isset($_POST['NetworkClass'])){
370 $this->NetworkClass = $_POST['NetworkClass'];
371 }
373 }
376 /* Check supplied data */
377 function check()
378 {
379 /* Call common method to give check the hook */
380 $message= plugin::check();
382 /* Check if zoneName is already in use */
383 $usedZones = $this->getUsedZoneNames();
384 if(($this->isNew == true)||($this->zoneName != $this->InitialzoneName)||($this->ReverseZone != $this->InitialReverseZone)){
385 /* if((isset($usedZones[$this->zoneName]))&&($this->zoneName != $this->InitialzoneName)){
386 $message[] =_("This zoneName is already in use");
387 }
388 if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitialReverseZone)){
389 $message[] =_("This reverse zone is already in use");
390 }*/
391 }
393 if(empty($this->zoneName)){
394 $message[] =sprintf(_("Please choose a valid zone name."));
395 }
397 if(empty($this->ReverseZone)){
398 $message[] =sprintf(_("Please choose a valid reverse zone name."));
399 }
401 if($this->zoneName != strtolower($this->zoneName)){
402 $message[] = _("Only lowercase strings are allowed as zone name.");
403 }
405 if(!is_numeric($this->sOAserial)){
406 $message[] = _("Please specify a numeric value for serial number.");
407 }
409 if(!is_numeric($this->sOArefresh)){
410 $message[] = _("Please specify a numeric value for refresh.");
411 }
413 if(!is_numeric($this->sOAttl)){
414 $message[] = _("Please specify a numeric value for ttl.");
415 }
417 if(!is_numeric($this->sOAexpire)){
418 $message[] = _("Please specify a numeric value for expire.");
419 }
421 if(!is_numeric($this->sOAretry)){
422 $message[] = _("Please specify a numeric value for retry.");
423 }
425 foreach($this->Records as $name => $values){
426 /* only lower-case is allowed in record entries ... */
427 if($values['value'] != strtolower($values['value'])){
428 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
429 }
430 }
432 /* Check class for given Zone Address */
433 $addr = preg_replace("/^[^\/]*+\//","",$this->ReverseZone);
435 /* Check for valid&complete IP address */
436 if(!is_ip($addr)){
437 $message[] = _("The given network address is not a valid, please specify a valid IP address.");
438 }
440 /* Check if given address matches selected network class */
441 switch($this->NetworkClass){
442 case 'A': {
443 if(!preg_match("/^[0-9]*\.0\.0\.0$/",$addr)){
444 $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.0.0.0"));
445 }
446 }
447 break;
448 case 'B': {
449 if(!preg_match("/^[0-9]*\.[0-9]*\.0\.0$/",$addr)){
450 $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.x.0.0"));
451 }
452 }
453 break;
454 case 'C': {
455 if(!preg_match("/^[0-9]*\.[0-9]*\.[0-9]*\.0$/",$addr)){
456 $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.x.x.0"));
457 }
458 }
459 break;
460 default : $message[] =sprintf(_("The given network class '%s' is not valid."),$this->NetworkClass);
461 }
463 return ($message);
464 }
466 /* This funtion returns all used Zonenames */
467 function getUsedZoneNames()
468 {
469 $ret = array();
470 $ldap = $this->config->get_ldap_link();
471 $ldap->cd($this->config->current['BASE']);
472 $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
473 while($attr = $ldap->fetch()){
474 if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
475 if(isset($attr['tXTRecord'][0])){
476 $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
477 $ret[$zn] =FlipIp(preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]));
478 }
479 }else{
480 $ret[$attr['zoneName'][0]]="";
481 }
482 }
483 return($ret);
484 }
486 /* Save to LDAP */
487 function save()
488 {
489 $ret =array();
490 foreach($this->attributes as $name){
491 $ret[$name] = $this->$name;
492 }
494 /* Create mx records
495 */
496 foreach($this->mXRecords as $key => $rec){
497 $rec['value']= $key." ".$rec['value'];
498 $this->Records [] = $rec;
499 }
502 $ret['RECORDS'] = $this->Records;
504 switch($this->NetworkClass){
505 case 'C' : $ret['ReverseZone']= preg_replace("/\.[0-9]*$/","",$this->ReverseZone);break;
506 case 'B' : $ret['ReverseZone']= preg_replace("/\.[0-9]*\.[0-9]*$/","",$this->ReverseZone);break;
507 case 'A' : $ret['ReverseZone']= preg_replace("/\.[0-9]*\.[0-9]*\.[0-9]*$/","",$this->ReverseZone);break;
508 default : trigger_error("Invalid network class given '".$this->NetworkClass."'");
509 }
511 $ret['InitialReverseZone']= $this->InitialReverseZone;
512 $ret['InitialzoneName'] = $this->InitialzoneName;
514 $ret['sOAmail'] = preg_replace("/\@/",".",$this->sOAmail);
516 foreach(array("sOAprimary","zoneName","sOAmail") as $attr){
517 if(!preg_match("/\.$/",$ret[$attr])){
518 if(!is_ip($ret[$attr])){
519 $ret[$attr] = $ret[$attr].".";
520 }
521 }
522 }
524 $ret['RECORDS'][] = array("type" => "nSRecord","value" => $ret['sOAprimary']) ;
525 return($ret);
526 }
529 /* This function generate a table row for each used record.
530 This table row displays the recordtype in a select box
531 and the specified value for the record, and a remove button.
532 The last element of the table also got an 'add' button.
533 */
534 function generateRecordsList($changeStateForRecords="")
535 {
536 $changeStateForRecords = "";
538 $str = "<table summary=''>";
539 foreach($this->Records as $key => $entry){
541 if($entry['type'] == "mXRecord") continue;
543 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
544 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
545 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
547 $str.=" <tr>".
548 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
549 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
550 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
551 "</tr>";
552 }
554 $str.= " <tr>".
555 " <td colspan=2></td><td>".
556 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
557 " </td>".
558 " </tr>".
559 "</table>";
560 return($str);
561 }
563 /* This function generates a select box out of $this->RecordTypes options.
564 The Parameter $selected is used to predefine an attribute.
565 $name is used to specify a post name
566 */
567 function generateRecordListBox($selected,$name)
568 {
569 $str = "<select name='".$name."' id='".$name."'>";
570 foreach($this->RecordTypes as $type => $value){
572 if(preg_match("/^mXRecord$/i",$value)) continue;
574 $use = "";
575 if($type == $selected){
576 $use = " selected ";
577 }
578 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
579 }
580 $str.="</select>";
581 return($str);
582 }
583 }
585 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
586 ?>