Code

Fixed categorie saving
[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       ksort($this->SubObjects);
74     }
75   }
77   function execute()
78   {
79         /* Call parent execute */
80         plugin::execute();
82     /* Fill templating stuff */
83     $smarty= get_smarty();
84     $display= "";
86     /* Add new sub object */
87     if(isset($_POST['AddSubObject'])){
88       $this->dialog= new $this->subClassName($this->config,"new");
89       $this->is_dialog=true;
90     }
92     if($this->dn != "new"){
93       $_SESSION['objectinfo']= $this->dn;
94     }
97     /* Edit selected Sub Object */
98     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
99       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$_POST['SubObject']]);
100       $_SESSION['objectinfo'] = $this->SubObjects[$_POST['SubObject']]['dn'];
101       $this->is_dialog=true;
102     }
103     
104     /* Remove Sub object */
105     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
106       if($this->SubObjects[$_POST['SubObject']]['status'] == "edited"){
107         $this->SubObjects[$_POST['SubObject']]['status']= "delete";
108       }else{
109         unset($this->SubObjects[$_POST['SubObject']]);
110       }
111     }
113     /* Save Dialog */
114     if(isset($_POST['SaveSubObject'])){
115       $this->dialog->save_object();
116       $msgs = $this->dialog->check();
117       if(count($msgs)>0){
118         foreach($msgs as $msg){
119           print_red($msg);
120         }
121       }else{
122         $obj = $this->dialog->save();
123         if(isset($obj['remove'])){
124           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
125             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
126           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
127             unset($this->SubObjects[$obj['remove']['from']]);
128           }
129           $obj['status'] = "new";
130           $this->SubObjects[$obj['remove']['to']] = $obj;
131           unset($this->SubObjects[$obj['remove']['to']]['remove']);
132         }else{
133           $this->SubObjects[$obj['cn']]=$obj;
134         }
135         $this->is_dialog=false;
136         unset($this->dialog);
137         $this->dialog=NULL;
138         ksort($this->SubObjects);
139       }
140     }
142     /* Cancel Dialog */
143     if(isset($_POST['CancelSubObject'])){
144       $this->is_dialog=false; 
145       unset($this->dialog);
146       $this->dialog=NULL;
147     }
149     /* Print dialog if $this->dialog is set */
150     if($this->dialog){
151       $this->dialog->save_object();
152       $display = $this->dialog->execute();
153       return($display);
154     }
156     $smarty->assign("SubObjects",$this->getList());
157     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
159       /* Magic quotes GPC, escapes every ' " \, to solve some security risks
160      * If we post the escaped strings they will be escaped again
161      */
162     foreach($this->attributes as $attrs){
163       if(get_magic_quotes_gpc()){
164         $smarty->assign($attrs,stripslashes($this->$attrs));
165       }else{
166         $smarty->assign($attrs,($this->$attrs));
167       }
168     }
171     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
172     return($display);
173   }
175   /* Generate listbox friendly SubObject list
176   */
177   function getList(){
178     $a_return=array();
179     foreach($this->SubObjects as $obj){
180       if($obj['status'] != "delete"){
182         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
183           if (preg_match("/\[\*\]/", $obj['description'])){
184             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
185           } else {
186             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
187           }
188         }else{
189           if (preg_match("/\[\*\]/", $obj['description'])){
190             $a_return[$obj['cn']]= $obj['cn'];
191           } else {
192             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
193           }
194         }
195       }
196     }
197     return($a_return);
198   }
200   /* Delete me, and all my subtrees
201    */
202   function remove_from_parent()
203   {
204     $ldap = $this->config->get_ldap_link();
205     $ldap->cd ($this->dn);
206     $ldap->rmdir_recursive($this->dn);
207     $this->handle_post_events("remove");    
208   }
211   /* Save data to object 
212    */
213   function save_object()
214   {
215     if(isset($_POST['FAIvariable_posted'])){
216       plugin::save_object();
217       foreach($this->attributes as $attrs){
218         if(isset($_POST[$attrs])){
219           $this->$attrs = $_POST[$attrs];
220         }
221       }
222     }
223   }
226   /* Check supplied data */
227   function check()
228   {
229     $message= array();
230     return ($message);
231   }
234   /* Save to LDAP */
235   function save()
236   {
237     plugin::save();
238  
239     $ldap = $this->config->get_ldap_link();
240   
241     $ldap->cat($this->dn);
242     if($ldap->count()!=0){
243       /* Write FAIscript to ldap*/
244       $ldap->cd($this->dn);
245       $ldap->modify($this->attrs);
246     }else{
247       /* Write FAIscript to ldap*/
248       $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $this->dn));
249       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
250       $ldap->cd($this->dn);
251       $ldap->add($this->attrs);
252     }
253     show_ldap_error($ldap->get_error());
254  
255     /* Prepare FAIscriptEntry to write it to ldap
256      * First sort array.
257      *  Because we must delete old entries first.
258      * After deletion, we perform add and modify 
259      */
260     $Objects = array();
261     foreach($this->SubObjects as $name => $obj){
262       if($obj['status'] == "delete"){
263         $Objects[$name] = $obj; 
264       }
265     }
266     foreach($this->SubObjects as $name => $obj){
267       if($obj['status'] != "delete"){
268         $Objects[$name] = $obj; 
269       }
270     }
272     foreach($Objects as $name => $obj){
274       foreach($this->sub64coded as $codeIt){
275         $obj[$codeIt]=base64_encode($obj[$codeIt]);
276       }
278       $tmp = array();
279       foreach($this->subAttributes as $attrs){
280         if(empty($obj[$attrs])){
281           $obj[$attrs] = array();
282         }
283         $tmp[$attrs] = $obj[$attrs];
284       }    
285         
286       $tmp['objectClass'] = $this->subClasses;
288       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
290       if($obj['status']=="new"){
291         $ldap->cat($sub_dn);
292         if($ldap->count()){
293           $obj['status']="modify";
294         }
295       }
296  
297       if($obj['status'] == "delete"){
298         $ldap->cd($sub_dn);
299         $ldap->rmdir_recursive($sub_dn);
300         $this->handle_post_events("remove");
301       }elseif($obj['status'] == "edited"){
302         $ldap->cd($sub_dn);
303         $ldap->modify($tmp);
304         $this->handle_post_events("modify");
305       }elseif($obj['status']=="new"){
307         if($tmp['description'] == array()){
308           unset($tmp['description']);
309         }
311         $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $sub_dn));
312         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
313         $ldap->cd($sub_dn);
314         $ldap->add($tmp); 
315         $this->handle_post_events("add");
316       }
317       show_ldap_error($ldap->get_error()); 
318     }
319   }
322 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
323 ?>