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
38 var $view_logged = FALSE;
39 var $FAIstate = "";
40 var $ui;
42 function faiHook ($config, $dn= NULL)
43 {
44 /* Load Attributes */
45 plugin::plugin ($config, $dn);
47 /* If "dn==new" we try to create a new entry
48 * Else we must read all objects from ldap which belong to this entry.
49 */
50 if($dn != "new"){
51 $this->dn =$dn;
53 /* Get FAIstate
54 */
55 if(isset($this->attrs['FAIstate'][0])){
56 $this->FAIstate = $this->attrs['FAIstate'][0];
57 }
59 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
60 */
61 $ldap = $this->config->get_ldap_link();
62 $ldap->cd ($this->dn);
63 $attrs_to_search = $this->subAttributes;
64 $attrs_to_search[] = "FAIstate";
65 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))", $attrs_to_search);
67 while($object = $ldap->fetch()){
69 /* Skip objects, that are tagged as removed */
70 if(isset($object['FAIstate'][0])){
71 if(preg_match("/removed$/",$object['FAIstate'][0])){
72 continue;
73 }
74 }
76 /* Set status for save management */
77 $objects = array();
78 $objects['status'] = "FreshLoaded";
79 $objects['dn'] = $object['dn'];
80 $objects = $this->get_object_attributes($objects,$this->subAttributes);
81 $this->SubObjects[$objects['cn']] = $objects;
82 }
83 }
84 $this->ui = get_userinfo();
85 }
88 /* Reload some attributes */
89 function get_object_attributes($object,$attributes)
90 {
91 $ldap = $this->config->get_ldap_link();
92 $ldap->cd($this->config->current['BASE']);
93 $ldap->cat($object['dn'],$attributes);
94 $tmp = $ldap->fetch();
96 foreach($attributes as $attrs){
97 if(isset($tmp[$attrs][0])){
98 $var = $tmp[$attrs][0];
100 /* Check if we must decode some attributes */
101 if(in_array_ics($attrs,$this->sub64coded)){
102 $var = base64_decode($var);
103 }
105 /* check if this is a binary entry */
106 if(in_array_ics($attrs,$this->subBinary)){
107 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
108 }
110 /* Fix slashes */
111 $var = addslashes($var);
112 $object[$attrs] = $var;
113 }
114 }
115 return($object);
116 }
119 function acl_base_for_current_object($dn)
120 {
121 if($dn == "new"){
122 if($this->dn == "new"){
123 $dn= $_SESSION['CurrentMainBase'];
124 }else{
125 $dn = $this->dn;
126 }
127 }
128 return($dn);
129 }
132 function getUsedFAItask($cn)
133 {
134 $ret = array();
135 foreach($this->SubObjects as $name => $class){
136 if($class['cn'] == $cn){
137 continue;
138 }
139 if($class['status'] != "delete"){
140 $ret[$class['FAItask']] = $class['FAItask'];
141 }
142 }
143 return($ret);
144 }
146 function execute()
147 {
148 /* Call parent execute */
149 plugin::execute();
151 if($this->is_account && !$this->view_logged){
152 $this->view_logged = TRUE;
153 new log("view","fai/".get_class($this),$this->dn);
154 }
156 /* Fill templating stuff */
157 $smarty= get_smarty();
158 $display= "";
160 /* New Listhandling
161 */
162 $once = true;
163 foreach($_POST as $name => $value){
164 if(preg_match("/^editscript_/",$name)&&($once)){
165 $once = false;
166 $entry = preg_replace("/^editscript_/","",$name);
167 $entry = base64_decode(preg_replace("/_.*/","",$entry));
169 $obj = $this->SubObjects[$entry];
170 if($obj['status'] == "FreshLoaded"){
171 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
172 }
174 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
175 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
176 $this->dialog->set_acl_category("fai");
178 $_SESSION['objectinfo'] = $obj['dn'];
179 $this->dialog->parent = &$this;
180 $this->is_dialog=true;
181 }
182 if(preg_match("/^deletescript_/",$name)&&($once)){
183 $entry = preg_replace("/^deletescript_/","",$name);
184 $entry = base64_decode(preg_replace("/_.*/","",$entry));
186 $dn = $this->acl_base_for_current_object($this->SubObjects[$entry]['dn']);
187 $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry") ;
189 if(preg_match("/d/",$acl)){
190 $once = false;
192 $status = $this->SubObjects[$entry]['status'];
193 if($status == "edited" || $status == "FreshLoaded"){
194 $this->SubObjects[$entry]['status']= "delete";
195 }else{
196 unset($this->SubObjects[$entry]);
197 }
198 }
199 }
200 }
201 ///// Ende new list handling
203 /* Add new sub object */
204 if(isset($_POST['AddSubObject'])){
206 $c_dn = $this->acl_base_for_current_object($this->dn);
207 $this->dialog= new $this->subClassName($this->config,"new");
208 $this->dialog->set_acl_base($c_dn);
209 $this->dialog->set_acl_category("fai");
210 $this->dialog->parent = &$this;
211 $this->is_dialog=true;
212 }
215 /* Save Dialog */
216 if(isset($_POST['SaveSubObject'])){
218 /* Perform post check*/
219 $this->dialog->save_object();
221 /* Get messages */
222 $msgs = $this->dialog->check();
224 /* print errors */
225 if(count($msgs)>0){
226 foreach($msgs as $msg){
227 print_red($msg);
228 }
229 }else{
231 /* Get return object */
232 $obj = $this->dialog->save();
233 if(isset($obj['remove'])){
235 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
237 /* Depending on status, set new status */
238 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
239 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
240 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
241 unset($this->SubObjects[$obj['remove']['from']]);
242 }
243 $obj['status'] = "new";
244 $this->SubObjects[$obj['remove']['to']] = $obj;
245 unset($this->SubObjects[$obj['remove']['to']]['remove']);
246 }else{
247 if($obj['status'] == "FreshLoaded"){
248 $obj['status'] = "edited";
249 }
250 $this->SubObjects[$obj['cn']]=$obj;
251 }
252 $this->is_dialog=false;
253 unset($this->dialog);
254 $this->dialog=NULL;
255 }
256 }
258 /* Sort entries */
259 $tmp = $keys = array();
260 foreach($this->SubObjects as $key => $entry){
261 $keys[$key]=$key;
262 }
263 natcasesort($keys);
264 foreach($keys as $key){
265 $tmp[$key]=$this->SubObjects[$key];
266 }
267 $this->SubObjects = $tmp;
269 /* Cancel Dialog */
270 if(isset($_POST['CancelSubObject'])){
271 $this->is_dialog=false;
272 unset($this->dialog);
273 $this->dialog=NULL;
274 }
276 /* Print dialog if $this->dialog is set */
277 if($this->dialog){
278 $this->dialog->save_object();
279 $display = $this->dialog->execute();
280 return($display);
281 }
285 /* Divlist added 28.02.2006
286 Containing FAIscripts
287 */
289 $divlist = new divSelectBox("FAIhooks");
290 $divlist->setHeight(400);
292 foreach($this->getList(true) as $key => $name){
294 $dn= $this->acl_base_for_current_object($name['dn']);
295 $acl = $this->ui->get_permissions($dn,"fai/faiHookEntry") ;
296 $act = "";
298 /* Check if this object is freezed, in this case hide the delete icon */
299 if($this->FAIstate == "freeze"){
300 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
301 }else{
302 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
303 if(preg_match("/d/",$acl)){
304 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
305 }
306 }
308 /* Check if we are allowed to use the export button for this object */
309 $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","FAIscript") ;
310 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
311 $down = "";
312 }else{
313 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
314 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
315 </a>";
316 }
318 /* Check if we are allowed to view the object */
319 $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","cn") ;
320 if(preg_match("/r/",$s_acl)){
321 $divlist->AddEntry(array( array("string"=>$name['name']),
322 array("string"=>$down , "attach" => "style='width:20px;'"),
323 array("string"=>str_replace("%s",base64_encode($key),$act),
324 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
325 }
326 }
327 $smarty->assign("Entry_divlist",$divlist->DrawList());
328 /* Divlist creation complete
329 */
331 $smarty->assign("SubObjects",$this->getList());
333 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
334 * If we post the escaped strings they will be escaped again
335 */
336 foreach($this->attributes as $attrs){
337 if(get_magic_quotes_gpc()){
338 $smarty->assign($attrs,stripslashes($this->$attrs));
339 }else{
340 $smarty->assign($attrs,($this->$attrs));
341 }
342 }
344 $tmp = $this->plInfo();
346 $c_dn = $this->acl_base_for_current_object($this->dn);
347 $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiHookEntry")) && $this->FAIstate!="freeze");
348 foreach($tmp['plProvidedAcls'] as $name => $translation){
349 $smarty->assign($name."ACL",$this->getacl($name));
350 }
352 $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
353 return($display);
354 }
356 /* Generate listbox friendly SubObject list
357 */
358 function getList($use_dns=false){
359 $a_return=array();
360 foreach($this->SubObjects as $obj){
361 if($obj['status'] != "delete"){
362 if($use_dns){
363 if((isset($obj['description']))&&(!empty($obj['description']))){
364 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
365 }else{
366 $a_return[$obj['cn']]['name']= $obj['cn'];
367 }
368 $a_return[$obj['cn']]['dn']= $obj['dn'];
369 }else{
370 if((isset($obj['description']))&&(!empty($obj['description']))){
371 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
372 }else{
373 $a_return[$obj['cn']]= $obj['cn'];
374 }
375 }
376 }
377 }
378 return($a_return);
379 }
382 /* Delete me, and all my subtrees
383 */
384 function remove_from_parent()
385 {
386 $ldap = $this->config->get_ldap_link();
387 $ldap->cd ($this->dn);
389 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
390 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
391 if($_SESSION['faifilter']['branch'] == "main"){
392 $use_dn = $this->dn;
393 }
395 prepare_to_save_FAI_object($use_dn,array(),true);
397 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
399 foreach($this->SubObjects as $name => $obj){
400 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
401 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
402 if($_SESSION['faifilter']['branch'] == "main"){
403 $use_dn = $obj['dn'];
404 }
405 prepare_to_save_FAI_object($use_dn,array(),true);
406 }
407 $this->handle_post_events("remove");
408 }
411 /* Save data to object
412 */
413 function save_object()
414 {
415 if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
416 plugin::save_object();
417 foreach($this->attributes as $attrs){
418 if(isset($_POST[$attrs])){
419 $this->$attrs = $_POST[$attrs];
420 }
421 }
422 }
423 }
426 /* Check supplied data */
427 function check()
428 {
429 /* Call common method to give check the hook */
430 $message= plugin::check();
432 return ($message);
433 }
436 /* Save to LDAP */
437 function save()
438 {
439 plugin::save();
441 $ldap = $this->config->get_ldap_link();
443 prepare_to_save_FAI_object($this->dn,$this->attrs);
444 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/hook with dn '%s' failed."),$this->dn));
446 if($this->initially_was_account){
447 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
448 }else{
449 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
450 }
452 /* Do object tagging */
453 $this->handle_object_tagging();
455 $ldap->cd($this->dn);
457 /* Prepare FAIscriptEntry to write it to ldap
458 * First sort array.
459 * Because we must delete old entries first.
460 * After deletion, we perform add and modify
461 */
462 $Objects = array();
464 /* We do not need to save untouched objects */
465 foreach($this->SubObjects as $name => $obj){
466 if($obj['status'] == "FreshLoaded"){
467 unset($this->SubObjects[$name]);
468 }
469 }
471 foreach($this->SubObjects as $name => $obj){
472 if($obj['status'] == "delete"){
473 $Objects[$name] = $obj;
474 }
475 }
476 foreach($this->SubObjects as $name => $obj){
477 if($obj['status'] != "delete"){
478 $Objects[$name] = $obj;
479 }
480 }
482 foreach($Objects as $name => $obj){
484 foreach($this->sub64coded as $codeIt){
485 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
486 }
488 $tmp = array();
489 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
490 foreach($attributes as $attrs){
491 if(empty($obj[$attrs])){
492 $obj[$attrs] = array();
493 }
494 if(!is_array($obj[$attrs])){
495 $tmp[$attrs] = stripslashes($obj[$attrs]);
496 }else{
497 $tmp[$attrs] = $obj[$attrs];
498 }
499 }
501 $tmp['objectClass'] = $this->subClasses;
503 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
505 if($obj['status']=="new"){
506 $ldap->cat($sub_dn,array("objectClass"));
507 if($ldap->count()){
508 $obj['status']="edited";
509 }
510 }
512 /* Check if gosaAdministrativeUnitTag is required as object class */
513 if($obj['status'] == "edited"){
514 $ldap->cat($sub_dn,array("objectClass"));
515 $attrs = $ldap->fetch();
516 if(isset($attrs['objectClass'])){
517 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
518 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
519 }
520 }
521 }
523 if($obj['status'] == "delete"){
524 prepare_to_save_FAI_object($sub_dn,array(),true);
525 $this->handle_post_events("remove");
526 }elseif($obj['status'] == "edited"){
527 prepare_to_save_FAI_object($sub_dn,$tmp);
528 $this->handle_post_events("modify");
529 }elseif($obj['status']=="new"){
530 prepare_to_save_FAI_object($sub_dn,$tmp);
531 $this->handle_post_events("add");
532 }
533 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
534 }
535 }
537 /* Return plugin informations for acl handling */
538 function plInfo()
539 {
540 return (array(
541 "plShortName" => _("Hook"),
542 "plDescription" => _("FAI hook"),
543 "plSelfModify" => FALSE,
544 "plDepends" => array(),
545 "plPriority" => 20,
546 "plSection" => array("administration"),
547 "plCategory" => array("fai"),
548 "plProvidedAcls" => array(
549 "cn" => _("Name")." ("._("Read only").")",
550 "description" => _("Description"))
551 ));
552 }
553 }
555 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
556 ?>