Code

3fdd7070f291a1832b6c2627df639c7921ea5fcd
[gosa.git] / trunk / gosa-plugins / fai / admin / fai / class_faiHook.inc
1 <?php
3 class faiHook extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account   = TRUE;
8   /* Attributes for this Object */
9   var $attributes       = array("cn","description");
11   /* ObjectClasses for this Object*/
12   var $objectclasses    = array("top","FAIclass","FAIhook");
14   /* Class name of the Ldap ObjectClass for the Sub Object */
15   var $subClass         = "FAIhookEntry";
16   var $subClasses       = array("top","FAIclass","FAIhookEntry");
18   /* Class name of the php class which allows us to edit a Sub Object */
19   var $subClassName     = "faiHookEntry";      
21   /* Attributes to initialise for each subObject */
22   var $subAttributes    = array("cn","description","FAItask"); 
23   var $sub_Load_Later   = array("FAIscript");
24   var $sub64coded       = array();
25   var $subBinary        = array("FAIscript");
27   /* Specific attributes */
28   var $cn               = "";       // The class name for this object
29   var $description      = "";       // The description for this set of partitions
30   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
31   var $SubObjects       = array();  // All leafobjects of this object
32   var $view_logged      = FALSE;
33   var $FAIstate         = "";  
34   var $ui;
36   function faiHook (&$config, $dn= NULL)
37   {
38     /* Load Attributes */
39     plugin::plugin ($config, $dn);
41     /* If "dn==new" we try to create a new entry
42      * Else we must read all objects from ldap which belong to this entry.
43      */
44     if($dn != "new"){
45       $this->dn =$dn;
46    
47       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
48        */
49       $res = FAI::get_all_objects_for_given_base($this->dn,"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
50       foreach($res as $obj){
52         /* Skip not relevant objects */
53         if(!preg_match("/".preg_quote($this->dn, '/')."$/i",$obj['dn'])) continue;
55         $objects = array();
56         $objects['status']      = "FreshLoaded";
57         $objects['dn']          = $obj['dn'];
58         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
59         $this->SubObjects[$objects['cn']] = $objects;
60       }
61     }
62     $this->is_new = FALSE;
63     if($this->dn == "new"){
64       $this->is_new =TRUE;
65     }
66     $this->ui = get_userinfo();
67   }
70   /* Reload some attributes */
71   function get_object_attributes($object,$attributes)
72   {
73     $ldap = $this->config->get_ldap_link();
74     $ldap->cd($this->config->current['BASE']);
75     $ldap->cat($object['dn'],$attributes);
76     $tmp  = $ldap->fetch();
78     foreach($attributes as $attrs){
79       if(isset($tmp[$attrs][0])){
80         $var = $tmp[$attrs][0];
82         /* Check if we must decode some attributes */
83         if(in_array_ics($attrs,$this->sub64coded)){
84           $var = base64_decode($var);
85         }
87         /*  check if this is a binary entry */
88         if(in_array_ics($attrs,$this->subBinary)){
89           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
90         }
92         /* Fix slashes */
93         $var = addslashes($var);
94         $object[$attrs] = $var;
95       }
96     }
97     return($object);
98   }
101   function acl_base_for_current_object($dn)
102   {
103     if($dn == "new" || $dn == ""){
104       if($this->dn == "new"){
105         $dn= $this->parent->parent->acl_base;
106       }else{
107         $dn = $this->dn;
108       }
109     }
110     return($dn);
111   }
114   function getUsedFAItask($cn)
115   {
116     $ret = array();
117     foreach($this->SubObjects as $name => $class){
118       if($class['cn'] == $cn){
119         continue;
120       } 
121       if($class['status'] != "delete"){
122         $ret[$class['FAItask']] = $class['FAItask'];
123       }
124     }
125     return($ret);
126   }
128   function execute()
129   {
130     /* Call parent execute */
131     plugin::execute();
133     if($this->is_account && !$this->view_logged){
134       $this->view_logged = TRUE;
135       new log("view","fai/".get_class($this),$this->dn);
136     }
138     /* Fill templating stuff */
139     $smarty= get_smarty();
140     $display= "";
142     /* New Listhandling
143      */
144     $once = true;
145     foreach($_POST as $name => $value){
146       if(preg_match("/^editscript_/",$name)&&($once)){
147         $once = false;
148         $entry = preg_replace("/^editscript_/","",$name);
149         $entry = base64_decode(preg_replace("/_.*/","",$entry));
151         $obj  = $this->SubObjects[$entry];
152         if($obj['status'] == "FreshLoaded"){
153           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
154         }
156         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
157         $this->dialog->FAIstate = $this->FAIstate;
158         $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
159         $this->dialog->set_acl_category("fai");
161         set_object_info($obj['dn']);
162         $this->dialog->parent = &$this;
163         $this->is_dialog=true;
164       }
165       if(preg_match("/^deletescript_/",$name)&&($once)){
166         $entry = preg_replace("/^deletescript_/","",$name);
167         $entry = base64_decode(preg_replace("/_.*/","",$entry));
169         $dn = $this->acl_base_for_current_object($this->SubObjects[$entry]['dn']);
170         $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry")  ;
172         if(preg_match("/d/",$acl)){
173           $once = false;
175           $status = $this->SubObjects[$entry]['status'];
176           if($status == "edited" || $status == "FreshLoaded"){
177             $this->SubObjects[$entry]['status']= "delete";
178           }else{
179             unset($this->SubObjects[$entry]);
180           }
181         }
182       }
183     }
185     /* File download requested */
186     if(isset($_GET['getFAIHook'])){
187       if(isset($this->SubObjects[base64_decode($_GET['getFAIHook'])])){
188         $obj = $this->SubObjects[base64_decode($_GET['getFAIHook'])];
189         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
190         send_binary_content(stripslashes($obj['FAIscript']),$obj['cn'].".FAIhook");
191       }
192     }
194     /* Edit entries via GET */
195     if(isset($_GET['act']) && isset($_GET['id'])){
196       if($_GET['act'] == "edit" && isset($this->SubObjects[base64_decode($_GET['id'])])){
197         $obj = $this->SubObjects[base64_decode($_GET['id'])];
198         if($obj['status'] == "FreshLoaded"){
199           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
200         }
201         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
202         $this->dialog->FAIstate = $this->FAIstate;
203         $this->dialog->acl = $this->acl;
204         $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
205         $this->dialog->set_acl_category("fai");
206         set_object_info($obj['dn']);
207         $this->dialog->parent = &$this;
208         $this->is_dialog=true;
209       }
210     }
212     ///// Ende new list handling
214     /* Add new sub object */
215     if(isset($_POST['AddSubObject'])){
217       $c_dn = $this->acl_base_for_current_object($this->dn);
218       $this->dialog= new $this->subClassName($this->config,"new");
219       $this->dialog->set_acl_base($c_dn);
220       $this->dialog->FAIstate = $this->FAIstate;
221       $this->dialog->set_acl_category("fai");
222       $this->dialog->parent = &$this;
223       $this->is_dialog=true;
224     }
227     /* Save Dialog */
228     if(isset($_POST['SaveSubObject'])){
230       /* Perform post check*/
231       $this->dialog->save_object();
233       /* Get messages */
234       $msgs = $this->dialog->check();
236       /* print errors */
237       if(count($msgs)>0){
238         foreach($msgs as $msg){
239           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
240         }
241       }else{
243         /* Get return object */
244         $obj = $this->dialog->save();
245         if(isset($obj['remove'])){
247           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
248   
249           /* Depending on status, set new status */
250           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
251             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
252           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
253             unset($this->SubObjects[$obj['remove']['from']]);
254           }
255           $obj['status'] = "new";
256           $this->SubObjects[$obj['remove']['to']] = $obj;
257           unset($this->SubObjects[$obj['remove']['to']]['remove']);
258         }else{
259           if($obj['status'] == "FreshLoaded"){
260             $obj['status'] = "edited";
261           }
262           $this->SubObjects[$obj['cn']]=$obj;
263         }
264         $this->is_dialog=false;
265         unset($this->dialog);
266         $this->dialog=FALSE;
267       }
268     }
270     /* Sort entries */
271     $tmp = $keys = array();
272     foreach($this->SubObjects as $key => $entry){
273       $keys[$key]=$key;
274     }
275     natcasesort($keys);
276     foreach($keys as $key){
277       $tmp[$key]=$this->SubObjects[$key];
278     }
279     $this->SubObjects = $tmp;
281     /* Cancel Dialog */
282     if(isset($_POST['CancelSubObject'])){
283       $this->is_dialog=false; 
284       unset($this->dialog);
285       $this->dialog=FALSE;
286     }
288     /* Print dialog if $this->dialog is set */
289     if(is_object($this->dialog)){
290       $this->dialog->save_object();
291       $display = $this->dialog->execute();
292       return($display);
293     }
297     /* Divlist            added 28.02.2006
298        Containing FAIscripts
299      */
301     $divlist = new divSelectBox("FAIhooks");
302     $divlist->setHeight(400);
304     foreach($this->getList(true) as $key => $name){
306       $dn= $this->acl_base_for_current_object($name['dn']);
307       $acl = $this->ui->get_permissions($dn,"fai/faiHookEntry")  ;
308       $act = "";
310       /* Check if this object is freezed, in this case hide the delete icon */
311       if(preg_match("/^freeze/", $this->FAIstate)){
312         $act .= "<input type='image' src='images/lists/edit.png'      name='editscript_%s'    title='"._("Edit")."' alt='"._("Edit")."'>";
313       }else{
314         $act .= "<input type='image' src='images/lists/edit.png'      name='editscript_%s'    title='"._("Edit")."' alt='"._("Edit")."'>";
315         if(preg_match("/d/",$acl)){
316           $act .="<input type='image' src='images/lists/trash.png' name='deletescript_%s'  title='"._("Delete")."' alt='"._("Delete")."'>";
317         }
318       }
320       /* Check if we are allowed to use the export button for this object */
321       $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","FAIscript")  ;
322       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
323         $down = "";
324       }else{
325         $down = "<a href='?plug=".$_GET['plug']."&getFAIHook=".base64_encode($key)."'>
326           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
327           </a>";
328       }
330       /* Check if we are allowed to view the object */
331       $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","cn")  ;
332       if(preg_match("/r/",$s_acl)){
334         $edit_link = "<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".base64_encode($key)."'>".$name['name']."</a>";
335         $divlist->AddEntry(array( array("string"=>$edit_link),
336               array("string"=>$down , "attach" => "style='width:20px;'"),
337               array("string"=>str_replace("%s",base64_encode($key),$act),
338                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
339       }
340     }
341     $smarty->assign("Entry_divlist",$divlist->DrawList());
342     /* Divlist creation complete
343      */
345     $smarty->assign("SubObjects",$this->getList());
347     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
348      * If we post the escaped strings they will be escaped again
349      */
350     foreach($this->attributes as $attrs){
351       if(get_magic_quotes_gpc()){
352         $smarty->assign($attrs,stripslashes($this->$attrs));
353       }else{
354         $smarty->assign($attrs,($this->$attrs));
355       }
356     }
358     $tmp = $this->plInfo();
359       
360     $c_dn = $this->acl_base_for_current_object($this->dn);
361     $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiHookEntry")) && !preg_match('/^freeze/', $this->FAIstate));
362     foreach($tmp['plProvidedAcls'] as $name => $translation){
363       $smarty->assign($name."ACL",$this->getacl($name));
364     }
366     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
367     return($display);
368   }
370   /* Generate listbox friendly SubObject list
371    */
372   function getList($use_dns=false){
373     $a_return=array();
374     foreach($this->SubObjects as $obj){
375       if($obj['status'] != "delete"){
377         $cn   = stripslashes($obj['cn']);
378         $desc = "";
380         if((isset($obj['description']))&&(!empty($obj['description']))){
381           $desc = " [".stripslashes($obj['description'])."]"; 
382         }
384         if($use_dns){
385           $a_return[$obj['cn']]['name']= $cn.$desc;
386           $a_return[$obj['cn']]['dn']= $obj['dn'];
387         }else{
388           $a_return[$obj['cn']] =  $cn.$desc;
389         }
390       }
391     }
392     return($a_return);
393   }
396   /* Delete me, and all my subtrees
397    */
398   function remove_from_parent()
399   {
400     if($this->acl_is_removeable()){
401       $ldap = $this->config->get_ldap_link();
402       $ldap->cd ($this->dn);
403       $release = $this->parent->parent->fai_release;
404       $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $this->dn);
405       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
406       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
408       foreach($this->SubObjects as $name => $obj){
409         $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $obj['dn']);
410         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
411       }
412       $this->handle_post_events("remove");
413     }
414   }
417   /* Save data to object 
418    */
419   function save_object()
420   {
421     if((isset($_POST['FAIhook_posted'])) && !preg_match("/^freeze/", $this->FAIstate)){
422       plugin::save_object();
423     }
424   }
427   /* Check supplied data */
428   function check()
429   {
430     /* Call common method to give check the hook */
431     $message= plugin::check();
433     /* Ensure that we do not overwrite an allready existing entry 
434      */
435     if($this->is_new){
436       $release = $this->parent->parent->fai_release;
437       $new_dn= 'cn='.$this->cn.",".get_ou('faiHookRDN').get_ou('faiBaseRDN').$release;
438       $res = faiManagement::check_class_name("FAIhook",$this->cn,$new_dn);
439       if(isset($res[$this->cn])){
440         $message[] = msgPool::duplicated(_("Name"));
441       }
442     }
444     return ($message);
445   }
448   /* Save to LDAP */
449   function save()
450   {
451     plugin::save();
453     $ldap = $this->config->get_ldap_link();
455     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
457     if($this->initially_was_account){
458       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
459     }else{
460       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
461     }
463     $ldap->cd($this->dn);
465     /* Prepare FAIscriptEntry to write it to ldap
466      * First sort array.
467      *  Because we must delete old entries first.
468      * After deletion, we perform add and modify 
469      */
470     $Objects = array();
472     /* We do not need to save untouched objects */
473     foreach($this->SubObjects as $name => $obj){
474       if($obj['status'] != "FreshLoaded"){
475         $Objects[$name] = $obj; 
476       }
477     }
479     foreach($Objects as $name => $obj){
481       foreach($this->sub64coded as $codeIt){
482         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
483       }
485       $tmp = array();
486       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
487       foreach($attributes as $attrs){
488         if(!isset($obj[$attrs])) continue; 
489         if($obj[$attrs] == ""){
490           $obj[$attrs] = array();
491         }
492         if(!is_array($obj[$attrs])){
493           $tmp[$attrs] = stripslashes($obj[$attrs]);
494         }else{
495           $tmp[$attrs] = $obj[$attrs];
496         }
497       }    
499       $tmp['objectClass'] = $this->subClasses;
501       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
503       if($obj['status']=="new"){
504         $ldap->cat($sub_dn,array("objectClass"));
505         if($ldap->count()){
506           $obj['status']="edited";
507         }
508       }
510       /* Tag object */
511       $ui= get_userinfo();
512       $this->tag_attrs($tmp, $sub_dn, $ui->gosaUnitTag);
514       if($obj['status'] == "delete"){
515         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
516         $this->handle_post_events("remove");
517       }elseif($obj['status'] == "edited"){
518         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
519         $this->handle_post_events("modify");
520       }elseif($obj['status']=="new"){
521         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
522         $this->handle_post_events("add");
523       }
524     }
525   }
528   function PrepareForCopyPaste($source)
529   {
530     plugin::PrepareForCopyPaste($source);
532     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
533      */
534     $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
535     foreach($res as $obj){
537       /* Skip not relevant objects */
538       if(!preg_match("/".preg_quote($source['dn'], '/')."$/i",$obj['dn'])) continue;
540       $objects = array();
541       $objects['status']      = "edited";
542       $objects['dn']          = $obj['dn'];
543       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
544       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
545       $this->SubObjects[$objects['cn']] = $objects;
546     }
547   }
550   /* Return plugin informations for acl handling */ 
551   static function plInfo()
552   {
553     return (array( 
554           "plShortName" => _("Hook"),
555           "plDescription" => _("FAI hook"),
556           "plSelfModify"  => FALSE,
557           "plDepends"     => array(),
558           "plPriority"    => 20,
559           "plSection"     => array("administration"),
560           "plCategory"    => array("fai"),
561           "plProvidedAcls" => array(
562             "cn"                => _("Name")." ("._("Read only").")",
563             "description"       => _("Description"))
564           ));
565   }
568   /*! \brief  Used for copy & paste.
569     Returns a HTML input mask, which allows to change the cn of this entry.
570     @param  Array   Array containing current status && a HTML template.
571    */
572   function getCopyDialog()
573   {
574     $vars = array("cn");
575     $smarty = get_smarty();
576     $smarty->assign("cn", htmlentities($this->cn));
577     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
578     $ret = array();
579     $ret['string'] = $str;
580     $ret['status'] = "";
581     return($ret);
582   }
585   /*! \brief  Used for copy & paste.
586     Some entries must be renamed to avaoid duplicate entries.
587    */
588   function saveCopyDialog()
589   {
590     if(isset($_POST['cn'])){
591       $this->cn = get_post('cn');
592     }
593   }
596 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
597 ?>