Code

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