Code

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