Code

Added copy & paste for ogroups
[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   var $FAIstate         = "";  
39   function faiHook ($config, $dn= NULL)
40   {
41     /* Load Attributes */
42     plugin::plugin ($config, $dn);
44     $this->acl ="#all#";
46     /* If "dn==new" we try to create a new entry
47      * Else we must read all objects from ldap which belong to this entry.
48      */
49     if($dn != "new"){
50       $this->dn =$dn;
51     
52       /* Set acls 
53        */
54       $ui   = get_userinfo();
55       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
56       $acli = get_module_permission($acl, "FAIclass", $this->dn);
57       $this->acl=$acli;
59       /* Get FAIstate
60        */
61       if(isset($this->attrs['FAIstate'][0])){
62         $this->FAIstate = $this->attrs['FAIstate'][0];
63       }
65       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
66        */
67       $ldap     = $this->config->get_ldap_link();
68       $ldap->cd ($this->dn);
69       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$this->subAttributes);
71       while($object = $ldap->fetch()){
72         /* Set status for save management */
74         foreach($this->subAttributes as $attrs){
75           if(!isset($object[$attrs][0])){
76             $this->SubObjects[$object['cn'][0]][$attrs]="";
77           }else{
78             $this->SubObjects[$object['cn'][0]][$attrs]=$object[$attrs][0];
79           }
80         }
82         $this->SubObjects[$object['cn'][0]]['status']      = "edited";
83         $this->SubObjects[$object['cn'][0]]['dn']          = $object['dn'];
85         foreach($this->sub64coded as $codeIt){
86           $this->SubObjects[$object['cn'][0]][$codeIt]=base64_decode($this->SubObjects[$object['cn'][0]][$codeIt]);
87         }
89         foreach($this->subAttributes as $attrs){
90           $this->SubObjects[$object['cn'][0]][$attrs]=addslashes($this->SubObjects[$object['cn'][0]][$attrs]);
91         }
92         $this->SubObjects[$object['cn'][0]]['FAIscript']   = addslashes($this->readBinary("FAIscript",$object['dn']));
93       }
94     }
95   }
97   function getUsedFAItask($cn)
98   {
99     $ret = array();
100     foreach($this->SubObjects as $name => $class){
101       if($class['cn'] == $cn){
102         continue;
103       } 
104       if($class['status'] != "delete"){
105         $ret[$class['FAItask']] = $class['FAItask'];
106       }
107     }
108     return($ret);
109   }
111   function execute()
112   {
113     /* Call parent execute */
114     plugin::execute();
116     /* Fill templating stuff */
117     $smarty= get_smarty();
118     $display= "";
120     /* New Listhandling
121      */
122     $once = true;
123     foreach($_POST as $name => $value){
124       if(preg_match("/^editscript_/",$name)&&($once)){
125         $once = false;
126         $entry = preg_replace("/^editscript_/","",$name);
127         $entry = base64_decode(preg_replace("/_.*/","",$entry));
128         $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$entry]);
129         $this->dialog->acl = $this->acl;
130         $_SESSION['objectinfo'] = $this->SubObjects[$entry]['dn'];
131         $this->dialog->parent = &$this;
132         $this->is_dialog=true;
133       }
134       if(preg_match("/^deletescript_/",$name)&&($once)){
135         $once = false;
136         $entry = preg_replace("/^deletescript_/","",$name);
137         $entry = base64_decode(preg_replace("/_.*/","",$entry));
138         if($this->SubObjects[$entry]['status'] == "edited"){
139           $this->SubObjects[$entry]['status']= "delete";
140         }else{
141           unset($this->SubObjects[$entry]);
142         }
143       }
144     }
145     ///// Ende new list handling
147     /* Add new sub object */
148     if(isset($_POST['AddSubObject'])){
149       $this->dialog= new $this->subClassName($this->config,"new");
150       $this->dialog->acl = $this->acl;
151       $this->dialog->parent = &$this;
152       $this->is_dialog=true;
153     }
155     if($this->dn != "new"){
156       $_SESSION['objectinfo']= $this->dn;
157     }
158     /* Edit selected Sub Object */
159     if((isset($_POST['EditSubObject']))&&(isset($_POST['SubObject']))){
160       $hook = $_POST['SubObject'][0];
162       $this->dialog= new $this->subClassName($this->config,$this->dn,$this->SubObjects[$hook]);
163       $this->dialog->acl = $this->acl;
164       $this->dialog->parent = &$this;
165       $_SESSION['objectinfo'] = $this->SubObjects[$hook]['dn'];
166       $this->is_dialog=true;
167     }
169     /* Remove Sub object */
170     if((isset($_POST['DelSubObject']))&&(isset($_POST['SubObject']))){
171       foreach($_POST['SubObject'] as $hook){
172         if($this->SubObjects[$hook]['status'] == "edited"){
173           $this->SubObjects[$hook]['status']= "delete";
174         }else{
175           unset($this->SubObjects[$hook]);
176         }
177       }
178     }
180     /* Save Dialog */
181     if(isset($_POST['SaveSubObject'])){
183       /* Perform post check*/
184       $this->dialog->save_object();
186       /* Get messages */
187       $msgs = $this->dialog->check();
189       /* print errors */
190       if(count($msgs)>0){
191         foreach($msgs as $msg){
192           print_red($msg);
193         }
194       }else{
196         /* Get return object */
197         $obj = $this->dialog->save();
198         if(isset($obj['remove'])){
200           /* Depending on status, set new status */
201           if($this->SubObjects[$obj['remove']['from']]['status']=="edited"){
202             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
203           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
204             unset($this->SubObjects[$obj['remove']['from']]);
205           }
206           $obj['status'] = "new";
207           $this->SubObjects[$obj['remove']['to']] = $obj;
208           unset($this->SubObjects[$obj['remove']['to']]['remove']);
209         }else{
210           $this->SubObjects[$obj['cn']]=$obj;
211         }
212         $this->is_dialog=false;
213         unset($this->dialog);
214         $this->dialog=NULL;
215       }
216     }
218     /* Sort entries */
219     $tmp = $keys = array();
220     foreach($this->SubObjects as $key => $entry){
221       $keys[$key]=$key;
222     }
223     natcasesort($keys);
224     foreach($keys as $key){
225       $tmp[$key]=$this->SubObjects[$key];
226     }
227     $this->SubObjects = $tmp;
229     /* Cancel Dialog */
230     if(isset($_POST['CancelSubObject'])){
231       $this->is_dialog=false; 
232       unset($this->dialog);
233       $this->dialog=NULL;
234     }
236     /* Print dialog if $this->dialog is set */
237     if($this->dialog){
238       $this->dialog->save_object();
239       $display = $this->dialog->execute();
240       return($display);
241     }
245     /* Divlist            added 28.02.2006
246        Containing FAIscripts
247      */
249     $divlist = new divSelectBox("FAIhooks");
250     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
251       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
252       $img_remo = ""; 
253     }else{
254       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
255       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
256     }
258     foreach($this->getList(true) as $key => $name){
260       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
261         $down = "";
262       }else{
263         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' target='_blank'>
264           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
265           </a>";
266       }
268       $divlist->AddEntry(array( array("string"=>$name['name']),
269             array("string"=>$down , "attach" => "style='width:20px;'"),
270             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
271               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
272     }
273     $smarty->assign("Entry_divlist",$divlist->DrawList());
274     /* Divlist creation complete
275      */
277     $smarty->assign("SubObjects",$this->getList());
278     $smarty->assign("SubObjectKeys",array_flip($this->getList()));
280     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
281      * If we post the escaped strings they will be escaped again
282      */
283     foreach($this->attributes as $attrs){
284       if(get_magic_quotes_gpc()){
285         $smarty->assign($attrs,stripslashes($this->$attrs));
286       }else{
287         $smarty->assign($attrs,($this->$attrs));
288       }
289     }
291     foreach($this->attributes as $attr){
292       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
293     }
295     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
296     return($display);
297   }
299   /* Generate listbox friendly SubObject list
300    */
301   function getList($use_dns=false){
302     $a_return=array();
303     foreach($this->SubObjects as $obj){
304       if($obj['status'] != "delete"){
305         if($use_dns){
306           if((isset($obj['description']))&&(!empty($obj['description']))){
307             $a_return[$obj['cn']]['name']= $obj['cn']." [".$obj['description']."]";
308           }else{
309             $a_return[$obj['cn']]['name']= $obj['cn'];
310           }
311           $a_return[$obj['cn']]['dn']= $obj['dn'];
312         }else{
313           if((isset($obj['description']))&&(!empty($obj['description']))){
314             $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
315           }else{
316             $a_return[$obj['cn']]= $obj['cn'];
317           }
318         }
319       }
320     }
321     return($a_return);
322   }
325   /* Delete me, and all my subtrees
326    */
327   function remove_from_parent()
328   {
329     $ldap = $this->config->get_ldap_link();
330     $ldap->cd ($this->dn);
331     $ldap->rmdir_recursive($this->dn);
332     $this->handle_post_events("remove");    
333   }
336   /* Save data to object 
337    */
338   function save_object()
339   {
340     if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
341       plugin::save_object();
342       foreach($this->attributes as $attrs){
343         if(isset($_POST[$attrs])){
344           $this->$attrs = $_POST[$attrs];
345         }
346       }
347     }
348   }
351   /* Check supplied data */
352   function check()
353   {
354     $message= array();
355     return ($message);
356   }
359   /* Save to LDAP */
360   function save()
361   {
362     plugin::save();
364     $ldap = $this->config->get_ldap_link();
366     $ldap->cat($this->dn);
367     if($ldap->count()!=0){     
368       /* Write FAIscript to ldap*/ 
369       $ldap->cd($this->dn);
370       $this->cleanup();
371       $ldap->modify ($this->attrs); 
373     }else{
374       /* Write FAIscript to ldap*/ 
375       $ldap->cd($this->config->current['BASE']);
376       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
377       $ldap->cd($this->dn);
378       $ldap->add($this->attrs);
379     }
380     show_ldap_error($ldap->get_error()); 
382     /* Prepare FAIscriptEntry to write it to ldap
383      * First sort array.
384      *  Because we must delete old entries first.
385      * After deletion, we perform add and modify 
386      */
387     $Objects = array();
388     foreach($this->SubObjects as $name => $obj){
389       if($obj['status'] == "delete"){
390         $Objects[$name] = $obj; 
391       }
392     }
393     foreach($this->SubObjects as $name => $obj){
394       if($obj['status'] != "delete"){
395         $Objects[$name] = $obj; 
396       }
397     }
399     foreach($Objects as $name => $obj){
401       foreach($this->sub64coded as $codeIt){
402         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
403       }
405       $tmp = array();
406       foreach($this->subAttributes as $attrs){
407         if(empty($obj[$attrs])){
408           $obj[$attrs] = array();
409         }
410         if(!is_array($obj[$attrs])){
411           $tmp[$attrs] = stripslashes($obj[$attrs]);
412         }else{
413           $tmp[$attrs] = $obj[$attrs];
414         }
415       }    
417       $tmp['objectClass'] = $this->subClasses;
419       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
421       if($obj['status']=="new"){
422         $ldap->cat($sub_dn);
423         if($ldap->count()){
424           $obj['status']="modify";
425         }
426       }
428       if($obj['status'] == "delete"){
429         $ldap->cd($sub_dn);
430         $ldap->rmdir_recursive($sub_dn);
431         $this->handle_post_events("remove");
432       }elseif($obj['status'] == "edited"){
433         $ldap->cd($sub_dn);
434         $this->cleanup();
435         $ldap->modify ($tmp); 
437         $this->handle_post_events("modify");
438       }elseif($obj['status']=="new"){
439         if($tmp['description']==array()){
440           unset($tmp['description']);
441         }
442         $ldap->cd($this->config->current['BASE']);
443         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
444         $ldap->cd($sub_dn);
445         $ldap->add($tmp); 
446         $this->handle_post_events("add");
447       }
448       show_ldap_error($ldap->get_error()); 
449     }
450   }
452   function readBinary($attr,$dn){
453     $Data  ="";
454     $ds= ldap_connect($this->config->current['SERVER']);
455     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
456     if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
457       ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
458       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
459     }
461     if(isset($this->config->current['TLS']) &&  $this->config->current['TLS'] == "true"){
462       ldap_start_tls($ds);
463     }
465     $r  = ldap_bind($ds);
466     $sr = @ldap_read($ds, $dn, $attr."=*", array($attr));
468     if ($sr) {
469       $ei=ldap_first_entry($ds, $sr);
470       if ($ei) {
471         if ($info = ldap_get_values_len($ds, $ei, $attr)){
472           $Data= $info[0];
473         }
474       }
475     }
477     /* close conncetion */
478     ldap_unbind($ds);
479     return($Data);
480   }
485 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
486 ?>