Code

Added delete button in result window
[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         = "";  
40   var $ui;
42   function faiHook ($config, $dn= NULL)
43   {
44     /* Load Attributes */
45     plugin::plugin ($config, $dn);
47     /* If "dn==new" we try to create a new entry
48      * Else we must read all objects from ldap which belong to this entry.
49      */
50     if($dn != "new"){
51       $this->dn =$dn;
52     
53       /* Get FAIstate
54        */
55       if(isset($this->attrs['FAIstate'][0])){
56         $this->FAIstate = $this->attrs['FAIstate'][0];
57       }
59       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
60        */
61       $ldap     = $this->config->get_ldap_link();
62       $ldap->cd ($this->dn);
63       $attrs_to_search = $this->subAttributes;
64       $attrs_to_search[] = "FAIstate";
65       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))", array("dn"));
67       while($object = $ldap->fetch()){
68         /* Set status for save management */
69         $objects = array();
70         $objects['status']      = "FreshLoaded";
71         $objects['dn']          = $object['dn'];
72         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
73         $this->SubObjects[$objects['cn']] = $objects;
74       }
75     }
76     $this->ui = get_userinfo();
77   }
80   /* Reload some attributes */
81   function get_object_attributes($object,$attributes)
82   {
83     $ldap = $this->config->get_ldap_link();
84     $ldap->cd($this->config->current['BASE']);
85     $ldap->cat($object['dn'],$attributes);
86     $tmp  = $ldap->fetch();
88     foreach($attributes as $attrs){
89       if(isset($tmp[$attrs][0])){
90         $var = $tmp[$attrs][0];
92         /* Check if we must decode some attributes */
93         if(in_array_ics($attrs,$this->sub64coded)){
94           $var = base64_decode($var);
95         }
97         /*  check if this is a binary entry */
98         if(in_array_ics($attrs,$this->subBinary)){
99           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
100         }
102         /* Fix slashes */
103         $var = addslashes($var);
104         $object[$attrs] = $var;
105       }
106     }
107     return($object);
108   }
111   function acl_base_for_current_object($dn)
112   {
113     if($dn == "new"){
114       if($this->dn == "new"){
115         $dn= $_SESSION['CurrentMainBase'];
116       }else{
117         $dn = $this->dn;
118       }
119     }
120     return($dn);
121   }
124   function getUsedFAItask($cn)
125   {
126     $ret = array();
127     foreach($this->SubObjects as $name => $class){
128       if($class['cn'] == $cn){
129         continue;
130       } 
131       if($class['status'] != "delete"){
132         $ret[$class['FAItask']] = $class['FAItask'];
133       }
134     }
135     return($ret);
136   }
138   function execute()
139   {
140     /* Call parent execute */
141     plugin::execute();
143     /* Fill templating stuff */
144     $smarty= get_smarty();
145     $display= "";
147     /* New Listhandling
148      */
149     $once = true;
150     foreach($_POST as $name => $value){
151       if(preg_match("/^editscript_/",$name)&&($once)){
152         $once = false;
153         $entry = preg_replace("/^editscript_/","",$name);
154         $entry = base64_decode(preg_replace("/_.*/","",$entry));
156         $obj  = $this->SubObjects[$entry];
157         if($obj['status'] == "FreshLoaded"){
158           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
159         }
161         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
162         $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
163         $this->dialog->set_acl_category("fai");
165         $_SESSION['objectinfo'] = $obj['dn'];
166         $this->dialog->parent = &$this;
167         $this->is_dialog=true;
168       }
169       if(preg_match("/^deletescript_/",$name)&&($once)){
170         $entry = preg_replace("/^deletescript_/","",$name);
171         $entry = base64_decode(preg_replace("/_.*/","",$entry));
173         $dn = $this->acl_base_for_current_object($this->SubObjects[$entry]['dn']);
174         $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry")  ;
176         if(preg_match("/d/",$acl)){
177           $once = false;
179           if(($this->SubObjects[$entry]['status'] == "edited")||($this->SubObjects[$entry]['status'] == "FreshLoaded")){
180             $this->SubObjects[$entry]['status']= "delete";
181           }else{
182             unset($this->SubObjects[$entry]);
183           }
184         }
185       }
186     }
187     ///// Ende new list handling
189     /* Add new sub object */
190     if(isset($_POST['AddSubObject'])){
192       $c_dn = $this->acl_base_for_current_object($this->dn);
193       $this->dialog= new $this->subClassName($this->config,"new");
194       $this->dialog->set_acl_base($c_dn);
195       $this->dialog->set_acl_category("fai");
196       $this->dialog->parent = &$this;
197       $this->is_dialog=true;
198     }
201     /* Save Dialog */
202     if(isset($_POST['SaveSubObject'])){
204       /* Perform post check*/
205       $this->dialog->save_object();
207       /* Get messages */
208       $msgs = $this->dialog->check();
210       /* print errors */
211       if(count($msgs)>0){
212         foreach($msgs as $msg){
213           print_red($msg);
214         }
215       }else{
217         /* Get return object */
218         $obj = $this->dialog->save();
219         if(isset($obj['remove'])){
221           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
222   
223           /* Depending on status, set new status */
224           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
225             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
226           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
227             unset($this->SubObjects[$obj['remove']['from']]);
228           }
229           $obj['status'] = "new";
230           $this->SubObjects[$obj['remove']['to']] = $obj;
231           unset($this->SubObjects[$obj['remove']['to']]['remove']);
232         }else{
233           if($obj['status'] == "FreshLoaded"){
234             $obj['status'] = "edited";
235           }
236           $this->SubObjects[$obj['cn']]=$obj;
237         }
238         $this->is_dialog=false;
239         unset($this->dialog);
240         $this->dialog=NULL;
241       }
242     }
244     /* Sort entries */
245     $tmp = $keys = array();
246     foreach($this->SubObjects as $key => $entry){
247       $keys[$key]=$key;
248     }
249     natcasesort($keys);
250     foreach($keys as $key){
251       $tmp[$key]=$this->SubObjects[$key];
252     }
253     $this->SubObjects = $tmp;
255     /* Cancel Dialog */
256     if(isset($_POST['CancelSubObject'])){
257       $this->is_dialog=false; 
258       unset($this->dialog);
259       $this->dialog=NULL;
260     }
262     /* Print dialog if $this->dialog is set */
263     if($this->dialog){
264       $this->dialog->save_object();
265       $display = $this->dialog->execute();
266       return($display);
267     }
271     /* Divlist            added 28.02.2006
272        Containing FAIscripts
273      */
275     $divlist = new divSelectBox("FAIhooks");
276     $divlist->setHeight(400);
278     foreach($this->getList(true) as $key => $name){
280       $dn= $this->acl_base_for_current_object($name['dn']);
281       $acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry")  ;
282       $act = "";
284       /* Check if this object is freezed, in this case hide the delete icon */
285       if($this->FAIstate == "freeze"){
286         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
287       }else{
288         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
289         if(preg_match("/d/",$acl)){
290           $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
291         }
292       }
294       /* Check if we are allowed to use the export button for this object */
295       $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","FAIscript")  ;
296       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
297         $down = "";
298       }else{
299         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
300           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
301           </a>";
302       }
304       /* Check if we are allowed to view the object */
305       $s_acl = $this->ui->get_permissions($dn,"fai/faiScriptEntry","cn")  ;
306       if(preg_match("/r/",$s_acl)){
307         $divlist->AddEntry(array( array("string"=>$name['name']),
308               array("string"=>$down , "attach" => "style='width:20px;'"),
309               array("string"=>str_replace("%s",base64_encode($key),$act),
310                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
311       }
312     }
313     $smarty->assign("Entry_divlist",$divlist->DrawList());
314     /* Divlist creation complete
315      */
317     $smarty->assign("SubObjects",$this->getList());
319     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
320      * If we post the escaped strings they will be escaped again
321      */
322     foreach($this->attributes as $attrs){
323       if(get_magic_quotes_gpc()){
324         $smarty->assign($attrs,stripslashes($this->$attrs));
325       }else{
326         $smarty->assign($attrs,($this->$attrs));
327       }
328     }
330     $tmp = $this->plInfo();
331       
332     $c_dn = $this->acl_base_for_current_object($this->dn);
333     $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiScriptEntry")) && $this->FAIstate!="freeze");
334     foreach($tmp['plProvidedAcls'] as $name => $translation){
335       $smarty->assign($name."ACL",$this->getacl($name));
336     }
338     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
339     return($display);
340   }
342   /* Generate listbox friendly SubObject list
343    */
344   function getList($use_dns=false){
345     $a_return=array();
346     foreach($this->SubObjects as $obj){
347       if($obj['status'] != "delete"){
348         if($use_dns){
349           if((isset($obj['description']))&&(!empty($obj['description']))){
350             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
351           }else{
352             $a_return[$obj['cn']]['name']= $obj['cn'];
353           }
354           $a_return[$obj['cn']]['dn']= $obj['dn'];
355         }else{
356           if((isset($obj['description']))&&(!empty($obj['description']))){
357             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
358           }else{
359             $a_return[$obj['cn']]= $obj['cn'];
360           }
361         }
362       }
363     }
364     return($a_return);
365   }
368   /* Delete me, and all my subtrees
369    */
370   function remove_from_parent()
371   {
372     $ldap = $this->config->get_ldap_link();
373     $ldap->cd ($this->dn);
375 #    $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
376     $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
377     if($_SESSION['faifilter']['branch'] == "main"){
378       $use_dn = $this->dn;
379     }
381     prepare_to_save_FAI_object($use_dn,array(),true);
383     foreach($this->SubObjects as $name => $obj){
384 #      $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $obj['dn']);
385       $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $obj['dn']);
386       if($_SESSION['faifilter']['branch'] == "main"){
387         $use_dn = $obj['dn'];
388       }
389       prepare_to_save_FAI_object($use_dn,array(),true);
390     }
391     $this->handle_post_events("remove");    
392   }
395   /* Save data to object 
396    */
397   function save_object()
398   {
399     if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
400       plugin::save_object();
401       foreach($this->attributes as $attrs){
402         if(isset($_POST[$attrs])){
403           $this->$attrs = $_POST[$attrs];
404         }
405       }
406     }
407   }
410   /* Check supplied data */
411   function check()
412   {
413     /* Call common method to give check the hook */
414     $message= plugin::check();
416     return ($message);
417   }
420   /* Save to LDAP */
421   function save()
422   {
423     plugin::save();
425     $ldap = $this->config->get_ldap_link();
427     prepare_to_save_FAI_object($this->dn,$this->attrs);
428     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/hook with dn '%s' failed."),$this->dn));
430     /* Do object tagging */
431     $this->handle_object_tagging();
433     $ldap->cd($this->dn);
435     /* Prepare FAIscriptEntry to write it to ldap
436      * First sort array.
437      *  Because we must delete old entries first.
438      * After deletion, we perform add and modify 
439      */
440     $Objects = array();
442     /* We do not need to save untouched objects */
443     foreach($this->SubObjects as $name => $obj){
444       if($obj['status'] == "FreshLoaded"){
445         unset($this->SubObjects[$name]);
446       }
447     }
449     foreach($this->SubObjects as $name => $obj){
450       if($obj['status'] == "delete"){
451         $Objects[$name] = $obj; 
452       }
453     }
454     foreach($this->SubObjects as $name => $obj){
455       if($obj['status'] != "delete"){
456         $Objects[$name] = $obj; 
457       }
458     }
460     foreach($Objects as $name => $obj){
462       foreach($this->sub64coded as $codeIt){
463         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
464       }
466       $tmp = array();
467       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
468       foreach($attributes as $attrs){
469         if(empty($obj[$attrs])){
470           $obj[$attrs] = array();
471         }
472         if(!is_array($obj[$attrs])){
473           $tmp[$attrs] = stripslashes($obj[$attrs]);
474         }else{
475           $tmp[$attrs] = $obj[$attrs];
476         }
477       }    
479       $tmp['objectClass'] = $this->subClasses;
481       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
483       if($obj['status']=="new"){
484         $ldap->cat($sub_dn,array("objectClass"));
485         if($ldap->count()){
486           $obj['status']="edited";
487         }
488       }
490       /* Check if gosaAdministrativeUnitTag is required as object class */
491       if($obj['status'] == "edited"){
492         $ldap->cat($sub_dn,array("objectClass"));
493         $attrs = $ldap->fetch();
494         if(isset($attrs['objectClass'])){
495           if(in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass'])){
496             $tmp['objectClass'][] = "gosaAdministrativeUnitTag";
497           }
498         }
499       }
501       if($obj['status'] == "delete"){
502         prepare_to_save_FAI_object($sub_dn,array(),true);
503         $this->handle_post_events("remove");
504       }elseif($obj['status'] == "edited"){
505         prepare_to_save_FAI_object($sub_dn,$tmp);
506         $this->handle_post_events("modify");
507       }elseif($obj['status']=="new"){
508         prepare_to_save_FAI_object($sub_dn,$tmp);
509         $this->handle_post_events("add");
510       }
511       $this->handle_object_tagging($sub_dn, $this->gosaUnitTag);
512     }
513   }
515   /* Return plugin informations for acl handling */ 
516   function plInfo()
517   {
518     return (array( 
519           "plShortName" => _("Hook"),
520           "plDescription" => _("FAI hook"),
521           "plSelfModify"  => FALSE,
522           "plDepends"     => array(),
523           "plPriority"    => 20,
524           "plSection"     => array("administration"),
525           "plCategory"    => array("fai"),
526           "plProvidedAcls" => array(
527             "cn"                => _("Name")." ("._("Read only").")",
528             "description"       => _("Description"))
529           ));
530   }
533 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
534 ?>