Code

Added category parameter to tabs.
[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         = "";  
41   function faiHook ($config, $dn= NULL)
42   {
43     /* Load Attributes */
44     plugin::plugin ($config, $dn);
46     /* If "dn==new" we try to create a new entry
47      * Else we must read all objects from ldap which belong to this entry.
48      */
49     if($dn != "new"){
50       $this->dn =$dn;
51     
52       /* Get FAIstate
53        */
54       if(isset($this->attrs['FAIstate'][0])){
55         $this->FAIstate = $this->attrs['FAIstate'][0];
56       }
58       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
59        */
60       $ldap     = $this->config->get_ldap_link();
61       $ldap->cd ($this->dn);
62       $attrs_to_search = $this->subAttributes;
63       $attrs_to_search[] = "FAIstate";
64       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))", array("dn"));
66       while($object = $ldap->fetch()){
67         /* Set status for save management */
68         $objects = array();
69         $objects['status']      = "FreshLoaded";
70         $objects['dn']          = $object['dn'];
71         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
72         $this->SubObjects[$objects['cn']] = $objects;
73       }
74     }
75   }
78   /* Reload some attributes */
79   function get_object_attributes($object,$attributes)
80   {
81     $ldap = $this->config->get_ldap_link();
82     $ldap->cd($this->config->current['BASE']);
83     $ldap->cat($object['dn'],$attributes);
84     $tmp  = $ldap->fetch();
86     foreach($attributes as $attrs){
87       if(isset($tmp[$attrs][0])){
88         $var = $tmp[$attrs][0];
90         /* Check if we must decode some attributes */
91         if(in_array_ics($attrs,$this->sub64coded)){
92           $var = base64_decode($var);
93         }
95         /*  check if this is a binary entry */
96         if(in_array_ics($attrs,$this->subBinary)){
97           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
98         }
100         /* Fix slashes */
101         $var = addslashes($var);
102         $object[$attrs] = $var;
103       }
104     }
105     return($object);
106   }
109   function getUsedFAItask($cn)
110   {
111     $ret = array();
112     foreach($this->SubObjects as $name => $class){
113       if($class['cn'] == $cn){
114         continue;
115       } 
116       if($class['status'] != "delete"){
117         $ret[$class['FAItask']] = $class['FAItask'];
118       }
119     }
120     return($ret);
121   }
123   function execute()
124   {
125     /* Call parent execute */
126     plugin::execute();
128     /* Fill templating stuff */
129     $smarty= get_smarty();
130     $display= "";
132     /* New Listhandling
133      */
134     $once = true;
135     foreach($_POST as $name => $value){
136       if(preg_match("/^editscript_/",$name)&&($once)){
137         $once = false;
138         $entry = preg_replace("/^editscript_/","",$name);
139         $entry = base64_decode(preg_replace("/_.*/","",$entry));
141         $obj  = $this->SubObjects[$entry];
142         if($obj['status'] == "FreshLoaded"){
143           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
144         }
145         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
146         $this->dialog->set_acl_base($_SESSION['CurrentMainBase']);
147         $this->dialog->set_acl_category("fai");
149         $_SESSION['objectinfo'] = $obj['dn'];
150         $this->dialog->parent = &$this;
151         $this->is_dialog=true;
152       }
153       if(preg_match("/^deletescript_/",$name)&&($once)){
154         $once = false;
155         $entry = preg_replace("/^deletescript_/","",$name);
156         $entry = base64_decode(preg_replace("/_.*/","",$entry));
158         if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
159           $this->SubObjects[$entry]['status']= "delete";
160         }else{
161           unset($this->SubObjects[$entry]);
162         }
163       }
164     }
165     ///// Ende new list handling
167     /* Add new sub object */
168     if(isset($_POST['AddSubObject'])){
169       $this->dialog= new $this->subClassName($this->config,"new");
170       $this->dialog->set_acl_base($_SESSION['CurrentMainBase']);
171       $this->dialog->set_acl_category("fai");
172       $this->dialog->parent = &$this;
173       $this->is_dialog=true;
174     }
177     /* Save Dialog */
178     if(isset($_POST['SaveSubObject'])){
180       /* Perform post check*/
181       $this->dialog->save_object();
183       /* Get messages */
184       $msgs = $this->dialog->check();
186       /* print errors */
187       if(count($msgs)>0){
188         foreach($msgs as $msg){
189           print_red($msg);
190         }
191       }else{
193         /* Get return object */
194         $obj = $this->dialog->save();
195         if(isset($obj['remove'])){
197           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
198   
199           /* Depending on status, set new status */
200           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
201             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
202           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
203             unset($this->SubObjects[$obj['remove']['from']]);
204           }
205           $obj['status'] = "new";
206           $this->SubObjects[$obj['remove']['to']] = $obj;
207           unset($this->SubObjects[$obj['remove']['to']]['remove']);
208         }else{
209           if($obj['status'] == "FreshLoaded"){
210             $obj['status'] = "edited";
211           }
212           $this->SubObjects[$obj['cn']]=$obj;
213         }
214         $this->is_dialog=false;
215         unset($this->dialog);
216         $this->dialog=NULL;
217       }
218     }
220     /* Sort entries */
221     $tmp = $keys = array();
222     foreach($this->SubObjects as $key => $entry){
223       $keys[$key]=$key;
224     }
225     natcasesort($keys);
226     foreach($keys as $key){
227       $tmp[$key]=$this->SubObjects[$key];
228     }
229     $this->SubObjects = $tmp;
231     /* Cancel Dialog */
232     if(isset($_POST['CancelSubObject'])){
233       $this->is_dialog=false; 
234       unset($this->dialog);
235       $this->dialog=NULL;
236     }
238     /* Print dialog if $this->dialog is set */
239     if($this->dialog){
240       $this->dialog->save_object();
241       $display = $this->dialog->execute();
242       return($display);
243     }
247     /* Divlist            added 28.02.2006
248        Containing FAIscripts
249      */
251     $divlist = new divSelectBox("FAIhooks");
252     $divlist->setHeight(400);
253     if($this->FAIstate == "freeze"){
254       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
255       $img_remo = ""; 
256     }else{
257       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
258       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
259     }
261     foreach($this->getList(true) as $key => $name){
263       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
264         $down = "";
265       }else{
266         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
267           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
268           </a>";
269       }
271       $divlist->AddEntry(array( array("string"=>$name['name']),
272             array("string"=>$down , "attach" => "style='width:20px;'"),
273             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
274               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
275     }
276     $smarty->assign("Entry_divlist",$divlist->DrawList());
277     /* Divlist creation complete
278      */
280     $smarty->assign("SubObjects",$this->getList());
282     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
283      * If we post the escaped strings they will be escaped again
284      */
285     foreach($this->attributes as $attrs){
286       if(get_magic_quotes_gpc()){
287         $smarty->assign($attrs,stripslashes($this->$attrs));
288       }else{
289         $smarty->assign($attrs,($this->$attrs));
290       }
291     }
293     $tmp = $this->plInfo();
294     $this->ui = get_userinfo();
295       
296     $c_dn = $this->dn;
297     if($c_dn == "new"){
298       $c_dn = $_SESSION['CurrentMainBase'];
299     }
301     $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiScriptEntry")) && $this->FAIstate!="freeze");
302     foreach($tmp['plProvidedAcls'] as $name => $translation){
303       $smarty->assign($name."ACL",$this->getacl($name));
304     }
306     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
307     return($display);
308   }
310   /* Generate listbox friendly SubObject list
311    */
312   function getList($use_dns=false){
313     $a_return=array();
314     foreach($this->SubObjects as $obj){
315       if($obj['status'] != "delete"){
316         if($use_dns){
317           if((isset($obj['description']))&&(!empty($obj['description']))){
318             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
319           }else{
320             $a_return[$obj['cn']]['name']= $obj['cn'];
321           }
322           $a_return[$obj['cn']]['dn']= $obj['dn'];
323         }else{
324           if((isset($obj['description']))&&(!empty($obj['description']))){
325             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
326           }else{
327             $a_return[$obj['cn']]= $obj['cn'];
328           }
329         }
330       }
331     }
332     return($a_return);
333   }
336   /* Delete me, and all my subtrees
337    */
338   function remove_from_parent()
339   {
340     $ldap = $this->config->get_ldap_link();
341     $ldap->cd ($this->dn);
343 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
344     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
345     if($_SESSION['faifilter']['branch'] == "main"){
346       $use_dn = $this->dn;
347     }
349     prepare_to_save_FAI_object($use_dn,array(),true);
351     foreach($this->SubObjects as $name => $obj){
352 #      $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
353       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
354       if($_SESSION['faifilter']['branch'] == "main"){
355         $use_dn = $obj['dn'];
356       }
357       prepare_to_save_FAI_object($use_dn,array(),true);
358     }
359     $this->handle_post_events("remove");    
360   }
363   /* Save data to object 
364    */
365   function save_object()
366   {
367     if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
368       plugin::save_object();
369       foreach($this->attributes as $attrs){
370         if(isset($_POST[$attrs])){
371           $this->$attrs = $_POST[$attrs];
372         }
373       }
374     }
375   }
378   /* Check supplied data */
379   function check()
380   {
381     /* Call common method to give check the hook */
382     $message= plugin::check();
384     return ($message);
385   }
388   /* Save to LDAP */
389   function save()
390   {
391     plugin::save();
393     $ldap = $this->config->get_ldap_link();
395     prepare_to_save_FAI_object($this->dn,$this->attrs);
396     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/hook with dn '%s' failed."),$this->dn));
398     /* Do object tagging */
399     $this->handle_object_tagging();
401     $ldap->cd($this->dn);
403     /* Prepare FAIscriptEntry to write it to ldap
404      * First sort array.
405      *  Because we must delete old entries first.
406      * After deletion, we perform add and modify 
407      */
408     $Objects = array();
410     /* We do not need to save untouched objects */
411     foreach($this->SubObjects as $name => $obj){
412       if($obj['status'] == "FreshLoaded"){
413         unset($this->SubObjects[$name]);
414       }
415     }
417     foreach($this->SubObjects as $name => $obj){
418       if($obj['status'] == "delete"){
419         $Objects[$name] = $obj; 
420       }
421     }
422     foreach($this->SubObjects as $name => $obj){
423       if($obj['status'] != "delete"){
424         $Objects[$name] = $obj; 
425       }
426     }
428     foreach($Objects as $name => $obj){
430       foreach($this->sub64coded as $codeIt){
431         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
432       }
434       $tmp = array();
435       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
436       foreach($attributes as $attrs){
437         if(empty($obj[$attrs])){
438           $obj[$attrs] = array();
439         }
440         if(!is_array($obj[$attrs])){
441           $tmp[$attrs] = stripslashes($obj[$attrs]);
442         }else{
443           $tmp[$attrs] = $obj[$attrs];
444         }
445       }    
447       $tmp['objectClass'] = $this->subClasses;
449       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
451       if($obj['status']=="new"){
452         $ldap->cat($sub_dn,array("objectClass"));
453         if($ldap->count()){
454           $obj['status']="edited";
455         }
456       }
458       /* Check if gosaAdministrativeUnitTag is required as object class */
459       if($obj['status'] == "edited"){
460         $ldap->cat($sub_dn,array("objectClass"));
461         $attrs = $ldap->fetch();
462         if(isset($attrs['objectClass'])){
463           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
464             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
465           }
466         }
467       }
469       if($obj['status'] == "delete"){
470         prepare_to_save_FAI_object($sub_dn,array(),true);
471         $this->handle_post_events("remove");
472       }elseif($obj['status'] == "edited"){
473         prepare_to_save_FAI_object($sub_dn,$tmp);
474         $this->handle_post_events("modify");
475       }elseif($obj['status']=="new"){
476         prepare_to_save_FAI_object($sub_dn,$tmp);
477         $this->handle_post_events("add");
478       }
479       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
480     }
481   }
483   /* Return plugin informations for acl handling */ 
484   function plInfo()
485   {
486     return (array( 
487           "plShortName" => _("Hook"),
488           "plDescription" => _("FAI hook"),
489           "plSelfModify"  => FALSE,
490           "plDepends"     => array(),
491           "plPriority"    => 0,
492           "plSection"     => array("administration"),
493           "plCategory"    => array("fai"),
494           "plProvidedAcls" => array(
495             "cn"                => _("Name"),
496             "description"       => _("Description"),
497             "FAItast"           => _("Task"),
498             "FAIscript"         => _("FAI script"))
499           ));
500   }
503 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
504 ?>