Code

Fixes "gosaAdministrativeUnitTag" problem for Templates / 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         = "";
39   function faiVariable ($config, $dn= NULL)
40   {
41     /* Load Attributes */
42     plugin::plugin ($config, $dn);
44     $this->acl ="#all#";
46     /* If "dn==new" we try to create a new entry
47      * Else we must read all objects from ldap which belong to this entry.
48      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
49      */
51     if($dn != "new"){
52       $this->dn =$dn;
54       /* Set acls
55        */
56       $ui   = get_userinfo();
57       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
58       $acli = get_module_permission($acl, "FAIclass", $this->dn);
59       $this->acl=$acli;
61       /* Get FAIstate
62        */
63       if(isset($this->attrs['FAIstate'][0])){
64         $this->FAIstate = $this->attrs['FAIstate'][0];
65       }
67       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
68        */
69       $ldap     = $this->config->get_ldap_link();
70       $ldap->cd ($this->dn);
71       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
73       while($object = $ldap->fetch()){
74         /* Set status for save management */
75   
76         foreach($this->subAttributes as $attrs){
77           if(!isset($object[$attrs][0])){
78             $this->SubObjects[$object['cn'][0]][$attrs]="";
79           }else{
80             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
81           }
82         }
83      
84         foreach($this->sub64coded as $codeIt){
85           $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
86         }
87  
88         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
89         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
90       }
91     }
92   }
94   function execute()
95   {
96         /* Call parent execute */
97         plugin::execute();
99     /* Fill templating stuff */
100     $smarty= get_smarty();
101     $display= "";
103     /* Add new sub object */
104     if(isset($_POST['AddSubObject'])){
105       $this->dialog= new $this->subClassName($this->config,"new");
106       $this->dialog->acl = $this->acl;
107       $this->is_dialog=true;
108     }
110     if($this->dn != "new"){
111       $_SESSION['objectinfo']= $this->dn;
112     }
115     /* Edit selected Sub Object */
116     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
118       $var = $_POST['SubObject'][0];
119       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
120       $this->dialog->acl = $this->acl;
121       $_SESSION['objectinfo'] = $this->SubObjects[$var]['dn'];
122       $this->is_dialog=true;
123     }
124     
125     /* Remove Sub object */
126     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
127       foreach($_POST['SubObject'] as $var){
128         if($this->SubObjects[$var]['status'] == "edited"){
129           $this->SubObjects[$var]['status']= "delete";
130         }else{
131           unset($this->SubObjects[$var]);
132         }
133       }
134     }
136     /* Save Dialog */
137     if(isset($_POST['SaveSubObject'])){
138       $this->dialog->save_object();
139       $msgs = $this->dialog->check();
140       if(count($msgs)>0){
141         foreach($msgs as $msg){
142           print_red($msg);
143         }
144       }else{
145         $obj = $this->dialog->save();
146         if(isset($obj['remove'])){
147           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
148             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
149           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
150             unset($this->SubObjects[$obj['remove']['from']]);
151           }
152           $obj['status'] = "new";
153           $this->SubObjects[$obj['remove']['to']] = $obj;
154           unset($this->SubObjects[$obj['remove']['to']]['remove']);
155         }else{
156           $this->SubObjects[$obj['cn']]=$obj;
157         }
158         $this->is_dialog=false;
159         unset($this->dialog);
160         $this->dialog=NULL;
161       }
162     }
164     /* Sort entries */
165     $tmp = $keys = array();
166     foreach($this->SubObjects as $key => $entry){
167       $keys[$key]=$key;
168     }
169     natcasesort($keys);
170     foreach($keys as $key){
171       $tmp[$key]=$this->SubObjects[$key];
172     } 
173     $this->SubObjects = $tmp;
175     /* Cancel Dialog */
176     if(isset($_POST['CancelSubObject'])){
177       $this->is_dialog=false; 
178       unset($this->dialog);
179       $this->dialog=NULL;
180     }
182     /* Print dialog if $this->dialog is set */
183     if($this->dialog){
184       $this->dialog->save_object();
185       $display = $this->dialog->execute();
186       return($display);
187     }
189     $smarty->assign("SubObjects",$this->getList());
191       /* Magic quotes GPC, escapes every ' " \, to solve some security risks
192      * If we post the escaped strings they will be escaped again
193      */
195     foreach($this->attributes as $attrs){
196       if(get_magic_quotes_gpc()){
197         $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
198       }else{
199         $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
200       }
201     }
203       foreach($this->attributes as $attr){
204       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
205     }
208     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
209     return($display);
210   }
212   /* Generate listbox friendly SubObject list
213   */
214   function getList(){
215     $a_return=array();
216     foreach($this->SubObjects as $obj){
217       if($obj['status'] != "delete"){
219         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
220           if (preg_match("/\[\*\]/", $obj['description'])){
221             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
222           } else {
223             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
224           }
225         }else{
226           if (preg_match("/\[\*\]/", $obj['description'])){
227             $a_return[$obj['cn']]= $obj['cn'];
228           } else {
229             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
230           }
231         }
232       }
233     }
234     return($a_return);
235   }
237   /* Delete me, and all my subtrees
238    */
239   function remove_from_parent()
240   {
241     $ldap = $this->config->get_ldap_link();
242     $ldap->cd ($this->dn);
243     $ldap->rmdir_recursive($this->dn);
244     $this->handle_post_events("remove");    
245   }
248   /* Save data to object 
249    */
250   function save_object()
251   {
252     if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
253       plugin::save_object();
254       foreach($this->attributes as $attrs){
255         if(isset($_POST[$attrs])){
256           $this->$attrs = $_POST[$attrs];
257         }
258       }
259     }
260   }
263   /* Check supplied data */
264   function check()
265   {
266     /* Call common method to give check the hook */
267     $message= plugin::check();
269     return ($message);
270   }
273   /* Save to LDAP */
274   function save()
275   {
276     plugin::save();
277  
278     $ldap = $this->config->get_ldap_link();
279   
280     $ldap->cat($this->dn,array("objectClass"));
281     if($ldap->count()!=0){
282       /* Write FAIscript to ldap*/
283       $ldap->cd($this->dn);
284       $this->cleanup();
285 $ldap->modify ($this->attrs); 
287     }else{
288       /* Write FAIscript to ldap*/
289       $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $this->dn));
290       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
291       $ldap->cd($this->dn);
292       $ldap->add($this->attrs);
293     }
294     show_ldap_error($ldap->get_error());
295  
296     /* Do object tagging */
297     $this->handle_object_tagging();
298     show_ldap_error($ldap->get_error());
299  
300     /* Prepare FAIscriptEntry to write it to ldap
301      * First sort array.
302      *  Because we must delete old entries first.
303      * After deletion, we perform add and modify 
304      */
305     $Objects = array();
306     foreach($this->SubObjects as $name => $obj){
307       if($obj['status'] == "delete"){
308         $Objects[$name] = $obj; 
309       }
310     }
311     foreach($this->SubObjects as $name => $obj){
312       if($obj['status'] != "delete"){
313         $Objects[$name] = $obj; 
314       }
315     }
317     foreach($Objects as $name => $obj){
319       foreach($this->sub64coded as $codeIt){
320         $obj[$codeIt]=base64_encode($obj[$codeIt]);
321       }
323       $tmp = array();
324       foreach($this->subAttributes as $attrs){
325         if(empty($obj[$attrs])){
326           $obj[$attrs] = array();
327         }
328         $tmp[$attrs] = $obj[$attrs];
329       }    
330         
331       $tmp['objectClass'] = $this->subClasses;
333       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
335       if($obj['status']=="new"){
336         $ldap->cat($sub_dn,array("objectClass"));
337         if($ldap->count()){
338           $obj['status']="edited";
339         }
340       }
342       /* Check if gosaAdministrativeUnitTag is required as object class */
343       if($obj['status'] == "edited"){
344         $ldap->cat($sub_dn,array("objectClass"));
345         $attrs = $ldap->fetch();
346         if(isset($attrs['objectClass'])){
347           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
348             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
349           }
350         }
351       }
352  
353       if($obj['status'] == "delete"){
354         $ldap->cd($sub_dn);
355         $ldap->rmdir_recursive($sub_dn);
356         $this->handle_post_events("remove");
357       }elseif($obj['status'] == "edited"){
358         $ldap->cd($sub_dn);
359         $this->cleanup();
360 $ldap->modify ($tmp); 
362         $this->handle_post_events("modify");
363       }elseif($obj['status']=="new"){
365         if($tmp['description'] == array()){
366           unset($tmp['description']);
367         }
369         $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $sub_dn));
370         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
371         $ldap->cd($sub_dn);
372         $ldap->add($tmp); 
373         $this->handle_post_events("add");
374       }
375       show_ldap_error($ldap->get_error()); 
377       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
378       show_ldap_error($ldap->get_error()); 
379     }
380   }
383 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
384 ?>