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 /* Get ldap connection
36 */
37 $ldap = $this->config->get_ldap_link();
38 $ldap->cd($this->config->current['BASE']);
40 /* Get zone content
41 */
42 $ldap->ls("(&(objectClass=dNSZone)(!(relativeDomainName=@)))",$this->dn,array("relativeDomainName"));
44 while($attrs = $ldap->fetch()){
45 $this->Devices[$attrs['relativeDomainName'][0]] = getDNSHostEntries($config,$attrs['relativeDomainName'][0],true);
46 $this->Devices[$attrs['relativeDomainName'][0]]['OrigCn'] = $attrs['relativeDomainName'][0];
47 }
49 if(!count($this->Devices)){
50 $this->disableDialog = true;
51 }
52 }
54 function execute()
55 {
56 plugin::execute();
59 /* Check posts for operations ...
60 */
61 $once = true;
62 foreach($_POST as $name => $value){
64 /* Add a new Record in given object
65 */
67 $tmp = preg_replace("/^.*_(.*)_.*$/","\\1",$name);
68 $tmp2 = split("\|",$tmp);
70 /* Add new host entry
71 */
72 if((preg_match("/^UserRecord_?/",$name)) && ($once)){
73 $once = false;
74 $entry = getDNSHostEntries($this->config,"",true);
75 $entry['exists'] = true;
76 $entry['zoneName'] = $this->zoneName;
77 $entry['RECORDS'][] = array("type" => "aRecord" , "value"=>"");
78 $this->Devices[_("New Entry")] = $entry;
79 }
81 if(count($tmp2) != 2) continue;
83 $Name = base64_decode($tmp2[0]);
84 $RecordID = $tmp2[1];
86 /* Add new REcord
87 */
88 if((preg_match("/^AddRecord_/",$name)) && ($once)){
89 $once = false;
90 $this->Devices[$Name]['RECORDS'][] = $this->Devices[$Name]['RECORDS'][$RecordID];
91 }
93 /* Remove record from given dn
94 */
95 if((preg_match("/^RemoveRecord_/",$name)) && ($once)){
96 $once = false;
97 if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
98 unset($this->Devices[$Name]['RECORDS'][$RecordID]);
99 }
100 }
102 }
104 /* Fill templating stuff */
105 $smarty= get_smarty();
106 $display= "";
108 $table = "";
109 foreach($this->Devices as $key => $dev){
110 $table .= $this->generateRecordConfigurationRow($key);
111 }
113 $smarty->assign("disableDialog",$this->disableDialog);
114 $smarty->assign("table",$table);;
115 $display.= $smarty->fetch(get_template_path('servDNSeditZoneEntries.tpl', TRUE));
116 return($display);
117 }
120 function save_object()
121 {
122 /* Possible attributes posted
123 */
124 foreach($_POST as $name => $value){
126 /* Extract informations out of post name
127 */
128 $tmp = preg_replace("/^.*_/","\\1",$name);
129 $tmp2 = split("\|",$tmp);
131 if(count($tmp2) != 2) continue;
133 $Name = base64_decode($tmp2[0]);
134 $RecordID = $tmp2[1];
136 /* Check for value change
137 */
138 if(preg_match("/ValueSelection_/",$name)){
139 if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
140 $this->Devices[$Name]['RECORDS'][$RecordID]['value'] = $value;
141 }
142 }
144 /* record type changed
145 */
146 if(preg_match("/^RecordTypeSelection_/",$name)){
147 if(isset($this->Devices[$Name]['RECORDS'][$RecordID])){
148 $this->Devices[$Name]['RECORDS'][$RecordID]['type'] = $value;
149 }
150 }
151 }
153 /* check for renamed entries
154 */
155 foreach($_POST as $name => $value){
157 /* Extract informations out of post name
158 */
159 $tmp = preg_replace("/^.*_/","\\1",$name);
160 $tmp2 = split("\|",$tmp);
162 if(count($tmp2) != 2) continue;
164 $Name = base64_decode($tmp2[0]);
165 $RecordID = $tmp2[1];
167 /* Host renamed
168 */
169 if(preg_match("/RenameHost_/",$name)){
170 if((isset($this->Devices[$Name])) && ($Name != $value)){
172 if(isset($this->Devices[$value])){
173 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));
174 }else{
175 $this->Devices[$value] = $this->Devices[$Name];
176 unset($this->Devices[$Name]);
177 }
178 }
179 }
180 }
181 }
184 /* check something
185 */
186 function check()
187 {
188 /* Call common method to give check the hook */
189 $message= plugin::check();
191 $ldap = $this->config->get_ldap_link();
192 $ldap->cd($this->config->current['BASE']);
194 $names = array();
195 foreach($this->Devices as $DevName => $device){
197 /* Checking entry name
198 */
199 if((!is_uid($DevName)) | (empty($DevName))){
200 $message[] = sprintf(_("Entry name '%s' contains invalid characters."), $DevName);
201 }
203 /* Renaming check for existing devices
204 */
205 if(isset($device['OrigCn']) && ($DevName != $device['OrigCn'] )){
206 $ldap->cd($this->config->current['BASE']);
207 $ldap->search("(relativeDomainName=".$DevName.")",array("relativeDomainName"));
208 if($ldap->count()){
209 $message[] = sprintf(_("Can not rename '%s' to '%s',the destination name already exists."),$device['OrigCn'],$DevName);
210 }
211 }elseif(!isset($device['OrigCn'])){
212 $ldap->cd($this->config->current['BASE']);
213 $ldap->search("(relativeDomainName=".$DevName.")",array("relativeDomainName"));
214 if($ldap->count()){
215 $message[] = sprintf(_("Can not create '%s',the destination name already exists."),$DevName);
216 }
217 }
219 /* Check names
220 */
221 if(!isset($names[$DevName])){
222 $names[$DevName] = "";
223 }else{
224 $message[] = sprintf(_("The name '%s' is used more than once."),$DevName);
225 }
227 /* Names should be written in lowercase
228 */
229 if(strtolower($DevName) != $DevName){
230 $message[] = sprintf(_("The host name '%s' should be written in lowercase."), $DevName);
231 }
233 /* Check records
234 */
235 $singleEntries = array("cNAMERecord","pTRRecord");
237 $tmp = array();
238 $tmp2 = array();
239 foreach($device['RECORDS'] as $Num => $Rec){
241 /* Check for multiple use of unique record types
242 */
243 if(in_array($Rec['type'],$singleEntries)){
244 if(!isset($tmp[$Rec['type']])){
245 $tmp[$Rec['type']] = "";
246 }else{
247 $message[] = sprintf(_("The record type '%s' is a unique type and can't be defined twice."),$type);
248 }
249 }
251 /* Check for empty / duplicate entries in record array
252 */
253 if(empty($Rec['value'])){
254 $message[] = sprintf(_("There is an empty '%s' for host '%s'."),$Rec['type'],$DevName);
255 }
257 /* Check for duplicate record entries
258 */
259 if(!isset($tmp[$Rec['type']][$Rec['value']])){
260 $tmp[$Rec['type']][$Rec['value']] = "";
261 }else{
262 $message[] = sprintf(_("There is a duplicate entry in '%s' for '%s'."),$Rec['type'],$DevName);
263 }
264 }
265 }
266 return ($message);
267 }
269 function save()
270 {
271 if($this->disableDialog) return;
273 $todo = array();
275 /* Create todolist
276 */
277 foreach($this->Devices as $name => $dev){
278 if(isset($dev['OrigCn'])){
279 if(count($dev['RECORDS'])){
280 $todo[] = getDNSHostEntriesDiff($this->config,$dev['OrigCn'],$dev,$name);
281 }else{
282 $dev['exists'] = false;
283 $todo[] = getDNSHostEntriesDiff($this->config,$dev['OrigCn'],$dev,$name);
284 }
285 }else{
286 if(count($dev['RECORDS'])){
287 $todo[] = getDNSHostEntriesDiff($this->config,"",$dev,$name);
288 }else{
289 $dev['exists'] = false;
290 $todo[] = getDNSHostEntriesDiff($this->config,"",$dev,$name);
291 }
292 }
293 }
295 $tmp = array();
296 $tmp['del'] = array();
297 $tmp['add'] = array();
298 $tmp['move'] = array();
299 foreach($todo as $to){
300 foreach($to as $type => $entries){
301 $tmp[$type] = array_merge($tmp[$type],$entries);
302 }
303 }
305 /* Get ldap link
306 */
307 $ldap = $this->config->get_ldap_link();
308 $ldap->cd ($this->config->current['BASE']);
310 /* move follwoing entries
311 */
312 foreach($tmp['move'] as $src => $dst){
313 $this->recursive_move($src,$dst);
314 }
316 /* Delete dns */
317 foreach($tmp['del'] as $dn => $del){
318 $ldap->cd($dn);
319 $ldap->rmdir_recursive($dn);
320 }
322 /* Add || Update new DNS entries
323 */
324 foreach($tmp['add'] as $dn => $attrs){
325 $ldap->cd($dn);
326 $ldap->cat($dn, array('dn'));
327 if(count($ldap->fetch())){
328 $ldap->cd($dn);
329 $ldap->modify ($attrs);
330 }else{
331 $ldap->cd($dn);
332 $ldap->add($attrs);
333 }
334 }
335 }
338 /* Create html table out of given entry
339 */
340 function generateRecordConfigurationRow($objKey){
342 /* Get some basic informations
343 */
344 $obj = $this->Devices[$objKey];
345 $objectName = $objKey;
347 /* Abort if emtpy
348 */
349 if(count($obj['RECORDS']) == 0) return "";
351 /* Set title
352 */
353 $str= "<br>";
355 $hostNameOnce = true;
357 /* Walk through all defined records
358 */
359 $str.= "<table cellspacing='0' cellpadding='0'>";
360 foreach($obj['RECORDS'] as $id => $record){
362 /* Create unique post name
363 */
364 $name = base64_encode($objKey)."|".$id;
366 $str .= "<tr><td style='width:170px;'>\n";
368 /* Only first host entry name should be editable
369 */
370 if($hostNameOnce){
371 $hostNameOnce = false;
372 $str .="<input type='text' name='RenameHost_".$name."' value='".$objectName."'>\n";
373 }
375 /* Create rest. Selectbox, icons ...
376 */
377 $str .="
378 </td>
379 <td style='width:90px;'>
380 ".$this->createRecordTypeSelection($record['type'],$name)."
381 </td>
382 <td>
383 <input type='text' value='".$record['value']."' name='ValueSelection_".$name."' style='width:250px;'>
384 </td>
385 <td style='width:50px;text-align:right;'>
386 <input type='image' name='AddRecord_".$name."' src='images/list_new.png' alt='"._("Add")."' title='"._("Add")."'>
387 <input type='image' name='RemoveRecord_".$name."' src='images/edittrash.png' alt='"._("Remove")."' title='"._("Remove")."'>
388 </td>
389 </tr>";
390 }
391 $str .="</table>";
392 return($str);
393 }
396 /* Create selectbox with all available option types
397 */
398 function createRecordTypeSelection($id,$refID){
400 $str = "\n<select name='RecordTypeSelection_".$refID."'>";
401 foreach($this->RecordTypes as $type => $atr) {
402 if($id == $type){
403 $str .="\n<option value='".$type."' selected >".strtoupper(preg_replace("/record/i","",$type))."</option>";
404 }else{
405 $str .="\n<option value='".$type."'>".strtoupper(preg_replace("/record/i","",$type))."</option>";
406 }
407 }
408 $str.= "\n</select>";
409 return($str);
410 }
413 function remove_from_parent()
414 {
415 }
417 }
419 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
420 ?>