2ab894396a8d66bfd86ade6d1b46c51cf0f3f481
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 $ReverseDN = "";
20 var $zoneName = "";
21 var $dNSClass = "IN";
22 var $status = "new";
24 var $sOAprimary = "";
25 var $sOAmail = "";
26 var $sOAserial = "";
27 var $sOArefresh = "3600";
28 var $sOAretry = "1800";
29 var $sOAexpire = "720000";
30 var $sOAttl = "6400";
32 var $Records = array();
33 var $mXRecords = array();
35 var $InitiallyZoneName = "";
36 var $InitiallyReverseZone = "";
37 var $isNew = true;
39 var $dialog = false;
41 function servdnseditZone ($config, $dn= NULL,$recordtypes,$attrs = array())
42 {
43 plugin::plugin ($config, $dn);
45 /* All types with required attrs */
46 $this->RecordTypes = $recordtypes;
48 if(!count($attrs)){
49 $this->InitiallyZoneName = "";
50 $this->InitiallyReverseZone = "";
51 $this->isNew = true;
52 $this->sOAserial = date("Ymd")."1";
53 }else{
54 $this->InitiallyZoneName = $attrs['zoneName'];
55 $this->InitiallyReverseZone = $attrs['ReverseZone'];
56 $this->isNew = false;
58 foreach($this->attributes as $value){
59 $this->$value = $attrs[$value];
60 }
61 if(isset($attrs['Records'])){
62 $this->Records = $attrs['Records'];
64 $tmp2 = array();
65 $usedPrio = array();
66 foreach($this->Records as $key => $rec){
67 if($rec['type'] == "mXRecord"){
68 $tmp = split(" ",$rec['value']);
69 $rec['value'] = $tmp[1];
70 $tmp2[$tmp[0]] = $rec;
71 unset($this->Records[$key]);
72 }
73 }
74 if(count($tmp2) != 0){
75 reset($tmp2);
76 ksort($tmp2);
77 }
78 $this->mXRecords = $tmp2;
79 }else{
80 $this->mXRecords = array();
81 $this->Records = array();
82 }
84 $str = date("Ymd");
85 if(preg_match("/^".$str."/",$this->sOAserial)){
86 $this->sOAserial = $this->sOAserial + 1;
87 }else{
88 $this->sOAserial = date("Ymd")."01";
89 }
90 }
91 }
93 /* TRansports the geiven Arraykey one position up*/
94 function ArrayUp($atr,$attrs)
95 {
96 $ret = $attrs;
97 $pos = $atr ;
98 $cn = count($attrs);
99 if(!(($pos == -1)||($pos == 1)||($pos >$cn))){
100 $before = array_slice($attrs,0,($pos-2));
101 $mitte = array_reverse(array_slice($attrs,($pos-2),2));
102 $unten = array_slice($attrs,$pos);
103 $ret = array();
104 $ret = $this->combineArrays($before,$mitte,$unten);
105 }
106 return($ret);
107 }
110 /* TRansports the geiven Arraykey one position up*/
111 function ArrayDown($atr,$attrs)
112 {
113 $ret = $attrs;
114 $pos = $atr ;
115 $cn = count($attrs);
116 if(!(($pos == -1)||($pos == $cn))){
117 $before = array_slice($attrs,0,($pos-1));
118 $mitte = array_reverse(array_slice($attrs,($pos-1),2));
119 $unten = array_slice($attrs,($pos+1));
120 $ret = array();
121 $ret = $this->combineArrays($before,$mitte,$unten);
122 }
123 return($ret);
124 }
126 /* Combine new array */
127 function combineArrays($ar0,$ar1,$ar2)
128 {
129 $ret = array();
130 if(is_array($ar0))
131 foreach($ar0 as $ar => $a){
132 $ret[]=$a;
133 }
134 if(is_array($ar1))
135 foreach($ar1 as $ar => $a){
136 $ret[]=$a;
137 }
138 if(is_array($ar2))
139 foreach($ar2 as $ar => $a){
140 $ret[]=$a;
141 }
142 return($ret);
143 }
145 function getpos($atr,$attrs)
146 {
147 $i = 0;
148 foreach($attrs as $attr => $name) {
149 $i++;
150 if($attr == $atr){
151 return($i);
152 }
153 }
154 return(-1);
155 }
158 function execute()
159 {
160 /* Call parent execute */
161 plugin::execute();
164 /* Fill templating stuff */
165 $smarty= get_smarty();
166 $display= "";
169 /* Open Zone Entry Edit Dialog
170 */
171 if(isset($_POST['EditZoneEntries'])){
172 $this->dialog= new servDNSeditZoneEntries($this->config,$this->dn,$this->InitiallyZoneName,$this->FlipIp($this->InitiallyReverseZone),$this->RecordTypes);
173 }
175 /* Save Zone Entry Edit Dialog
176 */
177 if(isset($_POST['SaveZoneEntryChanges'])){
178 $this->dialog->save_object();
179 if(count($this->dialog->check())){
180 $msgs = $this->dialog->check();
181 foreach($msgs as $msg){
182 print_red($msg);
183 }
184 }else{
185 $this->dialog->save();
186 $this->dialog = false;
187 }
188 }
190 /* Cancel Zone Entrie Edit Dialog
191 */
192 if(isset($_POST['CancelZoneEntryChanges'])){
193 $this->dialog = false;
194 }
196 /* Display any type of open dialogs
197 */
198 if($this->dialog){
199 $this->dialog->save_object();
200 return($this->dialog->execute());
201 }
203 $once =true;
204 foreach($_POST as $name => $value){
205 if((preg_match("/^MXup_/",$name)) && ($once)){
206 $once = false;
208 $id = preg_replace("/^MXup_/","",$name);
209 $id = preg_replace("/_.*$/","",$id);
210 $id = base64_decode($id);
212 $this->mXRecords = $this->ArrayUp(($id+1),$this->mXRecords);
213 }
214 if((preg_match("/^MXdown_/",$name)) && ($once)){
215 $once = false;
217 $id = preg_replace("/^MXdown_/","",$name);
218 $id = preg_replace("/_.*$/","",$id);
219 $id = base64_decode($id);
221 $this->mXRecords = $this->ArrayDown(($id+1),$this->mXRecords);
222 }
223 if((preg_match("/^MXdel_/",$name)) && ($once)){
224 $once = false;
226 $id = preg_replace("/^MXdel_/","",$name);
227 $id = preg_replace("/_.*$/","",$id);
228 $id = base64_decode($id);
230 unset($this->mXRecords[$id]);
232 $tmp =array();
233 foreach($this->mXRecords as $entry){
234 $tmp[] = $entry;
235 }
237 $this->mXRecords = $tmp;
238 }
239 }
241 if((isset($_POST['AddMXRecord'])) && (!empty($_POST['StrMXRecord']))){
242 $this->mXRecords[] = array("type"=>"mXRecord","inittype"=>"","value"=>trim($_POST['StrMXRecord']),"status"=>"new");
243 }
245 /* Handle Post events */
246 $once = true;
247 foreach($_POST as $name => $value){
249 /* Delete record if requested */
250 if((preg_match("/RemoveRecord_/",$name))&&($once)){
251 $once = false;
252 $id= preg_replace("/RemoveRecord_/","",$name);
253 if($this->Records[$id]['status']!= "new"){
254 $this->Records[$id]['status']= "deleted";
255 }else{
256 unset($this->Records[$id]);
257 }
258 }
259 }
261 /* Add new Zonerecord */
262 if(isset($_POST['AddNewRecord'])){
263 $this->Records[] = array("type"=>"aRecord","inittype"=>"","value"=>"","status"=>"new");
264 }
266 /* Fill in values */
267 foreach($this->attributes as $name){
268 $smarty->assign($name,$this->$name);
269 }
272 $div = new DivSelectBox("MxRecords");
273 $div->setHeight(120);
274 $recs = $this->mXRecords;
276 $oneup = "<input name='MXup_%s' type='image' src='images/sort_up.png' title='"._("Up")."' class='center'> ";
277 $onedown = "<input name='MXdown_%s' type='image' src='images/sort_down.png' title='"._("Down")."' class='center'> ";
278 $onedel = "<img src='images/empty.png' width='20' class='center'>
279 <input name='MXdel_%s' type='image' src='images/edittrash.png' title='"._("Delete")."' class='center'>";
281 foreach($recs as $key => $rec){
282 $div ->AddEntry(array(
283 array("string"=>$rec['value']),
284 /* array("string"=>$key,
285 "attach"=>"style='width:20px;'"),*/
286 array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
287 "attach"=>"style='width:70px;border-right:0px;'")
288 ));
289 }
291 /* Assign records list */
293 $smarty->assign("NotNew", false);
295 $smarty->assign("Mxrecords", $div->DrawList());
296 $smarty->assign("records" , $this->generateRecordsList());
298 /* Display tempalte */
299 $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
300 return($display);
301 }
303 function remove_from_parent()
304 {
305 }
307 /* Save data to object */
308 function save_object()
309 {
310 //plugin::save_object();
311 foreach($this->attributes as $attr){
312 if(isset($_POST[$attr])){
313 $this->$attr = $_POST[$attr];
314 }
315 }
317 foreach($this->Records as $id => $value){
318 if(isset($_POST['RecordTypeSelectedFor_'.$id])){
319 $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
320 }
321 if(isset($_POST['RecordValue_'.$id])){
322 $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
323 }
324 }
325 }
328 /* Check supplied data */
329 function check()
330 {
331 $message= array();
332 /* Check if zoneName is already in use */
333 $usedZones = $this->getUsedZoneNames();
334 if(($this->isNew == true)||($this->zoneName != $this->InitiallyZoneName)||($this->ReverseZone != $this->InitiallyReverseZone)){
335 if((isset($usedZones[$this->zoneName]))&&($this->zoneName != $this->InitiallyZoneName)){
336 $message[] =_("This zoneName is already in use");
337 }
338 if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitiallyReverseZone)){
339 $message[] =_("This reverse zone is already in use");
340 }
341 }
343 if(!preg_match("/\.$/",$this->sOAprimary)){
344 $message[] = _("Primary dns server must end with '.' to be a valid entry.");
345 }
347 if(!preg_match("/\.$/",$this->sOAmail)){
348 $message[] = _("Your specified mail address must end with '.' to be a valid record.");
349 }
351 if(preg_match("/@/",$this->sOAmail)){
352 $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
353 }
355 if(preg_match("/@/",$this->sOAmail)){
356 $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
357 }
359 if($this->zoneName != strtolower($this->zoneName)){
360 $message[] = _("Only lowercase strings are allowed as zone name.");
361 }
363 if(!is_numeric($this->sOAserial)){
364 $message[] = _("Please specify a numeric value for serial number.");
365 }
367 if(!is_numeric($this->sOArefresh)){
368 $message[] = _("Please specify a numeric value for refresh.");
369 }
371 if(!is_numeric($this->sOAttl)){
372 $message[] = _("Please specify a numeric value for ttl.");
373 }
375 if(!is_numeric($this->sOAexpire)){
376 $message[] = _("Please specify a numeric value for expire.");
377 }
379 if(!is_numeric($this->sOAretry)){
380 $message[] = _("Please specify a numeric value for retry.");
381 }
383 foreach($this->Records as $name => $values){
384 /* only lower-case is allowed in record entries ... */
385 if($values['value'] != strtolower($values['value'])){
386 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
387 }
388 }
389 return ($message);
390 }
392 /* This funtion returns all used Zonenames */
393 function getUsedZoneNames()
394 {
395 $ret = array();
396 $ldap = $this->config->get_ldap_link();
397 $ldap->cd($this->config->current['BASE']);
398 $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
399 while($attr = $ldap->fetch()){
400 if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
401 if(isset($attr['tXTRecord'][0])){
402 $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
403 $ret[$zn] = $this->FlipIp(preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]));
404 }
405 }else{
406 $ret[$attr['zoneName'][0]]="";
407 }
408 }
409 return($ret);
410 }
412 /* this is used to flip the ip address for example
413 12.3.45 -> 54.3.12
414 Because some entries (like zones) are store like that 54.3.12.in-addr.arpa
415 but we want to display 12.3.45.
416 */
417 function FlipIp($ip)
418 {
419 $tmp = array_reverse(split("\.",$ip));
420 $new = "";
421 foreach($tmp as $section){
422 $new .= $section.".";
423 }
424 return(preg_replace("/.$/","",$new));
425 }
428 /* Save to LDAP */
429 function save()
430 {
431 $ret =array();
432 foreach($this->attributes as $name){
433 $ret[$name] = $this->$name;
434 }
436 /* Create mx records
437 */
438 foreach($this->mXRecords as $key => $rec){
439 $rec['value']= $key." ".$rec['value'];
440 $this->Records [] = $rec;
441 }
443 $ret['Records'] = $this->Records;
445 return($ret);
446 }
449 /* This function generate a table row for each used record.
450 This table row displays the recordtype in a select box
451 and the specified value for the record, and a remove button.
452 The last element of the table also got an 'add' button.
453 */
454 function generateRecordsList($changeStateForRecords="")
455 {
456 $changeStateForRecords = "";
458 $str = "<table summary=''>";
459 foreach($this->Records as $key => $entry){
461 if($entry['type'] == "mXRecord") continue;
463 if($entry['status'] == "deleted") continue;
465 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
466 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
467 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
469 $str.=" <tr>".
470 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
471 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
472 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
473 "</tr>";
474 }
476 $str.= " <tr>".
477 " <td colspan=2></td><td>".
478 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
479 " </td>".
480 " </tr>".
481 "</table>";
482 return($str);
483 }
485 /* This function generates a select box out of $this->RecordTypes options.
486 The Parameter $selected is used to predefine an attribute.
487 $name is used to specify a post name
488 */
489 function generateRecordListBox($selected,$name)
490 {
491 $str = "<select name='".$name."' id='".$name."'>";
492 foreach($this->RecordTypes as $type => $value){
494 if(preg_match("/^mXRecord$/i",$value)) continue;
496 $use = "";
497 if($type == $selected){
498 $use = " selected ";
499 }
500 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
501 }
502 $str.="</select>";
503 return($str);
504 }
505 }
507 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
508 ?>