13ed5678e06b1d4e97a59b2f514d89549f44487c
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 = "";
39 var $dialog = false;
41 var $isNew = true;
43 var $ZoneObject = array();
45 function servdnseditZone ($config, $dn= NULL,$attrs = array())
46 {
47 plugin::plugin ($config, $dn);
49 /* All types with required attrs */
50 $this->RecordTypes = getDnsRecordTypes(true);
52 if(!count($attrs)){
53 $this->OldZoneName = "";
54 $this->OldReverseZone = "";
55 $this->isNew = true;
56 $this->sOAserial = date("Ymd")."1";
58 $this->InitialzoneName = "";//$attrs['InitialzoneName'];
59 $this->InitialReverseZone = "";//$attrs['InitialReverseZone'];
60 }else{
61 $this->ZoneObject = $attrs;
63 $this->OldZoneName = $attrs['zoneName'];
64 $this->OldReverseZone = $attrs['ReverseZone'];
66 $this->InitialzoneName = $attrs['InitialzoneName'];
67 $this->InitialReverseZone = $attrs['InitialReverseZone'];
69 $this->isNew = false;
71 foreach($this->attributes as $value){
72 $this->$value = $attrs[$value];
73 }
74 if(isset($attrs['RECORDS'])){
75 $this->Records = $attrs['RECORDS'];
77 $tmp2 = array();
78 $usedPrio = array();
79 foreach($this->Records as $key => $rec){
80 if($rec['type'] == "mXRecord"){
81 $tmp = split(" ",$rec['value']);
82 $rec['value'] = $tmp[1];
83 $tmp2[$tmp[0]] = $rec;
84 unset($this->Records[$key]);
85 }
86 }
87 if(count($tmp2) != 0){
88 reset($tmp2);
89 ksort($tmp2);
90 }
91 $this->mXRecords = $tmp2;
92 }else{
93 $this->mXRecords = array();
94 $this->Records = array();
95 }
97 $str = date("Ymd");
98 if(preg_match("/^".$str."/",$this->sOAserial)){
99 $this->sOAserial = $this->sOAserial + 1;
100 }else{
101 $this->sOAserial = date("Ymd")."01";
102 }
103 }
104 }
106 /* TRansports the geiven Arraykey one position up*/
107 function ArrayUp($atr,$attrs)
108 {
109 $ret = $attrs;
110 $pos = $atr ;
111 $cn = count($attrs);
112 if(!(($pos == -1)||($pos == 1)||($pos >$cn))){
113 $before = array_slice($attrs,0,($pos-2));
114 $mitte = array_reverse(array_slice($attrs,($pos-2),2));
115 $unten = array_slice($attrs,$pos);
116 $ret = array();
117 $ret = $this->combineArrays($before,$mitte,$unten);
118 }
119 return($ret);
120 }
123 /* TRansports the geiven Arraykey one position up*/
124 function ArrayDown($atr,$attrs)
125 {
126 $ret = $attrs;
127 $pos = $atr ;
128 $cn = count($attrs);
129 if(!(($pos == -1)||($pos == $cn))){
130 $before = array_slice($attrs,0,($pos-1));
131 $mitte = array_reverse(array_slice($attrs,($pos-1),2));
132 $unten = array_slice($attrs,($pos+1));
133 $ret = array();
134 $ret = $this->combineArrays($before,$mitte,$unten);
135 }
136 return($ret);
137 }
139 /* Combine new array */
140 function combineArrays($ar0,$ar1,$ar2)
141 {
142 $ret = array();
143 if(is_array($ar0))
144 foreach($ar0 as $ar => $a){
145 $ret[]=$a;
146 }
147 if(is_array($ar1))
148 foreach($ar1 as $ar => $a){
149 $ret[]=$a;
150 }
151 if(is_array($ar2))
152 foreach($ar2 as $ar => $a){
153 $ret[]=$a;
154 }
155 return($ret);
156 }
158 function getpos($atr,$attrs)
159 {
160 $i = 0;
161 foreach($attrs as $attr => $name) {
162 $i++;
163 if($attr == $atr){
164 return($i);
165 }
166 }
167 return(-1);
168 }
171 function execute()
172 {
173 /* Call parent execute */
174 plugin::execute();
177 /* Fill templating stuff */
178 $smarty= get_smarty();
179 $display= "";
181 /* Open Zone Entry Edit Dialog
182 */
183 if(!count($this->ZoneObject)){
184 $smarty->assign("AllowZoneEdit" , false);
185 }else{
186 $smarty->assign("AllowZoneEdit" , true);
187 if(isset($_POST['EditZoneEntries'])){
188 $this->dialog= new servDNSeditZoneEntries($this->config,$this->dn,$this->ZoneObject);
189 }
190 }
192 /* Save Zone Entry Edit Dialog
193 */
194 if(isset($_POST['SaveZoneEntryChanges'])){
195 $this->dialog->save_object();
196 if(count($this->dialog->check())){
197 $msgs = $this->dialog->check();
198 foreach($msgs as $msg){
199 print_red($msg);
200 }
201 }else{
202 $this->dialog->save();
203 $this->dialog = false;
204 }
205 }
207 /* Cancel Zone Entrie Edit Dialog
208 */
209 if(isset($_POST['CancelZoneEntryChanges'])){
210 $this->dialog = false;
211 }
213 /* Display any type of open dialogs
214 */
215 if($this->dialog){
216 $this->dialog->save_object();
217 return($this->dialog->execute());
218 }
220 $once =true;
221 foreach($_POST as $name => $value){
222 if((preg_match("/^MXup_/",$name)) && ($once)){
223 $once = false;
225 $id = preg_replace("/^MXup_/","",$name);
226 $id = preg_replace("/_.*$/","",$id);
227 $id = base64_decode($id);
229 $this->mXRecords = $this->ArrayUp(($id+1),$this->mXRecords);
230 }
231 if((preg_match("/^MXdown_/",$name)) && ($once)){
232 $once = false;
234 $id = preg_replace("/^MXdown_/","",$name);
235 $id = preg_replace("/_.*$/","",$id);
236 $id = base64_decode($id);
238 $this->mXRecords = $this->ArrayDown(($id+1),$this->mXRecords);
239 }
240 if((preg_match("/^MXdel_/",$name)) && ($once)){
241 $once = false;
243 $id = preg_replace("/^MXdel_/","",$name);
244 $id = preg_replace("/_.*$/","",$id);
245 $id = base64_decode($id);
247 unset($this->mXRecords[$id]);
249 $tmp =array();
250 foreach($this->mXRecords as $entry){
251 $tmp[] = $entry;
252 }
254 $this->mXRecords = $tmp;
255 }
256 }
258 if((isset($_POST['AddMXRecord'])) && (!empty($_POST['StrMXRecord']))){
259 $this->mXRecords[] = array("type"=>"mXRecord","value"=>trim($_POST['StrMXRecord']));
260 }
262 /* Handle Post events */
263 $once = true;
264 foreach($_POST as $name => $value){
266 /* Delete record if requested */
267 if((preg_match("/RemoveRecord_/",$name))&&($once)){
268 $once = false;
269 $id= preg_replace("/RemoveRecord_/","",$name);
270 unset($this->Records[$id]);
271 }
272 }
274 /* Add new Zonerecord */
275 if(isset($_POST['AddNewRecord'])){
276 $this->Records[] = array("type"=>"aRecord","value"=>"");
277 }
279 /* Fill in values */
280 foreach($this->attributes as $name){
281 $smarty->assign($name,$this->$name);
282 }
285 $div = new DivSelectBox("MxRecords");
286 $div->setHeight(120);
287 $recs = $this->mXRecords;
289 $oneup = "<input name='MXup_%s' type='image' src='images/sort_up.png' title='"._("Up")."' class='center'> ";
290 $onedown = "<input name='MXdown_%s' type='image' src='images/sort_down.png' title='"._("Down")."' class='center'> ";
291 $onedel = "<img src='images/empty.png' width='20' class='center'>
292 <input name='MXdel_%s' type='image' src='images/edittrash.png' title='"._("Delete")."' class='center'>";
294 foreach($recs as $key => $rec){
295 $div ->AddEntry(array(
296 array("string"=>$rec['value']),
297 /* array("string"=>$key,
298 "attach"=>"style='width:20px;'"),*/
299 array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
300 "attach"=>"style='width:70px;border-right:0px;'")
301 ));
302 }
304 /* Assign records list */
306 $smarty->assign("NotNew", false);
308 $smarty->assign("Mxrecords", $div->DrawList());
309 $smarty->assign("records" , $this->generateRecordsList());
311 /* Display tempalte */
312 $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
313 return($display);
314 }
316 function remove_from_parent()
317 {
318 }
320 /* Save data to object */
321 function save_object()
322 {
323 //plugin::save_object();
324 foreach($this->attributes as $attr){
325 if(isset($_POST[$attr])){
326 $this->$attr = $_POST[$attr];
327 }
328 }
330 foreach($this->Records as $id => $value){
331 if(isset($_POST['RecordTypeSelectedFor_'.$id])){
332 $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
333 }
334 if(isset($_POST['RecordValue_'.$id])){
335 $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
336 }
337 }
338 }
341 /* Check supplied data */
342 function check()
343 {
344 $message= array();
345 /* Check if zoneName is already in use */
346 $usedZones = $this->getUsedZoneNames();
347 if(($this->isNew == true)||($this->zoneName != $this->InitialzoneName)||($this->ReverseZone != $this->InitialReverseZone)){
348 if((isset($usedZones[$this->zoneName]))&&($this->zoneName != $this->InitialzoneName)){
349 $message[] =_("This zoneName is already in use");
350 }
351 if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitialReverseZone)){
352 $message[] =_("This reverse zone is already in use");
353 }
354 }
356 if(!preg_match("/\.$/",$this->sOAprimary)){
357 $message[] = _("Primary dns server must end with '.' to be a valid entry.");
358 }
360 if(!preg_match("/\.$/",$this->sOAmail)){
361 $message[] = _("Your specified mail address must end with '.' to be a valid record.");
362 }
364 if(preg_match("/@/",$this->sOAmail)){
365 $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
366 }
368 if(preg_match("/@/",$this->sOAmail)){
369 $message[] = _("Your mail address contains '@' replace this with '.' to enable GOsa to create a valid SOA record.");
370 }
372 if($this->zoneName != strtolower($this->zoneName)){
373 $message[] = _("Only lowercase strings are allowed as zone name.");
374 }
376 if(!is_numeric($this->sOAserial)){
377 $message[] = _("Please specify a numeric value for serial number.");
378 }
380 if(!is_numeric($this->sOArefresh)){
381 $message[] = _("Please specify a numeric value for refresh.");
382 }
384 if(!is_numeric($this->sOAttl)){
385 $message[] = _("Please specify a numeric value for ttl.");
386 }
388 if(!is_numeric($this->sOAexpire)){
389 $message[] = _("Please specify a numeric value for expire.");
390 }
392 if(!is_numeric($this->sOAretry)){
393 $message[] = _("Please specify a numeric value for retry.");
394 }
396 foreach($this->Records as $name => $values){
397 /* only lower-case is allowed in record entries ... */
398 if($values['value'] != strtolower($values['value'])){
399 $message[] = sprintf(_("Only lowercase is allowed, please check your '%ss'."),$values['type']);
400 }
401 }
402 return ($message);
403 }
405 /* This funtion returns all used Zonenames */
406 function getUsedZoneNames()
407 {
408 $ret = array();
409 $ldap = $this->config->get_ldap_link();
410 $ldap->cd($this->config->current['BASE']);
411 $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
412 while($attr = $ldap->fetch()){
413 if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
414 if(isset($attr['tXTRecord'][0])){
415 $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
416 $ret[$zn] =FlipIp(preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]));
417 }
418 }else{
419 $ret[$attr['zoneName'][0]]="";
420 }
421 }
422 return($ret);
423 }
425 /* Save to LDAP */
426 function save()
427 {
428 $ret =array();
429 foreach($this->attributes as $name){
430 $ret[$name] = $this->$name;
431 }
433 /* Create mx records
434 */
435 foreach($this->mXRecords as $key => $rec){
436 $rec['value']= $key." ".$rec['value'];
437 $this->Records [] = $rec;
438 }
440 $ret['RECORDS'] = $this->Records;
442 $ret['InitialReverseZone']= $this->InitialReverseZone;
443 $ret['InitialzoneName'] = $this->InitialzoneName;
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 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
464 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
465 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
467 $str.=" <tr>".
468 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
469 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
470 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
471 "</tr>";
472 }
474 $str.= " <tr>".
475 " <td colspan=2></td><td>".
476 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
477 " </td>".
478 " </tr>".
479 "</table>";
480 return($str);
481 }
483 /* This function generates a select box out of $this->RecordTypes options.
484 The Parameter $selected is used to predefine an attribute.
485 $name is used to specify a post name
486 */
487 function generateRecordListBox($selected,$name)
488 {
489 $str = "<select name='".$name."' id='".$name."'>";
490 foreach($this->RecordTypes as $type => $value){
492 if(preg_match("/^mXRecord$/i",$value)) continue;
494 $use = "";
495 if($type == $selected){
496 $use = " selected ";
497 }
498 $str.="\n <option value='".$type."' ".$use.">".strtoupper(preg_replace("/record/i","",$type))."</option>";
499 }
500 $str.="</select>";
501 return($str);
502 }
503 }
505 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
506 ?>