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