Code

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