Code

Added AutoScrollDown to "FAI branch IFrame" just for testing. Looks good.
[gosa.git] / plugins / admin / fai / class_faiVariable.inc
1 <?php
3 class faiVariable extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary      = "Manage server basic objects";
7   var $cli_description  = "Some longer text\nfor help";
8   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account   = TRUE;
13   /* Attributes for this Object */
14   var $attributes       = array("cn","description");
16   /* ObjectClasses for this Object*/
17   var $objectclasses    = array("top","FAIclass","FAIvariable");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "FAIvariableEntry";
21   var $subClasses       = array("top","FAIclass","FAIvariableEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "faiVariableEntry";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description","FAIvariableContent"); 
28   var $sub64coded       = array();  
30   /* Specific attributes */
31   var $cn               = "";       // The class name for this object
32   var $description      = "";       // The description for this set of partitions
33   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
34   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
35   var $SubObjects       = array();  // All leafobjects of this object
37   var $FAIstate         = "";
38   var $ui   ;
40   function faiVariable ($config, $dn= NULL)
41   {
42     /* Load Attributes */
43     plugin::plugin ($config, $dn);
45     if($dn != "new"){
46       $this->dn =$dn;
48       /* Get FAIstate
49        */
50       if(isset($this->attrs['FAIstate'][0])){
51         $this->FAIstate = $this->attrs['FAIstate'][0];
52       }
54       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
55        */
56       $ldap     = $this->config->get_ldap_link();
57       $ldap->cd ($this->dn);
59       $attrs_to_search = $this->subAttributes;
60       $attrs_to_search[] = "FAIstate";
61       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
63       while($object = $ldap->fetch()){
64       
65         /* Skip objects, that are tagged as removed */
66         if(isset($object['FAIstate'][0])){
67           if(preg_match("/removed$/",$object['FAIstate'][0])){
68             continue;
69           }
70         }
72         /* Set status for save management */
73         foreach($this->subAttributes as $attrs){
74           if(!isset($object[$attrs][0])){
75             $this->SubObjects[$object['cn'][0]][$attrs]="";
76           }else{
77             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
78           }
79         }
80      
81         foreach($this->sub64coded as $codeIt){
82           $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
83         }
84  
85         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
86         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
87       }
88     }
89     $this->ui = get_userinfo();
90   }
93   function acl_base_for_current_object($dn)
94   {
95     if($dn == "new"){
96       if($this->dn == "new"){
97         $dn= $_SESSION['CurrentMainBase'];
98       }else{
99         $dn = $this->dn;
100       }
101     }
102     return($dn);
103   }
106   function execute()
107   {
108     /* Call parent execute */
109     plugin::execute();
111     /* Fill templating stuff */
112     $smarty= get_smarty();
113     $display= "";
115     /* Add new sub object */
116     if(isset($_POST['AddSubObject'])){
117       $this->dialog= new $this->subClassName($this->config,"new");
118       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
119       $this->dialog->set_acl_category("fai");
120       $this->is_dialog=true;
121     }
123     if($this->dn != "new"){
124       $_SESSION['objectinfo']= $this->dn;
125     }
128     /* Edit selected Sub Object */
129     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
131       $var = $_POST['SubObject'][0];
132       $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
133       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
134       $this->dialog->set_acl_category("fai");
135       $this->dialog->set_acl_base($c_dn);
136       $_SESSION['objectinfo'] = $this->SubObjects[$var]['dn'];
137       $this->is_dialog=true;
138     }
139     
140     /* Remove Sub object */
141     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
142       foreach($_POST['SubObject'] as $var){
144         $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
145         $acl = $this->ui->get_permissions($c_dn,"fai/faiVariable");
146         if(preg_match("/d/",$acl)){
147           if($this->SubObjects[$var]['status'] == "edited"){
148             $this->SubObjects[$var]['status']= "delete";
149           }else{
150             unset($this->SubObjects[$var]);
151           }
152         }
153       }
154     }
156     /* Save Dialog */
157     if(isset($_POST['SaveSubObject'])){
158       $this->dialog->save_object();
159       $msgs = $this->dialog->check();
160       if(count($msgs)>0){
161         foreach($msgs as $msg){
162           print_red($msg);
163         }
164       }else{
165         $obj = $this->dialog->save();
166         if(isset($obj['remove'])){
167           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
168             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
169           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
170             unset($this->SubObjects[$obj['remove']['from']]);
171           }
172           $obj['status'] = "new";
173           $this->SubObjects[$obj['remove']['to']] = $obj;
174           unset($this->SubObjects[$obj['remove']['to']]['remove']);
175         }else{
176           $this->SubObjects[$obj['cn']]=$obj;
177         }
178         $this->is_dialog=false;
179         unset($this->dialog);
180         $this->dialog=NULL;
181       }
182     }
184     /* Sort entries */
185     $tmp = $keys = array();
186     foreach($this->SubObjects as $key => $entry){
187       $keys[$key]=$key;
188     }
189     natcasesort($keys);
190     foreach($keys as $key){
191       $tmp[$key]=$this->SubObjects[$key];
192     } 
193     $this->SubObjects = $tmp;
195     /* Cancel Dialog */
196     if(isset($_POST['CancelSubObject'])){
197       $this->is_dialog=false; 
198       unset($this->dialog);
199       $this->dialog=NULL;
200     }
202     /* Print dialog if $this->dialog is set */
203     if($this->dialog){
204       $this->dialog->save_object();
205       $display = $this->dialog->execute();
206       return($display);
207     }
209     $ui = get_userinfo();
210     $ret = $this->getList();
211     $tmp = array();
212     foreach($this->SubObjects as $key => $obj){
213       $acl = $ui->get_permissions($obj['dn'],"fai/faiVariableEntry");
214       if(preg_match("/r/",$acl)){
215         $tmp[$key] = $ret[$key];
216       } 
217     }
218     $smarty->assign("SubObjects",$tmp);
221     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
222      * If we post the escaped strings they will be escaped again
223      */
224     foreach($this->attributes as $attrs){
225       if(get_magic_quotes_gpc()){
226         $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
227       }else{
228         $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
229       }
230     }
232     $c_dn = $this->acl_base_for_current_object($this->dn);
233     $smarty->assign("is_createable",     preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
234     $smarty->assign("is_removeable",  preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
236     $tmp = $this->plInfo();
237     foreach($tmp['plProvidedAcls'] as $name => $translation) {
238       $smarty->assign($name."ACL",$this->getacl($name));
239     }
240     
242     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
243     return($display);
244   }
246   /* Generate listbox friendly SubObject list
247   */
248   function getList(){
249     $a_return=array();
250     foreach($this->SubObjects as $obj){
251       if($obj['status'] != "delete"){
253         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
254           if (preg_match("/\[\*\]/", $obj['description'])){
255             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
256           } else {
257             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
258           }
259         }else{
260           if (preg_match("/\[\*\]/", $obj['description'])){
261             $a_return[$obj['cn']]= $obj['cn'];
262           } else {
263             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
264           }
265         }
266       }
267     }
268     return($a_return);
269   }
271   /* Delete me, and all my subtrees
272    */
273   function remove_from_parent()
274   {
275     if($this->acl_is_removeable()){
276       $ldap = $this->config->get_ldap_link();
277       $ldap->cd ($this->dn);
279       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
280       if($_SESSION['faifilter']['branch'] == "main"){
281         $use_dn = $this->dn;
282       }
284       prepare_to_save_FAI_object($use_dn,array(),true);
286       foreach($this->SubObjects as $name => $obj){
287         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
288         if($_SESSION['faifilter']['branch'] == "main"){
289           $use_dn = $obj['dn'];
290         }
291         prepare_to_save_FAI_object($use_dn,array(),true);
292       }
293       $this->handle_post_events("remove");
294     }
295   }
298   /* Save data to object 
299    */
300   function save_object()
301   {
302     if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
303       plugin::save_object();
304       foreach($this->attributes as $attrs){
305         if(isset($_POST[$attrs])){
306           $this->$attrs = $_POST[$attrs];
307         }
308       }
309     }
310   }
313   /* Check supplied data */
314   function check()
315   {
316     /* Call common method to give check the hook */
317     $message= plugin::check();
319     return ($message);
320   }
323   /* Save to LDAP */
324   function save()
325   {
326     plugin::save();
327  
328     $ldap = $this->config->get_ldap_link();
329     prepare_to_save_FAI_object($this->dn,$this->attrs);
330     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/variable with dn '%s' failed."),$this->dn));
331  
332     /* Do object tagging */
333     $this->handle_object_tagging();
334  
335     /* Prepare FAIscriptEntry to write it to ldap
336      * First sort array.
337      *  Because we must delete old entries first.
338      * After deletion, we perform add and modify 
339      */
340     $Objects = array();
341     foreach($this->SubObjects as $name => $obj){
342       if($obj['status'] == "delete"){
343         $Objects[$name] = $obj; 
344       }
345     }
346     foreach($this->SubObjects as $name => $obj){
347       if($obj['status'] != "delete"){
348         $Objects[$name] = $obj; 
349       }
350     }
352     foreach($Objects as $name => $obj){
354       foreach($this->sub64coded as $codeIt){
355         $obj[$codeIt]=base64_encode($obj[$codeIt]);
356       }
358       $tmp = array();
359       foreach($this->subAttributes as $attrs){
360         if(empty($obj[$attrs])){
361           $obj[$attrs] = array();
362         }
363         $tmp[$attrs] = $obj[$attrs];
364       }    
365         
366       $tmp['objectClass'] = $this->subClasses;
368       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
370       if($obj['status']=="new"){
371         $ldap->cat($sub_dn,array("objectClass"));
372         if($ldap->count()){
373           $obj['status']="edited";
374         }
375       }
377       /* Check if gosaAdministrativeUnitTag is required as object class */
378       if($obj['status'] == "edited"){
379         $ldap->cat($sub_dn,array("objectClass"));
380         $attrs = $ldap->fetch();
381         if(isset($attrs['objectClass'])){
382           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
383             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
384           }
385         }
386       }
387       
388       if($obj['status'] == "delete"){
389         prepare_to_save_FAI_object($sub_dn,array(),true);
390         $this->handle_post_events("remove");
391       }elseif($obj['status'] == "edited"){
392         prepare_to_save_FAI_object($sub_dn,$tmp);
393         $this->handle_post_events("modify");
394       }elseif($obj['status']=="new"){
395         prepare_to_save_FAI_object($sub_dn,$tmp);
396         $this->handle_post_events("add");
397       }
399       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
400     }
401   }
403   
404   /* Return plugin informations for acl handling */ 
405   function plInfo()
406   {
407     return (array( 
408           "plShortName" => _("Variable"),
409           "plDescription" => _("FAI variable"),
410           "plSelfModify"  => FALSE,
411           "plDepends"     => array(),
412           "plPriority"    => 22,
413           "plSection"     => array("administration"),
414           "plCategory"    => array("fai"),
415           "plProvidedAcls" => array(
416             "cn"                => _("Name")." ("._("Read only").")",
417             "description"       => _("Description"))
418           ));
419   }
422 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
423 ?>