Code

Fixed hook acls
[gosa.git] / plugins / admin / fai / class_faiHook.inc
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;
52     
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."))", array("dn"));
67       while($object = $ldap->fetch()){
68         /* Set status for save management */
69         $objects = array();
70         $objects['status']      = "FreshLoaded";
71         $objects['dn']          = $object['dn'];
72         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
73         $this->SubObjects[$objects['cn']] = $objects;
74       }
75     }
76     $this->ui = get_userinfo();
77   }
80   /* Reload some attributes */
81   function get_object_attributes($object,$attributes)
82   {
83     $ldap = $this->config->get_ldap_link();
84     $ldap->cd($this->config->current['BASE']);
85     $ldap->cat($object['dn'],$attributes);
86     $tmp  = $ldap->fetch();
88     foreach($attributes as $attrs){
89       if(isset($tmp[$attrs][0])){
90         $var = $tmp[$attrs][0];
92         /* Check if we must decode some attributes */
93         if(in_array_ics($attrs,$this->sub64coded)){
94           $var = base64_decode($var);
95         }
97         /*  check if this is a binary entry */
98         if(in_array_ics($attrs,$this->subBinary)){
99           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
100         }
102         /* Fix slashes */
103         $var = addslashes($var);
104         $object[$attrs] = $var;
105       }
106     }
107     return($object);
108   }
111   function acl_base_for_current_object($dn)
112   {
113     if($dn == "new"){
114       if($this->dn == "new"){
115         $dn= $_SESSION['CurrentMainBase'];
116       }else{
117         $dn = $this->dn;
118       }
119     }
120     return($dn);
121   }
124   function getUsedFAItask($cn)
125   {
126     $ret = array();
127     foreach($this->SubObjects as $name => $class){
128       if($class['cn'] == $cn){
129         continue;
130       } 
131       if($class['status'] != "delete"){
132         $ret[$class['FAItask']] = $class['FAItask'];
133       }
134     }
135     return($ret);
136   }
138   function execute()
139   {
140     /* Call parent execute */
141     plugin::execute();
143     /* Fill templating stuff */
144     $smarty= get_smarty();
145     $display= "";
147     /* New Listhandling
148      */
149     $once = true;
150     foreach($_POST as $name => $value){
151       if(preg_match("/^editscript_/",$name)&&($once)){
152         $once = false;
153         $entry = preg_replace("/^editscript_/","",$name);
154         $entry = base64_decode(preg_replace("/_.*/","",$entry));
156         $obj  = $this->SubObjects[$entry];
157         if($obj['status'] == "FreshLoaded"){
158           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
159         }
161         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
162         $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
163         $this->dialog->set_acl_category("fai");
165         $_SESSION['objectinfo'] = $obj['dn'];
166         $this->dialog->parent = &$this;
167         $this->is_dialog=true;
168       }
169       if(preg_match("/^deletescript_/",$name)&&($once)){
170         $entry = preg_replace("/^deletescript_/","",$name);
171         $entry = base64_decode(preg_replace("/_.*/","",$entry));
173         $dn = $this->acl_base_for_current_object($this->SubObjects[$entry]['dn']);
174         $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry")  ;
176         if(preg_match("/d/",$acl)){
177           $once = false;
179           if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
180             $this->SubObjects[$entry]['status']= "delete";
181           }else{
182             unset($this->SubObjects[$entry]);
183           }
184         }
185       }
186     }
187     ///// Ende new list handling
189     /* Add new sub object */
190     if(isset($_POST['AddSubObject'])){
192       $c_dn = $this->acl_base_for_current_object($this->dn);
193       $this->dialog= new $this->subClassName($this->config,"new");
194       $this->dialog->set_acl_base($c_dn);
195       $this->dialog->set_acl_category("fai");
196       $this->dialog->parent = &$this;
197       $this->is_dialog=true;
198     }
201     /* Save Dialog */
202     if(isset($_POST['SaveSubObject'])){
204       /* Perform post check*/
205       $this->dialog->save_object();
207       /* Get messages */
208       $msgs = $this->dialog->check();
210       /* print errors */
211       if(count($msgs)>0){
212         foreach($msgs as $msg){
213           print_red($msg);
214         }
215       }else{
217         /* Get return object */
218         $obj = $this->dialog->save();
219         if(isset($obj['remove'])){
221           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
222   
223           /* Depending on status, set new status */
224           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
225             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
226           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
227             unset($this->SubObjects[$obj['remove']['from']]);
228           }
229           $obj['status'] = "new";
230           $this->SubObjects[$obj['remove']['to']] = $obj;
231           unset($this->SubObjects[$obj['remove']['to']]['remove']);
232         }else{
233           if($obj['status'] == "FreshLoaded"){
234             $obj['status'] = "edited";
235           }
236           $this->SubObjects[$obj['cn']]=$obj;
237         }
238         $this->is_dialog=false;
239         unset($this->dialog);
240         $this->dialog=NULL;
241       }
242     }
244     /* Sort entries */
245     $tmp = $keys = array();
246     foreach($this->SubObjects as $key => $entry){
247       $keys[$key]=$key;
248     }
249     natcasesort($keys);
250     foreach($keys as $key){
251       $tmp[$key]=$this->SubObjects[$key];
252     }
253     $this->SubObjects = $tmp;
255     /* Cancel Dialog */
256     if(isset($_POST['CancelSubObject'])){
257       $this->is_dialog=false; 
258       unset($this->dialog);
259       $this->dialog=NULL;
260     }
262     /* Print dialog if $this->dialog is set */
263     if($this->dialog){
264       $this->dialog->save_object();
265       $display = $this->dialog->execute();
266       return($display);
267     }
271     /* Divlist            added 28.02.2006
272        Containing FAIscripts
273      */
275     $divlist = new divSelectBox("FAIhooks");
276     $divlist->setHeight(400);
278     foreach($this->getList(true) as $key => $name){
280       $dn= $this->acl_base_for_current_object($name['dn']);
281       $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry")  ;
282       $act = "";
284       if($this->FAIstate == "freeze"){
285         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
286       }else{
287         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
288         if(preg_match("/d/",$acl)){
289           $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
290         }
291       }
293       $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript")  ;
294       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
295         $down = "";
296       }else{
297         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
298           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
299           </a>";
300       }
302       $divlist->AddEntry(array( array("string"=>$name['name']),
303             array("string"=>$down , "attach" => "style='width:20px;'"),
304             array("string"=>str_replace("%s",base64_encode($key),$act),
305               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
306     }
307     $smarty->assign("Entry_divlist",$divlist->DrawList());
308     /* Divlist creation complete
309      */
311     $smarty->assign("SubObjects",$this->getList());
313     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
314      * If we post the escaped strings they will be escaped again
315      */
316     foreach($this->attributes as $attrs){
317       if(get_magic_quotes_gpc()){
318         $smarty->assign($attrs,stripslashes($this->$attrs));
319       }else{
320         $smarty->assign($attrs,($this->$attrs));
321       }
322     }
324     $tmp = $this->plInfo();
325       
326     $c_dn = $this->acl_base_for_current_object($this->dn);
327     $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiScriptEntry")) && $this->FAIstate!="freeze");
328     foreach($tmp['plProvidedAcls'] as $name => $translation){
329       $smarty->assign($name."ACL",$this->getacl($name));
330     }
332     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
333     return($display);
334   }
336   /* Generate listbox friendly SubObject list
337    */
338   function getList($use_dns=false){
339     $a_return=array();
340     foreach($this->SubObjects as $obj){
341       if($obj['status'] != "delete"){
342         if($use_dns){
343           if((isset($obj['description']))&&(!empty($obj['description']))){
344             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
345           }else{
346             $a_return[$obj['cn']]['name']= $obj['cn'];
347           }
348           $a_return[$obj['cn']]['dn']= $obj['dn'];
349         }else{
350           if((isset($obj['description']))&&(!empty($obj['description']))){
351             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
352           }else{
353             $a_return[$obj['cn']]= $obj['cn'];
354           }
355         }
356       }
357     }
358     return($a_return);
359   }
362   /* Delete me, and all my subtrees
363    */
364   function remove_from_parent()
365   {
366     $ldap = $this->config->get_ldap_link();
367     $ldap->cd ($this->dn);
369 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
370     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
371     if($_SESSION['faifilter']['branch'] == "main"){
372       $use_dn = $this->dn;
373     }
375     prepare_to_save_FAI_object($use_dn,array(),true);
377     foreach($this->SubObjects as $name => $obj){
378 #      $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
379       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
380       if($_SESSION['faifilter']['branch'] == "main"){
381         $use_dn = $obj['dn'];
382       }
383       prepare_to_save_FAI_object($use_dn,array(),true);
384     }
385     $this->handle_post_events("remove");    
386   }
389   /* Save data to object 
390    */
391   function save_object()
392   {
393     if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
394       plugin::save_object();
395       foreach($this->attributes as $attrs){
396         if(isset($_POST[$attrs])){
397           $this->$attrs = $_POST[$attrs];
398         }
399       }
400     }
401   }
404   /* Check supplied data */
405   function check()
406   {
407     /* Call common method to give check the hook */
408     $message= plugin::check();
410     return ($message);
411   }
414   /* Save to LDAP */
415   function save()
416   {
417     plugin::save();
419     $ldap = $this->config->get_ldap_link();
421     prepare_to_save_FAI_object($this->dn,$this->attrs);
422     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/hook with dn '%s' failed."),$this->dn));
424     /* Do object tagging */
425     $this->handle_object_tagging();
427     $ldap->cd($this->dn);
429     /* Prepare FAIscriptEntry to write it to ldap
430      * First sort array.
431      *  Because we must delete old entries first.
432      * After deletion, we perform add and modify 
433      */
434     $Objects = array();
436     /* We do not need to save untouched objects */
437     foreach($this->SubObjects as $name => $obj){
438       if($obj['status'] == "FreshLoaded"){
439         unset($this->SubObjects[$name]);
440       }
441     }
443     foreach($this->SubObjects as $name => $obj){
444       if($obj['status'] == "delete"){
445         $Objects[$name] = $obj; 
446       }
447     }
448     foreach($this->SubObjects as $name => $obj){
449       if($obj['status'] != "delete"){
450         $Objects[$name] = $obj; 
451       }
452     }
454     foreach($Objects as $name => $obj){
456       foreach($this->sub64coded as $codeIt){
457         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
458       }
460       $tmp = array();
461       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
462       foreach($attributes as $attrs){
463         if(empty($obj[$attrs])){
464           $obj[$attrs] = array();
465         }
466         if(!is_array($obj[$attrs])){
467           $tmp[$attrs] = stripslashes($obj[$attrs]);
468         }else{
469           $tmp[$attrs] = $obj[$attrs];
470         }
471       }    
473       $tmp['objectClass'] = $this->subClasses;
475       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
477       if($obj['status']=="new"){
478         $ldap->cat($sub_dn,array("objectClass"));
479         if($ldap->count()){
480           $obj['status']="edited";
481         }
482       }
484       /* Check if gosaAdministrativeUnitTag is required as object class */
485       if($obj['status'] == "edited"){
486         $ldap->cat($sub_dn,array("objectClass"));
487         $attrs = $ldap->fetch();
488         if(isset($attrs['objectClass'])){
489           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
490             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
491           }
492         }
493       }
495       if($obj['status'] == "delete"){
496         prepare_to_save_FAI_object($sub_dn,array(),true);
497         $this->handle_post_events("remove");
498       }elseif($obj['status'] == "edited"){
499         prepare_to_save_FAI_object($sub_dn,$tmp);
500         $this->handle_post_events("modify");
501       }elseif($obj['status']=="new"){
502         prepare_to_save_FAI_object($sub_dn,$tmp);
503         $this->handle_post_events("add");
504       }
505       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
506     }
507   }
509   /* Return plugin informations for acl handling */ 
510   function plInfo()
511   {
512     return (array( 
513           "plShortName" => _("Hook"),
514           "plDescription" => _("FAI hook"),
515           "plSelfModify"  => FALSE,
516           "plDepends"     => array(),
517           "plPriority"    => 20,
518           "plSection"     => array("administration"),
519           "plCategory"    => array("fai"),
520           "plProvidedAcls" => array(
521             "cn"                => _("Name")." ("._("Read only").")",
522             "description"       => _("Description"))
523           ));
524   }
527 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
528 ?>