Code

Added several comments
[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       
214       $acl = $ui->get_permissions($obj['dn'],"fai/faiVariableEntry");
215      
216       if(preg_match("/r/",$acl)){
217         $tmp[$key] = $ret[$key];
218       } 
219       
220       $smarty->assign("SubObjects",$tmp);
221     }
224       /* Magic quotes GPC, escapes every ' " \, to solve some security risks
225      * If we post the escaped strings they will be escaped again
226      */
228     foreach($this->attributes as $attrs){
229       if(get_magic_quotes_gpc()){
230         $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
231       }else{
232         $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
233       }
234     }
236     $c_dn = $this->acl_base_for_current_object($this->dn);
237     $smarty->assign("is_createable",     preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
238     $smarty->assign("is_removeable",  preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
240     $tmp = $this->plInfo();
241     foreach($tmp['plProvidedAcls'] as $name => $translation) {
242       $smarty->assign($name."ACL",$this->getacl($name));
243     }
244     
246     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
247     return($display);
248   }
250   /* Generate listbox friendly SubObject list
251   */
252   function getList(){
253     $a_return=array();
254     foreach($this->SubObjects as $obj){
255       if($obj['status'] != "delete"){
257         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
258           if (preg_match("/\[\*\]/", $obj['description'])){
259             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
260           } else {
261             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
262           }
263         }else{
264           if (preg_match("/\[\*\]/", $obj['description'])){
265             $a_return[$obj['cn']]= $obj['cn'];
266           } else {
267             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
268           }
269         }
270       }
271     }
272     return($a_return);
273   }
275   /* Delete me, and all my subtrees
276    */
277   function remove_from_parent()
278   {
279     if($this->acl_is_removeable()){
280       $ldap = $this->config->get_ldap_link();
281       $ldap->cd ($this->dn);
283       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
284       if($_SESSION['faifilter']['branch'] == "main"){
285         $use_dn = $this->dn;
286       }
288       prepare_to_save_FAI_object($use_dn,array(),true);
290       foreach($this->SubObjects as $name => $obj){
291         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
292         if($_SESSION['faifilter']['branch'] == "main"){
293           $use_dn = $obj['dn'];
294         }
295         prepare_to_save_FAI_object($use_dn,array(),true);
296       }
297       $this->handle_post_events("remove");
298     }
299   }
302   /* Save data to object 
303    */
304   function save_object()
305   {
306     if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
307       plugin::save_object();
308       foreach($this->attributes as $attrs){
309         if(isset($_POST[$attrs])){
310           $this->$attrs = $_POST[$attrs];
311         }
312       }
313     }
314   }
317   /* Check supplied data */
318   function check()
319   {
320     /* Call common method to give check the hook */
321     $message= plugin::check();
323     return ($message);
324   }
327   /* Save to LDAP */
328   function save()
329   {
330     plugin::save();
331  
332     $ldap = $this->config->get_ldap_link();
333     prepare_to_save_FAI_object($this->dn,$this->attrs);
334     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/variable with dn '%s' failed."),$this->dn));
335  
336     /* Do object tagging */
337     $this->handle_object_tagging();
338  
339     /* Prepare FAIscriptEntry to write it to ldap
340      * First sort array.
341      *  Because we must delete old entries first.
342      * After deletion, we perform add and modify 
343      */
344     $Objects = array();
345     foreach($this->SubObjects as $name => $obj){
346       if($obj['status'] == "delete"){
347         $Objects[$name] = $obj; 
348       }
349     }
350     foreach($this->SubObjects as $name => $obj){
351       if($obj['status'] != "delete"){
352         $Objects[$name] = $obj; 
353       }
354     }
356     foreach($Objects as $name => $obj){
358       foreach($this->sub64coded as $codeIt){
359         $obj[$codeIt]=base64_encode($obj[$codeIt]);
360       }
362       $tmp = array();
363       foreach($this->subAttributes as $attrs){
364         if(empty($obj[$attrs])){
365           $obj[$attrs] = array();
366         }
367         $tmp[$attrs] = $obj[$attrs];
368       }    
369         
370       $tmp['objectClass'] = $this->subClasses;
372       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
374       if($obj['status']=="new"){
375         $ldap->cat($sub_dn,array("objectClass"));
376         if($ldap->count()){
377           $obj['status']="edited";
378         }
379       }
381       /* Check if gosaAdministrativeUnitTag is required as object class */
382       if($obj['status'] == "edited"){
383         $ldap->cat($sub_dn,array("objectClass"));
384         $attrs = $ldap->fetch();
385         if(isset($attrs['objectClass'])){
386           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
387             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
388           }
389         }
390       }
391       
392       if($obj['status'] == "delete"){
393         prepare_to_save_FAI_object($sub_dn,array(),true);
394         $this->handle_post_events("remove");
395       }elseif($obj['status'] == "edited"){
396         prepare_to_save_FAI_object($sub_dn,$tmp);
397         $this->handle_post_events("modify");
398       }elseif($obj['status']=="new"){
399         prepare_to_save_FAI_object($sub_dn,$tmp);
400         $this->handle_post_events("add");
401       }
403       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
404     }
405   }
407   
408   /* Return plugin informations for acl handling */ 
409   function plInfo()
410   {
411     return (array( 
412           "plShortName" => _("Variable"),
413           "plDescription" => _("FAI variable"),
414           "plSelfModify"  => FALSE,
415           "plDepends"     => array(),
416           "plPriority"    => 22,
417           "plSection"     => array("administration"),
418           "plCategory"    => array("fai"),
419           "plProvidedAcls" => array(
420             "cn"                => _("Name")." ("._("Read only").")",
421             "description"       => _("Description"))
422           ));
423   }
426 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
427 ?>