Code

Removed show_ldap_error() calls
[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();  
25   /* Specific attributes */
26   var $cn               = "";       // The class name for this object
27   var $description      = "";       // The description for this set of partitions
28   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
29   var $SubObjects       = array();  // All leafobjects of this object
31   var $FAIstate         = "";
32   var $ui   ;
33   var $view_logged      = FALSE;
35   function faiVariable (&$config, $dn= NULL)
36   {
37     /* Load Attributes */
38     plugin::plugin ($config, $dn);
40     if($dn != "new"){
41       $this->dn =$dn;
43       /* Get FAIstate
44        */
45       if(isset($this->attrs['FAIstate'][0])){
46         $this->FAIstate = $this->attrs['FAIstate'][0];
47       }
49       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
50        */
51       $ldap     = $this->config->get_ldap_link();
52       $ldap->cd ($this->dn);
54       $attrs_to_search = $this->subAttributes;
55       $attrs_to_search[] = "FAIstate";
56       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
58       while($object = $ldap->fetch()){
59       
60         /* Skip objects, that are tagged as removed */
61         if(isset($object['FAIstate'][0])){
62           if(preg_match("/removed$/",$object['FAIstate'][0])){
63             continue;
64           }
65         }
67         /* Set status for save management */
68         foreach($this->subAttributes as $attrs){
69           if(!isset($object[$attrs][0])){
70             $this->SubObjects[$object['cn'][0]][$attrs]="";
71           }else{
72             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
73           }
74         }
75      
76         foreach($this->sub64coded as $codeIt){
77           $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
78         }
79  
80         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
81         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
82       }
84     }
85     $this->ui = get_userinfo();
86   }
89   function acl_base_for_current_object($dn)
90   {
91     if($dn == "new"){
92       if($this->dn == "new"){
93         $dn = session::get('CurrentMainBase');
94       }else{
95         $dn = $this->dn;
96       }
97     }
98     return($dn);
99   }
102   function execute()
103   {
104     /* Call parent execute */
105     plugin::execute();
107     if($this->is_account && !$this->view_logged){
108       $this->view_logged = TRUE;
109       new log("view","fai/".get_class($this),$this->dn);
110     }
112     /* Fill templating stuff */
113     $smarty= get_smarty();
114     $display= "";
116     /* Add new sub object */
117     if(isset($_POST['AddSubObject'])){
118       $this->dialog= new $this->subClassName($this->config,"new");
119       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
120       $this->dialog->set_acl_category("fai");
121       $this->dialog->parent = &$this;
122       $this->is_dialog=true;
123     }
125     if($this->dn != "new"){
126       session::set('objectinfo',$this->dn);
127     }
130     /* Edit selected Sub Object */
131     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
133       $var = $_POST['SubObject'][0];
134       $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
135       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
136       $this->dialog->set_acl_category("fai");
137       $this->dialog->set_acl_base($c_dn);
138       $this->dialog->parent = &$this;
139       session::set('objectinfo',$this->SubObjects[$var]['dn']);
140       $this->is_dialog=true;
141     }
142     
143     /* Remove Sub object */
144     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
145       foreach($_POST['SubObject'] as $var){
147         $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
148         $acl = $this->ui->get_permissions($c_dn,"fai/faiVariable");
149         if(preg_match("/d/",$acl)){
150           if($this->SubObjects[$var]['status'] == "edited"){
151             $this->SubObjects[$var]['status']= "delete";
152           }else{
153             unset($this->SubObjects[$var]);
154           }
155         }
156       }
157     }
159     /* Save Dialog */
160     if(isset($_POST['SaveSubObject'])){
161       $this->dialog->save_object();
162       $msgs = $this->dialog->check();
163       if(count($msgs)>0){
164         foreach($msgs as $msg){
165           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
166         }
167       }else{
168         $obj = $this->dialog->save();
169         if(isset($obj['remove'])){
170           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
171             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
172           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
173             unset($this->SubObjects[$obj['remove']['from']]);
174           }
175           $obj['status'] = "new";
176           $this->SubObjects[$obj['remove']['to']] = $obj;
177           unset($this->SubObjects[$obj['remove']['to']]['remove']);
178         }else{
179           $this->SubObjects[$obj['cn']]=$obj;
180         }
181         $this->is_dialog=false;
182         unset($this->dialog);
183         $this->dialog=FALSE;
184       }
185     }
187     /* Sort entries */
188     $tmp = $keys = array();
189     foreach($this->SubObjects as $key => $entry){
190       $keys[$key]=$key;
191     }
192     natcasesort($keys);
193     foreach($keys as $key){
194       $tmp[$key]=$this->SubObjects[$key];
195     } 
196     $this->SubObjects = $tmp;
198     /* Cancel Dialog */
199     if(isset($_POST['CancelSubObject'])){
200       $this->is_dialog=false; 
201       unset($this->dialog);
202       $this->dialog=FALSE;
203     }
205     /* Print dialog if $this->dialog is set */
206     if(is_object($this->dialog)){
207       $this->dialog->save_object();
208       $display = $this->dialog->execute();
209       return($display);
210     }
212     $ui = get_userinfo();
213     $ret = $this->getList();
214     $tmp = array();
215     foreach($this->SubObjects as $key => $obj){
216       $acl = $ui->get_permissions($obj['dn'],"fai/faiVariableEntry");
217       if(preg_match("/r/",$acl) || $obj['dn'] == "new"){
218         $tmp[$key] = $ret[$key];
219       } 
220     }
221     $smarty->assign("SubObjects",$tmp);
224     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
225      * If we post the escaped strings they will be escaped again
226      */
227     foreach($this->attributes as $attrs){
228       if(get_magic_quotes_gpc()){
229         $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
230       }else{
231         $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
232       }
233     }
235     $c_dn = $this->acl_base_for_current_object($this->dn);
236     $smarty->assign("is_createable",     preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
237     $smarty->assign("is_removeable",  preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
239     $tmp = $this->plInfo();
240     foreach($tmp['plProvidedAcls'] as $name => $translation) {
241       $smarty->assign($name."ACL",$this->getacl($name));
242     }
243     
245     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
246     return($display);
247   }
249   /* Generate listbox friendly SubObject list
250   */
251   function getList(){
252     $a_return=array();
253     foreach($this->SubObjects as $obj){
254       if($obj['status'] != "delete"){
256         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
257           if (preg_match("/\[\*\]/", $obj['description'])){
258             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
259           } else {
260             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
261           }
262         }else{
263           if (preg_match("/\[\*\]/", $obj['description'])){
264             $a_return[$obj['cn']]= $obj['cn'];
265           } else {
266             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
267           }
268         }
269       }
270     }
271     return($a_return);
272   }
274   /* Delete me, and all my subtrees
275    */
276   function remove_from_parent()
277   {
278     if($this->acl_is_removeable()){
279       $ldap = $this->config->get_ldap_link();
280       $ldap->cd ($this->dn);
282       $faifilter = session::get('faifilter');
283       $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
284       if($faifilter['branch'] == "main"){
285         $use_dn = $this->dn;
286       }
287   
288       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
289       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
291       foreach($this->SubObjects as $name => $obj){
292         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
293         if($faifilter['branch'] == "main"){
294           $use_dn = $obj['dn'];
295         }
296         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
297       }
298       $this->handle_post_events("remove");
299     }
300   }
303   /* Save data to object 
304    */
305   function save_object()
306   {
307     if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
308       plugin::save_object();
309       foreach($this->attributes as $attrs){
310         if(isset($_POST[$attrs])){
311           $this->$attrs = $_POST[$attrs];
312         }
313       }
314     }
315   }
318   /* Check supplied data */
319   function check()
320   {
321     /* Call common method to give check the hook */
322     $message= plugin::check();
324     return ($message);
325   }
328   /* Save to LDAP */
329   function save()
330   {
331     plugin::save();
332  
333     $ldap = $this->config->get_ldap_link();
334     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
335     if (!$ldap->success()){
336       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
337     }
339     if($this->initially_was_account){
340       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
341     }else{
342       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
343     }
344  
345     /* Prepare FAIscriptEntry to write it to ldap
346      * First sort array.
347      *  Because we must delete old entries first.
348      * After deletion, we perform add and modify 
349      */
350     $Objects = array();
351     foreach($this->SubObjects as $name => $obj){
352       if($obj['status'] == "delete"){
353         $Objects[$name] = $obj; 
354       }
355     }
356     foreach($this->SubObjects as $name => $obj){
357       if($obj['status'] != "delete"){
358         $Objects[$name] = $obj; 
359       }
360     }
362     foreach($Objects as $name => $obj){
364       foreach($this->sub64coded as $codeIt){
365         $obj[$codeIt]=base64_encode($obj[$codeIt]);
366       }
368       $tmp = array();
369       foreach($this->subAttributes as $attrs){
370         if(empty($obj[$attrs])){
371           $obj[$attrs] = array();
372         }
373         $tmp[$attrs] = $obj[$attrs];
374       }    
375         
376       $tmp['objectClass'] = $this->subClasses;
378       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
380       if($obj['status']=="new"){
381         $ldap->cat($sub_dn,array("objectClass"));
382         if($ldap->count()){
383           $obj['status']="edited";
384         }
385       }
387       /* Tag object */
388       $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
389       
390       if($obj['status'] == "delete"){
391         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
392         $this->handle_post_events("remove");
393       }elseif($obj['status'] == "edited"){
394         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
395         $this->handle_post_events("modify");
396       }elseif($obj['status']=="new"){
397         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
398         $this->handle_post_events("add");
399       }
401     }
402   }
405   function PrepareForCopyPaste($source)
406   {
407     plugin::PrepareForCopyPaste($source);
409     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
410      */
411     $ldap     = $this->config->get_ldap_link();
412     $ldap->cd ($source['dn']);
413     $attrs_to_search = $this->subAttributes;
414     $attrs_to_search[] = "FAIstate";
415     $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
416     while($object = $ldap->fetch()){
418       /* Skip objects, that are tagged as removed */
419       if(isset($object['FAIstate'][0])){
420         if(preg_match("/removed$/",$object['FAIstate'][0])){
421           continue;
422         }
423       }
425       /* Set status for save management */
426       foreach($this->subAttributes as $attrs){
427         if(!isset($object[$attrs][0])){
428           $this->SubObjects[$object['cn'][0]][$attrs]="";
429         }else{
430           $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
431         }
432       }
433       foreach($this->sub64coded as $codeIt){
434         $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
435       }
436       $this->SubObjects[$object['cn'][0]]['status']      = "edited";
437       $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
438     }
439   } 
441  
442   /* Return plugin informations for acl handling */ 
443   static function plInfo()
444   {
445     return (array( 
446           "plShortName" => _("Variable"),
447           "plDescription" => _("FAI variable"),
448           "plSelfModify"  => FALSE,
449           "plDepends"     => array(),
450           "plPriority"    => 22,
451           "plSection"     => array("administration"),
452           "plCategory"    => array("fai"),
453           "plProvidedAcls" => array(
454             "cn"                => _("Name")." ("._("Read only").")",
455             "description"       => _("Description"))
456           ));
457   }
460 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
461 ?>