Code

Added external hook
[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     /* New Listhandling
104      */
105     $once = true;
106     foreach($_POST as $name => $value){
107       if(preg_match("/^editscript_/",$name)&&($once)){
108         $once = false;
109         $entry = preg_replace("/^editscript_/","",$name);
110         $entry = base64_decode(preg_replace("/_.*/","",$entry));
111         $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$entry]);
112         $_SESSION['objectinfo'] = $this->SubObjects[$entry]['dn'];
113         $this->dialog->parent = &$this;
114         $this->is_dialog=true;
115       }
116       if(preg_match("/^deletescript_/",$name)&&($once)){
117         $once = false;
118         $entry = preg_replace("/^deletescript_/","",$name);
119         $entry = base64_decode(preg_replace("/_.*/","",$entry));
120         if($this->SubObjects[$entry]['status'] == "edited"){
121           $this->SubObjects[$entry]['status']= "delete";
122         }else{
123           unset($this->SubObjects[$entry]);
124         }
125       }
126     }
127     ///// Ende new list handling
129     /* Add new sub object */
130     if(isset($_POST['AddSubObject'])){
131       $this->dialog= new $this->subClassName($this->config,"new");
132       $this->dialog->parent = &$this;
133       $this->is_dialog=true;
134     }
135   
136     if($this->dn != "new"){
137       $_SESSION['objectinfo']= $this->dn;
138     }
139     /* Edit selected Sub Object */
140     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
141       $hook = $_POST['SubObject'][0];
142     
143       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$hook]);
144       $this->dialog->parent = &$this;
145       $_SESSION['objectinfo'] = $this->SubObjects[$hook]['dn'];
146       $this->is_dialog=true;
147     }
148     
149     /* Remove Sub object */
150     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
151       foreach($_POST['SubObject'] as $hook){
152         if($this->SubObjects[$hook]['status'] == "edited"){
153           $this->SubObjects[$hook]['status']= "delete";
154         }else{
155           unset($this->SubObjects[$hook]);
156         }
157       }
158     }
160     /* Save Dialog */
161     if(isset($_POST['SaveSubObject'])){
163       /* Perform post check*/
164       $this->dialog->save_object();
166       /* Get messages */
167       $msgs = $this->dialog->check();
169       /* print errors */
170       if(count($msgs)>0){
171         foreach($msgs as $msg){
172           print_red($msg);
173         }
174       }else{
176         /* Get return object */
177         $obj = $this->dialog->save();
178         if(isset($obj['remove'])){
180           /* Depending on status, set new status */
181           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
182             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
183           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
184             unset($this->SubObjects[$obj['remove']['from']]);
185           }
186           $obj['status'] = "new";
187           $this->SubObjects[$obj['remove']['to']] = $obj;
188           unset($this->SubObjects[$obj['remove']['to']]['remove']);
189         }else{
190           $this->SubObjects[$obj['cn']]=$obj;
191         }
192         $this->is_dialog=false;
193         unset($this->dialog);
194         $this->dialog=NULL;
195       }
196     }
198     /* Sort entries */
199     $tmp = $keys = array();
200     foreach($this->SubObjects as $key => $entry){
201       $keys[$key]=$key;
202     }
203     natcasesort($keys);
204     foreach($keys as $key){
205       $tmp[$key]=$this->SubObjects[$key];
206     }
207     $this->SubObjects = $tmp;
209     /* Cancel Dialog */
210     if(isset($_POST['CancelSubObject'])){
211       $this->is_dialog=false; 
212       unset($this->dialog);
213       $this->dialog=NULL;
214     }
216     /* Print dialog if $this->dialog is set */
217     if($this->dialog){
218       $this->dialog->save_object();
219       $display = $this->dialog->execute();
220       return($display);
221     }
224     
225     /* Divlist            added 28.02.2006
226        Containing FAIscripts
227      */
228     $divlist = new divSelectBox("FAIhooks");
229     $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
230     $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
232     foreach($this->getList(true) as $key => $name){
234       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
235         $down = "";
236       }else{
237         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' target='_blank'>
238                       <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
239                  </a>";
240       }
242       $divlist->AddEntry(array( array("string"=>$name['name']),
243                                 array("string"=>$down , "attach" => "style='width:20px;'"),
244                                 array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
245                                       "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
246     }
247     $smarty->assign("Entry_divlist",$divlist->DrawList());
248     /* Divlist creation complete
249      */
254     $smarty->assign("SubObjects",$this->getList());
255     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
257      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
258      * If we post the escaped strings they will be escaped again
259      */
260     foreach($this->attributes as $attrs){
261       if(get_magic_quotes_gpc()){
262         $smarty->assign($attrs,stripslashes($this->$attrs));
263       }else{
264         $smarty->assign($attrs,($this->$attrs));
265       }
266     }
268     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
269     return($display);
270   }
272   /* Generate listbox friendly SubObject list
273   */
274   function getList($use_dns=false){
275     $a_return=array();
276     foreach($this->SubObjects as $obj){
277       if($obj['status'] != "delete"){
278         if($use_dns){
279           if((isset($obj['description']))&&(!empty($obj['description']))){
280             $a_return[$obj['cn']]['name']= $obj['cn']." [".$obj['description']."]";
281           }else{
282             $a_return[$obj['cn']]['name']= $obj['cn'];
283           }
284           $a_return[$obj['cn']]['dn']= $obj['dn'];
285         }else{
286           if((isset($obj['description']))&&(!empty($obj['description']))){
287             $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
288           }else{
289             $a_return[$obj['cn']]= $obj['cn'];
290           }
291         }
292       }
293     }
294     return($a_return);
295   }
298   /* Delete me, and all my subtrees
299    */
300   function remove_from_parent()
301   {
302     $ldap = $this->config->get_ldap_link();
303     $ldap->cd ($this->dn);
304     $ldap->rmdir_recursive($this->dn);
305     $this->handle_post_events("remove");    
306   }
309   /* Save data to object 
310    */
311   function save_object()
312   {
313     if(isset($_POST['FAIhook_posted'])){
314       plugin::save_object();
315       foreach($this->attributes as $attrs){
316         if(isset($_POST[$attrs])){
317           $this->$attrs = $_POST[$attrs];
318         }
319       }
320     }
321   }
324   /* Check supplied data */
325   function check()
326   {
327     $message= array();
328     return ($message);
329   }
332   /* Save to LDAP */
333   function save()
334   {
335     plugin::save();
336  
337     $ldap = $this->config->get_ldap_link();
339     $ldap->cat($this->dn);
340     if($ldap->count()!=0){     
341       /* Write FAIscript to ldap*/ 
342       $ldap->cd($this->dn);
343       $this->cleanup();
344 $ldap->modify ($this->attrs); 
346     }else{
347       /* Write FAIscript to ldap*/ 
348       $ldap->cd($this->config->current['BASE']);
349       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
350       $ldap->cd($this->dn);
351       $ldap->add($this->attrs);
352     }
353     show_ldap_error($ldap->get_error()); 
354  
355     /* Prepare FAIscriptEntry to write it to ldap
356      * First sort array.
357      *  Because we must delete old entries first.
358      * After deletion, we perform add and modify 
359      */
360     $Objects = array();
361     foreach($this->SubObjects as $name => $obj){
362       if($obj['status'] == "delete"){
363         $Objects[$name] = $obj; 
364       }
365     }
366     foreach($this->SubObjects as $name => $obj){
367       if($obj['status'] != "delete"){
368         $Objects[$name] = $obj; 
369       }
370     }
372     foreach($Objects as $name => $obj){
374       foreach($this->sub64coded as $codeIt){
375         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
376       }
378       $tmp = array();
379       foreach($this->subAttributes as $attrs){
380         if(empty($obj[$attrs])){
381           $obj[$attrs] = array();
382         }
383         if(!is_array($obj[$attrs])){
384           $tmp[$attrs] = stripslashes($obj[$attrs]);
385         }else{
386           $tmp[$attrs] = $obj[$attrs];
387         }
388       }    
390       $tmp['objectClass'] = $this->subClasses;
392       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
394       if($obj['status']=="new"){
395         $ldap->cat($sub_dn);
396         if($ldap->count()){
397           $obj['status']="modify";
398         }
399       }
400  
401       if($obj['status'] == "delete"){
402         $ldap->cd($sub_dn);
403         $ldap->rmdir_recursive($sub_dn);
404         $this->handle_post_events("remove");
405       }elseif($obj['status'] == "edited"){
406         $ldap->cd($sub_dn);
407         $this->cleanup();
408 $ldap->modify ($tmp); 
410         $this->handle_post_events("modify");
411       }elseif($obj['status']=="new"){
412         if($tmp['description']==array()){
413           unset($tmp['description']);
414         }
415         $ldap->cd($this->config->current['BASE']);
416         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
417         $ldap->cd($sub_dn);
418         $ldap->add($tmp); 
419         $this->handle_post_events("add");
420       }
421       show_ldap_error($ldap->get_error()); 
422     }
423   }
424   
425   function readBinary($attr,$dn){
426     $Data  ="";
427     $ds= ldap_connect($this->config->current['SERVER']);
428     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
429     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
430       ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
431       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
432     }
434     if(isset($this->config->current['TLS']) &&  $this->config->current['TLS'] == "true"){
435       ldap_start_tls($ds);
436     }
438     $r  = ldap_bind($ds);
439     $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
441     if ($sr) {
442       $ei=ldap_first_entry($ds, $sr);
443       if ($ei) {
444         if ($info = ldap_get_values_len($ds, $ei, $attr)){
445           $Data= $info[0];
446         }
447       }
448     }
450     /* close conncetion */
451     ldap_unbind($ds);
452     return($Data);
453   }
458 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
459 ?>