1a37e952fe84dde944e97eaef87ae8d6db0f3c95
1 <?php
3 class servdnseditZone extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account= TRUE;
7 var $attributes = array("zoneName","ReverseZone","dNSClass",
8 "sOAprimary","sOAmail","sOAserial","sOArefresh","sOAretry","sOAexpire","sOAttl");
9 var $objectclasses = array("whatever");
11 var $RecordTypes = array();
13 var $ReverseZone = "";
14 var $zoneName = "";
15 var $dNSClass = "IN";
17 var $sOAprimary = "";
18 var $sOAmail = "";
19 var $sOAserial = "";
20 var $sOArefresh = "3600";
21 var $sOAretry = "1800";
22 var $sOAexpire = "720000";
23 var $sOAttl = "6400";
25 var $Records = array();
26 var $mXRecords = array();
28 var $OldZoneName = ""; // To detect changes made with this edit
29 var $OldReverseZone = "";
31 var $InitialReverseZone = "";
32 var $InitialzoneName = "";
33 var $NetworkClass = "A" ; // One out of A,B,C
35 var $dialog = false;
37 var $zoneEditor = NULL;
39 var $isNew = true;
41 var $ZoneObject = array();
42 var $Zone_is_used = FALSE;
44 function servdnseditZone (&$config, $dn= NULL,$attrs = array())
45 {
46 plugin::plugin ($config, $dn);
48 /* All types with required attrs */
49 $this->RecordTypes = DNS::getDnsRecordTypes(true);
51 if(!count($attrs)){
52 $this->OldZoneName = "";
53 $this->OldReverseZone = "";
54 $this->isNew = true;
55 $this->sOAserial = date("Ymd")."1";
57 $this->InitialzoneName = "";//$attrs['InitialzoneName'];
58 $this->InitialReverseZone = "";//$attrs['InitialReverseZone'];
59 }else{
60 $this->ZoneObject = $attrs;
62 if(isset($attrs['zoneEditor'])){
63 $this->zoneEditor = $attrs['zoneEditor'];
64 }
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" && preg_match("/".normalizePreg($this->sOAprimary)."/",$rec['value'])){
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 }
115 /* Check if this used, in this case disable forward and reverse configuration */
116 $tmp = $this->getUsedZoneNames();
117 $this->Zone_is_used = FALSE;
118 if(isset($tmp[DNS::FlipIp($this->InitialReverseZone).".in-addr.arpa"])){
119 $this->Zone_is_used = TRUE;
120 }
121 }
123 /* Detect Network class */
124 if(!empty($this->ReverseZone)){
126 $dots = count(split("\.",$this->ReverseZone));
127 if($dots == 1){
128 $this->NetworkClass = "A";
129 $this->ReverseZone .= ".0.0.0";
130 }elseif($dots == 2){
131 $this->NetworkClass = "B";
132 $this->ReverseZone .= ".0.0";
133 }else{
134 $this->NetworkClass = "C";
135 $this->ReverseZone .= ".0";
136 }
137 }
138 }
140 /* TRansports the geiven Arraykey one position up*/
141 function ArrayUp($atr,$attrs)
142 {
143 $ret = $attrs;
144 $pos = $atr ;
145 $cn = count($attrs);
146 if(!(($pos == -1)||($pos == 1)||($pos >$cn))){
147 $before = array_slice($attrs,0,($pos-2));
148 $mitte = array_reverse(array_slice($attrs,($pos-2),2));
149 $unten = array_slice($attrs,$pos);
150 $ret = array();
151 $ret = $this->combineArrays($before,$mitte,$unten);
152 }
153 return($ret);
154 }
157 /* TRansports the geiven Arraykey one position up*/
158 function ArrayDown($atr,$attrs)
159 {
160 $ret = $attrs;
161 $pos = $atr ;
162 $cn = count($attrs);
163 if(!(($pos == -1)||($pos == $cn))){
164 $before = array_slice($attrs,0,($pos-1));
165 $mitte = array_reverse(array_slice($attrs,($pos-1),2));
166 $unten = array_slice($attrs,($pos+1));
167 $ret = array();
168 $ret = $this->combineArrays($before,$mitte,$unten);
169 }
170 return($ret);
171 }
173 /* Combine new array */
174 function combineArrays($ar0,$ar1,$ar2)
175 {
176 $ret = array();
177 if(is_array($ar0))
178 foreach($ar0 as $ar => $a){
179 $ret[]=$a;
180 }
181 if(is_array($ar1))
182 foreach($ar1 as $ar => $a){
183 $ret[]=$a;
184 }
185 if(is_array($ar2))
186 foreach($ar2 as $ar => $a){
187 $ret[]=$a;
188 }
189 return($ret);
190 }
192 function getpos($atr,$attrs)
193 {
194 $i = 0;
195 foreach($attrs as $attr => $name) {
196 $i++;
197 if($attr == $atr){
198 return($i);
199 }
200 }
201 return(-1);
202 }
205 function execute()
206 {
207 /* Call parent execute */
208 plugin::execute();
210 /* Fill templating stuff */
211 $smarty= get_smarty();
212 $smarty->assign("Zone_is_used",$this->Zone_is_used);
213 $ui = get_userinfo();
215 $smarty->assign("ACLs",$this->parent->getacl(""));
216 $display= "";
218 /* Open Zone Entry Edit Dialog
219 */
220 if(!count($this->ZoneObject)){
221 $smarty->assign("AllowZoneEdit" , false);
222 }else{
223 $smarty->assign("AllowZoneEdit" , true);
224 if(isset($_POST['EditZoneEntries'])){
225 if($this->zoneEditor == NULL){
226 $this->zoneEditor= new servDNSeditZoneEntries($this->config,$this->dn,$this->ZoneObject);
227 $this->zoneEditor->parent = $this;
228 }
229 $this->dialog = $this->zoneEditor;
230 }
231 }
233 /* Save Zone Entry Edit Dialog
234 */
235 if(isset($_POST['SaveZoneEntryChanges'])){
236 $this->dialog->save_object();
237 if(count($this->dialog->check())){
238 $msgs = $this->dialog->check();
239 foreach($msgs as $msg){
240 msg_dialog::display(_("Error"), $msg , ERROR_DIALOG);
241 }
242 }else{
243 $this->zoneEditor = clone $this->dialog;
244 $this->dialog = FALSE;
245 # $rev = DNS::FlipIp(DNS::getNameFromMix($this->InitialReverseZone)).".in-addr.arpa";
246 # $for = DNS::getNameFromMix($this->InitialzoneName);
247 #
248 # $this->parent->handle_post_events("modify",array("dn" => $this->dn,"zoneName" => $rev));
249 # $this->parent->handle_post_events("modify",array("dn" => $this->dn,"zoneName" => $for));
250 # $this->dialog = false;
251 }
252 }
254 /* Cancel Zone Entrie Edit Dialog
255 */
256 if(isset($_POST['CancelZoneEntryChanges'])){
257 $this->dialog = false;
258 }
260 /* Display any type of open dialogs
261 */
262 if(is_object($this->dialog)){
263 $this->dialog->save_object();
264 return($this->dialog->execute());
265 }
267 $once =true;
268 foreach($_POST as $name => $value){
269 if((preg_match("/^MXup_/",$name)) && ($once)){
270 $once = false;
272 $id = preg_replace("/^MXup_/","",$name);
273 $id = preg_replace("/_.*$/","",$id);
274 $id = base64_decode($id);
276 $this->mXRecords = $this->ArrayUp(($id+1),$this->mXRecords);
277 }
278 if((preg_match("/^MXdown_/",$name)) && ($once)){
279 $once = false;
281 $id = preg_replace("/^MXdown_/","",$name);
282 $id = preg_replace("/_.*$/","",$id);
283 $id = base64_decode($id);
285 $this->mXRecords = $this->ArrayDown(($id+1),$this->mXRecords);
286 }
287 if((preg_match("/^MXdel_/",$name)) && ($once)){
288 $once = false;
290 $id = preg_replace("/^MXdel_/","",$name);
291 $id = preg_replace("/_.*$/","",$id);
292 $id = base64_decode($id);
294 unset($this->mXRecords[$id]);
296 $tmp =array();
297 foreach($this->mXRecords as $entry){
298 $tmp[] = $entry;
299 }
301 $this->mXRecords = $tmp;
302 }
303 }
305 if((isset($_POST['AddMXRecord'])) && (!empty($_POST['StrMXRecord']))){
306 $this->mXRecords[] = array("type"=>"mXRecord","value"=>trim($_POST['StrMXRecord']));
307 }
309 /* Handle Post events */
310 $once = true;
311 foreach($_POST as $name => $value){
313 /* Delete record if requested */
314 if((preg_match("/RemoveRecord_/",$name))&&($once)){
315 $once = false;
316 $id= preg_replace("/RemoveRecord_/","",$name);
317 unset($this->Records[$id]);
318 }
319 }
321 /* Add new Zonerecord */
322 if(isset($_POST['AddNewRecord'])){
323 $this->Records[] = array("type"=>"aRecord","value"=>"");
324 }
326 /* Fill in values */
327 foreach($this->attributes as $name){
328 $smarty->assign($name,$this->$name);
329 }
332 $div = new divSelectBox("MxRecords");
333 $div->setHeight(120);
334 $recs = $this->mXRecords;
336 $oneup = "<input name='MXup_%s' type='image' src='images/sort_up.png' title='"._("Up")."' class='center'> ";
337 $onedown = "<input name='MXdown_%s' type='image' src='images/sort_down.png' title='"._("Down")."' class='center'> ";
338 $onedel = "<img src='images/empty.png' width='20' class='center'>
339 <input name='MXdel_%s' type='image' src='images/lists/trash.png' title='"._("Delete")."' class='center'>";
341 foreach($recs as $key => $rec){
342 $div ->AddEntry(array(
343 array("string"=>$rec['value']),
344 /* array("string"=>$key,
345 "attach"=>"style='width:20px;'"),*/
346 array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
347 "attach"=>"style='width:70px;border-right:0px;'")
348 ));
349 }
351 /* Assign records list */
352 $smarty->assign("NotNew", false);
353 $smarty->assign("Mxrecords", $div->DrawList());
354 $smarty->assign("records" , $this->generateRecordsList());
355 $smarty->assign("NetworkClass", $this->NetworkClass);
356 $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)"));
358 /* Display tempalte */
359 $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE, dirname(__FILE__)));
360 return($display);
361 }
363 function remove_from_parent()
364 {
365 }
367 /* Save data to object */
368 function save_object()
369 {
370 //plugin::save_object();
371 foreach($this->attributes as $attr){
373 if($this->Zone_is_used && in_array($attr,array("ReverseZone","zoneName"))){
374 continue;
375 }
377 if(isset($_POST[$attr])){
378 $this->$attr = $_POST[$attr];
379 }
380 }
382 foreach($this->Records as $id => $value){
383 if(isset($_POST['RecordTypeSelectedFor_'.$id])){
384 $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
385 }
386 if(isset($_POST['RecordValue_'.$id])){
387 $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
388 }
389 }
391 if(isset($_POST['NetworkClass']) && !$this->Zone_is_used){
392 $this->NetworkClass = $_POST['NetworkClass'];
393 }
395 }
398 /* Check supplied data */
399 function check()
400 {
401 /* Call common method to give check the hook */
402 $message= plugin::check();
404 /* Check if zoneName is already in use */
405 $usedZones = $this->getUsedZoneNames();
407 if(empty($this->zoneName)){
408 $message[] = msgPool::required(_("Zone name"));
409 }
411 if(empty($this->ReverseZone)){
412 $message[] = msgPool::required(_("Reverse zone"));
413 }
415 if($this->zoneName != strtolower($this->zoneName)){
416 $message[] = msgPool::invalid(_("Zone name"),"","",_("Only lowercase allowed"));
417 }
419 if(!is_numeric($this->sOAserial)){
420 $message[] = msgPool::invalid(_("Serial"),$this->sOAserial,"/[0-9]/");
421 }
423 if(!is_numeric($this->sOArefresh)){
424 $message[] = msgPool::invalid(_("Refresh"),$this->sOArefresh,"/[0-9]/");
425 }
427 if(!is_numeric($this->sOAttl)){
428 $message[] = msgPool::invalid(_("Time to life"),$this->sOAttl,"/[0-9]/");
429 }
431 if(!is_numeric($this->sOAexpire)){
432 $message[] = msgPool::invalid(_("Expire"),$this->sOAexpire,"/[0-9]/");
433 }
435 if(!is_numeric($this->sOAretry)){
436 $message[] = msgPool::invalid(_("Retry"),$this->sOAretry,"/[0-9]/");
437 }
439 foreach($this->Records as $name => $values){
440 /* only lower-case is allowed in record entries ... */
441 if($values['value'] != strtolower($values['value'])){
442 $message[] = msgPool::invalid($values['type'],"","",_("Only lowercase allowed"));
443 }
444 }
446 /* Check class for given Zone Address */
447 $addr = preg_replace("/^[^\/]*+\//","",$this->ReverseZone);
449 /* Check for valid&complete IP address */
450 if(!tests::is_ip($addr)){
451 $message[] = msgPool::invalid(_("Network address"));
452 }
454 /* Check if given address matches selected network class */
455 switch($this->NetworkClass){
456 case 'A': {
457 if(!preg_match("/^[0-9]*\.0\.0\.0$/",$addr)){
458 $message[] = sprintf(_("The specified network address and the network class (%s/%s) do not match!"), $this->NetworkClass, "255.0.0.0");
459 }
460 }
461 break;
462 case 'B': {
463 if(!preg_match("/^[0-9]*\.[0-9]*\.0\.0$/",$addr)){
464 $message[] = sprintf(_("The specified network address and the network class (%s/%s) do not match!"), $this->NetworkClass, "255.255.0.0");
465 }
466 }
467 break;
468 case 'C': {
469 if(!preg_match("/^[0-9]*\.[0-9]*\.[0-9]*\.0$/",$addr)){
470 $message[] = sprintf(_("The specified network address and the network class (%s/%s) do not match!"), $this->NetworkClass, "255.255.255.0");
471 }
472 }
473 break;
474 default : $message[] = msgPool::invalid(_("Network class"),$this->NetworkClass);
475 }
477 return ($message);
478 }
481 /* This funtion returns all used Zonenames
482 */
483 function getUsedZoneNames()
484 {
485 $ret = array();
486 $ldap = $this->config->get_ldap_link();
487 $ldap->cd($this->config->current['BASE']);
488 $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
489 while($attr = $ldap->fetch()){
490 $ret[$attr['zoneName'][0]][] = $attr['dn'];
491 }
492 return($ret);
493 }
496 /* Save to LDAP */
497 function save()
498 {
499 $ret =array();
500 foreach($this->attributes as $name){
501 $ret[$name] = $this->$name;
502 }
504 /* Create mx records
505 */
506 foreach($this->mXRecords as $key => $rec){
507 $rec['value']= $key." ".$rec['value'];
508 $this->Records [] = $rec;
509 }
512 $ret['RECORDS'] = $this->Records;
514 switch($this->NetworkClass){
515 case 'C' : $ret['ReverseZone']= preg_replace("/\.[0-9]*$/","",$this->ReverseZone);break;
516 case 'B' : $ret['ReverseZone']= preg_replace("/\.[0-9]*\.[0-9]*$/","",$this->ReverseZone);break;
517 case 'A' : $ret['ReverseZone']= preg_replace("/\.[0-9]*\.[0-9]*\.[0-9]*$/","",$this->ReverseZone);break;
518 default : trigger_error("Invalid network class given '".$this->NetworkClass."'");
519 }
521 $ret['InitialReverseZone']= $this->InitialReverseZone;
522 $ret['InitialzoneName'] = $this->InitialzoneName;
524 $ret['sOAmail'] = preg_replace("/\@/",".",$this->sOAmail);
526 foreach(array("sOAprimary","zoneName","sOAmail") as $attr){
527 if(!preg_match("/\.$/",$ret[$attr])){
528 if(!tests::is_ip($ret[$attr])){
529 $ret[$attr] = $ret[$attr].".";
530 }
531 }
532 }
534 $ret['RECORDS'][] = array("type" => "nSRecord","value" => $ret['sOAprimary']) ;
536 $ret['zoneEditor'] = $this->zoneEditor;
537 return($ret);
538 }
541 /* This function generate a table row for each used record.
542 This table row displays the recordtype in a select box
543 and the specified value for the record, and a remove button.
544 The last element of the table also got an 'add' button.
545 */
546 function generateRecordsList($changeStateForRecords="")
547 {
548 $changeStateForRecords = "";
550 $str = "<table summary=''>";
551 foreach($this->Records as $key => $entry){
553 if($entry['type'] == "mXRecord") continue;
555 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
556 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
557 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
559 $str.=" <tr>".
560 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
561 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
562 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
563 "</tr>";
564 }
566 $str.= " <tr>".
567 " <td colspan=2></td><td>".
568 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
569 " </td>".
570 " </tr>".
571 "</table>";
572 return($str);
573 }
575 /* This function generates a select box out of $this->RecordTypes options.
576 The Parameter $selected is used to predefine an attribute.
577 $name is used to specify a post name
578 */
579 function generateRecordListBox($selected,$name)
580 {
581 $str = "<select name='".$name."' id='".$name."'>";
582 foreach($this->RecordTypes as $type => $value){
584 if(preg_match("/^mXRecord$/i",$value)) continue;
586 $use = "";
587 if($type == $selected){
588 $use = " selected ";
589 }
590 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
591 }
592 $str.="</select>";
593 return($str);
594 }
597 }
598 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
599 ?>