Code

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