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