35b2143c12cb74743152c18e957844b611ad98aa
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("zoneName");
13 var $objectclasses= array("whatever");
15 var $Devices = array();
16 var $InitDevices = array();
17 var $zoneName = "";
18 var $reverseName = "";
19 var $RecordTypes= array();
21 function servDNSeditZoneEntries ($config, $dn= NULL,$zoneName,$reverseName,$RTypes)
22 {
23 plugin::plugin ($config, $dn);
25 $this->zoneName = $zoneName;
26 $this->reverseName = $reverseName;
27 $this->RecordTypes = $RTypes;
28 $this->RecordTypes['cNAMERecord'] = "relativeDomainName";
31 $ldap = $this->config->get_ldap_link();
32 $ldap->cd($this->config->current['BASE']);
34 /* Get all objects it this zone ...
35 */
36 $res = $ldap->search("(&(objectClass=dNSZone)(|(zoneName=".$zoneName.")(zoneName=".$reverseName.".in-addr.arpa))(!(relativeDomainName=@)))");
37 while($attrs = $ldap->fetch($res)){
38 foreach($this->RecordTypes as $atr => $val){
39 if((isset($attrs[$atr])) && (is_array($attrs[$atr]))){
40 unset($attrs[$val]['count']);
41 $dn = preg_replace("/^.*,cn/","cn",$attrs['dn']);
42 $this->Devices[$dn]['RECORDS'][$atr] = $attrs[$val];
43 }
44 }
45 }
47 /* Add additional informations
48 to all catched objects
49 */
50 foreach($this->Devices as $dn => $Recs){
51 $this->Devices[$dn]['OBJECT'] = $ldap->fetch($ldap->cat($dn,array("objectClass","cn")));
52 }
54 $this->Devices;
55 $this->InitDevices = $this->Devices;
56 }
58 function execute()
59 {
60 plugin::execute();
62 /* Check posts for operations ...
63 */
64 $once = true;
65 foreach($_POST as $name => $value){
67 /* Add a new Record in given object
68 */
69 if((preg_match("/^AddRecord_/",$name)) && ($once)){
70 $once = false;
71 $tmp = preg_replace("/^AddRecord_/","",$name);
72 $tmp = preg_replace("/_.*$/","",$tmp);
73 $tmp2 = split("\|",$tmp);
75 $dn = base64_decode($tmp2[0]);
76 $record = $tmp2[1];
77 $numrec = $tmp2[2];
79 $this->Devices[$dn]['RECORDS'][$record][] = "";
80 }
82 /* Remove record from given dn
83 */
84 if((preg_match("/^RemoveRecord_/",$name)) && ($once)){
85 $once = false;
86 $tmp = preg_replace("/^RemoveRecord_/","",$name);
87 $tmp = preg_replace("/_.*$/","",$tmp);
88 $tmp2 = split("\|",$tmp);
90 $dn = base64_decode($tmp2[0]);
91 $record = $tmp2[1];
92 $numrec = $tmp2[2];
94 unset($this->Devices[$dn]['RECORDS'][$record][$numrec]);
95 }
97 /* Don't know how i should implement this ...
98 */
99 if((preg_match("/^UserRecord_/",$name)) && ($once)){
100 $once = false;
101 $tmp = preg_replace("/^UserRecord_/","",$name);
102 $tmp = preg_replace("/_.*$/","",$tmp);
103 $tmp2 = split("\|",$tmp);
105 $dn = base64_decode($tmp2[0]);
106 $record = $tmp2[1];
107 $numrec = $tmp2[2];
109 $this->FreeDevices[$dn]['RECORDS']['User'][$numrec] = "";
110 }
111 }
114 /* Fill templating stuff */
115 $smarty= get_smarty();
116 $display= "";
118 $table = "";
119 foreach($this->Devices as $key => $dev){
120 $table .= $this->generateRecordConfigurationRow($key);
121 }
123 $smarty->assign("table",$table);;
124 $display.= $smarty->fetch(get_template_path('servDNSeditZoneEntries.tpl', TRUE));
125 return($display);
126 }
128 /* Create html table out of given entry
129 */
130 function generateRecordConfigurationRow($obj_dn){
132 /* get some basic inforamtions
133 */
134 $obj = $this->Devices[$obj_dn];
135 $objectName = $obj['OBJECT']['cn'][0];
137 // set title
138 $str = "<h3>".sprintf(_("Settings for '%s'"),$obj_dn)." : </h3>";
140 /* Walk through all defined records
141 */
142 foreach($obj['RECORDS'] as $id => $types){
144 /* Create table
145 */
146 $str.= "<table cellspacing='0' cellpadding='0'>";
147 foreach($types as $key => $type){
149 /* Create unique post name
150 */
151 $name = base64_encode($obj_dn)."|".$id."|".$key;
153 $str .= "<tr>
154 <td style='width:80px;'>
155 ".$objectName."
156 </td>
157 <td style='width:80px;'>
158 ".$this->createRecordTypeSelection($id,$name)."
159 </td>
160 <td>
161 <input type='text' value='".$type."' name='ValueSelection_".$name."' style='width:250px;'>
162 </td>
163 <td style='width:30px;text-align:right;'>
164 <input type='image' name='AddRecord_".$name."' src='images/crossref.png' alt='"._("Add")."' title='"._("Add")."'>
165 </td>
166 <td style='width:60px;text-align:right;'>
167 <input type='image' name='UserRecord_".$name."' src='images/select_default.png' alt='"._("New")."' title='"._("New")."'>
168 <input type='image' name='RemoveRecord_".$name."' src='images/edittrash.png' alt='"._("Remove")."' title='"._("Remove")."'>
169 </td>
170 </tr>";
171 }
172 $str .="</table>";
173 }
174 return($str);
175 }
177 /* Create selectbox with all available option types
178 */
179 function createRecordTypeSelection($id,$refID){
180 $str = "\n<select name='RecordTypeSelection_".$refID."'>";
181 foreach($this->RecordTypes as $type => $atr) {
182 if($id == $type){
183 $str .="\n<option value='".$type."' selected >".strtoupper(preg_replace("/record/i","",$type))."</option>";
184 }else{
185 $str .="\n<option value='".$type."'>".strtoupper(preg_replace("/record/i","",$type))."</option>";
186 }
187 }
188 $str.= "\n</select>";
189 return($str);
190 }
192 function remove_from_parent()
193 {
194 }
196 function save_object()
197 {
198 /* Save all form fields
199 */
201 /* Possible attributes posted
202 */
203 $arr = array("RecordTypeSelection_","ValueSelection_");
205 foreach($_POST as $name=>$value){
207 foreach($arr as $type)
209 /* Check if attribute was posted
210 */
211 if(preg_match("/".$type."/",$name)){
213 /* Extract informations out of postname
214 */
215 $action = $type;
216 $tmp = preg_replace("/^".$action."/","",$name);
217 $tmp = preg_replace("/_.*$/","",$tmp);
218 $tmp2 = split("\|",$tmp);
220 $dn = base64_decode($tmp2[0]);
221 $record = $tmp2[1];
222 $numrec = $tmp2[2];
224 /* Check which type of post was posted
225 */
227 // checkbox was posted
228 if($action == "RecordTypeSelection_"){
229 if($record != $value){
230 $str = $this->Devices[$dn]['RECORDS'][$record][$numrec];
231 unset($this->Devices[$dn]['RECORDS'][$record][$numrec]);
232 $this->Devices[$dn]['RECORDS'][$value][] = $str;
233 }
234 }
236 // entry value was psoted
237 if($action == "ValueSelection_"){
238 $this->Devices[$dn]['RECORDS'][$record][$numrec] = $value;
239 }
240 }
241 }
242 }
244 /* check something
245 */
246 function check()
247 {
248 $message= array();
249 // Nothing right now
250 return ($message);
251 }
253 function save()
254 {
255 /* Set all initial records to array()
256 This means they will be removed from the entry
257 */
258 foreach($this->InitDevices as $dn => $obj){
259 $attrs[$dn]= array();
260 foreach($obj['RECORDS'] as $rectype => $records){
261 $attrs[$dn][$rectype] = array();
262 }
263 }
265 /* Add new entries
266 */
267 foreach($this->Devices as $dn => $obj){
268 foreach($obj['RECORDS'] as $rectype => $records){
269 $attrs[$dn][$rectype] = array();
270 foreach($records as $rec){
271 $attrs[$dn][$rectype][] = $rec;
272 }
273 }
274 }
276 /* Get ldap link
277 */
278 $ldap = $this->config->get_ldap_link();
279 $ldap->cd ($this->config->current['BASE']);
281 /* Generate 'T odo' :-)
282 */
283 $tmp = ($this->generate_LDAP_entries($attrs));
285 /* Delete all objects which should be delted
286 */
287 foreach($tmp['del'] as $key => $ntr){
288 $ldap->rmdir($key);
289 }
291 /* Add append new / updated entries
292 */
293 foreach($tmp['addupdate'] as $key => $attrs){
294 $ldap->cat($key);
295 if($ldap->count() > 0){
296 $ldap->cd($key);
297 $ldap->modify($attrs);
298 }else{
299 $ldap->cd($this->config->current['BASE']);
300 $ldap->cd($key);
301 $ldap->add($attrs);
302 }
304 /* Display errors ...
305 */
306 if($ldap->error != "Success"){
307 echo "<br>".$ldap->error."<br>";
308 print_a($attrs);
309 }
310 }
311 }
314 function generate_LDAP_entries($objs)
315 {
316 $entries = array();
317 $delete = array();
319 /* these entries are added to base object ...
320 */
321 $normal = array("mDRecord","aRecord","tXTRecord","mXRecord","hInfoRecord","mInfoRecord","aFSDBRecord","SigRecord","KeyRecord","aAAARecord","nSRecord",
322 "LocRecord","nXTRecord","sRVRecord","nAPTRRecord","kXRecord","certRecord","a6Record","dSRecord","sSHFPRecord","rRSIGRecord","nSECRecord");
324 /* Create t odo
325 */
326 foreach($objs as $dn => $recs){
328 /* Get basic info
329 */
330 $obj = $this->Devices[$dn];
331 $objectName = $obj['OBJECT']['cn'][0];
333 /* Calculate records ...
334 */
335 foreach($recs as $type => $rec){
337 /* Simply add normal entries
338 */
339 if(in_array($type,$normal)){
340 $entries['relativeDomainName='.$objectName.','.$dn][$type] = $rec ;
341 }
343 /* Special handling for cNAMERecords
344 */
345 if($type == "cNAMERecord"){
346 if(isset($this->InitDevices[$dn]['RECORDS']['cNAMERecord'])){
347 foreach($this->InitDevices[$dn]['RECORDS']['cNAMERecord'] as $warmal){
348 $delete['relativeDomainName='.$warmal.','.$dn] = "";
349 }
350 }
351 foreach($rec as $r){
352 if(!empty($r)){
353 $entries['relativeDomainName='.$r.','.$dn]['cNAMERecord'] = $objectName;
354 $entries['relativeDomainName='.$r.','.$dn]['relativeDomainName'] = $r;
355 $entries['relativeDomainName='.$r.','.$dn]['objectClass'] = array("top","dNSZone");
356 $entries['relativeDomainName='.$r.','.$dn]['zoneName'] = $this->zoneName;
357 }
358 }
359 }
361 /* Special handling for ptrrecord
362 */
363 if($type == "pTRRecord"){
364 if(isset($this->InitDevices[$dn]['RECORDS']['pTRRecord'])){
365 foreach($this->InitDevices[$dn]['RECORDS']['pTRRecord'] as $warmal){
366 $delete['relativeDomainName='.$warmal.','.$dn] = "";
367 }
368 }
369 foreach($rec as $r){
370 if(!empty($r)){
371 $entries['relativeDomainName='.$r.','.$dn]['pTRRecord'] = $objectName;
372 $entries['relativeDomainName='.$r.','.$dn]['zoneName'] = $this->reverseName.".in-addr.arpa";
373 $entries['relativeDomainName='.$r.','.$dn]['relativeDomainName'] = $r;
374 $entries['relativeDomainName='.$r.','.$dn]['objectClass'] = array("top","dNSZone");
375 }
376 }
377 }
378 }
379 }
381 return(array("del"=> $delete , "addupdate"=> $entries));
382 }
384 }
386 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
387 ?>