Code

b5f99e948e0b0fa8166e8302f4c7927463352975
[gosa.git] / branches / old / 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("/".normalizePreg($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"){
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         session::set('objectinfo',$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         session::set('objectinfo',$obj['dn']);
205         $this->dialog->parent = &$this;
206         $this->is_dialog=true;
207       }
208     }
210     ///// Ende new list handling
212     /* Add new sub object */
213     if(isset($_POST['AddSubObject'])){
215       $c_dn = $this->acl_base_for_current_object($this->dn);
216       $this->dialog= new $this->subClassName($this->config,"new");
217       $this->dialog->set_acl_base($c_dn);
218       $this->dialog->FAIstate = $this->FAIstate;
219       $this->dialog->set_acl_category("fai");
220       $this->dialog->parent = &$this;
221       $this->is_dialog=true;
222     }
225     /* Save Dialog */
226     if(isset($_POST['SaveSubObject'])){
228       /* Perform post check*/
229       $this->dialog->save_object();
231       /* Get messages */
232       $msgs = $this->dialog->check();
234       /* print errors */
235       if(count($msgs)>0){
236         foreach($msgs as $msg){
237           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
238         }
239       }else{
241         /* Get return object */
242         $obj = $this->dialog->save();
243         if(isset($obj['remove'])){
245           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
246   
247           /* Depending on status, set new status */
248           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
249             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
250           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
251             unset($this->SubObjects[$obj['remove']['from']]);
252           }
253           $obj['status'] = "new";
254           $this->SubObjects[$obj['remove']['to']] = $obj;
255           unset($this->SubObjects[$obj['remove']['to']]['remove']);
256         }else{
257           if($obj['status'] == "FreshLoaded"){
258             $obj['status'] = "edited";
259           }
260           $this->SubObjects[$obj['cn']]=$obj;
261         }
262         $this->is_dialog=false;
263         unset($this->dialog);
264         $this->dialog=FALSE;
265       }
266     }
268     /* Sort entries */
269     $tmp = $keys = array();
270     foreach($this->SubObjects as $key => $entry){
271       $keys[$key]=$key;
272     }
273     natcasesort($keys);
274     foreach($keys as $key){
275       $tmp[$key]=$this->SubObjects[$key];
276     }
277     $this->SubObjects = $tmp;
279     /* Cancel Dialog */
280     if(isset($_POST['CancelSubObject'])){
281       $this->is_dialog=false; 
282       unset($this->dialog);
283       $this->dialog=FALSE;
284     }
286     /* Print dialog if $this->dialog is set */
287     if(is_object($this->dialog)){
288       $this->dialog->save_object();
289       $display = $this->dialog->execute();
290       return($display);
291     }
295     /* Divlist            added 28.02.2006
296        Containing FAIscripts
297      */
299     $divlist = new divSelectBox("FAIhooks");
300     $divlist->setHeight(400);
302     foreach($this->getList(true) as $key => $name){
304       $dn= $this->acl_base_for_current_object($name['dn']);
305       $acl = $this->ui->get_permissions($dn,"fai/faiHookEntry")  ;
306       $act = "";
308       /* Check if this object is freezed, in this case hide the delete icon */
309       if(preg_match("/^freeze/", $this->FAIstate)){
310         $act .= "<input type='image' src='images/lists/edit.png'      name='editscript_%s'    title='"._("Edit")."' alt='"._("Edit")."'>";
311       }else{
312         $act .= "<input type='image' src='images/lists/edit.png'      name='editscript_%s'    title='"._("Edit")."' alt='"._("Edit")."'>";
313         if(preg_match("/d/",$acl)){
314           $act .="<input type='image' src='images/lists/trash.png' name='deletescript_%s'  title='"._("Delete")."' alt='"._("Delete")."'>";
315         }
316       }
318       /* Check if we are allowed to use the export button for this object */
319       $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","FAIscript")  ;
320       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
321         $down = "";
322       }else{
323         $down = "<a href='?plug=".$_GET['plug']."&getFAIHook=".base64_encode($key)."'>
324           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
325           </a>";
326       }
328       /* Check if we are allowed to view the object */
329       $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","cn")  ;
330       if(preg_match("/r/",$s_acl)){
332         $edit_link = "<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".base64_encode($key)."'>".$name['name']."</a>";
333         $divlist->AddEntry(array( array("string"=>$edit_link),
334               array("string"=>$down , "attach" => "style='width:20px;'"),
335               array("string"=>str_replace("%s",base64_encode($key),$act),
336                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
337       }
338     }
339     $smarty->assign("Entry_divlist",$divlist->DrawList());
340     /* Divlist creation complete
341      */
343     $smarty->assign("SubObjects",$this->getList());
345     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
346      * If we post the escaped strings they will be escaped again
347      */
348     foreach($this->attributes as $attrs){
349       if(get_magic_quotes_gpc()){
350         $smarty->assign($attrs,stripslashes($this->$attrs));
351       }else{
352         $smarty->assign($attrs,($this->$attrs));
353       }
354     }
356     $tmp = $this->plInfo();
357       
358     $c_dn = $this->acl_base_for_current_object($this->dn);
359     $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiHookEntry")) && !preg_match('/^freeze/', $this->FAIstate));
360     foreach($tmp['plProvidedAcls'] as $name => $translation){
361       $smarty->assign($name."ACL",$this->getacl($name));
362     }
364     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
365     return($display);
366   }
368   /* Generate listbox friendly SubObject list
369    */
370   function getList($use_dns=false){
371     $a_return=array();
372     foreach($this->SubObjects as $obj){
373       if($obj['status'] != "delete"){
375         $cn   = stripslashes($obj['cn']);
376         $desc = "";
378         if((isset($obj['description']))&&(!empty($obj['description']))){
379           $desc = " [".stripslashes($obj['description'])."]"; 
380         }
382         if($use_dns){
383           $a_return[$obj['cn']]['name']= $cn.$desc;
384           $a_return[$obj['cn']]['dn']= $obj['dn'];
385         }else{
386           $a_return[$obj['cn']] =  $cn.$desc;
387         }
388       }
389     }
390     return($a_return);
391   }
394   /* Delete me, and all my subtrees
395    */
396   function remove_from_parent()
397   {
398     if($this->acl_is_removeable()){
399       $ldap = $this->config->get_ldap_link();
400       $ldap->cd ($this->dn);
401       $release = $this->parent->parent->fai_release;
402       $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $this->dn);
403       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
404       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
406       foreach($this->SubObjects as $name => $obj){
407         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $obj['dn']);
408         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
409       }
410       $this->handle_post_events("remove");
411     }
412   }
415   /* Save data to object 
416    */
417   function save_object()
418   {
419     if((isset($_POST['FAIhook_posted'])) && !preg_match("/^freeze/", $this->FAIstate)){
420       plugin::save_object();
421       foreach($this->attributes as $attrs){
422         if(isset($_POST[$attrs])){
423           $this->$attrs = $_POST[$attrs];
424         }
425       }
426     }
427   }
430   /* Check supplied data */
431   function check()
432   {
433     /* Call common method to give check the hook */
434     $message= plugin::check();
436     /* Ensure that we do not overwrite an allready existing entry 
437      */
438     if($this->is_new){
439       $release = $this->parent->parent->fai_release;
440       $new_dn= 'cn='.$this->cn.",".get_ou('faihookou').get_ou('faiou').$release;
441       $res = faiManagement::check_class_name("FAIhook",$this->cn,$new_dn);
442       if(isset($res[$this->cn])){
443         $message[] = msgPool::duplicated(_("Name"));
444       }
445     }
447     return ($message);
448   }
451   /* Save to LDAP */
452   function save()
453   {
454     plugin::save();
456     $ldap = $this->config->get_ldap_link();
458     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
460     if($this->initially_was_account){
461       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
462     }else{
463       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
464     }
466     $ldap->cd($this->dn);
468     /* Prepare FAIscriptEntry to write it to ldap
469      * First sort array.
470      *  Because we must delete old entries first.
471      * After deletion, we perform add and modify 
472      */
473     $Objects = array();
475     /* We do not need to save untouched objects */
476     foreach($this->SubObjects as $name => $obj){
477       if($obj['status'] != "FreshLoaded"){
478         $Objects[$name] = $obj; 
479       }
480     }
482     foreach($Objects as $name => $obj){
484       foreach($this->sub64coded as $codeIt){
485         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
486       }
488       $tmp = array();
489       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
490       foreach($attributes as $attrs){
491         if($obj[$attrs] == ""){
492           $obj[$attrs] = array();
493         }
494         if(!is_array($obj[$attrs])){
495           $tmp[$attrs] = stripslashes($obj[$attrs]);
496         }else{
497           $tmp[$attrs] = $obj[$attrs];
498         }
499       }    
501       $tmp['objectClass'] = $this->subClasses;
503       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
505       if($obj['status']=="new"){
506         $ldap->cat($sub_dn,array("objectClass"));
507         if($ldap->count()){
508           $obj['status']="edited";
509         }
510       }
512       /* Tag object */
513       $ui= get_userinfo();
514       $this->tag_attrs($tmp, $sub_dn, $ui->gosaUnitTag);
516       if($obj['status'] == "delete"){
517         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
518         $this->handle_post_events("remove");
519       }elseif($obj['status'] == "edited"){
520         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
521         $this->handle_post_events("modify");
522       }elseif($obj['status']=="new"){
523         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
524         $this->handle_post_events("add");
525       }
526     }
527   }
530   function PrepareForCopyPaste($source)
531   {
532     plugin::PrepareForCopyPaste($source);
534     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
535      */
536     $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
537     foreach($res as $obj){
539       /* Skip not relevant objects */
540       if(!preg_match("/".normalizePreg($source['dn'])."$/i",$obj['dn'])) continue;
542       $objects = array();
543       $objects['status']      = "edited";
544       $objects['dn']          = $obj['dn'];
545       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
546       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
547       $this->SubObjects[$objects['cn']] = $objects;
548     }
549   }
552   /* Return plugin informations for acl handling */ 
553   static function plInfo()
554   {
555     return (array( 
556           "plShortName" => _("Hook"),
557           "plDescription" => _("FAI hook"),
558           "plSelfModify"  => FALSE,
559           "plDepends"     => array(),
560           "plPriority"    => 20,
561           "plSection"     => array("administration"),
562           "plCategory"    => array("fai"),
563           "plProvidedAcls" => array(
564             "cn"                => _("Name")." ("._("Read only").")",
565             "description"       => _("Description"))
566           ));
567   }
570   /*! \brief  Used for copy & paste.
571     Returns a HTML input mask, which allows to change the cn of this entry.
572     @param  Array   Array containing current status && a HTML template.
573    */
574   function getCopyDialog()
575   {
576     $vars = array("cn");
577     $smarty = get_smarty();
578     $smarty->assign("cn", htmlentities($this->cn));
579     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
580     $ret = array();
581     $ret['string'] = $str;
582     $ret['status'] = "";
583     return($ret);
584   }
587   /*! \brief  Used for copy & paste.
588     Some entries must be renamed to avaoid duplicate entries.
589    */
590   function saveCopyDialog()
591   {
592     if(isset($_POST['cn'])){
593       $this->cn = get_post('cn');
594     }
595   }
598 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
599 ?>