Code

50e5349ad35f29dd70001030d775ff04f7bb01c9
[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       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$_POST['SubObject']]);
105       $_SESSION['objectinfo'] = $this->SubObjects[$_POST['SubObject']]['dn'];
106       $this->is_dialog=true;
107     }
108     
109     /* Remove Sub object */
110     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
111       if($this->SubObjects[$_POST['SubObject']]['status'] == "edited"){
112         $this->SubObjects[$_POST['SubObject']]['status']= "delete";
113       }else{
114         unset($this->SubObjects[$_POST['SubObject']]);
115       }
116     }
118     /* Save Dialog */
119     if(isset($_POST['SaveSubObject'])){
120       $this->dialog->save_object();
121       $msgs = $this->dialog->check();
122       if(count($msgs)>0){
123         foreach($msgs as $msg){
124           print_red($msg);
125         }
126       }else{
127         $obj = $this->dialog->save();
128         if(isset($obj['remove'])){
129           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
130             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
131           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
132             unset($this->SubObjects[$obj['remove']['from']]);
133           }
134           $obj['status'] = "new";
135           $this->SubObjects[$obj['remove']['to']] = $obj;
136           unset($this->SubObjects[$obj['remove']['to']]['remove']);
137         }else{
138           $this->SubObjects[$obj['cn']]=$obj;
139         }
140         $this->is_dialog=false;
141         unset($this->dialog);
142         $this->dialog=NULL;
143       }
144     }
146     /* Sort entries */
147     $tmp = $keys = array();
148     foreach($this->SubObjects as $key => $entry){
149       $keys[$key]=$key;
150     }
151     natcasesort($keys);
152     foreach($keys as $key){
153       $tmp[$key]=$this->SubObjects[$key];
154     }
155     $this->SubObjects = $tmp;
157     /* Cancel Dialog */
158     if(isset($_POST['CancelSubObject'])){
159       $this->is_dialog=false; 
160       unset($this->dialog);
161       $this->dialog=NULL;
162     }
164     /* Print dialog if $this->dialog is set */
165     if($this->dialog){
166       $this->dialog->save_object();
167       $display = $this->dialog->execute();
168       return($display);
169     }
171     $smarty->assign("SubObjects",$this->getList());
172     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
173      
174     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
175     * If we post the escaped strings they will be escaped again
176     */
177     foreach($this->attributes as $attrs){
178       if(get_magic_quotes_gpc()){
179         $smarty->assign($attrs,stripslashes($this->$attrs));
180       }else{
181         $smarty->assign($attrs,($this->$attrs));
182       }
183     }
185     $display.= $smarty->fetch(get_template_path('faiScript.tpl', TRUE));
186     return($display);
187   }
189   /* Generate listbox friendly SubObject list
190   */
191   function getList(){
192     $a_return=array();
193     foreach($this->SubObjects as $obj){
194       if($obj['status'] != "delete"){
195         if((isset($obj['description']))&&(!empty($obj['description']))){
196           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
197         }else{
198           $a_return[$obj['cn']]= $obj['cn'];
199         }
200       }
201     }
202     return($a_return);
203   }
205   /* Delete me, and all my subtrees
206    */
207   function remove_from_parent()
208   {
209     $ldap = $this->config->get_ldap_link();
210     $ldap->cd ($this->dn);
211     $ldap->rmdir_recursive($this->dn);
212     $this->handle_post_events("remove");    
213   }
216   /* Save data to object 
217    */
218   function save_object()
219   {
220     if(isset($_POST['FAIscript_posted'])){
221       plugin::save_object();
222       foreach($this->attributes as $attrs){
223         if(isset($_POST[$attrs])){
224           $this->$attrs = $_POST[$attrs];
225         }
226       }
227     }
228   }
231   /* Check supplied data */
232   function check()
233   {
234     $message= array();
235     return ($message);
236   }
239   /* Save to LDAP */
240   function save()
241   {
242     plugin::save();
243  
244     $ldap = $this->config->get_ldap_link();
245   
246     $ldap->cat($this->dn);
247     if($ldap->count()!=0){
248       /* Write FAIscript to ldap*/
249       $ldap->cd($this->dn);
250       $ldap->modify($this->attrs);
251     }else{
252       /* Write FAIscript to ldap*/
253       $ldap->cd($this->config->current['BASE']);
254       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
255       $ldap->cd($this->dn);
256       $ldap->add($this->attrs);
257     }
258     show_ldap_error($ldap->get_error());
259  
260     /* Prepare FAIscriptEntry to write it to ldap
261      * First sort array.
262      *  Because we must delete old entries first.
263      * After deletion, we perform add and modify 
264      */
265     $Objects = array();
266     foreach($this->SubObjects as $name => $obj){
267       if($obj['status'] == "delete"){
268         $Objects[$name] = $obj; 
269       }
270     }
271     foreach($this->SubObjects as $name => $obj){
272       if($obj['status'] != "delete"){
273         $Objects[$name] = $obj; 
274       }
275     }
277     foreach($Objects as $name => $obj){
279       foreach($this->sub64coded as $codeIt){
280         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
281       }
283       $tmp = array();
284       foreach($this->subAttributes as $attrs){
285         if(empty($obj[$attrs])){
286           $obj[$attrs] = array();
287         }
288         if(!is_array($obj[$attrs])){
289           $tmp[$attrs] = stripslashes($obj[$attrs]);
290         }else{
291           $tmp[$attrs] = $obj[$attrs];
292         }
293       }    
294         
295       $tmp['objectClass'] = $this->subClasses;
297       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
299       if($obj['status']=="new"){
300         $ldap->cat($sub_dn);
301         if($ldap->count()){
302           $obj['status']="modify";
303         }
304       }
306       if(empty($tmp['FAIpriority'])){
307         $tmp['FAIpriority']  ="0";
308       }
309   
310       if($obj['status'] == "delete"){
311         $ldap->cd($sub_dn);
312         $ldap->rmdir_recursive($sub_dn);
313         $this->handle_post_events("remove");
314       }elseif($obj['status'] == "edited"){
315         $ldap->cd($sub_dn);
316         $ldap->modify($tmp);
317         $this->handle_post_events("modify");
318       }elseif($obj['status']=="new"){
319         if($tmp['description']==array()){
320           unset($tmp['description']);
321         }
322         if($tmp['FAIscript']==array()){
323           $tmp['FAIscript']=" ";
324         }
325         $ldap->cd($this->config->current['BASE']);
326         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
327         $ldap->cd($sub_dn);
328         $ldap->add($tmp); 
329         $this->handle_post_events("add");
330       }
331       show_ldap_error($ldap->get_error()); 
332     }
333   }
336   function readBinary($attr,$dn){
337     $Data  ="";
338     $ds= ldap_connect($this->config->current['SERVER']);
339     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
340     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
341       ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
342       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
343     }
345     if(isset($this->config->current['TLS']) &&  $this->config->current['TLS'] == "true"){
346       ldap_start_tls($ds);
347     }
349     $r  = ldap_bind($ds);
350     $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
352     if ($sr) {
353       $ei=ldap_first_entry($ds, $sr);
354       if ($ei) {
355         if ($info = ldap_get_values_len($ds, $ei, $attr)){
356           $Data= $info[0];
357         }
358       }
359     }
361     /* close conncetion */
362     ldap_unbind($ds);
363     return($Data);
364   }
369 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
370 ?>