1 <?php
3 class faiHook extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account = TRUE;
8 /* Attributes for this Object */
9 var $attributes = array("cn","description");
11 /* ObjectClasses for this Object*/
12 var $objectclasses = array("top","FAIclass","FAIhook");
14 /* Class name of the Ldap ObjectClass for the Sub Object */
15 var $subClass = "FAIhookEntry";
16 var $subClasses = array("top","FAIclass","FAIhookEntry");
18 /* Class name of the php class which allows us to edit a Sub Object */
19 var $subClassName = "faiHookEntry";
21 /* Attributes to initialise for each subObject */
22 var $subAttributes = array("cn","description","FAItask");
23 var $sub_Load_Later = array("FAIscript");
24 var $sub64coded = array();
25 var $subBinary = array("FAIscript");
27 /* Specific attributes */
28 var $cn = ""; // The class name for this object
29 var $description = ""; // The description for this set of partitions
30 var $is_dialog = false; // specifies which buttons will be shown to save or abort
31 var $SubObjects = array(); // All leafobjects of this object
32 var $view_logged = FALSE;
33 var $FAIstate = "";
34 var $ui;
36 function faiHook (&$config, $dn= NULL)
37 {
38 /* Load Attributes */
39 plugin::plugin ($config, $dn);
41 /* If "dn==new" we try to create a new entry
42 * Else we must read all objects from ldap which belong to this entry.
43 */
44 if($dn != "new"){
45 $this->dn =$dn;
47 /* Get FAIstate
48 */
49 if(isset($this->attrs['FAIstate'][0])){
50 $this->FAIstate = $this->attrs['FAIstate'][0];
51 }
53 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
54 */
55 $ldap = $this->config->get_ldap_link();
56 $ldap->cd ($this->dn);
57 $attrs_to_search = $this->subAttributes;
58 $attrs_to_search[] = "FAIstate";
59 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))", $attrs_to_search);
61 $data = array();
62 while($object = $ldap->fetch()){
63 $data[] = $object;
64 }
65 foreach($data as $object){
67 /* Skip objects, that are tagged as removed */
68 if(isset($object['FAIstate'][0])){
69 if(preg_match("/removed$/",$object['FAIstate'][0])){
70 continue;
71 }
72 }
74 /* Set status for save management */
75 $objects = array();
76 $objects['status'] = "FreshLoaded";
77 $objects['dn'] = $object['dn'];
78 $objects = $this->get_object_attributes($objects,$this->subAttributes);
79 $this->SubObjects[$objects['cn']] = $objects;
80 }
81 }
82 $this->is_new = FALSE;
83 if($this->dn == "new"){
84 $this->is_new =TRUE;
85 }
86 $this->ui = get_userinfo();
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::get('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 if($this->is_account && !$this->view_logged){
154 $this->view_logged = TRUE;
155 new log("view","fai/".get_class($this),$this->dn);
156 }
158 /* Fill templating stuff */
159 $smarty= get_smarty();
160 $display= "";
162 /* New Listhandling
163 */
164 $once = true;
165 foreach($_POST as $name => $value){
166 if(preg_match("/^editscript_/",$name)&&($once)){
167 $once = false;
168 $entry = preg_replace("/^editscript_/","",$name);
169 $entry = base64_decode(preg_replace("/_.*/","",$entry));
171 $obj = $this->SubObjects[$entry];
172 if($obj['status'] == "FreshLoaded"){
173 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
174 }
176 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
177 $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
178 $this->dialog->set_acl_category("fai");
180 session::set('objectinfo',$obj['dn']);
181 $this->dialog->parent = &$this;
182 $this->is_dialog=true;
183 }
184 if(preg_match("/^deletescript_/",$name)&&($once)){
185 $entry = preg_replace("/^deletescript_/","",$name);
186 $entry = base64_decode(preg_replace("/_.*/","",$entry));
188 $dn = $this->acl_base_for_current_object($this->SubObjects[$entry]['dn']);
189 $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry") ;
191 if(preg_match("/d/",$acl)){
192 $once = false;
194 $status = $this->SubObjects[$entry]['status'];
195 if($status == "edited" || $status == "FreshLoaded"){
196 $this->SubObjects[$entry]['status']= "delete";
197 }else{
198 unset($this->SubObjects[$entry]);
199 }
200 }
201 }
202 }
204 /* File download requested */
205 if(isset($_GET['getFAIHook'])){
206 if(isset($this->SubObjects[base64_decode($_GET['getFAIHook'])])){
207 $obj = $this->SubObjects[base64_decode($_GET['getFAIHook'])];
208 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
209 send_binary_content(stripslashes($obj['FAIhook']),$obj['cn'].".FAIhook");
210 }
211 }
213 /* Edit entries via GET */
214 if(isset($_GET['act']) && isset($_GET['id'])){
215 if($_GET['act'] == "edit" && isset($this->SubObjects[base64_decode($_GET['id'])])){
216 $obj = $this->SubObjects[base64_decode($_GET['id'])];
217 if($obj['status'] == "FreshLoaded"){
218 $obj = $this->get_object_attributes($obj,$this->sub_Load_Later);
219 }
220 $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
221 $this->dialog->acl = $this->acl;
222 session::set('objectinfo',$obj['dn']);
223 $this->dialog->parent = &$this;
224 $this->is_dialog=true;
225 }
226 }
228 ///// Ende new list handling
230 /* Add new sub object */
231 if(isset($_POST['AddSubObject'])){
233 $c_dn = $this->acl_base_for_current_object($this->dn);
234 $this->dialog= new $this->subClassName($this->config,"new");
235 $this->dialog->set_acl_base($c_dn);
236 $this->dialog->set_acl_category("fai");
237 $this->dialog->parent = &$this;
238 $this->is_dialog=true;
239 }
242 /* Save Dialog */
243 if(isset($_POST['SaveSubObject'])){
245 /* Perform post check*/
246 $this->dialog->save_object();
248 /* Get messages */
249 $msgs = $this->dialog->check();
251 /* print errors */
252 if(count($msgs)>0){
253 foreach($msgs as $msg){
254 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
255 }
256 }else{
258 /* Get return object */
259 $obj = $this->dialog->save();
260 if(isset($obj['remove'])){
262 $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
264 /* Depending on status, set new status */
265 if($old_stat == "edited" || $old_stat == "FreshLoaded"){
266 $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
267 }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
268 unset($this->SubObjects[$obj['remove']['from']]);
269 }
270 $obj['status'] = "new";
271 $this->SubObjects[$obj['remove']['to']] = $obj;
272 unset($this->SubObjects[$obj['remove']['to']]['remove']);
273 }else{
274 if($obj['status'] == "FreshLoaded"){
275 $obj['status'] = "edited";
276 }
277 $this->SubObjects[$obj['cn']]=$obj;
278 }
279 $this->is_dialog=false;
280 unset($this->dialog);
281 $this->dialog=FALSE;
282 }
283 }
285 /* Sort entries */
286 $tmp = $keys = array();
287 foreach($this->SubObjects as $key => $entry){
288 $keys[$key]=$key;
289 }
290 natcasesort($keys);
291 foreach($keys as $key){
292 $tmp[$key]=$this->SubObjects[$key];
293 }
294 $this->SubObjects = $tmp;
296 /* Cancel Dialog */
297 if(isset($_POST['CancelSubObject'])){
298 $this->is_dialog=false;
299 unset($this->dialog);
300 $this->dialog=FALSE;
301 }
303 /* Print dialog if $this->dialog is set */
304 if(is_object($this->dialog)){
305 $this->dialog->save_object();
306 $display = $this->dialog->execute();
307 return($display);
308 }
312 /* Divlist added 28.02.2006
313 Containing FAIscripts
314 */
316 $divlist = new divSelectBox("FAIhooks");
317 $divlist->setHeight(400);
319 foreach($this->getList(true) as $key => $name){
321 $dn= $this->acl_base_for_current_object($name['dn']);
322 $acl = $this->ui->get_permissions($dn,"fai/faiHookEntry") ;
323 $act = "";
325 /* Check if this object is freezed, in this case hide the delete icon */
326 if(preg_match("/^freeze/", $this->FAIstate)){
327 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("Edit")."' alt='"._("Edit")."'>";
328 }else{
329 $act .= "<input type='image' src='images/edit.png' name='editscript_%s' title='"._("Edit")."' alt='"._("Edit")."'>";
330 if(preg_match("/d/",$acl)){
331 $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s' title='"._("Delete")."' alt='"._("Delete")."'>";
332 }
333 }
335 /* Check if we are allowed to use the export button for this object */
336 $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","FAIscript") ;
337 if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
338 $down = "";
339 }else{
340 $down = "<a href='?plug=".$_GET['plug']."&getFAIHook=".base64_encode($key)."'>
341 <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
342 </a>";
343 }
345 /* Check if we are allowed to view the object */
346 $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","cn") ;
347 if(preg_match("/r/",$s_acl)){
349 $edit_link = "<a href='?plug=".$_GET['plug']."&act=edit&id=".base64_encode($key)."'>".$name['name']."</a>";
350 $divlist->AddEntry(array( array("string"=>$edit_link),
351 array("string"=>$down , "attach" => "style='width:20px;'"),
352 array("string"=>str_replace("%s",base64_encode($key),$act),
353 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
354 }
355 }
356 $smarty->assign("Entry_divlist",$divlist->DrawList());
357 /* Divlist creation complete
358 */
360 $smarty->assign("SubObjects",$this->getList());
362 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
363 * If we post the escaped strings they will be escaped again
364 */
365 foreach($this->attributes as $attrs){
366 if(get_magic_quotes_gpc()){
367 $smarty->assign($attrs,stripslashes($this->$attrs));
368 }else{
369 $smarty->assign($attrs,($this->$attrs));
370 }
371 }
373 $tmp = $this->plInfo();
375 $c_dn = $this->acl_base_for_current_object($this->dn);
376 $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiHookEntry")) && !preg_match('/^freeze/', $this->FAIstate));
377 foreach($tmp['plProvidedAcls'] as $name => $translation){
378 $smarty->assign($name."ACL",$this->getacl($name));
379 }
381 $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
382 return($display);
383 }
385 /* Generate listbox friendly SubObject list
386 */
387 function getList($use_dns=false){
388 $a_return=array();
389 foreach($this->SubObjects as $obj){
390 if($obj['status'] != "delete"){
392 $cn = stripslashes($obj['cn']);
393 $desc = "";
395 if((isset($obj['description']))&&(!empty($obj['description']))){
396 $desc = " [".stripslashes($obj['description'])."]";
397 }
399 if($use_dns){
400 $a_return[$obj['cn']]['name']= $cn.$desc;
401 $a_return[$obj['cn']]['dn']= $obj['dn'];
402 }else{
403 $a_return[$obj['cn']] = $cn.$desc;
404 }
405 }
406 }
407 return($a_return);
408 }
411 /* Delete me, and all my subtrees
412 */
413 function remove_from_parent()
414 {
415 $ldap = $this->config->get_ldap_link();
416 $ldap->cd ($this->dn);
418 $faifilter = session::get('faifilter');
419 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
420 if($faifilter['branch'] == "main"){
421 $use_dn = $this->dn;
422 }
424 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
426 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
428 foreach($this->SubObjects as $name => $obj){
429 $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
430 if($faifilter['branch'] == "main"){
431 $use_dn = $obj['dn'];
432 }
433 FAI::prepare_to_save_FAI_object($use_dn,array(),true);
434 }
435 $this->handle_post_events("remove");
436 }
439 /* Save data to object
440 */
441 function save_object()
442 {
443 if((isset($_POST['FAIhook_posted'])) && !preg_match("/^freeze/", $this->FAIstate)){
444 plugin::save_object();
445 foreach($this->attributes as $attrs){
446 if(isset($_POST[$attrs])){
447 $this->$attrs = $_POST[$attrs];
448 }
449 }
450 }
451 }
454 /* Check supplied data */
455 function check()
456 {
457 /* Call common method to give check the hook */
458 $message= plugin::check();
460 /* Ensure that we do not overwrite an allready existing entry
461 */
462 if($this->is_new){
463 $new_dn= 'cn='.$this->cn.",".get_ou('faihookou').get_ou('faiou').session::get('CurrentMainBase');
464 $faifilter = session::get('faifilter');
465 if($faifilter['branch']!="main"){
466 $new_dn ='cn='.$this->cn.",".get_ou('faihookou').$faifilter['branch'];
467 }
469 $res = faiManagement::check_class_name("FAIhook",$this->cn,$new_dn);
470 if(isset($res[$this->cn])){
471 $message[] = msgPool::duplicated(_("Name"));
472 }
473 }
475 return ($message);
476 }
479 /* Save to LDAP */
480 function save()
481 {
482 plugin::save();
484 $ldap = $this->config->get_ldap_link();
486 FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
488 if($this->initially_was_account){
489 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
490 }else{
491 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
492 }
494 $ldap->cd($this->dn);
496 /* Prepare FAIscriptEntry to write it to ldap
497 * First sort array.
498 * Because we must delete old entries first.
499 * After deletion, we perform add and modify
500 */
501 $Objects = array();
503 /* We do not need to save untouched objects */
504 foreach($this->SubObjects as $name => $obj){
505 if($obj['status'] != "FreshLoaded"){
506 $Objects[$name] = $obj;
507 }
508 }
510 foreach($Objects as $name => $obj){
512 foreach($this->sub64coded as $codeIt){
513 $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
514 }
516 $tmp = array();
517 $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
518 foreach($attributes as $attrs){
519 if(empty($obj[$attrs])){
520 $obj[$attrs] = array();
521 }
522 if(!is_array($obj[$attrs])){
523 $tmp[$attrs] = stripslashes($obj[$attrs]);
524 }else{
525 $tmp[$attrs] = $obj[$attrs];
526 }
527 }
529 $tmp['objectClass'] = $this->subClasses;
531 $sub_dn = "cn=".$obj['cn'].",".$this->dn;
533 if($obj['status']=="new"){
534 $ldap->cat($sub_dn,array("objectClass"));
535 if($ldap->count()){
536 $obj['status']="edited";
537 }
538 }
540 /* Tag object */
541 $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
543 if($obj['status'] == "delete"){
544 FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
545 $this->handle_post_events("remove");
546 }elseif($obj['status'] == "edited"){
547 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
548 $this->handle_post_events("modify");
549 }elseif($obj['status']=="new"){
550 FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
551 $this->handle_post_events("add");
552 }
553 }
554 }
557 function PrepareForCopyPaste($source)
558 {
559 plugin::PrepareForCopyPaste($source);
561 /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
562 */
563 $ldap = $this->config->get_ldap_link();
564 $ldap->cd ($source['dn']);
566 $attrs_to_search = $this->subAttributes;
567 $attrs_to_search[] = "FAIstate";
568 $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
570 while($object = $ldap->fetch()){
572 /* Skip objects, that are tagged as removed */
573 if(isset($object['FAIstate'][0])){
574 if(preg_match("/removed$/",$object['FAIstate'][0])){
575 continue;
576 }
577 }
579 /* Set status for save management */
580 $objects = array();
581 $objects['status'] = "edited";
582 $objects['dn'] = $object['dn'];
583 $objects = $this->get_object_attributes($objects,$this->subAttributes);
584 $objects = $this->get_object_attributes($objects,$this->sub_Load_Later);
586 $this->SubObjects[$objects['cn']] = $objects;
587 }
588 }
591 /* Return plugin informations for acl handling */
592 static function plInfo()
593 {
594 return (array(
595 "plShortName" => _("Hook"),
596 "plDescription" => _("FAI hook"),
597 "plSelfModify" => FALSE,
598 "plDepends" => array(),
599 "plPriority" => 20,
600 "plSection" => array("administration"),
601 "plCategory" => array("fai"),
602 "plProvidedAcls" => array(
603 "cn" => _("Name")." ("._("Read only").")",
604 "description" => _("Description"))
605 ));
606 }
609 /*! \brief Used for copy & paste.
610 Returns a HTML input mask, which allows to change the cn of this entry.
611 @param Array Array containing current status && a HTML template.
612 */
613 function getCopyDialog()
614 {
615 $vars = array("cn");
616 $smarty = get_smarty();
617 $smarty->assign("cn", htmlentities($this->cn));
618 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
619 $ret = array();
620 $ret['string'] = $str;
621 $ret['status'] = "";
622 return($ret);
623 }
626 /*! \brief Used for copy & paste.
627 Some entries must be renamed to avaoid duplicate entries.
628 */
629 function saveCopyDialog()
630 {
631 if(isset($_POST['cn'])){
632 $this->cn = get_post('cn');
633 }
634 }
635 }
637 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
638 ?>