Code

0cc84fbeb619af9ee609c3f173665037a3a77705
[gosa.git] / plugins / admin / fai / class_faiHook.inc
1 <?php
3 class faiHook 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","FAIhook");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "FAIhookEntry";
21   var $subClasses       = array("top","FAIclass","FAIhookEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "faiHookEntry";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description","FAItask","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 faiHook ($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      */
45     if($dn != "new"){
46       $this->dn =$dn;
48       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
49        */
50       $ldap     = $this->config->get_ldap_link();
51       $ldap->cd ($this->dn);
52       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
54       while($object = $ldap->fetch()){
55         /* Set status for save management */
56   
57         foreach($this->subAttributes as $attrs){
58           if(!isset($object[$attrs][0])){
59             $this->SubObjects[$object['cn'][0]][$attrs]="";
60           }else{
61             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
62           }
63         }
64      
65         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
66         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
68         foreach($this->sub64coded as $codeIt){
69           $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
70         }
72         foreach($this->subAttributes as $attrs){
73           $this->SubObjects[$object['cn'][0]][$attrs]=addslashes($this->SubObjects[$object['cn'][0]][$attrs]);
74         }
75         $this->SubObjects[$object['cn'][0]]['FAIscript']   = addslashes($this->readBinary("FAIscript",$object['dn']));
76       }
77     }
78   }
80   function execute()
81   {
82     /* Fill templating stuff */
83     $smarty= get_smarty();
84     $display= "";
86     /* Add new sub object */
87     if(isset($_POST['AddSubObject'])){
88       $this->dialog= new $this->subClassName($this->config,"new");
89       $this->is_dialog=true;
90     }
92     $_SESSION['objectinfo']= $this->dn;
93     /* Edit selected Sub Object */
94     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
95       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$_POST['SubObject']]);
96       $_SESSION['objectinfo'] = $this->SubObjects[$_POST['SubObject']]['dn'];
97       $this->is_dialog=true;
98     }
99     
100     /* Remove Sub object */
101     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
102       if($this->SubObjects[$_POST['SubObject']]['status'] == "edited"){
103         $this->SubObjects[$_POST['SubObject']]['status']= "delete";
104       }else{
105         unset($this->SubObjects[$_POST['SubObject']]);
106       }
107     }
109     /* Save Dialog */
110     if(isset($_POST['SaveSubObject'])){
112       /* Perform post check*/
113       $this->dialog->save_object();
115       /* Get messages */
116       $msgs = $this->dialog->check();
118       /* print errors */
119       if(count($msgs)>0){
120         foreach($msgs as $msg){
121           print_red($msg);
122         }
123       }else{
125         /* Get return object */
126         $obj = $this->dialog->save();
127         if(isset($obj['remove'])){
129           /* Depending on status, set new status */
130           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
131             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
132           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
133             unset($this->SubObjects[$obj['remove']['from']]);
134           }
135           $obj['status'] = "new";
136           $this->SubObjects[$obj['remove']['to']] = $obj;
137           unset($this->SubObjects[$obj['remove']['to']]['remove']);
138         }else{
139           $this->SubObjects[$obj['cn']]=$obj;
140         }
141         $this->is_dialog=false;
142         unset($this->dialog);
143         $this->dialog=NULL;
144       }
145     }
147     /* Cancel Dialog */
148     if(isset($_POST['CancelSubObject'])){
149       $this->is_dialog=false; 
150       unset($this->dialog);
151       $this->dialog=NULL;
152     }
154     /* Print dialog if $this->dialog is set */
155     if($this->dialog){
156       $this->dialog->save_object();
157       $display = $this->dialog->execute();
158       return($display);
159     }
161     $smarty->assign("SubObjects",$this->getList());
162     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
164      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
165      * If we post the escaped strings they will be escaped again
166      */
167     foreach($this->attributes as $attrs){
168       if(get_magic_quotes_gpc()){
169         $smarty->assign($attrs,stripslashes($this->$attrs));
170       }else{
171         $smarty->assign($attrs,($this->$attrs));
172       }
173     }
176     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
177     return($display);
178   }
180   /* Generate listbox friendly SubObject list
181   */
182   function getList(){
183     $a_return=array();
184     foreach($this->SubObjects as $obj){
185       if($obj['status'] != "delete"){
186         if((isset($obj['description']))&&(!empty($obj['description']))){
187           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
188         }else{
189           $a_return[$obj['cn']]= $obj['cn'];
190         }
191       }
192     }
193     return($a_return);
194   }
196   /* Delete me, and all my subtrees
197    */
198   function remove_from_parent()
199   {
200     $ldap = $this->config->get_ldap_link();
201     $ldap->cd ($this->dn);
202     $ldap->rmdir_recursive($this->dn);
203     $this->handle_post_events("remove");    
204   }
207   /* Save data to object 
208    */
209   function save_object()
210   {
211     if(isset($_POST['FAIhook_posted'])){
212       plugin::save_object();
213       foreach($this->attributes as $attrs){
214         if(isset($_POST[$attrs])){
215           $this->$attrs = $_POST[$attrs];
216         }
217       }
218     }
219   }
222   /* Check supplied data */
223   function check()
224   {
225     $message= array();
226     return ($message);
227   }
230   /* Save to LDAP */
231   function save()
232   {
233     plugin::save();
234  
235     $ldap = $this->config->get_ldap_link();
237     $ldap->cat($this->dn);
238     if($ldap->count()!=0){     
239       /* Write FAIscript to ldap*/ 
240       $ldap->cd($this->dn);
241       $ldap->modify($this->attrs);
242     }else{
243       /* Write FAIscript to ldap*/ 
244       $ldap->cd($this->config->current['BASE']);
245       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
246       $ldap->cd($this->dn);
247       $ldap->add($this->attrs);
248     }
249     show_ldap_error($ldap->get_error()); 
250  
251     /* Prepare FAIscriptEntry to write it to ldap
252      * First sort array.
253      *  Because we must delete old entries first.
254      * After deletion, we perform add and modify 
255      */
256     $Objects = array();
257     foreach($this->SubObjects as $name => $obj){
258       if($obj['status'] == "delete"){
259         $Objects[$name] = $obj; 
260       }
261     }
262     foreach($this->SubObjects as $name => $obj){
263       if($obj['status'] != "delete"){
264         $Objects[$name] = $obj; 
265       }
266     }
268     foreach($Objects as $name => $obj){
270       foreach($this->sub64coded as $codeIt){
271         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
272       }
274       $tmp = array();
275       foreach($this->subAttributes as $attrs){
276         if(empty($obj[$attrs])){
277           $obj[$attrs] = array();
278         }
279         if(!is_array($obj[$attrs])){
280           $tmp[$attrs] = stripslashes($obj[$attrs]);
281         }else{
282           $tmp[$attrs] = $obj[$attrs];
283         }
284       }    
286       $tmp['objectClass'] = $this->subClasses;
288       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
290       if($obj['status']=="new"){
291         $ldap->cat($sub_dn);
292         if($ldap->count()){
293           $obj['status']="modify";
294         }
295       }
296  
297       if($obj['status'] == "delete"){
298         $ldap->cd($sub_dn);
299         $ldap->rmdir_recursive($sub_dn);
300         $this->handle_post_events("remove");
301       }elseif($obj['status'] == "edited"){
302         $ldap->cd($sub_dn);
303         $ldap->modify($tmp);
304         $this->handle_post_events("modify");
305       }elseif($obj['status']=="new"){
306         if($tmp['description']==array()){
307           unset($tmp['description']);
308         }
309         $ldap->cd($this->config->current['BASE']);
310         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_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   }
318   
319   function readBinary($attr,$dn){
320     $Data  ="";
321     $ds= ldap_connect($this->config->current['SERVER']);
322     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
323     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
324       ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
325       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
326     }
328     if(isset($this->config->current['TLS']) &&  $this->config->current['TLS'] == "true"){
329       ldap_start_tls($ds);
330     }
332     $r  = ldap_bind($ds);
333     $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
335     if ($sr) {
336       $ei=ldap_first_entry($ds, $sr);
337       if ($ei) {
338         if ($info = ldap_get_values_len($ds, $ei, $attr)){
339           $Data= $info[0];
340         }
341       }
342     }
344     /* close conncetion */
345     ldap_unbind($ds);
346     return($Data);
347   }
352 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
353 ?>