Code

Added multideletion for FAIscripts
[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       $ldap->modify($this->attrs);
256     }else{
257       /* Write FAIscript to ldap*/
258       $ldap->cd($this->config->current['BASE']);
259       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
260       $ldap->cd($this->dn);
261       $ldap->add($this->attrs);
262     }
263     show_ldap_error($ldap->get_error());
264  
265     /* Prepare FAIscriptEntry to write it to ldap
266      * First sort array.
267      *  Because we must delete old entries first.
268      * After deletion, we perform add and modify 
269      */
270     $Objects = array();
271     foreach($this->SubObjects as $name => $obj){
272       if($obj['status'] == "delete"){
273         $Objects[$name] = $obj; 
274       }
275     }
276     foreach($this->SubObjects as $name => $obj){
277       if($obj['status'] != "delete"){
278         $Objects[$name] = $obj; 
279       }
280     }
282     foreach($Objects as $name => $obj){
284       foreach($this->sub64coded as $codeIt){
285         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
286       }
288       $tmp = array();
289       foreach($this->subAttributes as $attrs){
290         if(empty($obj[$attrs])){
291           $obj[$attrs] = array();
292         }
293         if(!is_array($obj[$attrs])){
294           $tmp[$attrs] = stripslashes($obj[$attrs]);
295         }else{
296           $tmp[$attrs] = $obj[$attrs];
297         }
298       }    
299         
300       $tmp['objectClass'] = $this->subClasses;
302       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
304       if($obj['status']=="new"){
305         $ldap->cat($sub_dn);
306         if($ldap->count()){
307           $obj['status']="modify";
308         }
309       }
311       if(empty($tmp['FAIpriority'])){
312         $tmp['FAIpriority']  ="0";
313       }
314   
315       if($obj['status'] == "delete"){
316         $ldap->cd($sub_dn);
317         $ldap->rmdir_recursive($sub_dn);
318         $this->handle_post_events("remove");
319       }elseif($obj['status'] == "edited"){
320         $ldap->cd($sub_dn);
321         $ldap->modify($tmp);
322         $this->handle_post_events("modify");
323       }elseif($obj['status']=="new"){
324         if($tmp['description']==array()){
325           unset($tmp['description']);
326         }
327         if($tmp['FAIscript']==array()){
328           $tmp['FAIscript']=" ";
329         }
330         $ldap->cd($this->config->current['BASE']);
331         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
332         $ldap->cd($sub_dn);
333         $ldap->add($tmp); 
334         $this->handle_post_events("add");
335       }
336       show_ldap_error($ldap->get_error()); 
337     }
338   }
341   function readBinary($attr,$dn){
342     $Data  ="";
343     $ds= ldap_connect($this->config->current['SERVER']);
344     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
345     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
346       ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
347       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
348     }
350     if(isset($this->config->current['TLS']) &&  $this->config->current['TLS'] == "true"){
351       ldap_start_tls($ds);
352     }
354     $r  = ldap_bind($ds);
355     $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
357     if ($sr) {
358       $ei=ldap_first_entry($ds, $sr);
359       if ($ei) {
360         if ($info = ldap_get_values_len($ds, $ei, $attr)){
361           $Data= $info[0];
362         }
363       }
364     }
366     /* close conncetion */
367     ldap_unbind($ds);
368     return($Data);
369   }
374 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
375 ?>