Code

Updated divlists.
[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           $status = $this->SubObjects[$entry]['status'];
188           if($status == "edited" || $status == "FreshLoaded"){
189             $this->SubObjects[$entry]['status']= "delete";
190           }else{
191             unset($this->SubObjects[$entry]);
192           }
193         }
194       }
195     }
196     ///// Ende new list handling
198     /* Add new sub object */
199     if(isset($_POST['AddSubObject'])){
201       $c_dn = $this->acl_base_for_current_object($this->dn);
202       $this->dialog= new $this->subClassName($this->config,"new");
203       $this->dialog->set_acl_base($c_dn);
204       $this->dialog->set_acl_category("fai");
205       $this->dialog->parent = &$this;
206       $this->is_dialog=true;
207     }
210     /* Save Dialog */
211     if(isset($_POST['SaveSubObject'])){
213       /* Perform post check*/
214       $this->dialog->save_object();
216       /* Get messages */
217       $msgs = $this->dialog->check();
219       /* print errors */
220       if(count($msgs)>0){
221         foreach($msgs as $msg){
222           print_red($msg);
223         }
224       }else{
226         /* Get return object */
227         $obj = $this->dialog->save();
228         if(isset($obj['remove'])){
230           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
231   
232           /* Depending on status, set new status */
233           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
234             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
235           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
236             unset($this->SubObjects[$obj['remove']['from']]);
237           }
238           $obj['status'] = "new";
239           $this->SubObjects[$obj['remove']['to']] = $obj;
240           unset($this->SubObjects[$obj['remove']['to']]['remove']);
241         }else{
242           if($obj['status'] == "FreshLoaded"){
243             $obj['status'] = "edited";
244           }
245           $this->SubObjects[$obj['cn']]=$obj;
246         }
247         $this->is_dialog=false;
248         unset($this->dialog);
249         $this->dialog=NULL;
250       }
251     }
253     /* Sort entries */
254     $tmp = $keys = array();
255     foreach($this->SubObjects as $key => $entry){
256       $keys[$key]=$key;
257     }
258     natcasesort($keys);
259     foreach($keys as $key){
260       $tmp[$key]=$this->SubObjects[$key];
261     }
262     $this->SubObjects = $tmp;
264     /* Cancel Dialog */
265     if(isset($_POST['CancelSubObject'])){
266       $this->is_dialog=false; 
267       unset($this->dialog);
268       $this->dialog=NULL;
269     }
271     /* Print dialog if $this->dialog is set */
272     if($this->dialog){
273       $this->dialog->save_object();
274       $display = $this->dialog->execute();
275       return($display);
276     }
280     /* Divlist            added 28.02.2006
281        Containing FAIscripts
282      */
284     $divlist = new divSelectBox("FAIhooks");
285     $divlist->setHeight(400);
287     foreach($this->getList(true) as $key => $name){
289       $dn= $this->acl_base_for_current_object($name['dn']);
290       $acl = $this->ui->get_permissions($dn,"fai/faiHookEntry")  ;
291       $act = "";
293       /* Check if this object is freezed, in this case hide the delete icon */
294       if($this->FAIstate == "freeze"){
295         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
296       }else{
297         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
298         if(preg_match("/d/",$acl)){
299           $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
300         }
301       }
303       /* Check if we are allowed to use the export button for this object */
304       $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","FAIscript")  ;
305       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
306         $down = "";
307       }else{
308         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
309           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
310           </a>";
311       }
313       /* Check if we are allowed to view the object */
314       $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","cn")  ;
315       if(preg_match("/r/",$s_acl)){
316         $divlist->AddEntry(array( array("string"=>$name['name']),
317               array("string"=>$down , "attach" => "style='width:20px;'"),
318               array("string"=>str_replace("%s",base64_encode($key),$act),
319                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
320       }
321     }
322     $smarty->assign("Entry_divlist",$divlist->DrawList());
323     /* Divlist creation complete
324      */
326     $smarty->assign("SubObjects",$this->getList());
328     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
329      * If we post the escaped strings they will be escaped again
330      */
331     foreach($this->attributes as $attrs){
332       if(get_magic_quotes_gpc()){
333         $smarty->assign($attrs,stripslashes($this->$attrs));
334       }else{
335         $smarty->assign($attrs,($this->$attrs));
336       }
337     }
339     $tmp = $this->plInfo();
340       
341     $c_dn = $this->acl_base_for_current_object($this->dn);
342     $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiHookEntry")) && $this->FAIstate!="freeze");
343     foreach($tmp['plProvidedAcls'] as $name => $translation){
344       $smarty->assign($name."ACL",$this->getacl($name));
345     }
347     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
348     return($display);
349   }
351   /* Generate listbox friendly SubObject list
352    */
353   function getList($use_dns=false){
354     $a_return=array();
355     foreach($this->SubObjects as $obj){
356       if($obj['status'] != "delete"){
357         if($use_dns){
358           if((isset($obj['description']))&&(!empty($obj['description']))){
359             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
360           }else{
361             $a_return[$obj['cn']]['name']= $obj['cn'];
362           }
363           $a_return[$obj['cn']]['dn']= $obj['dn'];
364         }else{
365           if((isset($obj['description']))&&(!empty($obj['description']))){
366             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
367           }else{
368             $a_return[$obj['cn']]= $obj['cn'];
369           }
370         }
371       }
372     }
373     return($a_return);
374   }
377   /* Delete me, and all my subtrees
378    */
379   function remove_from_parent()
380   {
381     $ldap = $this->config->get_ldap_link();
382     $ldap->cd ($this->dn);
384 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
385     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
386     if($_SESSION['faifilter']['branch'] == "main"){
387       $use_dn = $this->dn;
388     }
390     prepare_to_save_FAI_object($use_dn,array(),true);
392     foreach($this->SubObjects as $name => $obj){
393 #      $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
394       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
395       if($_SESSION['faifilter']['branch'] == "main"){
396         $use_dn = $obj['dn'];
397       }
398       prepare_to_save_FAI_object($use_dn,array(),true);
399     }
400     $this->handle_post_events("remove");    
401   }
404   /* Save data to object 
405    */
406   function save_object()
407   {
408     if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
409       plugin::save_object();
410       foreach($this->attributes as $attrs){
411         if(isset($_POST[$attrs])){
412           $this->$attrs = $_POST[$attrs];
413         }
414       }
415     }
416   }
419   /* Check supplied data */
420   function check()
421   {
422     /* Call common method to give check the hook */
423     $message= plugin::check();
425     return ($message);
426   }
429   /* Save to LDAP */
430   function save()
431   {
432     plugin::save();
434     $ldap = $this->config->get_ldap_link();
436     prepare_to_save_FAI_object($this->dn,$this->attrs);
437     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/hook with dn '%s' failed."),$this->dn));
439     /* Do object tagging */
440     $this->handle_object_tagging();
442     $ldap->cd($this->dn);
444     /* Prepare FAIscriptEntry to write it to ldap
445      * First sort array.
446      *  Because we must delete old entries first.
447      * After deletion, we perform add and modify 
448      */
449     $Objects = array();
451     /* We do not need to save untouched objects */
452     foreach($this->SubObjects as $name => $obj){
453       if($obj['status'] == "FreshLoaded"){
454         unset($this->SubObjects[$name]);
455       }
456     }
458     foreach($this->SubObjects as $name => $obj){
459       if($obj['status'] == "delete"){
460         $Objects[$name] = $obj; 
461       }
462     }
463     foreach($this->SubObjects as $name => $obj){
464       if($obj['status'] != "delete"){
465         $Objects[$name] = $obj; 
466       }
467     }
469     foreach($Objects as $name => $obj){
471       foreach($this->sub64coded as $codeIt){
472         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
473       }
475       $tmp = array();
476       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
477       foreach($attributes as $attrs){
478         if(empty($obj[$attrs])){
479           $obj[$attrs] = array();
480         }
481         if(!is_array($obj[$attrs])){
482           $tmp[$attrs] = stripslashes($obj[$attrs]);
483         }else{
484           $tmp[$attrs] = $obj[$attrs];
485         }
486       }    
488       $tmp['objectClass'] = $this->subClasses;
490       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
492       if($obj['status']=="new"){
493         $ldap->cat($sub_dn,array("objectClass"));
494         if($ldap->count()){
495           $obj['status']="edited";
496         }
497       }
499       /* Check if gosaAdministrativeUnitTag is required as object class */
500       if($obj['status'] == "edited"){
501         $ldap->cat($sub_dn,array("objectClass"));
502         $attrs = $ldap->fetch();
503         if(isset($attrs['objectClass'])){
504           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
505             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
506           }
507         }
508       }
510       if($obj['status'] == "delete"){
511         prepare_to_save_FAI_object($sub_dn,array(),true);
512         $this->handle_post_events("remove");
513       }elseif($obj['status'] == "edited"){
514         prepare_to_save_FAI_object($sub_dn,$tmp);
515         $this->handle_post_events("modify");
516       }elseif($obj['status']=="new"){
517         prepare_to_save_FAI_object($sub_dn,$tmp);
518         $this->handle_post_events("add");
519       }
520       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
521     }
522   }
524   /* Return plugin informations for acl handling */ 
525   function plInfo()
526   {
527     return (array( 
528           "plShortName" => _("Hook"),
529           "plDescription" => _("FAI hook"),
530           "plSelfModify"  => FALSE,
531           "plDepends"     => array(),
532           "plPriority"    => 20,
533           "plSection"     => array("administration"),
534           "plCategory"    => array("fai"),
535           "plProvidedAcls" => array(
536             "cn"                => _("Name")." ("._("Read only").")",
537             "description"       => _("Description"))
538           ));
539   }
542 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
543 ?>