Code

Freezed releases can now be viewed, but not changed ...
[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   var $FAIstate         = "";
39   function faiVariable ($config, $dn= NULL)
40   {
41     /* Load Attributes */
42     plugin::plugin ($config, $dn);
44     $this->acl ="#all#";
46     /* If "dn==new" we try to create a new entry
47      * Else we must read all objects from ldap which belong to this entry.
48      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
49      */
51     if($dn != "new"){
52       $this->dn =$dn;
54       /* Set acls
55        */
56       $ui   = get_userinfo();
57       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
58       $acli = get_module_permission($acl, "FAIclass", $this->dn);
59       $this->acl=$acli;
61       /* Get FAIstate
62        */
63       if(isset($this->attrs['FAIstate'][0])){
64         $this->FAIstate = $this->attrs['FAIstate'][0];
65       }
67       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
68        */
69       $ldap     = $this->config->get_ldap_link();
70       $ldap->cd ($this->dn);
71       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
73       while($object = $ldap->fetch()){
74         /* Set status for save management */
75   
76         foreach($this->subAttributes as $attrs){
77           if(!isset($object[$attrs][0])){
78             $this->SubObjects[$object['cn'][0]][$attrs]="";
79           }else{
80             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
81           }
82         }
83      
84         foreach($this->sub64coded as $codeIt){
85           $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
86         }
87  
88         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
89         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
90       }
91     }
92   }
94   function execute()
95   {
96         /* Call parent execute */
97         plugin::execute();
99     /* Fill templating stuff */
100     $smarty= get_smarty();
101     $display= "";
103     /* Add new sub object */
104     if(isset($_POST['AddSubObject'])){
105       $this->dialog= new $this->subClassName($this->config,"new");
106       $this->dialog->acl = $this->acl;
107       $this->is_dialog=true;
108     }
110     if($this->dn != "new"){
111       $_SESSION['objectinfo']= $this->dn;
112     }
115     /* Edit selected Sub Object */
116     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
118       $var = $_POST['SubObject'][0];
119     
120       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$var]);
121       $this->dialog->acl = $this->acl;
122       $_SESSION['objectinfo'] = $this->SubObjects[$var]['dn'];
123       $this->is_dialog=true;
124     }
125     
126     /* Remove Sub object */
127     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
128       foreach($_POST['SubObject'] as $var){
129         if($this->SubObjects[$var]['status'] == "edited"){
130           $this->SubObjects[$var]['status']= "delete";
131         }else{
132           unset($this->SubObjects[$var]);
133         }
134       }
135     }
137     /* Save Dialog */
138     if(isset($_POST['SaveSubObject'])){
139       $this->dialog->save_object();
140       $msgs = $this->dialog->check();
141       if(count($msgs)>0){
142         foreach($msgs as $msg){
143           print_red($msg);
144         }
145       }else{
146         $obj = $this->dialog->save();
147         if(isset($obj['remove'])){
148           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
149             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
150           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
151             unset($this->SubObjects[$obj['remove']['from']]);
152           }
153           $obj['status'] = "new";
154           $this->SubObjects[$obj['remove']['to']] = $obj;
155           unset($this->SubObjects[$obj['remove']['to']]['remove']);
156         }else{
157           $this->SubObjects[$obj['cn']]=$obj;
158         }
159         $this->is_dialog=false;
160         unset($this->dialog);
161         $this->dialog=NULL;
162       }
163     }
165     /* Sort entries */
166     $tmp = $keys = array();
167     foreach($this->SubObjects as $key => $entry){
168       $keys[$key]=$key;
169     }
170     natcasesort($keys);
171     foreach($keys as $key){
172       $tmp[$key]=$this->SubObjects[$key];
173     } 
174     $this->SubObjects = $tmp;
176     /* Cancel Dialog */
177     if(isset($_POST['CancelSubObject'])){
178       $this->is_dialog=false; 
179       unset($this->dialog);
180       $this->dialog=NULL;
181     }
183     /* Print dialog if $this->dialog is set */
184     if($this->dialog){
185       $this->dialog->save_object();
186       $display = $this->dialog->execute();
187       return($display);
188     }
190     $smarty->assign("SubObjects",$this->getList());
191     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
193       /* Magic quotes GPC, escapes every ' " \, to solve some security risks
194      * If we post the escaped strings they will be escaped again
195      */
197     foreach($this->attributes as $attrs){
198       if(get_magic_quotes_gpc()){
199         $smarty->assign($attrs,htmlentities (stripslashes(utf8_decode($this->$attrs))));
200       }else{
201         $smarty->assign($attrs,htmlentities (utf8_decode($this->$attrs)));
202       }
203     }
205       foreach($this->attributes as $attr){
206       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
207     }
210     $display.= $smarty->fetch(get_template_path('faiVariable.tpl', TRUE));
211     return($display);
212   }
214   /* Generate listbox friendly SubObject list
215   */
216   function getList(){
217     $a_return=array();
218     foreach($this->SubObjects as $obj){
219       if($obj['status'] != "delete"){
221         if((isset($obj['description']))&&(!empty($obj['description']))&&(!preg_match("/\[\*\]/",$obj['description']))){
222           if (preg_match("/\[\*\]/", $obj['description'])){
223             $a_return[$obj['cn']]= $obj['cn']." [".preg_replace("/\s*\[\*\]\s*/", "", $obj['description'])."]";
224           } else {
225             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent']." [".$obj['description']."]";
226           }
227         }else{
228           if (preg_match("/\[\*\]/", $obj['description'])){
229             $a_return[$obj['cn']]= $obj['cn'];
230           } else {
231             $a_return[$obj['cn']]= $obj['cn']."=".$obj['FAIvariableContent'];
232           }
233         }
234       }
235     }
236     return($a_return);
237   }
239   /* Delete me, and all my subtrees
240    */
241   function remove_from_parent()
242   {
243     $ldap = $this->config->get_ldap_link();
244     $ldap->cd ($this->dn);
245     $ldap->rmdir_recursive($this->dn);
246     $this->handle_post_events("remove");    
247   }
250   /* Save data to object 
251    */
252   function save_object()
253   {
254     if((isset($_POST['FAIvariable_posted'])) && ($this->FAIstate != "freeze") ){
255       plugin::save_object();
256       foreach($this->attributes as $attrs){
257         if(isset($_POST[$attrs])){
258           $this->$attrs = $_POST[$attrs];
259         }
260       }
261     }
262   }
265   /* Check supplied data */
266   function check()
267   {
268     $message= array();
269     return ($message);
270   }
273   /* Save to LDAP */
274   function save()
275   {
276     plugin::save();
277  
278     $ldap = $this->config->get_ldap_link();
279   
280     $ldap->cat($this->dn);
281     if($ldap->count()!=0){
282       /* Write FAIscript to ldap*/
283       $ldap->cd($this->dn);
284       $this->cleanup();
285 $ldap->modify ($this->attrs); 
287     }else{
288       /* Write FAIscript to ldap*/
289       $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $this->dn));
290       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
291       $ldap->cd($this->dn);
292       $ldap->add($this->attrs);
293     }
294     show_ldap_error($ldap->get_error());
295  
296     /* Prepare FAIscriptEntry to write it to ldap
297      * First sort array.
298      *  Because we must delete old entries first.
299      * After deletion, we perform add and modify 
300      */
301     $Objects = array();
302     foreach($this->SubObjects as $name => $obj){
303       if($obj['status'] == "delete"){
304         $Objects[$name] = $obj; 
305       }
306     }
307     foreach($this->SubObjects as $name => $obj){
308       if($obj['status'] != "delete"){
309         $Objects[$name] = $obj; 
310       }
311     }
313     foreach($Objects as $name => $obj){
315       foreach($this->sub64coded as $codeIt){
316         $obj[$codeIt]=base64_encode($obj[$codeIt]);
317       }
319       $tmp = array();
320       foreach($this->subAttributes as $attrs){
321         if(empty($obj[$attrs])){
322           $obj[$attrs] = array();
323         }
324         $tmp[$attrs] = $obj[$attrs];
325       }    
326         
327       $tmp['objectClass'] = $this->subClasses;
329       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
331       if($obj['status']=="new"){
332         $ldap->cat($sub_dn);
333         if($ldap->count()){
334           $obj['status']="modify";
335         }
336       }
337  
338       if($obj['status'] == "delete"){
339         $ldap->cd($sub_dn);
340         $ldap->rmdir_recursive($sub_dn);
341         $this->handle_post_events("remove");
342       }elseif($obj['status'] == "edited"){
343         $ldap->cd($sub_dn);
344         $this->cleanup();
345 $ldap->modify ($tmp); 
347         $this->handle_post_events("modify");
348       }elseif($obj['status']=="new"){
350         if($tmp['description'] == array()){
351           unset($tmp['description']);
352         }
354         $ldap->cd(preg_replace('/^[^,]+,[^,]+,/', '', $sub_dn));
355         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
356         $ldap->cd($sub_dn);
357         $ldap->add($tmp); 
358         $this->handle_post_events("add");
359       }
360       show_ldap_error($ldap->get_error()); 
361     }
362   }
365 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
366 ?>