Code

Added \$this->cleanup before all calls of $ldap->modify
[gosa.git] / plugins / admin / fai / class_faiScript.inc
1 <?php
3 class faiScript 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","FAIscript");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "FAIscriptEntry";
21   var $subClasses       = array("top","FAIclass","FAIscriptEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "faiScriptEntry";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description","FAIpriority","FAIscript"); 
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 faiScript ($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         }
70         foreach($this->subAttributes as $attrs){
71           $this->SubObjects[$object['cn'][0]][$attrs]=addslashes($this->SubObjects[$object['cn'][0]][$attrs]);
72         }
74         $this->SubObjects[$object['cn'][0]]['FAIscript']   = addslashes ($this->readBinary("FAIscript",$object['dn']));
75  
76         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
77         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
78       }
79     }
80   }
82   function execute()
83   {
84         /* Call parent execute */
85         plugin::execute();
87     /* Fill templating stuff */
88     $smarty= get_smarty();
89     $display= "";
91     /* Add new sub object */
92     if(isset($_POST['AddSubObject'])){
93       $this->dialog= new $this->subClassName($this->config,"new");
94       $this->is_dialog=true;
95     }
97     if($this->dn != "new"){
98       $_SESSION['objectinfo']= $this->dn;
99     }
102     /* Edit selected Sub Object */
103     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
104       $script = $_POST['SubObject'][0];
105     
106       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$script]);
107       $_SESSION['objectinfo'] = $this->SubObjects[$script]['dn'];
108       $this->is_dialog=true;
109     }
110     
111     /* Remove Sub object */
112     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
114       foreach($_POST['SubObject'] as $script){
115         if($this->SubObjects[$script]['status'] == "edited"){
116           $this->SubObjects[$script]['status']= "delete";
117         }else{
118           unset($this->SubObjects[$script]);
119         }
120       }
121     }
123     /* Save Dialog */
124     if(isset($_POST['SaveSubObject'])){
125       $this->dialog->save_object();
126       $msgs = $this->dialog->check();
127       if(count($msgs)>0){
128         foreach($msgs as $msg){
129           print_red($msg);
130         }
131       }else{
132         $obj = $this->dialog->save();
133         if(isset($obj['remove'])){
134           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
135             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
136           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
137             unset($this->SubObjects[$obj['remove']['from']]);
138           }
139           $obj['status'] = "new";
140           $this->SubObjects[$obj['remove']['to']] = $obj;
141           unset($this->SubObjects[$obj['remove']['to']]['remove']);
142         }else{
143           $this->SubObjects[$obj['cn']]=$obj;
144         }
145         $this->is_dialog=false;
146         unset($this->dialog);
147         $this->dialog=NULL;
148       }
149     }
151     /* Sort entries */
152     $tmp = $keys = array();
153     foreach($this->SubObjects as $key => $entry){
154       $keys[$key]=$key;
155     }
156     natcasesort($keys);
157     foreach($keys as $key){
158       $tmp[$key]=$this->SubObjects[$key];
159     }
160     $this->SubObjects = $tmp;
162     /* Cancel Dialog */
163     if(isset($_POST['CancelSubObject'])){
164       $this->is_dialog=false; 
165       unset($this->dialog);
166       $this->dialog=NULL;
167     }
169     /* Print dialog if $this->dialog is set */
170     if($this->dialog){
171       $this->dialog->save_object();
172       $display = $this->dialog->execute();
173       return($display);
174     }
176     $smarty->assign("SubObjects",$this->getList());
177     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
178      
179     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
180     * If we post the escaped strings they will be escaped again
181     */
182     foreach($this->attributes as $attrs){
183       if(get_magic_quotes_gpc()){
184         $smarty->assign($attrs,stripslashes($this->$attrs));
185       }else{
186         $smarty->assign($attrs,($this->$attrs));
187       }
188     }
190     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
191     return($display);
192   }
194   /* Generate listbox friendly SubObject list
195   */
196   function getList(){
197     $a_return=array();
198     foreach($this->SubObjects as $obj){
199       if($obj['status'] != "delete"){
200         if((isset($obj['description']))&&(!empty($obj['description']))){
201           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
202         }else{
203           $a_return[$obj['cn']]= $obj['cn'];
204         }
205       }
206     }
207     return($a_return);
208   }
210   /* Delete me, and all my subtrees
211    */
212   function remove_from_parent()
213   {
214     $ldap = $this->config->get_ldap_link();
215     $ldap->cd ($this->dn);
216     $ldap->rmdir_recursive($this->dn);
217     $this->handle_post_events("remove");    
218   }
221   /* Save data to object 
222    */
223   function save_object()
224   {
225     if(isset($_POST['FAIscript_posted'])){
226       plugin::save_object();
227       foreach($this->attributes as $attrs){
228         if(isset($_POST[$attrs])){
229           $this->$attrs = $_POST[$attrs];
230         }
231       }
232     }
233   }
236   /* Check supplied data */
237   function check()
238   {
239     $message= array();
240     return ($message);
241   }
244   /* Save to LDAP */
245   function save()
246   {
247     plugin::save();
248  
249     $ldap = $this->config->get_ldap_link();
250   
251     $ldap->cat($this->dn);
252     if($ldap->count()!=0){
253       /* Write FAIscript to ldap*/
254       $ldap->cd($this->dn);
255       $this->cleanup();
256 $ldap->modify ($this->attrs); 
258     }else{
259       /* Write FAIscript to ldap*/
260       $ldap->cd($this->config->current['BASE']);
261       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
262       $ldap->cd($this->dn);
263       $ldap->add($this->attrs);
264     }
265     show_ldap_error($ldap->get_error());
266  
267     /* Prepare FAIscriptEntry to write it to ldap
268      * First sort array.
269      *  Because we must delete old entries first.
270      * After deletion, we perform add and modify 
271      */
272     $Objects = array();
273     foreach($this->SubObjects as $name => $obj){
274       if($obj['status'] == "delete"){
275         $Objects[$name] = $obj; 
276       }
277     }
278     foreach($this->SubObjects as $name => $obj){
279       if($obj['status'] != "delete"){
280         $Objects[$name] = $obj; 
281       }
282     }
284     foreach($Objects as $name => $obj){
286       foreach($this->sub64coded as $codeIt){
287         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
288       }
290       $tmp = array();
291       foreach($this->subAttributes as $attrs){
292         if(empty($obj[$attrs])){
293           $obj[$attrs] = array();
294         }
295         if(!is_array($obj[$attrs])){
296           $tmp[$attrs] = stripslashes($obj[$attrs]);
297         }else{
298           $tmp[$attrs] = $obj[$attrs];
299         }
300       }    
301         
302       $tmp['objectClass'] = $this->subClasses;
304       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
306       if($obj['status']=="new"){
307         $ldap->cat($sub_dn);
308         if($ldap->count()){
309           $obj['status']="modify";
310         }
311       }
313       if(empty($tmp['FAIpriority'])){
314         $tmp['FAIpriority']  ="0";
315       }
316   
317       if($obj['status'] == "delete"){
318         $ldap->cd($sub_dn);
319         $ldap->rmdir_recursive($sub_dn);
320         $this->handle_post_events("remove");
321       }elseif($obj['status'] == "edited"){
322         $ldap->cd($sub_dn);
323         $this->cleanup();
324 $ldap->modify ($tmp); 
326         $this->handle_post_events("modify");
327       }elseif($obj['status']=="new"){
328         if($tmp['description']==array()){
329           unset($tmp['description']);
330         }
331         if($tmp['FAIscript']==array()){
332           $tmp['FAIscript']=" ";
333         }
334         $ldap->cd($this->config->current['BASE']);
335         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
336         $ldap->cd($sub_dn);
337         $ldap->add($tmp); 
338         $this->handle_post_events("add");
339       }
340       show_ldap_error($ldap->get_error()); 
341     }
342   }
345   function readBinary($attr,$dn){
346     $Data  ="";
347     $ds= ldap_connect($this->config->current['SERVER']);
348     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
349     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
350       ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
351       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
352     }
354     if(isset($this->config->current['TLS']) &&  $this->config->current['TLS'] == "true"){
355       ldap_start_tls($ds);
356     }
358     $r  = ldap_bind($ds);
359     $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
361     if ($sr) {
362       $ei=ldap_first_entry($ds, $sr);
363       if ($ei) {
364         if ($info = ldap_get_values_len($ds, $ei, $attr)){
365           $Data= $info[0];
366         }
367       }
368     }
370     /* close conncetion */
371     ldap_unbind($ds);
372     return($Data);
373   }
378 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
379 ?>