Code

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