Code

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