Code

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