Code

Reverted last commit
[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     $divlist->setHeight(400);
251     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
252       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
253       $img_remo = ""; 
254     }else{
255       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
256       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
257     }
259     foreach($this->getList(true) as $key => $name){
261       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
262         $down = "";
263       }else{
264         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."' target='_blank'>
265           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
266           </a>";
267       }
269       $divlist->AddEntry(array( array("string"=>$name['name']),
270             array("string"=>$down , "attach" => "style='width:20px;'"),
271             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
272               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
273     }
274     $smarty->assign("Entry_divlist",$divlist->DrawList());
275     /* Divlist creation complete
276      */
278     $smarty->assign("SubObjects",$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     /* Call common method to give check the hook */
355     $message= plugin::check();
357     return ($message);
358   }
361   /* Save to LDAP */
362   function save()
363   {
364     plugin::save();
366     $ldap = $this->config->get_ldap_link();
368     $ldap->cat($this->dn);
369     if($ldap->count()!=0){     
370       /* Write FAIscript to ldap*/ 
371       $ldap->cd($this->dn);
372       $this->cleanup();
373       $ldap->modify ($this->attrs); 
375     }else{
376       /* Write FAIscript to ldap*/ 
377       $ldap->cd($this->config->current['BASE']);
378       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
379       $ldap->cd($this->dn);
380       $ldap->add($this->attrs);
381     }
382     show_ldap_error($ldap->get_error()); 
384     /* Do object tagging */
385     $this->handle_object_tagging();
386     show_ldap_error($ldap->get_error()); 
388     $ldap->cd($this->dn);
390     /* Prepare FAIscriptEntry to write it to ldap
391      * First sort array.
392      *  Because we must delete old entries first.
393      * After deletion, we perform add and modify 
394      */
395     $Objects = array();
396     foreach($this->SubObjects as $name => $obj){
397       if($obj['status'] == "delete"){
398         $Objects[$name] = $obj; 
399       }
400     }
401     foreach($this->SubObjects as $name => $obj){
402       if($obj['status'] != "delete"){
403         $Objects[$name] = $obj; 
404       }
405     }
407     foreach($Objects as $name => $obj){
409       foreach($this->sub64coded as $codeIt){
410         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
411       }
413       $tmp = array();
414       foreach($this->subAttributes as $attrs){
415         if(empty($obj[$attrs])){
416           $obj[$attrs] = array();
417         }
418         if(!is_array($obj[$attrs])){
419           $tmp[$attrs] = stripslashes($obj[$attrs]);
420         }else{
421           $tmp[$attrs] = $obj[$attrs];
422         }
423       }    
425       $tmp['objectClass'] = $this->subClasses;
427       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
429       if($obj['status']=="new"){
430         $ldap->cat($sub_dn);
431         if($ldap->count()){
432           $obj['status']="modify";
433         }
434       }
436       if($obj['status'] == "delete"){
437         $ldap->cd($sub_dn);
438         $ldap->rmdir_recursive($sub_dn);
439         $this->handle_post_events("remove");
440       }elseif($obj['status'] == "edited"){
441         $ldap->cd($sub_dn);
442         $this->cleanup();
443         $ldap->modify ($tmp); 
445         $this->handle_post_events("modify");
446       }elseif($obj['status']=="new"){
447         if($tmp['description']==array()){
448           unset($tmp['description']);
449         }
450         $ldap->cd($this->config->current['BASE']);
451         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
452         $ldap->cd($sub_dn);
453         $ldap->add($tmp); 
454         $this->handle_post_events("add");
455       }
456       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
457       show_ldap_error($ldap->get_error()); 
458     }
459   }
462 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
463 ?>