Code

f6c4a5562658d6e832de1c1d3d5d1dc7c93c3d56
[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   function faiVariable ($config, $dn= NULL)
38   {
39     /* Load Attributes */
40     plugin::plugin ($config, $dn);
42     /* If "dn==new" we try to create a new entry
43      * Else we must read all objects from ldap which belong to this entry.
44      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
45      */
46     if($dn != "new"){
47       $this->dn =$dn;
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);
53       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
55       while($object = $ldap->fetch()){
56         /* Set status for save management */
57   
58         foreach($this->subAttributes as $attrs){
59           if(!isset($object[$attrs][0])){
60             $this->SubObjects[$object['cn'][0]][$attrs]="";
61           }else{
62             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
63           }
64         }
65      
66         foreach($this->sub64coded as $codeIt){
67           $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
68         }
69  
70         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
71         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
72       }
73     }
74   }
76   function execute()
77   {
78         /* Call parent execute */
79         plugin::execute();
81     /* Fill templating stuff */
82     $smarty= get_smarty();
83     $display= "";
85     /* Add new sub object */
86     if(isset($_POST['AddSubObject'])){
87       $this->dialog= new $this->subClassName($this->config,"new");
88       $this->is_dialog=true;
89     }
91     if($this->dn != "new"){
92       $_SESSION['objectinfo']= $this->dn;
93     }
96     /* Edit selected Sub Object */
97     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
99       $var = $_POST['SubObject'][0];
100     
101       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
102       $_SESSION['objectinfo'] = $this->SubObjects[$var]['dn'];
103       $this->is_dialog=true;
104     }
105     
106     /* Remove Sub object */
107     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
108       foreach($_POST['SubObject'] as $var){
109         if($this->SubObjects[$var]['status'] == "edited"){
110           $this->SubObjects[$var]['status']= "delete";
111         }else{
112           unset($this->SubObjects[$var]);
113         }
114       }
115     }
117     /* Save Dialog */
118     if(isset($_POST['SaveSubObject'])){
119       $this->dialog->save_object();
120       $msgs = $this->dialog->check();
121       if(count($msgs)>0){
122         foreach($msgs as $msg){
123           print_red($msg);
124         }
125       }else{
126         $obj = $this->dialog->save();
127         if(isset($obj['remove'])){
128           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
129             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
130           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
131             unset($this->SubObjects[$obj['remove']['from']]);
132           }
133           $obj['status'] = "new";
134           $this->SubObjects[$obj['remove']['to']] = $obj;
135           unset($this->SubObjects[$obj['remove']['to']]['remove']);
136         }else{
137           $this->SubObjects[$obj['cn']]=$obj;
138         }
139         $this->is_dialog=false;
140         unset($this->dialog);
141         $this->dialog=NULL;
142       }
143     }
145     /* Sort entries */
146     $tmp = $keys = array();
147     foreach($this->SubObjects as $key => $entry){
148       $keys[$key]=$key;
149     }
150     natcasesort($keys);
151     foreach($keys as $key){
152       $tmp[$key]=$this->SubObjects[$key];
153     } 
154     $this->SubObjects = $tmp;
156     /* Cancel Dialog */
157     if(isset($_POST['CancelSubObject'])){
158       $this->is_dialog=false; 
159       unset($this->dialog);
160       $this->dialog=NULL;
161     }
163     /* Print dialog if $this->dialog is set */
164     if($this->dialog){
165       $this->dialog->save_object();
166       $display = $this->dialog->execute();
167       return($display);
168     }
170     $smarty->assign("SubObjects",$this->getList());
171     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
173       /* Magic quotes GPC, escapes every ' " \, to solve some security risks
174      * If we post the escaped strings they will be escaped again
175      */
177     foreach($this->attributes as $attrs){
178       if(get_magic_quotes_gpc()){
179         $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
180       }else{
181         $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
182       }
183     }
185     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
186     return($display);
187   }
189   /* Generate listbox friendly SubObject list
190   */
191   function getList(){
192     $a_return=array();
193     foreach($this->SubObjects as $obj){
194       if($obj['status'] != "delete"){
196         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
197           if (preg_match("/\[\*\]/", $obj['description'])){
198             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
199           } else {
200             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
201           }
202         }else{
203           if (preg_match("/\[\*\]/", $obj['description'])){
204             $a_return[$obj['cn']]= $obj['cn'];
205           } else {
206             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
207           }
208         }
209       }
210     }
211     return($a_return);
212   }
214   /* Delete me, and all my subtrees
215    */
216   function remove_from_parent()
217   {
218     $ldap = $this->config->get_ldap_link();
219     $ldap->cd ($this->dn);
220     $ldap->rmdir_recursive($this->dn);
221     $this->handle_post_events("remove");    
222   }
225   /* Save data to object 
226    */
227   function save_object()
228   {
229     if(isset($_POST['FAIvariable_posted'])){
230       plugin::save_object();
231       foreach($this->attributes as $attrs){
232         if(isset($_POST[$attrs])){
233           $this->$attrs = $_POST[$attrs];
234         }
235       }
236     }
237   }
240   /* Check supplied data */
241   function check()
242   {
243     $message= array();
244     return ($message);
245   }
248   /* Save to LDAP */
249   function save()
250   {
251     plugin::save();
252  
253     $ldap = $this->config->get_ldap_link();
254   
255     $ldap->cat($this->dn);
256     if($ldap->count()!=0){
257       /* Write FAIscript to ldap*/
258       $ldap->cd($this->dn);
259       $this->cleanup();
260 $ldap->modify ($this->attrs); 
262     }else{
263       /* Write FAIscript to ldap*/
264       $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $this->dn));
265       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
266       $ldap->cd($this->dn);
267       $ldap->add($this->attrs);
268     }
269     show_ldap_error($ldap->get_error());
270  
271     /* Prepare FAIscriptEntry to write it to ldap
272      * First sort array.
273      *  Because we must delete old entries first.
274      * After deletion, we perform add and modify 
275      */
276     $Objects = array();
277     foreach($this->SubObjects as $name => $obj){
278       if($obj['status'] == "delete"){
279         $Objects[$name] = $obj; 
280       }
281     }
282     foreach($this->SubObjects as $name => $obj){
283       if($obj['status'] != "delete"){
284         $Objects[$name] = $obj; 
285       }
286     }
288     foreach($Objects as $name => $obj){
290       foreach($this->sub64coded as $codeIt){
291         $obj[$codeIt]=base64_encode($obj[$codeIt]);
292       }
294       $tmp = array();
295       foreach($this->subAttributes as $attrs){
296         if(empty($obj[$attrs])){
297           $obj[$attrs] = array();
298         }
299         $tmp[$attrs] = $obj[$attrs];
300       }    
301         
302       $tmp['objectClass'] = $this->subClasses;
304       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
306       if($obj['status']=="new"){
307         $ldap->cat($sub_dn);
308         if($ldap->count()){
309           $obj['status']="modify";
310         }
311       }
312  
313       if($obj['status'] == "delete"){
314         $ldap->cd($sub_dn);
315         $ldap->rmdir_recursive($sub_dn);
316         $this->handle_post_events("remove");
317       }elseif($obj['status'] == "edited"){
318         $ldap->cd($sub_dn);
319         $this->cleanup();
320 $ldap->modify ($tmp); 
322         $this->handle_post_events("modify");
323       }elseif($obj['status']=="new"){
325         if($tmp['description'] == array()){
326           unset($tmp['description']);
327         }
329         $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $sub_dn));
330         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
331         $ldap->cd($sub_dn);
332         $ldap->add($tmp); 
333         $this->handle_post_events("add");
334       }
335       show_ldap_error($ldap->get_error()); 
336     }
337   }
340 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
341 ?>