Code

Updated image
[gosa.git] / 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_all_objects_for_given_base($this->dn,"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
47       foreach($res as $obj){
49         /* Skip not relevant objects */
50         if(!preg_match("/".normalizePreg($this->dn)."$/i",$obj['dn'])) continue;
52         $objects = array();
53         $objects['description'] = "";
54         $objects['status']      = "FreshLoaded";
55         $objects['dn']          = $obj['dn'];
56         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
57         $this->SubObjects[$objects['cn']] = $objects;
58       }
59     }
60     $this->is_new = FALSE;
61     if($this->dn == "new"){
62       $this->is_new =TRUE;
63     }
64     $this->ui = get_userinfo();
65   }
68   function acl_base_for_current_object($dn)
69   {
70     if($dn == "new"){
71       if($this->dn == "new"){
72         $dn = session::get('CurrentMainBase');
73       }else{
74         $dn = $this->dn;
75       }
76     }
77     return($dn);
78   }
81   function execute()
82   {
83     /* Call parent execute */
84     plugin::execute();
86     if($this->is_account && !$this->view_logged){
87       $this->view_logged = TRUE;
88       new log("view","fai/".get_class($this),$this->dn);
89     }
91     /* Fill templating stuff */
92     $smarty= get_smarty();
93     $display= "";
95     /* Add new sub object */
96     if(isset($_POST['AddSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
97       $this->dialog= new $this->subClassName($this->config,"new");
98       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
99       $this->dialog->set_acl_category("fai");
100       $this->dialog->parent = &$this;
101       $this->dialog->FAIstate = $this->FAIstate;
102       $this->is_dialog=true;
103     }
105     if($this->dn != "new"){
106       session::set('objectinfo',$this->dn);
107     }
110     /* Edit selected Sub Object */
111     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
113       $var = $_POST['SubObject'][0];
114       $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
115       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
116       $this->dialog->set_acl_category("fai");
117       $this->dialog->set_acl_base($c_dn);
118       $this->dialog->FAIstate = $this->FAIstate;
119       $this->dialog->parent = &$this;
120       session::set('objectinfo',$this->SubObjects[$var]['dn']);
121       $this->is_dialog=true;
122     }
123     
124     /* Remove Sub object */
125     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject'])) && !preg_match("/freeze/i",$this->FAIstate)){
126       foreach($_POST['SubObject'] as $var){
128         $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
129         $acl = $this->ui->get_permissions($c_dn,"fai/faiVariable");
130         if(preg_match("/d/",$acl)){
131           if($this->SubObjects[$var]['status'] == "edited"){
132             $this->SubObjects[$var]['status']= "delete";
133           }else{
134             unset($this->SubObjects[$var]);
135           }
136         }
137       }
138     }
140     /* Save Dialog */
141     if(isset($_POST['SaveSubObject']) && !preg_match("/freeze/i",$this->FAIstate)){
142       $this->dialog->save_object();
143       $msgs = $this->dialog->check();
144       if(count($msgs)>0){
145         foreach($msgs as $msg){
146           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
147         }
148       }else{
149         $obj = $this->dialog->save();
150         if(isset($obj['remove'])){
151           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
152             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
153           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
154             unset($this->SubObjects[$obj['remove']['from']]);
155           }
156           $obj['status'] = "new";
157           $this->SubObjects[$obj['remove']['to']] = $obj;
158           unset($this->SubObjects[$obj['remove']['to']]['remove']);
159         }else{
160           $this->SubObjects[$obj['cn']]=$obj;
161         }
162         $this->is_dialog=false;
163         unset($this->dialog);
164         $this->dialog=FALSE;
165       }
166     }
168     /* Sort entries */
169     $tmp = $keys = array();
170     foreach($this->SubObjects as $key => $entry){
171       $keys[$key]=$key;
172     }
173     natcasesort($keys);
174     foreach($keys as $key){
175       $tmp[$key]=$this->SubObjects[$key];
176     } 
177     $this->SubObjects = $tmp;
179     /* Cancel Dialog */
180     if(isset($_POST['CancelSubObject'])){
181       $this->is_dialog=false; 
182       unset($this->dialog);
183       $this->dialog=FALSE;
184     }
186     /* Print dialog if $this->dialog is set */
187     if(is_object($this->dialog)){
188       $this->dialog->save_object();
189       $display = $this->dialog->execute();
190       return($display);
191     }
193     $ui = get_userinfo();
194     $ret = $this->getList();
195     $tmp = array();
196     foreach($this->SubObjects as $key => $obj){
197       $acl = $ui->get_permissions($obj['dn'],"fai/faiVariableEntry");
198       if((preg_match("/r/",$acl) || $obj['dn'] == "new") && isset($ret[$key])){
199         $tmp[$key] = $ret[$key];
200       } 
201     }
202     $smarty->assign("SubObjects",$tmp);
205     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
206      * If we post the escaped strings they will be escaped again
207      */
208     foreach($this->attributes as $attrs){
209       if(get_magic_quotes_gpc()){
210         $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
211       }else{
212         $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
213       }
214     }
216     $c_dn = $this->acl_base_for_current_object($this->dn);
217     $smarty->assign("is_createable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && !preg_match("/freeze/", $this->FAIstate));
218     $smarty->assign("is_removeable",  preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && !preg_match("/freeze/", $this->FAIstate));
220     $tmp = $this->plInfo();
221     foreach($tmp['plProvidedAcls'] as $name => $translation) {
222       $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/i",$this->FAIstate)));
223     }
224     
226     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
227     return($display);
228   }
230   /* Generate listbox friendly SubObject list
231   */
232   function getList(){
233     $a_return=array();
234     foreach($this->SubObjects as $obj){
235       if($obj['status'] != "delete"){
237         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
238           if (preg_match("/\[\*\]/", $obj['description'])){
239             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
240           } else {
241             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
242           }
243         }else{
244           if (preg_match("/\[\*\]/", $obj['description'])){
245             $a_return[$obj['cn']]= $obj['cn'];
246           } else {
247             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
248           }
249         }
250       }
251     }
252     return($a_return);
253   }
255   /* Delete me, and all my subtrees
256    */
257   function remove_from_parent()
258   {
259     if($this->acl_is_removeable()){
260       $ldap = $this->config->get_ldap_link();
261       $ldap->cd ($this->dn);
262       $release = $this->parent->parent->fai_release;
263       $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $this->dn);
264       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
265       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
267       foreach($this->SubObjects as $name => $obj){
268         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $obj['dn']);
269         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
270       }
271       $this->handle_post_events("remove");
272     }
273   }
276   /* Save data to object 
277    */
278   function save_object()
279   {
280     if((isset($_POST['FAIvariable_posted'])) && !preg_match("/freeze/", $this->FAIstate) ){
281       plugin::save_object();
282       foreach($this->attributes as $attrs){
283         if(isset($_POST[$attrs])){
284           $this->$attrs = $_POST[$attrs];
285         }
286       }
287     }
288   }
291   /* Check supplied data */
292   function check()
293   {
294     /* Call common method to give check the hook */
295     $message= plugin::check();
297     /* Ensure that we do not overwrite an allready existing entry 
298      */
299     if($this->is_new){
300       $release = $this->parent->parent->fai_release;
301       $new_dn= 'cn='.$this->cn.",".get_ou('faivariableou').get_ou('faiou').$release;
302       $res = faiManagement::check_class_name("FAIvariable",$this->cn,$new_dn);
303       if(isset($res[$this->cn])){
304         $message[] = msgPool::duplicated(_("Name"));
305       }
306     }
308     return ($message);
309   }
312   /* Save to LDAP */
313   function save()
314   {
315     plugin::save();
316  
317     $ldap = $this->config->get_ldap_link();
318     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
320     if($this->initially_was_account){
321       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
322     }else{
323       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
324     }
325  
326     /* Prepare FAIscriptEntry to write it to ldap
327      * First sort array.
328      *  Because we must delete old entries first.
329      * After deletion, we perform add and modify 
330      */
331     $Objects = array();
332     foreach($this->SubObjects as $name => $obj){
333       if($obj['status'] == "delete"){
334         $Objects[$name] = $obj; 
335       }
336     }
337     foreach($this->SubObjects as $name => $obj){
338       if($obj['status'] != "delete"){
339         $Objects[$name] = $obj; 
340       }
341     }
343     foreach($Objects as $name => $obj){
345       foreach($this->sub64coded as $codeIt){
346         $obj[$codeIt]=base64_encode($obj[$codeIt]);
347       }
349       $tmp = array();
350       foreach($this->subAttributes as $attrs){
351         if(empty($obj[$attrs])){
352           $obj[$attrs] = array();
353         }
354         $tmp[$attrs] = $obj[$attrs];
355       }    
356         
357       $tmp['objectClass'] = $this->subClasses;
359       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
361       if($obj['status']=="new"){
362         $ldap->cat($sub_dn,array("objectClass"));
363         if($ldap->count()){
364           $obj['status']="edited";
365         }
366       }
368       /* Tag object */
369       $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
370       
371       if($obj['status'] == "delete"){
372         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
373         $this->handle_post_events("remove");
374       }elseif($obj['status'] == "edited"){
375         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
376         $this->handle_post_events("modify");
377       }elseif($obj['status']=="new"){
378         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
379         $this->handle_post_events("add");
380       }
382     }
383   }
386   function PrepareForCopyPaste($source)
387   {
388     plugin::PrepareForCopyPaste($source);
390     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
391      */
392     $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
393     foreach($res as $obj){
395       /* Skip not relevant objects */
396       if(!preg_match("/".normalizePreg($source['dn'])."$/i",$obj['dn'])) continue;
398       $objects = array();
399       $objects['status']      = "edited";
400       $objects['dn']          = $obj['dn'];
401       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
402       $this->SubObjects[$objects['cn']] = $objects;
403     }
404   } 
406  
407   /* Return plugin informations for acl handling */ 
408   static function plInfo()
409   {
410     return (array( 
411           "plShortName" => _("Variable"),
412           "plDescription" => _("FAI variable"),
413           "plSelfModify"  => FALSE,
414           "plDepends"     => array(),
415           "plPriority"    => 22,
416           "plSection"     => array("administration"),
417           "plCategory"    => array("fai"),
418           "plProvidedAcls" => array(
419             "cn"                => _("Name")." ("._("Read only").")",
420             "description"       => _("Description"))
421           ));
422   }
425   /*! \brief  Used for copy & paste.
426     Returns a HTML input mask, which allows to change the cn of this entry.
427     @param  Array   Array containing current status && a HTML template.
428    */
429   function getCopyDialog()
430   {
431     $vars = array("cn");
432     $smarty = get_smarty();
433     $smarty->assign("cn", htmlentities($this->cn));
434     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
435     $ret = array();
436     $ret['string'] = $str;
437     $ret['status'] = "";
438     return($ret);
439   }
442   /*! \brief  Used for copy & paste.
443     Some entries must be renamed to avaoid duplicate entries.
444    */
445   function saveCopyDialog()
446   {
447     if(isset($_POST['cn'])){
448       $this->cn = get_post('cn');
449     }
450   }
452   /* Reload some attributes */
453   function get_object_attributes($object,$attributes)
454   {
455     $ldap = $this->config->get_ldap_link();
456     $ldap->cd($this->config->current['BASE']);
457     $ldap->cat($object['dn'],$attributes);
458     $tmp  = $ldap->fetch();
460     foreach($attributes as $attrs){
461       if(isset($tmp[$attrs][0])){
462         $var = $tmp[$attrs][0];
464         /* Check if we must decode some attributes */
465         if(in_array_ics($attrs,$this->sub64coded)){
466           $var = base64_decode($var);
467         }
469         /*  check if this is a binary entry */
470         if(in_array_ics($attrs,$this->subBinary)){
471           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
472         }
474         /* Fix slashes */
475         $var = addslashes($var);
476         $object[$attrs] = $var;
477       }
478     }
479     return($object);
480   }
484 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
485 ?>