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 $this->dialog = false;
236 }
237 }
239 /* Cancel Zone Entrie Edit Dialog
240 */
241 if(isset($_POST['CancelZoneEntryChanges'])){
242 $this->dialog = false;
243 }
245 /* Display any type of open dialogs
246 */
247 if($this->dialog){
248 $this->dialog->save_object();
249 return($this->dialog->execute());
250 }
252 $once =true;
253 foreach($_POST as $name => $value){
254 if((preg_match("/^MXup_/",$name)) && ($once)){
255 $once = false;
257 $id = preg_replace("/^MXup_/","",$name);
258 $id = preg_replace("/_.*$/","",$id);
259 $id = base64_decode($id);
261 $this->mXRecords = $this->ArrayUp(($id+1),$this->mXRecords);
262 }
263 if((preg_match("/^MXdown_/",$name)) && ($once)){
264 $once = false;
266 $id = preg_replace("/^MXdown_/","",$name);
267 $id = preg_replace("/_.*$/","",$id);
268 $id = base64_decode($id);
270 $this->mXRecords = $this->ArrayDown(($id+1),$this->mXRecords);
271 }
272 if((preg_match("/^MXdel_/",$name)) && ($once)){
273 $once = false;
275 $id = preg_replace("/^MXdel_/","",$name);
276 $id = preg_replace("/_.*$/","",$id);
277 $id = base64_decode($id);
279 unset($this->mXRecords[$id]);
281 $tmp =array();
282 foreach($this->mXRecords as $entry){
283 $tmp[] = $entry;
284 }
286 $this->mXRecords = $tmp;
287 }
288 }
290 if((isset($_POST['AddMXRecord'])) && (!empty($_POST['StrMXRecord']))){
291 $this->mXRecords[] = array("type"=>"mXRecord","value"=>trim($_POST['StrMXRecord']));
292 }
294 /* Handle Post events */
295 $once = true;
296 foreach($_POST as $name => $value){
298 /* Delete record if requested */
299 if((preg_match("/RemoveRecord_/",$name))&&($once)){
300 $once = false;
301 $id= preg_replace("/RemoveRecord_/","",$name);
302 unset($this->Records[$id]);
303 }
304 }
306 /* Add new Zonerecord */
307 if(isset($_POST['AddNewRecord'])){
308 $this->Records[] = array("type"=>"aRecord","value"=>"");
309 }
311 /* Fill in values */
312 foreach($this->attributes as $name){
313 $smarty->assign($name,$this->$name);
314 }
317 $div = new DivSelectBox("MxRecords");
318 $div->setHeight(120);
319 $recs = $this->mXRecords;
321 $oneup = "<input name='MXup_%s' type='image' src='images/sort_up.png' title='"._("Up")."' class='center'> ";
322 $onedown = "<input name='MXdown_%s' type='image' src='images/sort_down.png' title='"._("Down")."' class='center'> ";
323 $onedel = "<img src='images/empty.png' width='20' class='center'>
324 <input name='MXdel_%s' type='image' src='images/edittrash.png' title='"._("Delete")."' class='center'>";
326 foreach($recs as $key => $rec){
327 $div ->AddEntry(array(
328 array("string"=>$rec['value']),
329 /* array("string"=>$key,
330 "attach"=>"style='width:20px;'"),*/
331 array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
332 "attach"=>"style='width:70px;border-right:0px;'")
333 ));
334 }
336 /* Assign records list */
337 $smarty->assign("NotNew", false);
338 $smarty->assign("Mxrecords", $div->DrawList());
339 $smarty->assign("records" , $this->generateRecordsList());
340 $smarty->assign("NetworkClass", $this->NetworkClass);
341 $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)"));
343 /* Display tempalte */
344 $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
345 return($display);
346 }
348 function remove_from_parent()
349 {
350 }
352 /* Save data to object */
353 function save_object()
354 {
355 //plugin::save_object();
356 foreach($this->attributes as $attr){
357 if(isset($_POST[$attr])){
358 $this->$attr = $_POST[$attr];
359 }
360 }
362 foreach($this->Records as $id => $value){
363 if(isset($_POST['RecordTypeSelectedFor_'.$id])){
364 $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
365 }
366 if(isset($_POST['RecordValue_'.$id])){
367 $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
368 }
369 }
371 if(isset($_POST['NetworkClass'])){
372 $this->NetworkClass = $_POST['NetworkClass'];
373 }
375 }
378 /* Check supplied data */
379 function check()
380 {
381 /* Call common method to give check the hook */
382 $message= plugin::check();
384 /* Check if zoneName is already in use */
385 $usedZones = $this->getUsedZoneNames();
386 if(($this->isNew == true)||($this->zoneName != $this->InitialzoneName)||($this->ReverseZone != $this->InitialReverseZone)){
387 /* if((isset($usedZones[$this->zoneName]))&&($this->zoneName != $this->InitialzoneName)){
388 $message[] =_("This zoneName is already in use");
389 }
390 if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitialReverseZone)){
391 $message[] =_("This reverse zone is already in use");
392 }*/
393 }
395 if(empty($this->zoneName)){
396 $message[] =sprintf(_("Please choose a valid zone name."));
397 }
399 if(empty($this->ReverseZone)){
400 $message[] =sprintf(_("Please choose a valid reverse zone name."));
401 }
403 if($this->zoneName != strtolower($this->zoneName)){
404 $message[] = _("Only lowercase strings are allowed as zone name.");
405 }
407 if(!is_numeric($this->sOAserial)){
408 $message[] = _("Please specify a numeric value for serial number.");
409 }
411 if(!is_numeric($this->sOArefresh)){
412 $message[] = _("Please specify a numeric value for refresh.");
413 }
415 if(!is_numeric($this->sOAttl)){
416 $message[] = _("Please specify a numeric value for ttl.");
417 }
419 if(!is_numeric($this->sOAexpire)){
420 $message[] = _("Please specify a numeric value for expire.");
421 }
423 if(!is_numeric($this->sOAretry)){
424 $message[] = _("Please specify a numeric value for retry.");
425 }
427 foreach($this->Records as $name => $values){
428 /* only lower-case is allowed in record entries ... */
429 if($values['value'] != strtolower($values['value'])){
430 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
431 }
432 }
434 /* Check class for given Zone Address */
435 $addr = preg_replace("/^[^\/]*+\//","",$this->ReverseZone);
437 /* Check for valid&complete IP address */
438 if(!is_ip($addr)){
439 $message[] = _("The given network address is not a valid, please specify a valid IP address.");
440 }
442 /* Check if given address matches selected network class */
443 switch($this->NetworkClass){
444 case 'A': {
445 if(!preg_match("/^[0-9]*\.0\.0\.0$/",$addr)){
446 $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.0.0.0"));
447 }
448 }
449 break;
450 case 'B': {
451 if(!preg_match("/^[0-9]*\.[0-9]*\.0\.0$/",$addr)){
452 $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.x.0.0"));
453 }
454 }
455 break;
456 case 'C': {
457 if(!preg_match("/^[0-9]*\.[0-9]*\.[0-9]*\.0$/",$addr)){
458 $message[] = sprintf(_("The specified network address is not matching with the specified zone class, try it this way x.x.x.0"));
459 }
460 }
461 break;
462 default : $message[] =sprintf(_("The given network class '%s' is not valid."),$this->NetworkClass);
463 }
465 return ($message);
466 }
468 /* This funtion returns all used Zonenames */
469 function getUsedZoneNames()
470 {
471 $ret = array();
472 $ldap = $this->config->get_ldap_link();
473 $ldap->cd($this->config->current['BASE']);
474 $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
475 while($attr = $ldap->fetch()){
476 if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
477 if(isset($attr['tXTRecord'][0])){
478 $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
479 $ret[$zn] =FlipIp(preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]));
480 }
481 }else{
482 $ret[$attr['zoneName'][0]]="";
483 }
484 }
485 return($ret);
486 }
488 /* Save to LDAP */
489 function save()
490 {
491 $ret =array();
492 foreach($this->attributes as $name){
493 $ret[$name] = $this->$name;
494 }
496 /* Create mx records
497 */
498 foreach($this->mXRecords as $key => $rec){
499 $rec['value']= $key." ".$rec['value'];
500 $this->Records [] = $rec;
501 }
504 $ret['RECORDS'] = $this->Records;
506 switch($this->NetworkClass){
507 case 'C' : $ret['ReverseZone']= preg_replace("/\.[0-9]*$/","",$this->ReverseZone);break;
508 case 'B' : $ret['ReverseZone']= preg_replace("/\.[0-9]*\.[0-9]*$/","",$this->ReverseZone);break;
509 case 'A' : $ret['ReverseZone']= preg_replace("/\.[0-9]*\.[0-9]*\.[0-9]*$/","",$this->ReverseZone);break;
510 default : trigger_error("Invalid network class given '".$this->NetworkClass."'");
511 }
513 $ret['InitialReverseZone']= $this->InitialReverseZone;
514 $ret['InitialzoneName'] = $this->InitialzoneName;
516 $ret['sOAmail'] = preg_replace("/\@/",".",$this->sOAmail);
518 foreach(array("sOAprimary","zoneName","sOAmail") as $attr){
519 if(!preg_match("/\.$/",$ret[$attr])){
520 if(!is_ip($ret[$attr])){
521 $ret[$attr] = $ret[$attr].".";
522 }
523 }
524 }
526 $ret['RECORDS'][] = array("type" => "nSRecord","value" => $ret['sOAprimary']) ;
527 return($ret);
528 }
531 /* This function generate a table row for each used record.
532 This table row displays the recordtype in a select box
533 and the specified value for the record, and a remove button.
534 The last element of the table also got an 'add' button.
535 */
536 function generateRecordsList($changeStateForRecords="")
537 {
538 $changeStateForRecords = "";
540 $str = "<table summary=''>";
541 foreach($this->Records as $key => $entry){
543 if($entry['type'] == "mXRecord") continue;
545 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
546 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
547 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
549 $str.=" <tr>".
550 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
551 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
552 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
553 "</tr>";
554 }
556 $str.= " <tr>".
557 " <td colspan=2></td><td>".
558 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
559 " </td>".
560 " </tr>".
561 "</table>";
562 return($str);
563 }
565 /* This function generates a select box out of $this->RecordTypes options.
566 The Parameter $selected is used to predefine an attribute.
567 $name is used to specify a post name
568 */
569 function generateRecordListBox($selected,$name)
570 {
571 $str = "<select name='".$name."' id='".$name."'>";
572 foreach($this->RecordTypes as $type => $value){
574 if(preg_match("/^mXRecord$/i",$value)) continue;
576 $use = "";
577 if($type == $selected){
578 $use = " selected ";
579 }
580 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
581 }
582 $str.="</select>";
583 return($str);
584 }
585 }
587 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
588 ?>