Code

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