Code

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