Code

Also display new created variables
[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       }
89       @log::log("view","fai/".get_class($this),$this->dn);
90     }
91     $this->ui = get_userinfo();
92   }
95   function acl_base_for_current_object($dn)
96   {
97     if($dn == "new"){
98       if($this->dn == "new"){
99         $dn= $_SESSION['CurrentMainBase'];
100       }else{
101         $dn = $this->dn;
102       }
103     }
104     return($dn);
105   }
108   function execute()
109   {
110     /* Call parent execute */
111     plugin::execute();
113     /* Fill templating stuff */
114     $smarty= get_smarty();
115     $display= "";
117     /* Add new sub object */
118     if(isset($_POST['AddSubObject'])){
119       $this->dialog= new $this->subClassName($this->config,"new");
120       $this->dialog->set_acl_base($this->acl_base_for_current_object($this->dn));
121       $this->dialog->set_acl_category("fai");
122       $this->is_dialog=true;
123     }
125     if($this->dn != "new"){
126       $_SESSION['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       $_SESSION['objectinfo'] = $this->SubObjects[$var]['dn'];
139       $this->is_dialog=true;
140     }
141     
142     /* Remove Sub object */
143     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
144       foreach($_POST['SubObject'] as $var){
146         $c_dn = $this->acl_base_for_current_object($this->SubObjects[$var]['dn']);
147         $acl = $this->ui->get_permissions($c_dn,"fai/faiVariable");
148         if(preg_match("/d/",$acl)){
149           if($this->SubObjects[$var]['status'] == "edited"){
150             $this->SubObjects[$var]['status']= "delete";
151           }else{
152             unset($this->SubObjects[$var]);
153           }
154         }
155       }
156     }
158     /* Save Dialog */
159     if(isset($_POST['SaveSubObject'])){
160       $this->dialog->save_object();
161       $msgs = $this->dialog->check();
162       if(count($msgs)>0){
163         foreach($msgs as $msg){
164           print_red($msg);
165         }
166       }else{
167         $obj = $this->dialog->save();
168         if(isset($obj['remove'])){
169           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
170             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
171           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
172             unset($this->SubObjects[$obj['remove']['from']]);
173           }
174           $obj['status'] = "new";
175           $this->SubObjects[$obj['remove']['to']] = $obj;
176           unset($this->SubObjects[$obj['remove']['to']]['remove']);
177         }else{
178           $this->SubObjects[$obj['cn']]=$obj;
179         }
180         $this->is_dialog=false;
181         unset($this->dialog);
182         $this->dialog=NULL;
183       }
184     }
186     /* Sort entries */
187     $tmp = $keys = array();
188     foreach($this->SubObjects as $key => $entry){
189       $keys[$key]=$key;
190     }
191     natcasesort($keys);
192     foreach($keys as $key){
193       $tmp[$key]=$this->SubObjects[$key];
194     } 
195     $this->SubObjects = $tmp;
197     /* Cancel Dialog */
198     if(isset($_POST['CancelSubObject'])){
199       $this->is_dialog=false; 
200       unset($this->dialog);
201       $this->dialog=NULL;
202     }
204     /* Print dialog if $this->dialog is set */
205     if($this->dialog){
206       $this->dialog->save_object();
207       $display = $this->dialog->execute();
208       return($display);
209     }
211     $ui = get_userinfo();
212     $ret = $this->getList();
213     $tmp = array();
214     foreach($this->SubObjects as $key => $obj){
215       $acl = $ui->get_permissions($obj['dn'],"fai/faiVariableEntry");
216       if(preg_match("/r/",$acl) || $obj['dn'] == "new"){
217         $tmp[$key] = $ret[$key];
218       } 
219     }
220     $smarty->assign("SubObjects",$tmp);
223     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
224      * If we post the escaped strings they will be escaped again
225      */
226     foreach($this->attributes as $attrs){
227       if(get_magic_quotes_gpc()){
228         $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
229       }else{
230         $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
231       }
232     }
234     $c_dn = $this->acl_base_for_current_object($this->dn);
235     $smarty->assign("is_createable",     preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
236     $smarty->assign("is_removeable",  preg_match("/d/",$this->ui->get_permissions($c_dn,"fai/faiVariableEntry")) && $this->FAIstate!="freeze");
238     $tmp = $this->plInfo();
239     foreach($tmp['plProvidedAcls'] as $name => $translation) {
240       $smarty->assign($name."ACL",$this->getacl($name));
241     }
242     
244     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
245     return($display);
246   }
248   /* Generate listbox friendly SubObject list
249   */
250   function getList(){
251     $a_return=array();
252     foreach($this->SubObjects as $obj){
253       if($obj['status'] != "delete"){
255         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
256           if (preg_match("/\[\*\]/", $obj['description'])){
257             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
258           } else {
259             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
260           }
261         }else{
262           if (preg_match("/\[\*\]/", $obj['description'])){
263             $a_return[$obj['cn']]= $obj['cn'];
264           } else {
265             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
266           }
267         }
268       }
269     }
270     return($a_return);
271   }
273   /* Delete me, and all my subtrees
274    */
275   function remove_from_parent()
276   {
277     if($this->acl_is_removeable()){
278       $ldap = $this->config->get_ldap_link();
279       $ldap->cd ($this->dn);
281       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
282       if($_SESSION['faifilter']['branch'] == "main"){
283         $use_dn = $this->dn;
284       }
285   
286       prepare_to_save_FAI_object($use_dn,array(),true);
287       @log::log("remove","fai/".get_class($this),$use_dn,$this->attributes);
289       foreach($this->SubObjects as $name => $obj){
290         $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
291         if($_SESSION['faifilter']['branch'] == "main"){
292           $use_dn = $obj['dn'];
293         }
294         prepare_to_save_FAI_object($use_dn,array(),true);
295       }
296       $this->handle_post_events("remove");
297     }
298   }
301   /* Save data to object 
302    */
303   function save_object()
304   {
305     if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
306       plugin::save_object();
307       foreach($this->attributes as $attrs){
308         if(isset($_POST[$attrs])){
309           $this->$attrs = $_POST[$attrs];
310         }
311       }
312     }
313   }
316   /* Check supplied data */
317   function check()
318   {
319     /* Call common method to give check the hook */
320     $message= plugin::check();
322     return ($message);
323   }
326   /* Save to LDAP */
327   function save()
328   {
329     plugin::save();
330  
331     $ldap = $this->config->get_ldap_link();
332     prepare_to_save_FAI_object($this->dn,$this->attrs);
333     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/variable with dn '%s' failed."),$this->dn));
335     if($this->initially_was_account){
336       @log::log("modify","fai/".get_class($this),$this->dn,$this->attributes);
337     }else{
338       @log::log("create","fai/".get_class($this),$this->dn,$this->attributes);
339     }
340  
341     /* Do object tagging */
342     $this->handle_object_tagging();
343  
344     /* Prepare FAIscriptEntry to write it to ldap
345      * First sort array.
346      *  Because we must delete old entries first.
347      * After deletion, we perform add and modify 
348      */
349     $Objects = array();
350     foreach($this->SubObjects as $name => $obj){
351       if($obj['status'] == "delete"){
352         $Objects[$name] = $obj; 
353       }
354     }
355     foreach($this->SubObjects as $name => $obj){
356       if($obj['status'] != "delete"){
357         $Objects[$name] = $obj; 
358       }
359     }
361     foreach($Objects as $name => $obj){
363       foreach($this->sub64coded as $codeIt){
364         $obj[$codeIt]=base64_encode($obj[$codeIt]);
365       }
367       $tmp = array();
368       foreach($this->subAttributes as $attrs){
369         if(empty($obj[$attrs])){
370           $obj[$attrs] = array();
371         }
372         $tmp[$attrs] = $obj[$attrs];
373       }    
374         
375       $tmp['objectClass'] = $this->subClasses;
377       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
379       if($obj['status']=="new"){
380         $ldap->cat($sub_dn,array("objectClass"));
381         if($ldap->count()){
382           $obj['status']="edited";
383         }
384       }
386       /* Check if gosaAdministrativeUnitTag is required as object class */
387       if($obj['status'] == "edited"){
388         $ldap->cat($sub_dn,array("objectClass"));
389         $attrs = $ldap->fetch();
390         if(isset($attrs['objectClass'])){
391           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
392             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
393           }
394         }
395       }
396       
397       if($obj['status'] == "delete"){
398         prepare_to_save_FAI_object($sub_dn,array(),true);
399         $this->handle_post_events("remove");
400       }elseif($obj['status'] == "edited"){
401         prepare_to_save_FAI_object($sub_dn,$tmp);
402         $this->handle_post_events("modify");
403       }elseif($obj['status']=="new"){
404         prepare_to_save_FAI_object($sub_dn,$tmp);
405         $this->handle_post_events("add");
406       }
408       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
409     }
410   }
412   
413   /* Return plugin informations for acl handling */ 
414   function plInfo()
415   {
416     return (array( 
417           "plShortName" => _("Variable"),
418           "plDescription" => _("FAI variable"),
419           "plSelfModify"  => FALSE,
420           "plDepends"     => array(),
421           "plPriority"    => 22,
422           "plSection"     => array("administration"),
423           "plCategory"    => array("fai"),
424           "plProvidedAcls" => array(
425             "cn"                => _("Name")." ("._("Read only").")",
426             "description"       => _("Description"))
427           ));
428   }
431 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
432 ?>