Code

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