Code

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