1 <?php
3 class faiScript 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;
13 /* Attributes for this Object */
14 var $attributes = array("cn","description");
16 /* ObjectClasses for this Object*/
17 var $objectclasses = array("top","FAIclass","FAIscript");
19 /* Class name of the Ldap ObjectClass for the Sub Object */
20 var $subClass = "FAIscriptEntry";
21 var $subClasses = array("top","FAIclass","FAIscriptEntry");
23 /* Class name of the php class which allows us to edit a Sub Object */
24 var $subClassName = "faiScriptEntry";
26 /* Attributes to initialise for each subObject */
27 var $subAttributes = array("cn","description","FAIpriority","FAIscript");
28 var $sub64coded = array();
30 /* Specific attributes */
31 var $cn = ""; // The class name for this object
32 var $description = ""; // The description for this set of partitions
33 var $is_dialog = false; // specifies which buttons will be shown to save or abort
34 var $dialog = NULL; // a dialog, e.g. new disk dialog
35 var $SubObjects = array(); // All leafobjects of this object
37 function faiScript ($config, $dn= NULL)
38 {
39 /* Load Attributes */
40 plugin::plugin ($config, $dn);
42 /* If "dn==new" we try to create a new entry
43 * Else we must read all objects from ldap which belong to this entry.
44 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
45 */
46 if($dn != "new"){
47 $this->dn =$dn;
49 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
50 */
51 $ldap = $this->config->get_ldap_link();
52 $ldap->cd ($this->dn);
53 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
55 while($object = $ldap->fetch()){
56 /* Set status for save management */
58 foreach($this->subAttributes as $attrs){
59 if(!isset($object[$attrs][0])){
60 $this->SubObjects[$object['cn'][0]][$attrs]="";
61 }else{
62 $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
63 }
64 }
66 foreach($this->sub64coded as $codeIt){
67 $this->SubObjects[$object['cn'][0]][$codeIt]=(base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]));
68 }
70 foreach($this->subAttributes as $attrs){
71 $this->SubObjects[$object['cn'][0]][$attrs]=addslashes($this->SubObjects[$object['cn'][0]][$attrs]);
72 }
74 $this->SubObjects[$object['cn'][0]]['FAIscript'] = addslashes ($this->readBinary("FAIscript",$object['dn']));
76 $this->SubObjects[$object['cn'][0]]['status'] = "edited";
77 $this->SubObjects[$object['cn'][0]]['dn'] = $object['dn'];
78 }
79 }
80 }
82 function execute()
83 {
84 /* Call parent execute */
85 plugin::execute();
87 /* Fill templating stuff */
88 $smarty= get_smarty();
89 $display= "";
91 /* Add new sub object */
92 if(isset($_POST['AddSubObject'])){
93 $this->dialog= new $this->subClassName($this->config,"new");
94 $this->is_dialog=true;
95 }
97 if($this->dn != "new"){
98 $_SESSION['objectinfo']= $this->dn;
99 }
102 /* New Listhandling
103 */
104 $once = true;
105 foreach($_POST as $name => $value){
106 if(preg_match("/^editscript_/",$name)&&($once)){
107 $once = false;
108 $entry = preg_replace("/^editscript_/","",$name);
109 $entry = base64_decode(preg_replace("/_.*/","",$entry));
110 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$entry]);
111 $_SESSION['objectinfo'] = $this->SubObjects[$entry]['dn'];
112 $this->is_dialog=true;
113 }
114 if(preg_match("/^deletescript_/",$name)&&($once)){
115 $once = false;
116 $entry = preg_replace("/^deletescript_/","",$name);
117 $entry = base64_decode(preg_replace("/_.*/","",$entry));
118 if($this->SubObjects[$entry]['status'] == "edited"){
119 $this->SubObjects[$entry]['status']= "delete";
120 }else{
121 unset($this->SubObjects[$entry]);
122 }
123 }
124 }
125 ///// Ende new list handling
128 /* Edit selected Sub Object */
129 if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
130 $script = $_POST['SubObject'][0];
132 $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$script]);
133 $_SESSION['objectinfo'] = $this->SubObjects[$script]['dn'];
134 $this->is_dialog=true;
135 }
137 /* Remove Sub object */
138 if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
140 foreach($_POST['SubObject'] as $script){
141 if($this->SubObjects[$script]['status'] == "edited"){
142 $this->SubObjects[$script]['status']= "delete";
143 }else{
144 unset($this->SubObjects[$script]);
145 }
146 }
147 }
149 /* Save Dialog */
150 if(isset($_POST['SaveSubObject'])){
151 $this->dialog->save_object();
152 $msgs = $this->dialog->check();
153 if(count($msgs)>0){
154 foreach($msgs as $msg){
155 print_red($msg);
156 }
157 }else{
158 $obj = $this->dialog->save();
159 if(isset($obj['remove'])){
160 if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
161 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
162 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
163 unset($this->SubObjects[$obj['remove']['from']]);
164 }
165 $obj['status'] = "new";
166 $this->SubObjects[$obj['remove']['to']] = $obj;
167 unset($this->SubObjects[$obj['remove']['to']]['remove']);
168 }else{
169 $this->SubObjects[$obj['cn']]=$obj;
170 }
171 $this->is_dialog=false;
172 unset($this->dialog);
173 $this->dialog=NULL;
174 }
175 }
177 /* Sort entries */
178 $tmp = $keys = array();
179 foreach($this->SubObjects as $key => $entry){
180 $keys[$key]=$key;
181 }
182 natcasesort($keys);
183 foreach($keys as $key){
184 $tmp[$key]=$this->SubObjects[$key];
185 }
186 $this->SubObjects = $tmp;
188 /* Cancel Dialog */
189 if(isset($_POST['CancelSubObject'])){
190 $this->is_dialog=false;
191 unset($this->dialog);
192 $this->dialog=NULL;
193 }
195 /* Print dialog if $this->dialog is set */
196 if($this->dialog){
197 $this->dialog->save_object();
198 $display = $this->dialog->execute();
199 return($display);
200 }
202 /* Divlist added 23.02.2006
203 Containing FAIscripts
204 */
205 $divlist = new divSelectBox("FAIscripts");
206 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
207 $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
208 foreach($this->getList(true) as $key => $name){
209 $divlist->AddEntry(array( array("string"=>$name['name']),
210 array("string"=>"<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' target='_blank'>download</a>"),
211 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),"attach"=>"style='border-right: 0px;width:40px;'")));
212 }
213 $smarty->assign("Entry_divlist",$divlist->DrawList());
215 /* Divlist creation complete
216 */
218 $smarty->assign("SubObjects",$this->getList());
219 $smarty->assign("SubObjectKeys",array_flip($this->getList()));
221 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
222 * If we post the escaped strings they will be escaped again
223 */
224 foreach($this->attributes as $attrs){
225 if(get_magic_quotes_gpc()){
226 $smarty->assign($attrs,stripslashes($this->$attrs));
227 }else{
228 $smarty->assign($attrs,($this->$attrs));
229 }
230 }
232 $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
233 return($display);
234 }
236 /* Generate listbox friendly SubObject list
237 */
238 function getList($use_dns=false){
239 $a_return=array();
240 foreach($this->SubObjects as $obj){
241 if($obj['status'] != "delete"){
242 if($use_dns){
243 if((isset($obj['description']))&&(!empty($obj['description']))){
244 $a_return[$obj['cn']]['name']= $obj['cn']." [".$obj['description']."]";
245 }else{
246 $a_return[$obj['cn']]['name']= $obj['cn'];
247 }
248 $a_return[$obj['cn']]['dn']= $obj['dn'];
249 }else{
250 if((isset($obj['description']))&&(!empty($obj['description']))){
251 $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
252 }else{
253 $a_return[$obj['cn']]= $obj['cn'];
254 }
255 }
256 }
257 }
258 return($a_return);
259 }
261 /* Delete me, and all my subtrees
262 */
263 function remove_from_parent()
264 {
265 $ldap = $this->config->get_ldap_link();
266 $ldap->cd ($this->dn);
267 $ldap->rmdir_recursive($this->dn);
268 $this->handle_post_events("remove");
269 }
272 /* Save data to object
273 */
274 function save_object()
275 {
276 if(isset($_POST['FAIscript_posted'])){
277 plugin::save_object();
278 foreach($this->attributes as $attrs){
279 if(isset($_POST[$attrs])){
280 $this->$attrs = $_POST[$attrs];
281 }
282 }
283 }
284 }
287 /* Check supplied data */
288 function check()
289 {
290 $message= array();
291 return ($message);
292 }
295 /* Save to LDAP */
296 function save()
297 {
298 plugin::save();
300 $ldap = $this->config->get_ldap_link();
302 $ldap->cat($this->dn);
303 if($ldap->count()!=0){
304 /* Write FAIscript to ldap*/
305 $ldap->cd($this->dn);
306 $this->cleanup();
307 $ldap->modify ($this->attrs);
309 }else{
310 /* Write FAIscript to ldap*/
311 $ldap->cd($this->config->current['BASE']);
312 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
313 $ldap->cd($this->dn);
314 $ldap->add($this->attrs);
315 }
316 show_ldap_error($ldap->get_error());
318 /* Prepare FAIscriptEntry to write it to ldap
319 * First sort array.
320 * Because we must delete old entries first.
321 * After deletion, we perform add and modify
322 */
323 $Objects = array();
324 foreach($this->SubObjects as $name => $obj){
325 if($obj['status'] == "delete"){
326 $Objects[$name] = $obj;
327 }
328 }
329 foreach($this->SubObjects as $name => $obj){
330 if($obj['status'] != "delete"){
331 $Objects[$name] = $obj;
332 }
333 }
335 foreach($Objects as $name => $obj){
337 foreach($this->sub64coded as $codeIt){
338 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
339 }
341 $tmp = array();
342 foreach($this->subAttributes as $attrs){
343 if(empty($obj[$attrs])){
344 $obj[$attrs] = array();
345 }
346 if(!is_array($obj[$attrs])){
347 $tmp[$attrs] = stripslashes($obj[$attrs]);
348 }else{
349 $tmp[$attrs] = $obj[$attrs];
350 }
351 }
353 $tmp['objectClass'] = $this->subClasses;
355 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
357 if($obj['status']=="new"){
358 $ldap->cat($sub_dn);
359 if($ldap->count()){
360 $obj['status']="modify";
361 }
362 }
364 if(empty($tmp['FAIpriority'])){
365 $tmp['FAIpriority'] ="0";
366 }
368 if($obj['status'] == "delete"){
369 $ldap->cd($sub_dn);
370 $ldap->rmdir_recursive($sub_dn);
371 $this->handle_post_events("remove");
372 }elseif($obj['status'] == "edited"){
373 $ldap->cd($sub_dn);
374 $this->cleanup();
375 $ldap->modify ($tmp);
377 $this->handle_post_events("modify");
378 }elseif($obj['status']=="new"){
379 if($tmp['description']==array()){
380 unset($tmp['description']);
381 }
382 if($tmp['FAIscript']==array()){
383 $tmp['FAIscript']=" ";
384 }
385 $ldap->cd($this->config->current['BASE']);
386 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
387 $ldap->cd($sub_dn);
388 $ldap->add($tmp);
389 $this->handle_post_events("add");
390 }
391 show_ldap_error($ldap->get_error());
392 }
393 }
396 function readBinary($attr,$dn){
397 $Data ="";
398 $ds= ldap_connect($this->config->current['SERVER']);
399 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
400 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
401 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
402 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
403 }
405 if(isset($this->config->current['TLS']) && $this->config->current['TLS'] == "true"){
406 ldap_start_tls($ds);
407 }
409 $r = ldap_bind($ds);
410 $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
412 if ($sr) {
413 $ei=ldap_first_entry($ds, $sr);
414 if ($ei) {
415 if ($info = ldap_get_values_len($ds, $ei, $attr)){
416 $Data= $info[0];
417 }
418 }
419 }
421 /* close conncetion */
422 ldap_unbind($ds);
423 return($Data);
424 }
427 }
429 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
430 ?>