Code

Switched html_options from values+output to options
[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( $ldap->get_attribute($object['dn'], "FAIscript",$r_array=0));
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());
279     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
280      * If we post the escaped strings they will be escaped again
281      */
282     foreach($this->attributes as $attrs){
283       if(get_magic_quotes_gpc()){
284         $smarty->assign($attrs,stripslashes($this->$attrs));
285       }else{
286         $smarty->assign($attrs,($this->$attrs));
287       }
288     }
290     foreach($this->attributes as $attr){
291       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
292     }
294     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
295     return($display);
296   }
298   /* Generate listbox friendly SubObject list
299    */
300   function getList($use_dns=false){
301     $a_return=array();
302     foreach($this->SubObjects as $obj){
303       if($obj['status'] != "delete"){
304         if($use_dns){
305           if((isset($obj['description']))&&(!empty($obj['description']))){
306             $a_return[$obj['cn']]['name']= $obj['cn']." [".$obj['description']."]";
307           }else{
308             $a_return[$obj['cn']]['name']= $obj['cn'];
309           }
310           $a_return[$obj['cn']]['dn']= $obj['dn'];
311         }else{
312           if((isset($obj['description']))&&(!empty($obj['description']))){
313             $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
314           }else{
315             $a_return[$obj['cn']]= $obj['cn'];
316           }
317         }
318       }
319     }
320     return($a_return);
321   }
324   /* Delete me, and all my subtrees
325    */
326   function remove_from_parent()
327   {
328     $ldap = $this->config->get_ldap_link();
329     $ldap->cd ($this->dn);
330     $ldap->rmdir_recursive($this->dn);
331     $this->handle_post_events("remove");    
332   }
335   /* Save data to object 
336    */
337   function save_object()
338   {
339     if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
340       plugin::save_object();
341       foreach($this->attributes as $attrs){
342         if(isset($_POST[$attrs])){
343           $this->$attrs = $_POST[$attrs];
344         }
345       }
346     }
347   }
350   /* Check supplied data */
351   function check()
352   {
353     /* Call common method to give check the hook */
354     $message= plugin::check();
356     return ($message);
357   }
360   /* Save to LDAP */
361   function save()
362   {
363     plugin::save();
365     $ldap = $this->config->get_ldap_link();
367     $ldap->cat($this->dn);
368     if($ldap->count()!=0){     
369       /* Write FAIscript to ldap*/ 
370       $ldap->cd($this->dn);
371       $this->cleanup();
372       $ldap->modify ($this->attrs); 
374     }else{
375       /* Write FAIscript to ldap*/ 
376       $ldap->cd($this->config->current['BASE']);
377       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
378       $ldap->cd($this->dn);
379       $ldap->add($this->attrs);
380     }
381     show_ldap_error($ldap->get_error()); 
383     /* Prepare FAIscriptEntry to write it to ldap
384      * First sort array.
385      *  Because we must delete old entries first.
386      * After deletion, we perform add and modify 
387      */
388     $Objects = array();
389     foreach($this->SubObjects as $name => $obj){
390       if($obj['status'] == "delete"){
391         $Objects[$name] = $obj; 
392       }
393     }
394     foreach($this->SubObjects as $name => $obj){
395       if($obj['status'] != "delete"){
396         $Objects[$name] = $obj; 
397       }
398     }
400     foreach($Objects as $name => $obj){
402       foreach($this->sub64coded as $codeIt){
403         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
404       }
406       $tmp = array();
407       foreach($this->subAttributes as $attrs){
408         if(empty($obj[$attrs])){
409           $obj[$attrs] = array();
410         }
411         if(!is_array($obj[$attrs])){
412           $tmp[$attrs] = stripslashes($obj[$attrs]);
413         }else{
414           $tmp[$attrs] = $obj[$attrs];
415         }
416       }    
418       $tmp['objectClass'] = $this->subClasses;
420       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
422       if($obj['status']=="new"){
423         $ldap->cat($sub_dn);
424         if($ldap->count()){
425           $obj['status']="modify";
426         }
427       }
429       if($obj['status'] == "delete"){
430         $ldap->cd($sub_dn);
431         $ldap->rmdir_recursive($sub_dn);
432         $this->handle_post_events("remove");
433       }elseif($obj['status'] == "edited"){
434         $ldap->cd($sub_dn);
435         $this->cleanup();
436         $ldap->modify ($tmp); 
438         $this->handle_post_events("modify");
439       }elseif($obj['status']=="new"){
440         if($tmp['description']==array()){
441           unset($tmp['description']);
442         }
443         $ldap->cd($this->config->current['BASE']);
444         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
445         $ldap->cd($sub_dn);
446         $ldap->add($tmp); 
447         $this->handle_post_events("add");
448       }
449       show_ldap_error($ldap->get_error()); 
450     }
451   }
454 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
455 ?>