1 <?php
3 class faiHook 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","FAIhook");
19 /* Class name of the Ldap ObjectClass for the Sub Object */
20 var $subClass = "FAIhookEntry";
21 var $subClasses = array("top","FAIclass","FAIhookEntry");
23 /* Class name of the php class which allows us to edit a Sub Object */
24 var $subClassName = "faiHookEntry";
26 /* Attributes to initialise for each subObject */
27 var $subAttributes = array("cn","description","FAItask");
28 var $sub_Load_Later = array("FAIscript");
29 var $sub64coded = array();
30 var $subBinary = array("FAIscript");
32 /* Specific attributes */
33 var $cn = ""; // The class name for this object
34 var $description = ""; // The description for this set of partitions
35 var $is_dialog = false; // specifies which buttons will be shown to save or abort
36 var $dialog = NULL; // a dialog, e.g. new disk dialog
37 var $SubObjects = array(); // All leafobjects of this object
39 var $FAIstate = "";
41 function faiHook ($config, $dn= NULL)
42 {
43 /* Load Attributes */
44 plugin::plugin ($config, $dn);
46 $this->acl ="#all#";
48 /* If "dn==new" we try to create a new entry
49 * Else we must read all objects from ldap which belong to this entry.
50 */
51 if($dn != "new"){
52 $this->dn =$dn;
54 /* Set acls
55 */
56 $ui = get_userinfo();
57 $acl = get_permissions ($this->dn, $ui->subtreeACL);
58 $acli = get_module_permission($acl, "FAIclass", $this->dn);
59 $this->acl=$acli;
61 /* Get FAIstate
62 */
63 if(isset($this->attrs['FAIstate'][0])){
64 $this->FAIstate = $this->attrs['FAIstate'][0];
65 }
67 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
68 */
69 $ldap = $this->config->get_ldap_link();
70 $ldap->cd ($this->dn);
71 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",array("dn"));
73 while($object = $ldap->fetch()){
75 /* Set status for save management */
76 $objects = array();
77 $objects['status'] = "FreshLoaded";
78 $objects['dn'] = $object['dn'];
79 $objects = $this->get_object_attributes($objects,$this->subAttributes);
80 $this->SubObjects[$objects['cn']] = $objects;
81 }
82 }
83 }
86 /* Reload some attributes */
87 function get_object_attributes($object,$attributes)
88 {
89 $ldap = $this->config->get_ldap_link();
90 $ldap->cd($this->config->current['BASE']);
91 $ldap->cat($object['dn'],$attributes);
92 $tmp = $ldap->fetch();
94 foreach($attributes as $attrs){
95 if(isset($tmp[$attrs][0])){
96 $var = $tmp[$attrs][0];
98 /* Check if we must decode some attributes */
99 if(in_array_ics($attrs,$this->sub64coded)){
100 $var = base64_decode($var);
101 }
103 /* check if this is a binary entry */
104 if(in_array_ics($attrs,$this->subBinary)){
105 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
106 }
108 /* Fix slashes */
109 $var = addslashes($var);
110 $object[$attrs] = $var;
111 }
112 }
113 return($object);
114 }
117 function getUsedFAItask($cn)
118 {
119 $ret = array();
120 foreach($this->SubObjects as $name => $class){
121 if($class['cn'] == $cn){
122 continue;
123 }
124 if($class['status'] != "delete"){
125 $ret[$class['FAItask']] = $class['FAItask'];
126 }
127 }
128 return($ret);
129 }
132 function execute()
133 {
134 /* Call parent execute */
135 plugin::execute();
137 /* Fill templating stuff */
138 $smarty= get_smarty();
139 $display= "";
141 /* New Listhandling
142 */
143 $once = true;
144 foreach($_POST as $name => $value){
145 if(preg_match("/^editscript_/",$name)&&($once)){
146 $once = false;
147 $entry = preg_replace("/^editscript_/","",$name);
148 $entry = base64_decode(preg_replace("/_.*/","",$entry));
150 $obj = $this->SubObjects[$entry];
151 if($obj['status'] == "FreshLoaded"){
152 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
153 }
155 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
156 $this->dialog->acl = $this->acl;
157 $_SESSION['objectinfo'] = $obj['dn'];
158 $this->dialog->parent = &$this;
159 $this->is_dialog=true;
160 }
161 if(preg_match("/^deletescript_/",$name)&&($once)){
162 $once = false;
163 $entry = preg_replace("/^deletescript_/","",$name);
164 $entry = base64_decode(preg_replace("/_.*/","",$entry));
166 $status = $this->SubObjects[$entry]['status'];
167 if($status == "edited" || $status == "FreshLoaded"){
168 $this->SubObjects[$entry]['status']= "delete";
169 }else{
170 unset($this->SubObjects[$entry]);
171 }
172 }
173 }
174 ///// Ende new list handling
176 /* Add new sub object */
177 if(isset($_POST['AddSubObject'])){
178 $this->dialog= new $this->subClassName($this->config,"new");
179 $this->dialog->acl = $this->acl;
180 $this->dialog->parent = &$this;
181 $this->is_dialog=true;
182 }
184 if($this->dn != "new"){
185 $_SESSION['objectinfo']= $this->dn;
186 }
188 /* Save Dialog */
189 if(isset($_POST['SaveSubObject'])){
191 /* Perform post check*/
192 $this->dialog->save_object();
194 /* Get messages */
195 $msgs = $this->dialog->check();
197 /* print errors */
198 if(count($msgs)>0){
199 foreach($msgs as $msg){
200 print_red($msg);
201 }
202 }else{
204 /* Get return object */
205 $obj = $this->dialog->save();
206 if(isset($obj['remove'])){
207 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
209 /* Depending on status, set new status */
210 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
211 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
212 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
213 unset($this->SubObjects[$obj['remove']['from']]);
214 }
215 $obj['status'] = "new";
216 $this->SubObjects[$obj['remove']['to']] = $obj;
217 unset($this->SubObjects[$obj['remove']['to']]['remove']);
218 }else{
219 if($obj['status'] == "FreshLoaded"){
220 $obj['status'] = "edited";
221 }
222 $this->SubObjects[$obj['cn']]=$obj;
223 }
225 $this->is_dialog=false;
226 unset($this->dialog);
227 $this->dialog=NULL;
228 }
229 }
231 /* Sort entries */
232 $tmp = $keys = array();
233 foreach($this->SubObjects as $key => $entry){
234 $keys[$key]=$key;
235 }
236 natcasesort($keys);
237 foreach($keys as $key){
238 $tmp[$key]=$this->SubObjects[$key];
239 }
240 $this->SubObjects = $tmp;
242 /* Cancel Dialog */
243 if(isset($_POST['CancelSubObject'])){
244 $this->is_dialog=false;
245 unset($this->dialog);
246 $this->dialog=NULL;
247 }
249 /* Print dialog if $this->dialog is set */
250 if($this->dialog){
251 $this->dialog->save_object();
252 $display = $this->dialog->execute();
253 return($display);
254 }
258 /* Divlist added 28.02.2006
259 Containing FAIscripts
260 */
262 $divlist = new divSelectBox("FAIhooks");
263 $divlist->setHeight(400);
264 if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
265 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
266 $img_remo = "";
267 }else{
268 $img_edit = "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
269 $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
270 }
272 foreach($this->getList(true) as $key => $name){
274 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
275 $down = "";
276 }else{
277 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
278 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
279 </a>";
280 }
282 $divlist->AddEntry(array( array("string"=>$name['name']),
283 array("string"=>$down , "attach" => "style='width:20px;'"),
284 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
285 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
286 }
287 $smarty->assign("Entry_divlist",$divlist->DrawList());
288 /* Divlist creation complete
289 */
291 $smarty->assign("SubObjects",$this->getList());
293 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
294 * If we post the escaped strings they will be escaped again
295 */
296 foreach($this->attributes as $attrs){
297 if(get_magic_quotes_gpc()){
298 $smarty->assign($attrs,stripslashes($this->$attrs));
299 }else{
300 $smarty->assign($attrs,($this->$attrs));
301 }
302 }
304 foreach($this->attributes as $attr){
305 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
306 }
308 $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
309 return($display);
310 }
312 /* Generate listbox friendly SubObject list
313 */
314 function getList($use_dns=false){
315 $a_return=array();
316 foreach($this->SubObjects as $obj){
317 if($obj['status'] != "delete"){
318 if($use_dns){
319 if((isset($obj['description']))&&(!empty($obj['description']))){
320 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
321 }else{
322 $a_return[$obj['cn']]['name']= $obj['cn'];
323 }
324 $a_return[$obj['cn']]['dn']= $obj['dn'];
325 }else{
326 if((isset($obj['description']))&&(!empty($obj['description']))){
327 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
328 }else{
329 $a_return[$obj['cn']]= $obj['cn'];
330 }
331 }
332 }
333 }
334 return($a_return);
335 }
338 /* Delete me, and all my subtrees
339 */
340 function remove_from_parent()
341 {
342 $ldap = $this->config->get_ldap_link();
343 $ldap->cd ($this->dn);
344 $ldap->rmdir_recursive($this->dn);
345 show_ldap_error($ldap->get_error(), _("Removing FAI hook base failed"));
346 $this->handle_post_events("remove");
347 }
350 /* Save data to object
351 */
352 function save_object()
353 {
354 if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
355 plugin::save_object();
356 foreach($this->attributes as $attrs){
357 if(isset($_POST[$attrs])){
358 $this->$attrs = $_POST[$attrs];
359 }
360 }
361 }
362 }
365 /* Check supplied data */
366 function check()
367 {
368 /* Call common method to give check the hook */
369 $message= plugin::check();
371 return ($message);
372 }
375 /* Save to LDAP */
376 function save()
377 {
378 plugin::save();
380 $ldap = $this->config->get_ldap_link();
382 $ldap->cat($this->dn,array("objectClass"));
383 if($ldap->count()!=0){
384 /* Write FAIscript to ldap*/
385 $ldap->cd($this->dn);
386 $this->cleanup();
387 $ldap->modify ($this->attrs);
389 }else{
390 /* Write FAIscript to ldap*/
391 $ldap->cd($this->config->current['BASE']);
392 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
393 $ldap->cd($this->dn);
394 $ldap->add($this->attrs);
395 }
396 show_ldap_error($ldap->get_error(), _("Saving FAI hook base failed"));
398 /* Do object tagging */
399 $this->handle_object_tagging();
401 $ldap->cd($this->dn);
403 /* Prepare FAIscriptEntry to write it to ldap
404 * First sort array.
405 * Because we must delete old entries first.
406 * After deletion, we perform add and modify
407 */
408 $Objects = array();
411 /* We do not need to save untouched objects */
412 foreach($this->SubObjects as $name => $obj){
413 if($obj['status'] == "FreshLoaded"){
414 unset($this->SubObjects[$name]);
415 }
416 }
418 /* Add objects that must be removed first.*/
419 foreach($this->SubObjects as $name => $obj){
420 if($obj['status'] == "delete"){
421 $Objects[$name] = $obj;
422 }
423 }
425 /* Add objects to add/modify */
426 foreach($this->SubObjects as $name => $obj){
427 if($obj['status'] != "delete"){
428 $Objects[$name] = $obj;
429 }
430 }
432 /* Walk through list of objects */
433 foreach($Objects as $name => $obj){
435 /* Encode attribtues if required */
436 foreach($this->sub64coded as $codeIt){
437 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
438 }
440 /* Create ldap entry */
441 $tmp = array();
442 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
443 foreach($attributes as $attrs){
445 if(empty($obj[$attrs])){
446 $obj[$attrs] = array();
447 }
448 if(!is_array($obj[$attrs])){
449 $tmp[$attrs] = stripslashes($obj[$attrs]);
450 }else{
451 $tmp[$attrs] = $obj[$attrs];
452 }
453 }
455 $tmp['objectClass'] = $this->subClasses;
457 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
459 if($obj['status']=="new"){
460 $ldap->cat($sub_dn,array("objectClass"));
461 if($ldap->count()){
462 $obj['status']="edited";
463 }
464 }
466 /* Check if gosaAdministrativeUnitTag is required as object class */
467 if($obj['status'] == "edited"){
468 $ldap->cat($sub_dn,array("objectClass"));
469 $attrs = $ldap->fetch();
470 if(isset($attrs['objectClass'])){
471 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
472 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
473 }
474 }
475 }
477 if($obj['status'] == "delete"){
478 $ldap->cd($sub_dn);
479 $ldap->rmdir_recursive($sub_dn);
480 $this->handle_post_events("remove");
481 show_ldap_error($ldap->get_error(), _("Removing FAI hook failed"));
482 }elseif($obj['status'] == "edited"){
483 $ldap->cd($sub_dn);
484 $this->cleanup();
485 $ldap->modify ($tmp);
486 $this->handle_post_events("modify");
487 show_ldap_error($ldap->get_error(), _("Saving FAI hook failed"));
488 }elseif($obj['status']=="new"){
489 if($tmp['description']==array()){
490 unset($tmp['description']);
491 }
492 $ldap->cd($this->config->current['BASE']);
493 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
494 $ldap->cd($sub_dn);
495 $ldap->add($tmp);
496 $this->handle_post_events("add");
497 show_ldap_error($ldap->get_error(), _("Saving FAI hook failed"));
498 }
499 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
500 }
501 }
502 }
504 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
505 ?>