Code

Templates were broken when downloaded
[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"); 
28   var $sub_Load_Later   = array("FAIscript"); 
29   var $sub64coded       = array();
30   var $subBinary        = array("FAIscript");
32   /* Specific attributes */
33   var $cn               = "";       // The class name for this object
34   var $description      = "";       // The description for this set of partitions
35   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
36   var $dialog           = NULL;     // a dialog, e.g. new disk dialog
37   var $SubObjects       = array();  // All leafobjects of this object
39   var $FAIstate         = "";  
41   function faiHook ($config, $dn= NULL)
42   {
43     /* Load Attributes */
44     plugin::plugin ($config, $dn);
46     $this->acl ="#all#";
48     /* If "dn==new" we try to create a new entry
49      * Else we must read all objects from ldap which belong to this entry.
50      */
51     if($dn != "new"){
52       $this->dn =$dn;
53     
54       /* Set acls 
55        */
56       $ui   = get_userinfo();
57       $acl  = get_permissions ($this->dn, $ui->subtreeACL);
58       $acli = get_module_permission($acl, "FAIclass", $this->dn);
59       $this->acl=$acli;
61       /* Get FAIstate
62        */
63       if(isset($this->attrs['FAIstate'][0])){
64         $this->FAIstate = $this->attrs['FAIstate'][0];
65       }
67       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
68        */
69       $ldap     = $this->config->get_ldap_link();
70       $ldap->cd ($this->dn);
71       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",array("dn"));
73       while($object = $ldap->fetch()){
75         /* Set status for save management */
76         $objects = array();
77         $objects['status']      = "FreshLoaded";
78         $objects['dn']          = $object['dn'];
79         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
80         $this->SubObjects[$objects['cn']] = $objects;
81       }
82     }
83   }
86   /* Reload some attributes */
87   function get_object_attributes($object,$attributes)
88   {
89     $ldap = $this->config->get_ldap_link();
90     $ldap->cd($this->config->current['BASE']);
91     $ldap->cat($object['dn'],$attributes);
92     $tmp  = $ldap->fetch();
93   
94     foreach($attributes as $attrs){
95       if(isset($tmp[$attrs][0])){
96         $var = $tmp[$attrs][0];
98         /* Check if we must decode some attributes */
99         if(in_array_ics($attrs,$this->sub64coded)){
100           $var = base64_decode($var);
101         }
103         /*  check if this is a binary entry */
104         if(in_array_ics($attrs,$this->subBinary)){
105           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
106         }
108         /* Fix slashes */
109         $var = addslashes($var);
110         $object[$attrs] = $var;
111       }
112     }
113     return($object);
114   }
117   function getUsedFAItask($cn)
118   {
119     $ret = array();
120     foreach($this->SubObjects as $name => $class){
121       if($class['cn'] == $cn){
122         continue;
123       } 
124       if($class['status'] != "delete"){
125         $ret[$class['FAItask']] = $class['FAItask'];
126       }
127     }
128     return($ret);
129   }
132   function execute()
133   {
134     /* Call parent execute */
135     plugin::execute();
137     /* Fill templating stuff */
138     $smarty= get_smarty();
139     $display= "";
141     /* New Listhandling
142      */
143     $once = true;
144     foreach($_POST as $name => $value){
145       if(preg_match("/^editscript_/",$name)&&($once)){
146         $once = false;
147         $entry = preg_replace("/^editscript_/","",$name);
148         $entry = base64_decode(preg_replace("/_.*/","",$entry));
150         $obj  = $this->SubObjects[$entry];
151         if($obj['status'] == "FreshLoaded"){
152           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
153         }
155         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
156         $this->dialog->acl = $this->acl;
157         $_SESSION['objectinfo'] = $obj['dn'];
158         $this->dialog->parent = &$this;
159         $this->is_dialog=true;
160       }
161       if(preg_match("/^deletescript_/",$name)&&($once)){
162         $once = false;
163         $entry = preg_replace("/^deletescript_/","",$name);
164         $entry = base64_decode(preg_replace("/_.*/","",$entry));
165         if($this->SubObjects[$entry]['status'] == "edited"){
166           $this->SubObjects[$entry]['status']= "delete";
167         }else{
168           unset($this->SubObjects[$entry]);
169         }
170       }
171     }
172     ///// Ende new list handling
174     /* Add new sub object */
175     if(isset($_POST['AddSubObject'])){
176       $this->dialog= new $this->subClassName($this->config,"new");
177       $this->dialog->acl = $this->acl;
178       $this->dialog->parent = &$this;
179       $this->is_dialog=true;
180     }
182     if($this->dn != "new"){
183       $_SESSION['objectinfo']= $this->dn;
184     }
186     /* Save Dialog */
187     if(isset($_POST['SaveSubObject'])){
189       /* Perform post check*/
190       $this->dialog->save_object();
192       /* Get messages */
193       $msgs = $this->dialog->check();
195       /* print errors */
196       if(count($msgs)>0){
197         foreach($msgs as $msg){
198           print_red($msg);
199         }
200       }else{
202         /* Get return object */
203         $obj = $this->dialog->save();
204         if(isset($obj['remove'])){
205           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
207           /* Depending on status, set new status */
208           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
209             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
210           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
211             unset($this->SubObjects[$obj['remove']['from']]);
212           }
213           $obj['status'] = "new";
214           $this->SubObjects[$obj['remove']['to']] = $obj;
215           unset($this->SubObjects[$obj['remove']['to']]['remove']);
216         }else{
217           if($obj['status'] == "FreshLoaded"){
218             $obj['status'] = "edited";
219           }
220           $this->SubObjects[$obj['cn']]=$obj;
221         }
222     
223         $this->is_dialog=false;
224         unset($this->dialog);
225         $this->dialog=NULL;
226       }
227     }
229     /* Sort entries */
230     $tmp = $keys = array();
231     foreach($this->SubObjects as $key => $entry){
232       $keys[$key]=$key;
233     }
234     natcasesort($keys);
235     foreach($keys as $key){
236       $tmp[$key]=$this->SubObjects[$key];
237     }
238     $this->SubObjects = $tmp;
240     /* Cancel Dialog */
241     if(isset($_POST['CancelSubObject'])){
242       $this->is_dialog=false; 
243       unset($this->dialog);
244       $this->dialog=NULL;
245     }
247     /* Print dialog if $this->dialog is set */
248     if($this->dialog){
249       $this->dialog->save_object();
250       $display = $this->dialog->execute();
251       return($display);
252     }
256     /* Divlist            added 28.02.2006
257        Containing FAIscripts
258      */
260     $divlist = new divSelectBox("FAIhooks");
261     $divlist->setHeight(400);
262     if((chkacl($this->acl,"cn")!="") || ($this->FAIstate == "freeze")){
263       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
264       $img_remo = ""; 
265     }else{
266       $img_edit = "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
267       $img_remo = "<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
268     }
270     foreach($this->getList(true) as $key => $name){
272       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new")){
273         $down = "";
274       }else{
275         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
276           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
277           </a>";
278       }
280       $divlist->AddEntry(array( array("string"=>$name['name']),
281             array("string"=>$down , "attach" => "style='width:20px;'"),
282             array("string"=>str_replace("%s",base64_encode($key),$img_edit.$img_remo),
283               "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
284     }
285     $smarty->assign("Entry_divlist",$divlist->DrawList());
286     /* Divlist creation complete
287      */
289     $smarty->assign("SubObjects",$this->getList());
291     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
292      * If we post the escaped strings they will be escaped again
293      */
294     foreach($this->attributes as $attrs){
295       if(get_magic_quotes_gpc()){
296         $smarty->assign($attrs,stripslashes($this->$attrs));
297       }else{
298         $smarty->assign($attrs,($this->$attrs));
299       }
300     }
302     foreach($this->attributes as $attr){
303       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
304     }
306     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
307     return($display);
308   }
310   /* Generate listbox friendly SubObject list
311    */
312   function getList($use_dns=false){
313     $a_return=array();
314     foreach($this->SubObjects as $obj){
315       if($obj['status'] != "delete"){
316         if($use_dns){
317           if((isset($obj['description']))&&(!empty($obj['description']))){
318             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
319           }else{
320             $a_return[$obj['cn']]['name']= $obj['cn'];
321           }
322           $a_return[$obj['cn']]['dn']= $obj['dn'];
323         }else{
324           if((isset($obj['description']))&&(!empty($obj['description']))){
325             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
326           }else{
327             $a_return[$obj['cn']]= $obj['cn'];
328           }
329         }
330       }
331     }
332     return($a_return);
333   }
336   /* Delete me, and all my subtrees
337    */
338   function remove_from_parent()
339   {
340     $ldap = $this->config->get_ldap_link();
341     $ldap->cd ($this->dn);
342     $ldap->rmdir_recursive($this->dn);
343     show_ldap_error($ldap->get_error(), _("Removing FAI hook base failed")); 
344     $this->handle_post_events("remove");    
345   }
348   /* Save data to object 
349    */
350   function save_object()
351   {
352     if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
353       plugin::save_object();
354       foreach($this->attributes as $attrs){
355         if(isset($_POST[$attrs])){
356           $this->$attrs = $_POST[$attrs];
357         }
358       }
359     }
360   }
363   /* Check supplied data */
364   function check()
365   {
366     /* Call common method to give check the hook */
367     $message= plugin::check();
369     return ($message);
370   }
373   /* Save to LDAP */
374   function save()
375   {
376     plugin::save();
378     $ldap = $this->config->get_ldap_link();
380     $ldap->cat($this->dn,array("objectClass"));
381     if($ldap->count()!=0){     
382       /* Write FAIscript to ldap*/ 
383       $ldap->cd($this->dn);
384       $this->cleanup();
385       $ldap->modify ($this->attrs); 
387     }else{
388       /* Write FAIscript to ldap*/ 
389       $ldap->cd($this->config->current['BASE']);
390       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
391       $ldap->cd($this->dn);
392       $ldap->add($this->attrs);
393     }
394     show_ldap_error($ldap->get_error(), _("Saving FAI hook base failed")); 
396     /* Do object tagging */
397     $this->handle_object_tagging();
399     $ldap->cd($this->dn);
401     /* Prepare FAIscriptEntry to write it to ldap
402      * First sort array.
403      *  Because we must delete old entries first.
404      * After deletion, we perform add and modify 
405      */
406     $Objects = array();
409     /* We do not need to save untouched objects */
410     foreach($this->SubObjects as $name => $obj){
411       if($obj['status'] == "FreshLoaded"){
412         unset($this->SubObjects[$name]);
413       } 
414     }
416     /* Add objects that must be removed first.*/
417     foreach($this->SubObjects as $name => $obj){
418       if($obj['status'] == "delete"){
419         $Objects[$name] = $obj; 
420       }
421     }
423     /* Add objects to add/modify */
424     foreach($this->SubObjects as $name => $obj){
425       if($obj['status'] != "delete"){
426         $Objects[$name] = $obj; 
427       }
428     }
430     /* Walk through list of objects */
431     foreach($Objects as $name => $obj){
433       /* Encode attribtues if required */
434       foreach($this->sub64coded as $codeIt){
435         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
436       }
438       /* Create ldap entry */
439       $tmp = array();
440       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
441       foreach($attributes as $attrs){
442   
443         if(empty($obj[$attrs])){
444           $obj[$attrs] = array();
445         }
446         if(!is_array($obj[$attrs])){
447           $tmp[$attrs] = stripslashes($obj[$attrs]);
448         }else{
449           $tmp[$attrs] = $obj[$attrs];
450         }
451       }    
453       $tmp['objectClass'] = $this->subClasses;
455       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
457       if($obj['status']=="new"){
458         $ldap->cat($sub_dn,array("objectClass"));
459         if($ldap->count()){
460           $obj['status']="edited";
461         }
462       }
464       /* Check if gosaAdministrativeUnitTag is required as object class */
465       if($obj['status'] == "edited"){
466         $ldap->cat($sub_dn,array("objectClass"));
467         $attrs = $ldap->fetch();
468         if(isset($attrs['objectClass'])){
469           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
470             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
471           }
472         }
473       }
475       if($obj['status'] == "delete"){
476         $ldap->cd($sub_dn);
477         $ldap->rmdir_recursive($sub_dn);
478         $this->handle_post_events("remove");
479         show_ldap_error($ldap->get_error(), _("Removing FAI hook failed")); 
480       }elseif($obj['status'] == "edited"){
481         $ldap->cd($sub_dn);
482         $this->cleanup();
483         $ldap->modify ($tmp); 
484         $this->handle_post_events("modify");
485         show_ldap_error($ldap->get_error(), _("Saving FAI hook failed")); 
486       }elseif($obj['status']=="new"){
487         if($tmp['description']==array()){
488           unset($tmp['description']);
489         }
490         $ldap->cd($this->config->current['BASE']);
491         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $sub_dn));
492         $ldap->cd($sub_dn);
493         $ldap->add($tmp); 
494         $this->handle_post_events("add");
495         show_ldap_error($ldap->get_error(), _("Saving FAI hook failed")); 
496       }
497       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
498     }
499   }
502 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
503 ?>