Code

Apply patch for #3716
[gosa.git] / trunk / gosa-plugins / fai / admin / fai / class_faiHook.inc
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       $res = FAI::get_leaf_objects($this->dn, $this->cn, $this->subClass, get_ou('faiHookRDN'));
48       foreach($res as $obj){
49         $objects = array();
50         $objects['status']      = "FreshLoaded";
51         $objects['dn']          = $obj['dn'];
52         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
53         $this->SubObjects[$objects['cn']] = $objects;
54       }
55     }
56     $this->is_new = FALSE;
57     if($this->dn == "new"){
58       $this->is_new =TRUE;
59     }
60     $this->ui = get_userinfo();
61   }
64   /* Reload some attributes */
65   function get_object_attributes($object,$attributes)
66   {
67     $ldap = $this->config->get_ldap_link();
68     $ldap->cd($this->config->current['BASE']);
69     $ldap->cat($object['dn'],$attributes);
70     $tmp  = $ldap->fetch();
72     foreach($attributes as $attrs){
73       if(isset($tmp[$attrs][0])){
74         $var = $tmp[$attrs][0];
76         /* Check if we must decode some attributes */
77         if(in_array_ics($attrs,$this->sub64coded)){
78           $var = base64_decode($var);
79         }
81         /*  check if this is a binary entry */
82         if(in_array_ics($attrs,$this->subBinary)){
83           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
84         }
86         /* Fix slashes */
87         $var = addslashes($var);
88         $object[$attrs] = $var;
89       }
90     }
91     return($object);
92   }
95   function acl_base_for_current_object($dn)
96   {
97     if($dn == "new" || $dn == ""){
98       if($this->dn == "new"){
99         $dn= $this->parent->parent->acl_base;
100       }else{
101         $dn = $this->dn;
102       }
103     }
104     return($dn);
105   }
108   function getUsedFAItask($cn)
109   {
110     $ret = array();
111     foreach($this->SubObjects as $name => $class){
112       if($class['cn'] == $cn){
113         continue;
114       } 
115       if($class['status'] != "delete"){
116         $ret[$class['FAItask']] = $class['FAItask'];
117       }
118     }
119     return($ret);
120   }
122   function execute()
123   {
124     /* Call parent execute */
125     plugin::execute();
127     if($this->is_account && !$this->view_logged){
128       $this->view_logged = TRUE;
129       new log("view","fai/".get_class($this),$this->dn);
130     }
132     /* Fill templating stuff */
133     $smarty= get_smarty();
134     $display= "";
136     /* New Listhandling
137      */
138     $once = true;
139     foreach($_POST as $name => $value){
140       if(preg_match("/^editscript_/",$name)&&($once)){
141         $once = false;
142         $entry = preg_replace("/^editscript_/","",$name);
143         $entry = base64_decode(preg_replace("/_.*/","",$entry));
145         $obj  = $this->SubObjects[$entry];
146         if($obj['status'] == "FreshLoaded"){
147           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
148         }
150         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
151         $this->dialog->FAIstate = $this->FAIstate;
152         $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
153         $this->dialog->set_acl_category("fai");
155         set_object_info($obj['dn']);
156         $this->dialog->parent = &$this;
157         $this->is_dialog=true;
158       }
159       if(preg_match("/^deletescript_/",$name)&&($once)){
160         $entry = preg_replace("/^deletescript_/","",$name);
161         $entry = base64_decode(preg_replace("/_.*/","",$entry));
163         $dn = $this->acl_base_for_current_object($this->SubObjects[$entry]['dn']);
164         $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry")  ;
166         if(preg_match("/d/",$acl)){
167           $once = false;
169           $status = $this->SubObjects[$entry]['status'];
170           if($status == "edited" || $status == "FreshLoaded"){
171             $this->SubObjects[$entry]['status']= "delete";
172           }else{
173             unset($this->SubObjects[$entry]);
174           }
175         }
176       }
177     }
179     /* File download requested */
180     if(isset($_GET['getFAIHook'])){
181       if(isset($this->SubObjects[base64_decode($_GET['getFAIHook'])])){
182         $obj = $this->SubObjects[base64_decode($_GET['getFAIHook'])];
183         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
184         send_binary_content(stripslashes($obj['FAIscript']),$obj['cn'].".FAIhook");
185       }
186     }
188     /* Edit entries via GET */
189     if(isset($_GET['act']) && isset($_GET['id'])){
190       if($_GET['act'] == "edit" && isset($this->SubObjects[base64_decode($_GET['id'])])){
191         $obj = $this->SubObjects[base64_decode($_GET['id'])];
192         if($obj['status'] == "FreshLoaded"){
193           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
194         }
195         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
196         $this->dialog->FAIstate = $this->FAIstate;
197         $this->dialog->acl = $this->acl;
198         $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
199         $this->dialog->set_acl_category("fai");
200         set_object_info($obj['dn']);
201         $this->dialog->parent = &$this;
202         $this->is_dialog=true;
203       }
204     }
206     ///// Ende new list handling
208     /* Add new sub object */
209     if(isset($_POST['AddSubObject'])){
211       $c_dn = $this->acl_base_for_current_object($this->dn);
212       $this->dialog= new $this->subClassName($this->config,"new");
213       $this->dialog->set_acl_base($c_dn);
214       $this->dialog->FAIstate = $this->FAIstate;
215       $this->dialog->set_acl_category("fai");
216       $this->dialog->parent = &$this;
217       $this->is_dialog=true;
218     }
221     /* Save Dialog */
222     if(isset($_POST['SaveSubObject'])){
224       /* Perform post check*/
225       $this->dialog->save_object();
227       /* Get messages */
228       $msgs = $this->dialog->check();
230       /* print errors */
231       if(count($msgs)>0){
232         foreach($msgs as $msg){
233           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
234         }
235       }else{
237         /* Get return object */
238         $obj = $this->dialog->save();
239         if(isset($obj['remove'])){
241           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
242   
243           /* Depending on status, set new status */
244           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
245             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
246           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
247             unset($this->SubObjects[$obj['remove']['from']]);
248           }
249           $obj['status'] = "new";
250           $this->SubObjects[$obj['remove']['to']] = $obj;
251           unset($this->SubObjects[$obj['remove']['to']]['remove']);
252         }else{
253           if($obj['status'] == "FreshLoaded"){
254             $obj['status'] = "edited";
255           }
256           $this->SubObjects[$obj['cn']]=$obj;
257         }
258         $this->is_dialog=false;
259         unset($this->dialog);
260         $this->dialog=FALSE;
261       }
262     }
264     /* Sort entries */
265     $tmp = $keys = array();
266     foreach($this->SubObjects as $key => $entry){
267       $keys[$key]=$key;
268     }
269     natcasesort($keys);
270     foreach($keys as $key){
271       $tmp[$key]=$this->SubObjects[$key];
272     }
273     $this->SubObjects = $tmp;
275     /* Cancel Dialog */
276     if(isset($_POST['CancelSubObject'])){
277       $this->is_dialog=false; 
278       unset($this->dialog);
279       $this->dialog=FALSE;
280     }
282     /* Print dialog if $this->dialog is set */
283     if(is_object($this->dialog)){
284       $this->dialog->save_object();
285       $display = $this->dialog->execute();
286       return($display);
287     }
291     /* Divlist            added 28.02.2006
292        Containing FAIscripts
293      */
295     $divlist = new divSelectBox("FAIhooks");
296     $divlist->setHeight(400);
298     foreach($this->getList(true) as $key => $name){
300       $dn= $this->acl_base_for_current_object($name['dn']);
301       $acl = $this->ui->get_permissions($dn,"fai/faiHookEntry")  ;
302       $act = "";
304       /* Check if this object is freezed, in this case hide the delete icon */
305       if(preg_match("/^freeze/", $this->FAIstate)){
306         $act .= "<input type='image' src='images/lists/edit.png'      name='editscript_%s'    title='"._("Edit")."' alt='"._("Edit")."'>";
307       }else{
308         $act .= "<input type='image' src='images/lists/edit.png'      name='editscript_%s'    title='"._("Edit")."' alt='"._("Edit")."'>";
309         if(preg_match("/d/",$acl)){
310           $act .="<input type='image' src='images/lists/trash.png' name='deletescript_%s'  title='"._("Delete")."' alt='"._("Delete")."'>";
311         }
312       }
314       /* Check if we are allowed to use the export button for this object */
315       $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","FAIscript")  ;
316       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
317         $down = "";
318       }else{
319         $down = "<a href='?plug=".$_GET['plug']."&getFAIHook=".base64_encode($key)."'>
320           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
321           </a>";
322       }
324       /* Check if we are allowed to view the object */
325       $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","cn")  ;
326       if(preg_match("/r/",$s_acl)){
328         $edit_link = "<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".base64_encode($key)."'>".$name['name']."</a>";
329         $divlist->AddEntry(array( array("string"=>$edit_link),
330               array("string"=>$down , "attach" => "style='width:20px;'"),
331               array("string"=>str_replace("%s",base64_encode($key),$act),
332                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
333       }
334     }
335     $smarty->assign("Entry_divlist",$divlist->DrawList());
336     /* Divlist creation complete
337      */
339     $smarty->assign("SubObjects",$this->getList());
341     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
342      * If we post the escaped strings they will be escaped again
343      */
344     foreach($this->attributes as $attrs){
345       if(get_magic_quotes_gpc()){
346         $smarty->assign($attrs,stripslashes($this->$attrs));
347       }else{
348         $smarty->assign($attrs,($this->$attrs));
349       }
350     }
352     $tmp = $this->plInfo();
353       
354     $c_dn = $this->acl_base_for_current_object($this->dn);
355     $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiHookEntry")) && !preg_match('/^freeze/', $this->FAIstate));
356     foreach($tmp['plProvidedAcls'] as $name => $translation){
357       $smarty->assign($name."ACL",$this->getacl($name));
358     }
360     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
361     return($display);
362   }
364   /* Generate listbox friendly SubObject list
365    */
366   function getList($use_dns=false){
367     $a_return=array();
368     foreach($this->SubObjects as $obj){
369       if($obj['status'] != "delete"){
371         $cn   = stripslashes($obj['cn']);
372         $desc = "";
374         if((isset($obj['description']))&&(!empty($obj['description']))){
375           $desc = " [".stripslashes($obj['description'])."]"; 
376         }
378         if($use_dns){
379           $a_return[$obj['cn']]['name']= $cn.$desc;
380           $a_return[$obj['cn']]['dn']= $obj['dn'];
381         }else{
382           $a_return[$obj['cn']] =  $cn.$desc;
383         }
384       }
385     }
386     return($a_return);
387   }
390   /* Delete me, and all my subtrees
391    */
392   function remove_from_parent()
393   {
394     if($this->acl_is_removeable()){
395       $ldap = $this->config->get_ldap_link();
396       $ldap->cd ($this->dn);
397       $release = $this->parent->parent->fai_release;
398       $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
399       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
400       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
402       foreach($this->SubObjects as $name => $obj){
403         $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $obj['dn']);
404         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
405       }
406       $this->handle_post_events("remove");
407     }
408   }
411   /* Save data to object 
412    */
413   function save_object()
414   {
415     if((isset($_POST['FAIhook_posted'])) && !preg_match("/^freeze/", $this->FAIstate)){
416       plugin::save_object();
417     }
418   }
421   /* Check supplied data */
422   function check()
423   {
424     /* Call common method to give check the hook */
425     $message= plugin::check();
427     /* Ensure that we do not overwrite an allready existing entry 
428      */
429     if($this->is_new){
430       $release = $this->parent->parent->fai_release;
431       $new_dn= 'cn='.$this->cn.",".get_ou('faiHookRDN').get_ou('faiBaseRDN').$release;
432       $res = faiManagement::check_class_name("FAIhook",$this->cn,$new_dn);
433       if(isset($res[$this->cn])){
434         $message[] = msgPool::duplicated(_("Name"));
435       }
436     }
438     return ($message);
439   }
442   /* Save to LDAP */
443   function save()
444   {
445     plugin::save();
447     $ldap = $this->config->get_ldap_link();
449     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
451     if($this->initially_was_account){
452       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
453     }else{
454       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
455     }
457     $ldap->cd($this->dn);
459     /* Prepare FAIscriptEntry to write it to ldap
460      * First sort array.
461      *  Because we must delete old entries first.
462      * After deletion, we perform add and modify 
463      */
464     $Objects = array();
466     /* We do not need to save untouched objects */
467     foreach($this->SubObjects as $name => $obj){
468       if($obj['status'] != "FreshLoaded"){
469         $Objects[$name] = $obj; 
470       }
471     }
473     foreach($Objects as $name => $obj){
475       foreach($this->sub64coded as $codeIt){
476         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
477       }
479       $tmp = array();
480       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
481       foreach($attributes as $attrs){
482         if(!isset($obj[$attrs])) continue; 
483         if($obj[$attrs] == ""){
484           $obj[$attrs] = array();
485         }
486         if(!is_array($obj[$attrs])){
487           $tmp[$attrs] = stripslashes($obj[$attrs]);
488         }else{
489           $tmp[$attrs] = $obj[$attrs];
490         }
491       }    
493       $tmp['objectClass'] = $this->subClasses;
495       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
497       if($obj['status']=="new"){
498         $ldap->cat($sub_dn,array("objectClass"));
499         if($ldap->count()){
500           $obj['status']="edited";
501         }
502       }
504       /* Tag object */
505       $ui= get_userinfo();
506       $this->tag_attrs($tmp, $sub_dn, $ui->gosaUnitTag);
508       if($obj['status'] == "delete"){
509         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
510         $this->handle_post_events("remove");
511       }elseif($obj['status'] == "edited"){
512         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
513         $this->handle_post_events("modify");
514       }elseif($obj['status']=="new"){
515         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
516         $this->handle_post_events("add");
517       }
518     }
519   }
522   function PrepareForCopyPaste($source)
523   {
524     plugin::PrepareForCopyPaste($source);
526     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
527      */
528     $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
529     foreach($res as $obj){
531       /* Skip not relevant objects */
532       if(!preg_match("/".preg_quote($source['dn'], '/')."$/i",$obj['dn'])) continue;
534       $objects = array();
535       $objects['status']      = "edited";
536       $objects['dn']          = $obj['dn'];
537       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
538       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
539       $this->SubObjects[$objects['cn']] = $objects;
540     }
541   }
544   /* Return plugin informations for acl handling */ 
545   static function plInfo()
546   {
547     return (array( 
548           "plShortName" => _("Hook"),
549           "plDescription" => _("FAI hook"),
550           "plSelfModify"  => FALSE,
551           "plDepends"     => array(),
552           "plPriority"    => 20,
553           "plSection"     => array("administration"),
554           "plCategory"    => array("fai"),
555           "plProvidedAcls" => array(
556             "cn"                => _("Name")." ("._("Read only").")",
557             "description"       => _("Description"))
558           ));
559   }
562   /*! \brief  Used for copy & paste.
563     Returns a HTML input mask, which allows to change the cn of this entry.
564     @param  Array   Array containing current status && a HTML template.
565    */
566   function getCopyDialog()
567   {
568     $vars = array("cn");
569     $smarty = get_smarty();
570     $smarty->assign("cn", htmlentities($this->cn));
571     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
572     $ret = array();
573     $ret['string'] = $str;
574     $ret['status'] = "";
575     return($ret);
576   }
579   /*! \brief  Used for copy & paste.
580     Some entries must be renamed to avaoid duplicate entries.
581    */
582   function saveCopyDialog()
583   {
584     if(isset($_POST['cn'])){
585       $this->cn = get_post('cn');
586     }
587   }
590 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
591 ?>