Code

Apply patch for #3716
[gosa.git] / trunk / gosa-plugins / fai / admin / fai / class_faiVariable.inc
1 <?php
3 class faiVariable 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","FAIvariable");
14   /* Class name of the Ldap ObjectClass for the Sub Object */
15   var $subClass         = "FAIvariableEntry";
16   var $subClasses       = array("top","FAIclass","FAIvariableEntry");
18   /* Class name of the php class which allows us to edit a Sub Object */
19   var $subClassName     = "faiVariableEntry";      
21   /* Attributes to initialise for each subObject */
22   var $subAttributes    = array("cn","description","FAIvariableContent"); 
23   var $sub64coded       = array();  
24   var $subBinary        = array();
26   /* Specific attributes */
27   var $cn               = "";       // The class name for this object
28   var $description      = "";       // The description for this set of partitions
29   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
30   var $SubObjects       = array();  // All leafobjects of this object
32   var $FAIstate         = "";
33   var $ui   ;
34   var $view_logged      = FALSE;
36   function faiVariable (&$config, $dn= NULL)
37   {
38     /* Load Attributes */
39     plugin::plugin ($config, $dn);
41     if($dn != "new"){
42       $this->dn =$dn;
44       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
45        */
46       $res = FAI::get_leaf_objects($this->dn, $this->cn, $this->subClass, get_ou('faiVariableRDN'));
47       foreach($res as $obj){
48         $objects = array();
49         $objects['description'] = "";
50         $objects['status']      = "FreshLoaded";
51         $objects['dn']          = $obj['dn'];
52         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
53         $this->SubObjects[$objects['cn']] = $objects;
54       }
55     }
56     $this->is_new = FALSE;
57     if($this->dn == "new"){
58       $this->is_new =TRUE;
59     }
60     $this->ui = get_userinfo();
61   }
64   function acl_base_for_current_object($dn)
65   {
66     if($dn == "new" || $dn == ""){
67       if($this->dn == "new"){
68         $dn= $this->parent->parent->acl_base;
69       }else{
70         $dn = $this->dn;
71       }
72     }
73     return($dn);
74   }
77   function execute()
78   {
79     /* Call parent execute */
80     plugin::execute();
82     if($this->is_account && !$this->view_logged){
83       $this->view_logged = TRUE;
84       new log("view","fai/".get_class($this),$this->dn);
85     }
87     /* Fill templating stuff */
88     $smarty= get_smarty();
89     $display= "";
91     /* Add new sub object */
92     if(isset($_POST['AddSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
93       $this->dialog= new $this->subClassName($this->config,"new");
94       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
95       $this->dialog->set_acl_category("fai");
96       $this->dialog->parent = &$this;
97       $this->dialog->FAIstate = $this->FAIstate;
98       $this->is_dialog=true;
99     }
101     if($this->dn != "new"){
102       set_object_info($this->dn);
103     }
106     /* Edit selected Sub Object */
107     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
109       $var = $_POST['SubObject'][0];
110       $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
111       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
112       $this->dialog->set_acl_category("fai");
113       $this->dialog->set_acl_base($c_dn);
114       $this->dialog->FAIstate = $this->FAIstate;
115       $this->dialog->parent = &$this;
116       set_object_info($this->SubObjects[$var]['dn']);
117       $this->is_dialog=true;
118     }
119     
120     /* Remove Sub object */
121     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject'])) && !preg_match("/freeze/i",$this->FAIstate)){
122       foreach($_POST['SubObject'] as $var){
124         $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
125         $acl = $this->ui->get_permissions($c_dn,"fai/faiVariable");
126         if(preg_match("/d/",$acl)){
127           if($this->SubObjects[$var]['status'] != "new"){
128             $this->SubObjects[$var]['status']= "delete";
129           }else{
130             unset($this->SubObjects[$var]);
131           }
132         }
133       }
134     }
136     /* Save Dialog */
137     if(isset($_POST['SaveSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
138       $this->dialog->save_object();
139       $msgs = $this->dialog->check();
140       if(count($msgs)>0){
141         foreach($msgs as $msg){
142           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
143         }
144       }else{
145         $obj = $this->dialog->save();
146         if(isset($obj['remove'])){
147           if($this->SubObjects[$obj['remove']['from']]['status'] != "new"){
148             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
149           }else{
150             unset($this->SubObjects[$obj['remove']['from']]);
151           }
152           $obj['status'] = "new";
153           $this->SubObjects[$obj['remove']['to']] = $obj;
154           unset($this->SubObjects[$obj['remove']['to']]['remove']);
155         }else{
156           $obj['status'] = "edited";
157           $this->SubObjects[$obj['cn']]=$obj;
158         }
159         $this->is_dialog=false;
160         unset($this->dialog);
161         $this->dialog=FALSE;
162       }
163     }
165     /* Sort entries */
166     $tmp = $keys = array();
167     foreach($this->SubObjects as $key => $entry){
168       $keys[$key]=$key;
169     }
170     natcasesort($keys);
171     foreach($keys as $key){
172       $tmp[$key]=$this->SubObjects[$key];
173     } 
174     $this->SubObjects = $tmp;
176     /* Cancel Dialog */
177     if(isset($_POST['CancelSubObject'])){
178       $this->is_dialog=false; 
179       unset($this->dialog);
180       $this->dialog=FALSE;
181     }
183     /* Print dialog if $this->dialog is set */
184     if(is_object($this->dialog)){
185       $this->dialog->save_object();
186       $display = $this->dialog->execute();
187       return($display);
188     }
190     $ui = get_userinfo();
191     $ret = $this->getList();
192     $tmp = array();
193     foreach($this->SubObjects as $key => $obj){
194       $acl = $ui->get_permissions($obj['dn'],"fai/faiVariableEntry");
195       if((preg_match("/r/",$acl) || $obj['dn'] == "new") && isset($ret[$key])){
196         $tmp[$key] = $ret[$key];
197       } 
198     }
199     $smarty->assign("SubObjects",$tmp);
202     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
203      * If we post the escaped strings they will be escaped again
204      */
205     foreach($this->attributes as $attrs){
206       if(get_magic_quotes_gpc()){
207         $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
208       }else{
209         $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
210       }
211     }
213     $c_dn = $this->acl_base_for_current_object($this->dn);
214     $smarty->assign("is_createable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && !preg_match("/freeze/", $this->FAIstate));
215     $smarty->assign("is_removeable",  preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && !preg_match("/freeze/", $this->FAIstate));
217     $tmp = $this->plInfo();
218     foreach($tmp['plProvidedAcls'] as $name => $translation) {
219       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/i",$this->FAIstate)));
220     }
221     
223     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
224     return($display);
225   }
227   /* Generate listbox friendly SubObject list
228   */
229   function getList(){
230     $a_return=array();
231     foreach($this->SubObjects as $obj){
232       if($obj['status'] != "delete"){
234         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
235           if (preg_match("/\[\*\]/", $obj['description'])){
236             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
237           } else {
238             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
239           }
240         }else{
241           if (preg_match("/\[\*\]/", $obj['description'])){
242             $a_return[$obj['cn']]= $obj['cn'];
243           } else {
244             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
245           }
246         }
247       }
248     }
249     return($a_return);
250   }
252   /* Delete me, and all my subtrees
253    */
254   function remove_from_parent()
255   {
256     if($this->acl_is_removeable()){
257       $ldap = $this->config->get_ldap_link();
258       $ldap->cd ($this->dn);
259       $release = $this->parent->parent->fai_release;
260       $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
261       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
262       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
264       foreach($this->SubObjects as $name => $obj){
265         $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $obj['dn']);
266         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
267       }
268       $this->handle_post_events("remove");
269     }
270   }
273   /* Save data to object 
274    */
275   function save_object()
276   {
277     if((isset($_POST['FAIvariable_posted'])) && !preg_match("/freeze/", $this->FAIstate) ){
278       plugin::save_object();
279     }
280   }
283   /* Check supplied data */
284   function check()
285   {
286     /* Call common method to give check the hook */
287     $message= plugin::check();
289     /* Ensure that we do not overwrite an allready existing entry 
290      */
291     if($this->is_new){
292       $release = $this->parent->parent->fai_release;
293       $new_dn= 'cn='.$this->cn.",".get_ou('faiVariableRDN').get_ou('faiBaseRDN').$release;
294       $res = faiManagement::check_class_name("FAIvariable",$this->cn,$new_dn);
295       if(isset($res[$this->cn])){
296         $message[] = msgPool::duplicated(_("Name"));
297       }
298     }
300     return ($message);
301   }
304   /* Save to LDAP */
305   function save()
306   {
307     plugin::save();
308  
309     $ldap = $this->config->get_ldap_link();
310     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
312     if($this->initially_was_account){
313       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
314     }else{
315       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
316     }
317  
318     /* Prepare FAIscriptEntry to write it to ldap
319      * First sort array.
320      *  Because we must delete old entries first.
321      * After deletion, we perform add and modify 
322      */
323     $Objects = $this->SubObjects;
324     foreach($Objects as $name => $obj){
326       foreach($this->sub64coded as $codeIt){
327         $obj[$codeIt]=base64_encode($obj[$codeIt]);
328       }
330       $tmp = array();
331       foreach($this->subAttributes as $attrs){
332         if(!isset($obj[$attrs]) || $obj[$attrs] == ""){
333           $obj[$attrs] = array();
334         }
335         $tmp[$attrs] = $obj[$attrs];
336       }    
337         
338       $tmp['objectClass'] = $this->subClasses;
340       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
342       if($obj['status']=="new"){
343         $ldap->cat($sub_dn,array("objectClass"));
344         if($ldap->count()){
345           $obj['status']="edited";
346         }
347       }
349       /* Tag object */
350       $ui= get_userinfo();
351       $this->tag_attrs($tmp, $sub_dn, $ui->gosaUnitTag);
352       
353       if($obj['status'] == "delete"){
354         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
355         $this->handle_post_events("remove");
356       }elseif($obj['status'] == "edited"){
357         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
358         $this->handle_post_events("modify");
359       }elseif($obj['status']=="new"){
360         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
361         $this->handle_post_events("add");
362       }
364     }
365   }
368   function PrepareForCopyPaste($source)
369   {
370     plugin::PrepareForCopyPaste($source);
372     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
373      */
374     $res = FAI::get_all_objects_for_given_object($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
375     foreach($res as $obj){
377       $objects = array();
378       $objects['status']      = "edited";
379       $objects['dn']          = $obj['dn'];
380       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
381       $this->SubObjects[$objects['cn']] = $objects;
382     }
383   } 
385  
386   /* Return plugin informations for acl handling */ 
387   static function plInfo()
388   {
389     return (array( 
390           "plShortName" => _("Variable"),
391           "plDescription" => _("FAI variable"),
392           "plSelfModify"  => FALSE,
393           "plDepends"     => array(),
394           "plPriority"    => 22,
395           "plSection"     => array("administration"),
396           "plCategory"    => array("fai"),
397           "plProvidedAcls" => array(
398             "cn"                => _("Name")." ("._("Read only").")",
399             "description"       => _("Description"))
400           ));
401   }
404   /*! \brief  Used for copy & paste.
405     Returns a HTML input mask, which allows to change the cn of this entry.
406     @param  Array   Array containing current status && a HTML template.
407    */
408   function getCopyDialog()
409   {
410     $vars = array("cn");
411     $smarty = get_smarty();
412     $smarty->assign("cn", htmlentities($this->cn));
413     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
414     $ret = array();
415     $ret['string'] = $str;
416     $ret['status'] = "";
417     return($ret);
418   }
421   /*! \brief  Used for copy & paste.
422     Some entries must be renamed to avaoid duplicate entries.
423    */
424   function saveCopyDialog()
425   {
426     if(isset($_POST['cn'])){
427       $this->cn = get_post('cn');
428     }
429   }
431   /* Reload some attributes */
432   function get_object_attributes($object,$attributes)
433   {
434     $ldap = $this->config->get_ldap_link();
435     $ldap->cd($this->config->current['BASE']);
436     $ldap->cat($object['dn'],$attributes);
437     $tmp  = $ldap->fetch();
439     foreach($attributes as $attrs){
440       if(isset($tmp[$attrs][0])){
441         $var = $tmp[$attrs][0];
443         /* Check if we must decode some attributes */
444         if(in_array_ics($attrs,$this->sub64coded)){
445           $var = base64_decode($var);
446         }
448         /*  check if this is a binary entry */
449         if(in_array_ics($attrs,$this->subBinary)){
450           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
451         }
453         /* Fix slashes */
454         $var = addslashes($var);
455         $object[$attrs] = $var;
456       }
457     }
458     return($object);
459   }
463 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
464 ?>