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 = "";
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();
86 @log::log("view","fai/".get_class($this),$this->dn);
87 }
90 /* Reload some attributes */
91 function get_object_attributes($object,$attributes)
92 {
93 $ldap = $this->config->get_ldap_link();
94 $ldap->cd($this->config->current['BASE']);
95 $ldap->cat($object['dn'],$attributes);
96 $tmp = $ldap->fetch();
98 foreach($attributes as $attrs){
99 if(isset($tmp[$attrs][0])){
100 $var = $tmp[$attrs][0];
102 /* Check if we must decode some attributes */
103 if(in_array_ics($attrs,$this->sub64coded)){
104 $var = base64_decode($var);
105 }
107 /* check if this is a binary entry */
108 if(in_array_ics($attrs,$this->subBinary)){
109 $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
110 }
112 /* Fix slashes */
113 $var = addslashes($var);
114 $object[$attrs] = $var;
115 }
116 }
117 return($object);
118 }
121 function acl_base_for_current_object($dn)
122 {
123 if($dn == "new"){
124 if($this->dn == "new"){
125 $dn= $_SESSION['CurrentMainBase'];
126 }else{
127 $dn = $this->dn;
128 }
129 }
130 return($dn);
131 }
134 function getUsedFAItask($cn)
135 {
136 $ret = array();
137 foreach($this->SubObjects as $name => $class){
138 if($class['cn'] == $cn){
139 continue;
140 }
141 if($class['status'] != "delete"){
142 $ret[$class['FAItask']] = $class['FAItask'];
143 }
144 }
145 return($ret);
146 }
148 function execute()
149 {
150 /* Call parent execute */
151 plugin::execute();
153 /* Fill templating stuff */
154 $smarty= get_smarty();
155 $display= "";
157 /* New Listhandling
158 */
159 $once = true;
160 foreach($_POST as $name => $value){
161 if(preg_match("/^editscript_/",$name)&&($once)){
162 $once = false;
163 $entry = preg_replace("/^editscript_/","",$name);
164 $entry = base64_decode(preg_replace("/_.*/","",$entry));
166 $obj = $this->SubObjects[$entry];
167 if($obj['status'] == "FreshLoaded"){
168 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
169 }
171 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
172 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
173 $this->dialog->set_acl_category("fai");
175 $_SESSION['objectinfo'] = $obj['dn'];
176 $this->dialog->parent = &$this;
177 $this->is_dialog=true;
178 }
179 if(preg_match("/^deletescript_/",$name)&&($once)){
180 $entry = preg_replace("/^deletescript_/","",$name);
181 $entry = base64_decode(preg_replace("/_.*/","",$entry));
183 $dn = $this->acl_base_for_current_object($this->SubObjects[$entry]['dn']);
184 $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry") ;
186 if(preg_match("/d/",$acl)){
187 $once = false;
189 $status = $this->SubObjects[$entry]['status'];
190 if($status == "edited" || $status == "FreshLoaded"){
191 $this->SubObjects[$entry]['status']= "delete";
192 }else{
193 unset($this->SubObjects[$entry]);
194 }
195 }
196 }
197 }
198 ///// Ende new list handling
200 /* Add new sub object */
201 if(isset($_POST['AddSubObject'])){
203 $c_dn = $this->acl_base_for_current_object($this->dn);
204 $this->dialog= new $this->subClassName($this->config,"new");
205 $this->dialog->set_acl_base($c_dn);
206 $this->dialog->set_acl_category("fai");
207 $this->dialog->parent = &$this;
208 $this->is_dialog=true;
209 }
212 /* Save Dialog */
213 if(isset($_POST['SaveSubObject'])){
215 /* Perform post check*/
216 $this->dialog->save_object();
218 /* Get messages */
219 $msgs = $this->dialog->check();
221 /* print errors */
222 if(count($msgs)>0){
223 foreach($msgs as $msg){
224 print_red($msg);
225 }
226 }else{
228 /* Get return object */
229 $obj = $this->dialog->save();
230 if(isset($obj['remove'])){
232 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
234 /* Depending on status, set new status */
235 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
236 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
237 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
238 unset($this->SubObjects[$obj['remove']['from']]);
239 }
240 $obj['status'] = "new";
241 $this->SubObjects[$obj['remove']['to']] = $obj;
242 unset($this->SubObjects[$obj['remove']['to']]['remove']);
243 }else{
244 if($obj['status'] == "FreshLoaded"){
245 $obj['status'] = "edited";
246 }
247 $this->SubObjects[$obj['cn']]=$obj;
248 }
249 $this->is_dialog=false;
250 unset($this->dialog);
251 $this->dialog=NULL;
252 }
253 }
255 /* Sort entries */
256 $tmp = $keys = array();
257 foreach($this->SubObjects as $key => $entry){
258 $keys[$key]=$key;
259 }
260 natcasesort($keys);
261 foreach($keys as $key){
262 $tmp[$key]=$this->SubObjects[$key];
263 }
264 $this->SubObjects = $tmp;
266 /* Cancel Dialog */
267 if(isset($_POST['CancelSubObject'])){
268 $this->is_dialog=false;
269 unset($this->dialog);
270 $this->dialog=NULL;
271 }
273 /* Print dialog if $this->dialog is set */
274 if($this->dialog){
275 $this->dialog->save_object();
276 $display = $this->dialog->execute();
277 return($display);
278 }
282 /* Divlist added 28.02.2006
283 Containing FAIscripts
284 */
286 $divlist = new divSelectBox("FAIhooks");
287 $divlist->setHeight(400);
289 foreach($this->getList(true) as $key => $name){
291 $dn= $this->acl_base_for_current_object($name['dn']);
292 $acl = $this->ui->get_permissions($dn,"fai/faiHookEntry") ;
293 $act = "";
295 /* Check if this object is freezed, in this case hide the delete icon */
296 if($this->FAIstate == "freeze"){
297 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
298 }else{
299 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("edit")."' alt='"._("edit")."'>";
300 if(preg_match("/d/",$acl)){
301 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("delete")."' alt='"._("delete")."'>";
302 }
303 }
305 /* Check if we are allowed to use the export button for this object */
306 $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","FAIscript") ;
307 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
308 $down = "";
309 }else{
310 $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
311 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
312 </a>";
313 }
315 /* Check if we are allowed to view the object */
316 $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","cn") ;
317 if(preg_match("/r/",$s_acl)){
318 $divlist->AddEntry(array( array("string"=>$name['name']),
319 array("string"=>$down , "attach" => "style='width:20px;'"),
320 array("string"=>str_replace("%s",base64_encode($key),$act),
321 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
322 }
323 }
324 $smarty->assign("Entry_divlist",$divlist->DrawList());
325 /* Divlist creation complete
326 */
328 $smarty->assign("SubObjects",$this->getList());
330 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
331 * If we post the escaped strings they will be escaped again
332 */
333 foreach($this->attributes as $attrs){
334 if(get_magic_quotes_gpc()){
335 $smarty->assign($attrs,stripslashes($this->$attrs));
336 }else{
337 $smarty->assign($attrs,($this->$attrs));
338 }
339 }
341 $tmp = $this->plInfo();
343 $c_dn = $this->acl_base_for_current_object($this->dn);
344 $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiHookEntry")) && $this->FAIstate!="freeze");
345 foreach($tmp['plProvidedAcls'] as $name => $translation){
346 $smarty->assign($name."ACL",$this->getacl($name));
347 }
349 $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
350 return($display);
351 }
353 /* Generate listbox friendly SubObject list
354 */
355 function getList($use_dns=false){
356 $a_return=array();
357 foreach($this->SubObjects as $obj){
358 if($obj['status'] != "delete"){
359 if($use_dns){
360 if((isset($obj['description']))&&(!empty($obj['description']))){
361 $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
362 }else{
363 $a_return[$obj['cn']]['name']= $obj['cn'];
364 }
365 $a_return[$obj['cn']]['dn']= $obj['dn'];
366 }else{
367 if((isset($obj['description']))&&(!empty($obj['description']))){
368 $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
369 }else{
370 $a_return[$obj['cn']]= $obj['cn'];
371 }
372 }
373 }
374 }
375 return($a_return);
376 }
379 /* Delete me, and all my subtrees
380 */
381 function remove_from_parent()
382 {
383 $ldap = $this->config->get_ldap_link();
384 $ldap->cd ($this->dn);
386 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
387 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
388 if($_SESSION['faifilter']['branch'] == "main"){
389 $use_dn = $this->dn;
390 }
392 prepare_to_save_FAI_object($use_dn,array(),true);
394 @log::log("remove","fai/".get_class($this),$use_dn,$this->attributes);
396 foreach($this->SubObjects as $name => $obj){
397 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
398 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
399 if($_SESSION['faifilter']['branch'] == "main"){
400 $use_dn = $obj['dn'];
401 }
402 prepare_to_save_FAI_object($use_dn,array(),true);
403 }
404 $this->handle_post_events("remove");
405 }
408 /* Save data to object
409 */
410 function save_object()
411 {
412 if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
413 plugin::save_object();
414 foreach($this->attributes as $attrs){
415 if(isset($_POST[$attrs])){
416 $this->$attrs = $_POST[$attrs];
417 }
418 }
419 }
420 }
423 /* Check supplied data */
424 function check()
425 {
426 /* Call common method to give check the hook */
427 $message= plugin::check();
429 return ($message);
430 }
433 /* Save to LDAP */
434 function save()
435 {
436 plugin::save();
438 $ldap = $this->config->get_ldap_link();
440 prepare_to_save_FAI_object($this->dn,$this->attrs);
441 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/hook with dn '%s' failed."),$this->dn));
443 if($this->initially_was_account){
444 @log::log("modify","fai/".get_class($this),$this->dn,$this->attributes);
445 }else{
446 @log::log("create","fai/".get_class($this),$this->dn,$this->attributes);
447 }
449 /* Do object tagging */
450 $this->handle_object_tagging();
452 $ldap->cd($this->dn);
454 /* Prepare FAIscriptEntry to write it to ldap
455 * First sort array.
456 * Because we must delete old entries first.
457 * After deletion, we perform add and modify
458 */
459 $Objects = array();
461 /* We do not need to save untouched objects */
462 foreach($this->SubObjects as $name => $obj){
463 if($obj['status'] == "FreshLoaded"){
464 unset($this->SubObjects[$name]);
465 }
466 }
468 foreach($this->SubObjects as $name => $obj){
469 if($obj['status'] == "delete"){
470 $Objects[$name] = $obj;
471 }
472 }
473 foreach($this->SubObjects as $name => $obj){
474 if($obj['status'] != "delete"){
475 $Objects[$name] = $obj;
476 }
477 }
479 foreach($Objects as $name => $obj){
481 foreach($this->sub64coded as $codeIt){
482 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
483 }
485 $tmp = array();
486 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
487 foreach($attributes as $attrs){
488 if(empty($obj[$attrs])){
489 $obj[$attrs] = array();
490 }
491 if(!is_array($obj[$attrs])){
492 $tmp[$attrs] = stripslashes($obj[$attrs]);
493 }else{
494 $tmp[$attrs] = $obj[$attrs];
495 }
496 }
498 $tmp['objectClass'] = $this->subClasses;
500 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
502 if($obj['status']=="new"){
503 $ldap->cat($sub_dn,array("objectClass"));
504 if($ldap->count()){
505 $obj['status']="edited";
506 }
507 }
509 /* Check if gosaAdministrativeUnitTag is required as object class */
510 if($obj['status'] == "edited"){
511 $ldap->cat($sub_dn,array("objectClass"));
512 $attrs = $ldap->fetch();
513 if(isset($attrs['objectClass'])){
514 if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
515 $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
516 }
517 }
518 }
520 if($obj['status'] == "delete"){
521 prepare_to_save_FAI_object($sub_dn,array(),true);
522 $this->handle_post_events("remove");
523 }elseif($obj['status'] == "edited"){
524 prepare_to_save_FAI_object($sub_dn,$tmp);
525 $this->handle_post_events("modify");
526 }elseif($obj['status']=="new"){
527 prepare_to_save_FAI_object($sub_dn,$tmp);
528 $this->handle_post_events("add");
529 }
530 $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
531 }
532 }
534 /* Return plugin informations for acl handling */
535 function plInfo()
536 {
537 return (array(
538 "plShortName" => _("Hook"),
539 "plDescription" => _("FAI hook"),
540 "plSelfModify" => FALSE,
541 "plDepends" => array(),
542 "plPriority" => 20,
543 "plSection" => array("administration"),
544 "plCategory" => array("fai"),
545 "plProvidedAcls" => array(
546 "cn" => _("Name")." ("._("Read only").")",
547 "description" => _("Description"))
548 ));
549 }
550 }
552 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
553 ?>