1 <?php
3 class servDNSeditZoneEntries 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();
13 var $objectclasses = array("whatever");
15 var $Devices = array();
17 var $zoneName = ""; // ZoneName of currently edited Zone
18 var $reverseName = ""; // ReverseZone of the currently edited Zone
20 var $RecordTypes = array(); // Possible record type.
22 var $disableDialog = false; // Dialog will be disabled, if this zone is new
24 function servDNSeditZoneEntries (&$config,$dn, &$zoneObject)
25 {
26 plugin::plugin ($config, $dn);
28 /* Initialise class
29 */
30 $this->RecordTypes = getDnsRecordTypes();
31 $this->dn = "zoneName=".$zoneObject['InitialzoneName'].",".$dn;
32 $this->zoneName = $zoneObject['InitialzoneName'];
33 $this->reverseName = $zoneObject['InitialReverseZone'];
35 /* Remove nSRecord from listed types */
36 if(isset($this->RecordTypes['nSRecord'])){
37 unset($this->RecordTypes['nSRecord']);
38 }
39 /* Remove nSRecord from listed types */
40 if(isset($this->RecordTypes['pTRRecord'])){
41 unset($this->RecordTypes['pTRRecord']);
42 }
44 /* Get ldap connection
45 */
46 $ldap = $this->config->get_ldap_link();
47 $ldap->cd($this->config->current['BASE']);
49 /* Get zone content
50 */
51 $ldap->ls("(&(objectClass=dNSZone)(!(relativeDomainName=@)))",$this->dn,array("relativeDomainName"));
53 while($attrs = $ldap->fetch()){
54 $this->Devices[$attrs['relativeDomainName'][0]] = getDNSHostEntries($config,$attrs['relativeDomainName'][0],true);
55 $this->Devices[$attrs['relativeDomainName'][0]]['OrigCn'] = $attrs['relativeDomainName'][0];
56 }
58 $ldap->cat($this->dn,array("objectClass"));
60 $this->disableDialog = true;
61 if(count($this->Devices)|| $ldap->count()){
62 $this->disableDialog = false;
63 }
64 }
66 function execute()
67 {
68 plugin::execute();
70 /* Fill templating stuff */
71 $smarty= get_smarty();
72 $display= "";
74 $table = "";
75 foreach($this->Devices as $key => $dev){
76 $table .= $this->generateRecordConfigurationRow($key);
77 }
79 $smarty->assign("disableDialog",$this->disableDialog);
80 $smarty->assign("table",$table);;
81 $display.= $smarty->fetch(get_template_path('servDNSeditZoneEntries.tpl', TRUE));
82 return($display);
83 }
86 function save_object()
87 {
88 /* Check posts for operations ...
89 */
90 $once = true;
91 $ptr_updates = array();
92 foreach($_POST as $name => $value){
94 /* Add a new Record in given object
95 */
97 $tmp = preg_replace("/^.*_(.*)_.*$/","\\1",$name);
98 $tmp2 = split("\|",$tmp);
100 /* Add new host entry
101 */
102 if((preg_match("/^UserRecord_?/",$name)) && ($once)){
103 $once = false;
104 $entry = getDNSHostEntries($this->config,"",true);
105 $entry['exists'] = true;
106 $entry['zoneName'] = strtoupper($this->attrs['cn'][0])."/".$this->zoneName;
107 $entry['RECORDS'][] = array("type" => "aRecord" , "value"=>"");
108 $this->Devices[_("New entry")] = $entry;
109 }
111 if(count($tmp2) != 2) continue;
113 $Name = base64_decode($tmp2[0]);
114 $RecordID = $tmp2[1];
116 /* Add new REcord
117 */
118 if((preg_match("/^AddRecord_/",$name)) && ($once)){
119 $once = false;
120 $this->Devices[$Name]['RECORDS'][] = $this->Devices[$Name]['RECORDS'][$RecordID];
121 }
123 /* Remove record from given dn
124 */
125 if((preg_match("/^RemoveRecord_/",$name)) && ($once)){
126 $once = false;
127 if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
128 unset($this->Devices[$Name]['RECORDS'][$RecordID]);
129 }
131 /* Check if there is at least one visible record. Else remove complete entry */
132 $visible = false;
133 foreach($this->Devices[$Name]['RECORDS'] as $rec){
134 if(in_array($rec['type'],$this->RecordTypes)){
135 $visible = true;
136 break;
137 }
138 }
139 if(!$visible && isset($this->Devices[$Name]['RECORDS'])){
140 $this->Devices[$Name]['RECORDS'] = array();
141 }
142 }
143 }
145 /* Possible attributes posted
146 */
147 foreach($_POST as $name => $value){
149 /* Extract informations out of post name
150 */
151 $tmp = preg_replace("/^.*_/","\\1",$name);
152 $tmp2 = split("\|",$tmp);
154 if(count($tmp2) != 2) continue;
156 $Name = base64_decode($tmp2[0]);
157 $RecordID = $tmp2[1];
159 /* Check for value change
160 */
161 if(preg_match("/ValueSelection_/",$name)){
162 if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
164 /* Update value */
165 $old = $this->Devices[$Name]['RECORDS'][$RecordID]['value'];
166 $this->Devices[$Name]['RECORDS'][$RecordID]['value'] = $value;
168 /* Handle pTRRecord */
169 if(!isset($ptr_updates[$Name]) && $this->Devices[$Name]['RECORDS'][$RecordID]['type'] == "aRecord"){
171 $found = false;
172 $ip = $value;
173 $match = preg_replace("/^[^\/]*+\//","",$this->reverseName);
174 $ip = preg_replace("/^".normalizePreg($match)."/","",$ip);
175 $ip = preg_replace("/^\./","",$ip);
177 foreach($this->Devices[$Name]['RECORDS'] as $key => $dev){
178 if($dev['type'] == "pTRRecord"){
179 $ptr_updates[$Name] = $Name;
180 $this->Devices[$Name]['RECORDS'][$key]['value'] = $ip;
181 $found = true;
182 break;
183 }
184 }
185 if(!$found){
186 $dev = array('type'=> 'pTRRecord', 'value' => $ip);
187 $this->Devices[$Name]['RECORDS'][] = $dev;
188 }
189 }
190 }
191 }
193 /* record type changed
194 */
195 if(preg_match("/^RecordTypeSelection_/",$name)){
196 if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
197 $this->Devices[$Name]['RECORDS'][$RecordID]['type'] = $value;
198 }
199 }
200 }
202 /* check for renamed entries
203 */
204 foreach($_POST as $name => $value){
206 /* Extract informations out of post name
207 */
208 $tmp = preg_replace("/^.*_/","\\1",$name);
209 $tmp2 = split("\|",$tmp);
211 if(count($tmp2) != 2) continue;
213 $Name = base64_decode($tmp2[0]);
214 $RecordID = $tmp2[1];
216 /* Host renamed
217 */
218 if(preg_match("/RenameHost_/",$name)){
219 if((isset($this->Devices[$Name])) && ($Name != $value)){
221 if(isset($this->Devices[$value])){
222 print_red(sprintf(_("Can't rename '%s' to '%s' there is already an entry with the same name in our zone editing dialog."),$Name,$value));
223 }else{
224 $this->Devices[$value] = $this->Devices[$Name];
225 unset($this->Devices[$Name]);
226 }
227 }
228 }
229 }
230 }
233 /* check something
234 */
235 function check()
236 {
237 /* Call common method to give check the hook */
238 $message= plugin::check();
240 $ldap = $this->config->get_ldap_link();
241 $ldap->cd($this->config->current['BASE']);
243 $names = array();
244 foreach($this->Devices as $DevName => $device){
246 /* Don't need to check empty values ... */
247 if(!count($device['RECORDS'])) continue;
249 /* Checking entry name
250 */
251 if(!preg_match("/^[a-z0-9_\.-]+$/i", $DevName) || (empty($DevName))){
252 $message[] = sprintf(_("Entry name '%s' contains invalid characters."), $DevName);
253 }
255 /* Renaming check for existing devices
256 */
257 if(isset($device['OrigCn']) && ($DevName != $device['OrigCn'] )){
258 $ldap->cd($this->config->current['BASE']);
259 $ldap->search("(relativeDomainName=".$DevName.")",array("relativeDomainName"));
260 if($ldap->count()){
261 $message[] = sprintf(_("Can not rename '%s' to '%s',the destination name already exists."),$device['OrigCn'],$DevName);
262 }
263 }elseif(!isset($device['OrigCn'])){
264 $ldap->cd($this->config->current['BASE']);
265 $ldap->search("(relativeDomainName=".$DevName.")",array("relativeDomainName"));
266 if($ldap->count()){
267 $message[] = sprintf(_("Can not create '%s',the destination name already exists."),$DevName);
268 }
269 }
271 /* Check names
272 */
273 if(!isset($names[$DevName])){
274 $names[$DevName] = "";
275 }else{
276 $message[] = sprintf(_("The name '%s' is used more than once."),$DevName);
277 }
279 /* Names should be written in lowercase
280 */
281 # if(strtolower($DevName) != $DevName){
282 # $message[] = sprintf(_("The host name '%s' should be written in lowercase."), $DevName);
283 # }
285 /* Check records
286 */
287 $singleEntries = array("cNAMERecord","pTRRecord");
289 $tmp = array();
290 $tmp2 = array();
291 foreach($device['RECORDS'] as $Num => $Rec){
293 /* Check for multiple use of unique record types
294 */
295 if(in_array($Rec['type'],$singleEntries)){
296 if(!isset($tmp[$Rec['type']])){
297 $tmp[$Rec['type']] = "";
298 }else{
299 $message[] = sprintf(_("The record type '%s' is a unique type and can't be defined twice."),$type);
300 }
301 }
303 /* Check for empty / duplicate entries in record array
304 */
305 if(empty($Rec['value'])){
306 $message[] = sprintf(_("There is an empty '%s' for host '%s'."),$Rec['type'],$DevName);
307 }
309 /* Check for duplicate record entries
310 */
311 if(!isset($tmp[$Rec['type']][$Rec['value']])){
312 $tmp[$Rec['type']][$Rec['value']] = "";
313 }else{
314 $message[] = sprintf(_("There is a duplicate entry in '%s' for '%s'."),$Rec['type'],$DevName);
315 }
316 }
317 }
318 return ($message);
319 }
321 function save()
322 {
323 if($this->disableDialog) return;
325 $todo = array();
329 /* Create todolist
330 */
331 foreach($this->Devices as $name => $dev){
332 if(isset($dev['OrigCn'])){
333 if(count($dev['RECORDS'])){
334 $todo[] = getDNSHostEntriesDiff($this->config,$dev['OrigCn'],$dev,$name);
335 }else{
336 $dev['exists'] = false;
337 $todo[] = getDNSHostEntriesDiff($this->config,$dev['OrigCn'],$dev,$name);
338 }
339 }else{
340 if(count($dev['RECORDS'])){
341 $todo[] = getDNSHostEntriesDiff($this->config,"",$dev,$name);
342 }else{
343 $dev['exists'] = false;
344 $todo[] = getDNSHostEntriesDiff($this->config,"",$dev,$name);
345 }
346 }
347 }
349 $tmp = array();
350 $tmp['del'] = array();
351 $tmp['add'] = array();
352 $tmp['move'] = array();
353 foreach($todo as $to){
354 foreach($to as $type => $entries){
355 $tmp[$type] = array_merge($tmp[$type],$entries);
356 }
357 }
359 /* Get ldap link
360 */
361 $ldap = $this->config->get_ldap_link();
362 $ldap->cd ($this->config->current['BASE']);
364 /* move follwoing entries
365 */
366 foreach($tmp['move'] as $src => $dst){
367 $this->recursive_move($src,$dst);
368 }
370 /* Delete dns */
371 foreach($tmp['del'] as $dn => $del){
372 $ldap->cd($dn);
373 $ldap->rmdir_recursive($dn);
374 if(is_object($this->parent->parent)){
375 $this->parent->parent->handle_post_events("remove",array("dn" => $dn));
376 }
377 }
379 /* Add || Update new DNS entries
380 */
381 foreach($tmp['add'] as $dn => $attrs){
382 $ldap->cd($dn);
383 $ldap->cat($dn, array('dn'));
384 if(count($ldap->fetch())){
385 $ldap->cd($dn);
386 $ldap->modify ($attrs);
387 if(is_object($this->parent->parent)){
388 $this->parent->parent->handle_post_events("modify",array("dn" => $dn));
389 }
390 }else{
391 $ldap->cd($dn);
392 $ldap->add($attrs);
393 if(is_object($this->parent->parent)){
394 $this->parent->parent->handle_post_events("create",array("dn" => $dn));
395 }
396 }
397 }
398 }
401 /* Create html table out of given entry
402 */
403 function generateRecordConfigurationRow($objKey){
405 /* Get some basic informations
406 */
407 $obj = $this->Devices[$objKey];
408 $objectName = $objKey;
410 /* Abort if emtpy
411 */
412 if(count($obj['RECORDS']) == 0) return "";
414 /* Set title
415 */
416 $str= "<br>";
418 $hostNameOnce = true;
420 /* Walk through all defined records
421 */
422 $str.= "<table cellspacing='0' cellpadding='0'>";
423 foreach($obj['RECORDS'] as $id => $record){
425 /* Skip not selectable entries */
426 if(!isset($this->RecordTypes [$record['type']])) {
427 continue;
428 }
430 /* Create unique post name
431 */
432 $name = base64_encode($objKey)."|".$id;
434 $str .= "<tr><td style='width:170px;'>\n";
436 /* Only first host entry name should be editable
437 */
438 if($hostNameOnce){
439 $hostNameOnce = false;
440 $str .="<input type='text' name='RenameHost_".$name."' value='".$objectName."'>\n";
441 }
443 /* Create rest. Selectbox, icons ...
444 */
445 $str .="
446 </td>
447 <td style='width:90px;'>
448 ".$this->createRecordTypeSelection($record['type'],$name)."
449 </td>
450 <td>
451 <input type='text' value='".$record['value']."' name='ValueSelection_".$name."' style='width:250px;'>
452 </td>
453 <td style='width:75px;text-align:right;'>
454 <input type='image' name='AddRecord_".$name."' src='images/list_new.png' alt='"._("Add")."' title='"._("Add")."'>
455 <input type='image' name='RemoveRecord_".$name."' src='images/edittrash.png' alt='"._("Remove")."' title='"._("Remove")."'>
456 ";
458 # if($record['type'] == "aRecord"){
459 # $str .="<input type='image' name='AddPtr_".$name."' src='images/network.png'
460 # alt='"._("Add PTR")."' title='"._("Add PTR record")."'>";
461 # }else{
462 # $str .= "<img src='images/empty.png' alt=''>";
463 # }
465 $str.=
466 "</td>
467 </tr>";
468 }
469 $str .="</table>";
470 return($str);
471 }
474 /* Create selectbox with all available option types
475 */
476 function createRecordTypeSelection($id,$refID){
478 $str = "\n<select name='RecordTypeSelection_".$refID."'>";
479 foreach($this->RecordTypes as $type => $atr) {
480 if($id == $type){
481 $str .="\n<option value='".$type."' selected >".strtoupper(preg_replace("/record/i","",$type))."</option>";
482 }else{
483 $str .="\n<option value='".$type."'>".strtoupper(preg_replace("/record/i","",$type))."</option>";
484 }
485 }
486 $str.= "\n</select>";
487 return($str);
488 }
491 function remove_from_parent()
492 {
493 }
495 }
497 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
498 ?>