d563fc30256bedcf818f08f4ec2031733426e4fa
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[0];
70 $tmp2[$tmp[1]] = $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 }
83 $str = date("Ymd");
84 if(preg_match("/^".$str."/",$this->sOAserial)){
85 $this->sOAserial = $this->sOAserial + 1;
86 }else{
87 $this->sOAserial = date("Ymd")."01";
88 }
89 }
90 }
92 /* TRansports the geiven Arraykey one position up*/
93 function ArrayUp($atr,$attrs)
94 {
95 $ret = $attrs;
96 $pos = $atr ;
97 $cn = count($attrs);
98 if(!(($pos == -1)||($pos == 1)||($pos >$cn))){
99 $before = array_slice($attrs,0,($pos-2));
100 $mitte = array_reverse(array_slice($attrs,($pos-2),2));
101 $unten = array_slice($attrs,$pos);
102 $ret = array();
103 $ret = $this->combineArrays($before,$mitte,$unten);
104 }
105 return($ret);
106 }
109 /* TRansports the geiven Arraykey one position up*/
110 function ArrayDown($atr,$attrs)
111 {
112 $ret = $attrs;
113 $pos = $atr ;
114 $cn = count($attrs);
115 if(!(($pos == -1)||($pos == $cn))){
116 $before = array_slice($attrs,0,($pos-1));
117 $mitte = array_reverse(array_slice($attrs,($pos-1),2));
118 $unten = array_slice($attrs,($pos+1));
119 $ret = array();
120 $ret = $this->combineArrays($before,$mitte,$unten);
121 }
122 return($ret);
123 }
125 /* Combine new array */
126 function combineArrays($ar0,$ar1,$ar2)
127 {
128 $ret = array();
129 if(is_array($ar0))
130 foreach($ar0 as $ar => $a){
131 $ret[]=$a;
132 }
133 if(is_array($ar1))
134 foreach($ar1 as $ar => $a){
135 $ret[]=$a;
136 }
137 if(is_array($ar2))
138 foreach($ar2 as $ar => $a){
139 $ret[]=$a;
140 }
141 return($ret);
142 }
144 function getpos($atr,$attrs)
145 {
146 $i = 0;
147 foreach($attrs as $attr => $name) {
148 $i++;
149 if($attr == $atr){
150 return($i);
151 }
152 }
153 return(-1);
154 }
157 function execute()
158 {
159 /* Call parent execute */
160 plugin::execute();
163 /* Fill templating stuff */
164 $smarty= get_smarty();
165 $display= "";
168 /* Open Zone Entry Edit Dialog
169 */
170 if(isset($_POST['EditZoneEntries'])){
171 $this->dialog= new servDNSeditZoneEntries($this->config,$this->dn,$this->zoneName,$this->FlipIp($this->ReverseZone),$this->RecordTypes);
172 }
174 /* Save Zone Entry Edit Dialog
175 */
176 if(isset($_POST['SaveZoneEntryChanges'])){
177 $this->dialog->save_object();
178 if(count($this->dialog->check())){
179 $msgs = $this->dialog->check();
180 foreach($msgs as $msg){
181 print_red($msg);
182 }
183 }else{
184 $this->dialog->save();
185 $this->dialog = false;
186 }
187 }
189 /* Cancel Zone Entrie Edit Dialog
190 */
191 if(isset($_POST['CancelZoneEntryChanges'])){
192 $this->dialog = false;
193 }
195 /* Display any type of open dialogs
196 */
197 if($this->dialog){
198 $this->dialog->save_object();
199 return($this->dialog->execute());
200 }
202 $once =true;
203 foreach($_POST as $name => $value){
204 if((preg_match("/^MXup_/",$name)) && ($once)){
205 $once = false;
207 $id = preg_replace("/^MXup_/","",$name);
208 $id = preg_replace("/_.*$/","",$id);
209 $id = base64_decode($id);
211 $this->mXRecords = $this->ArrayUp(($id+1),$this->mXRecords);
212 }
213 if((preg_match("/^MXdown_/",$name)) && ($once)){
214 $once = false;
216 $id = preg_replace("/^MXdown_/","",$name);
217 $id = preg_replace("/_.*$/","",$id);
218 $id = base64_decode($id);
220 $this->mXRecords = $this->ArrayDown(($id+1),$this->mXRecords);
221 }
222 if((preg_match("/^MXdel_/",$name)) && ($once)){
223 $once = false;
225 $id = preg_replace("/^MXdel_/","",$name);
226 $id = preg_replace("/_.*$/","",$id);
227 $id = base64_decode($id);
229 unset($this->mXRecords[$id]);
231 $tmp =array();
232 foreach($this->mXRecords as $entry){
233 $tmp[] = $entry;
234 }
236 $this->mXRecords = $tmp;
237 }
238 }
240 if((isset($_POST['AddMXRecord'])) && (!empty($_POST['StrMXRecord']))){
241 $this->mXRecords[] = array("type"=>"mXRecord","inittype"=>"","value"=>trim($_POST['StrMXRecord']),"status"=>"new");
242 }
244 /* Handle Post events */
245 $once = true;
246 foreach($_POST as $name => $value){
248 /* Delete record if requested */
249 if((preg_match("/RemoveRecord_/",$name))&&($once)){
250 $once = false;
251 $id= preg_replace("/RemoveRecord_/","",$name);
252 if($this->Records[$id]['status']!= "new"){
253 $this->Records[$id]['status']= "deleted";
254 }else{
255 unset($this->Records[$id]);
256 }
257 }
258 }
260 /* Add new Zonerecord */
261 if(isset($_POST['AddNewRecord'])){
262 $this->Records[] = array("type"=>"aRecord","inittype"=>"","value"=>"","status"=>"new");
263 }
265 /* Fill in values */
266 foreach($this->attributes as $name){
267 $smarty->assign($name,$this->$name);
268 }
271 $div = new DivSelectBox("MxRecords");
272 $div->setHeight(120);
273 $recs = $this->mXRecords;
275 $oneup = "<input name='MXup_%s' type='image' src='images/sort_up.png' title='"._("Up")."' class='center'> ";
276 $onedown = "<input name='MXdown_%s' type='image' src='images/sort_down.png' title='"._("Down")."' class='center'> ";
277 $onedel = "<img src='images/empty.png' width='20' class='center'>
278 <input name='MXdel_%s' type='image' src='images/edittrash.png' title='"._("Delete")."' class='center'>";
280 foreach($recs as $key => $rec){
281 $div ->AddEntry(array(
282 array("string"=>$rec['value']),
283 /* array("string"=>$key,
284 "attach"=>"style='width:20px;'"),*/
285 array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
286 "attach"=>"style='width:70px;border-right:0px;'")
287 ));
288 }
290 /* Assign records list */
293 if($this->dn != "new"){
294 $smarty->assign("NotNew", true);
295 }else{
296 $smarty->assign("NotNew", false);
297 }
299 $smarty->assign("Mxrecords", $div->DrawList());
300 $smarty->assign("records" , $this->generateRecordsList());
302 /* Display tempalte */
303 $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
304 return($display);
305 }
307 function remove_from_parent()
308 {
309 }
311 /* Save data to object */
312 function save_object()
313 {
314 //plugin::save_object();
315 foreach($this->attributes as $attr){
316 if(isset($_POST[$attr])){
317 $this->$attr = $_POST[$attr];
318 }
319 }
321 foreach($this->Records as $id => $value){
322 if(isset($_POST['RecordTypeSelectedFor_'.$id])){
323 $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
324 }
325 if(isset($_POST['RecordValue_'.$id])){
326 $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
327 }
328 }
329 }
332 /* Check supplied data */
333 function check()
334 {
335 $message= array();
336 /* Check if zoneName is already in use */
337 $usedZones = $this->getUsedZoneNames();
338 if(($this->isNew == true)||($this->zoneName != $this->InitiallyZoneName)||($this->ReverseZone != $this->InitiallyReverseZone)){
339 if((isset($usedZones[$this->zoneName]))&&($this->zoneName != $this->InitiallyZoneName)){
340 $message[] =_("This zoneName is already in use");
341 }
342 if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitiallyReverseZone)){
343 $message[] =_("This reverse zone is already in use");
344 }
345 }
347 if(!preg_match("/\.$/",$this->sOAprimary)){
348 $message[] = _("Primary dns server must end with '.' to be a valid entry.");
349 }
351 if(!preg_match("/\.$/",$this->sOAmail)){
352 $message[] = _("Your specified mail address must end with '.' to be a valid 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(preg_match("/@/",$this->sOAmail)){
360 $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
361 }
363 if($this->zoneName != strtolower($this->zoneName)){
364 $message[] = _("Only lowercase strings are allowed as zone name.");
365 }
367 if(!is_numeric($this->sOAserial)){
368 $message[] = _("Please specify a numeric value for serial number.");
369 }
371 if(!is_numeric($this->sOArefresh)){
372 $message[] = _("Please specify a numeric value for refresh.");
373 }
375 if(!is_numeric($this->sOAttl)){
376 $message[] = _("Please specify a numeric value for ttl.");
377 }
379 if(!is_numeric($this->sOAexpire)){
380 $message[] = _("Please specify a numeric value for expire.");
381 }
383 if(!is_numeric($this->sOAretry)){
384 $message[] = _("Please specify a numeric value for retry.");
385 }
387 foreach($this->Records as $name => $values){
388 /* only lower-case is allowed in record entries ... */
389 if($values['value'] != strtolower($values['value'])){
390 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
391 }
392 }
393 return ($message);
394 }
396 /* This funtion returns all used Zonenames */
397 function getUsedZoneNames()
398 {
399 $ret = array();
400 $ldap = $this->config->get_ldap_link();
401 $ldap->cd($this->config->current['BASE']);
402 $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
403 while($attr = $ldap->fetch()){
404 if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
405 if(isset($attr['tXTRecord'][0])){
406 $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
407 $ret[$zn] = $this->FlipIp(preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]));
408 }
409 }else{
410 $ret[$attr['zoneName'][0]]="";
411 }
412 }
413 return($ret);
414 }
416 /* this is used to flip the ip address for example
417 12.3.45 -> 54.3.12
418 Because some entries (like zones) are store like that 54.3.12.in-addr.arpa
419 but we want to display 12.3.45.
420 */
421 function FlipIp($ip)
422 {
423 $tmp = array_reverse(split("\.",$ip));
424 $new = "";
425 foreach($tmp as $section){
426 $new .= $section.".";
427 }
428 return(preg_replace("/.$/","",$new));
429 }
432 /* Save to LDAP */
433 function save()
434 {
435 $ret =array();
436 foreach($this->attributes as $name){
437 $ret[$name] = $this->$name;
438 }
440 foreach($this->mXRecords as $key => $rec){
441 $rec['value'].= " ".$key;
442 $this->Records [] = $rec;
443 }
445 $ret['Records'] = $this->Records;
447 return($ret);
448 }
451 /* This function generate a table row for each used record.
452 This table row displays the recordtype in a select box
453 and the specified value for the record, and a remove button.
454 The last element of the table also got an 'add' button.
455 */
456 function generateRecordsList($changeStateForRecords="")
457 {
458 $changeStateForRecords = "";
460 $str = "<table summary=''>";
461 foreach($this->Records as $key => $entry){
463 if($entry['type'] == "mXRecord") continue;
465 if($entry['status'] == "deleted") continue;
467 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
468 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
469 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
471 $str.=" <tr>".
472 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
473 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
474 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
475 "</tr>";
476 }
478 $str.= " <tr>".
479 " <td colspan=2></td><td>".
480 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
481 " </td>".
482 " </tr>".
483 "</table>";
484 return($str);
485 }
487 /* This function generates a select box out of $this->RecordTypes options.
488 The Parameter $selected is used to predefine an attribute.
489 $name is used to specify a post name
490 */
491 function generateRecordListBox($selected,$name)
492 {
493 $str = "<select name='".$name."' id='".$name."'>";
494 foreach($this->RecordTypes as $type => $value){
496 if(preg_match("/^mXRecord$/i",$value)) continue;
498 $use = "";
499 if($type == $selected){
500 $use = " selected ";
501 }
502 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
503 }
504 $str.="</select>";
505 return($str);
506 }
507 }
509 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
510 ?>