Code

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