Code

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