Code

Updated ACL
[gosa.git] / 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       $ldap     = $this->config->get_ldap_link();
50       $ldap->cd ($this->dn);
51       $attrs_to_search = $this->subAttributes;
52       $attrs_to_search[] = "FAIstate";
53       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))", $attrs_to_search);
55       $data = array();
56       while($object = $ldap->fetch()){
57         $data[] = $object;
58       }
59       foreach($data as $object){
61        /* Skip objects, that are tagged as removed */
62         if(isset($object['FAIstate'][0])){
63           if(preg_match("/removed$/",$object['FAIstate'][0])){
64             continue;
65           }
66         }
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->is_new = FALSE;
77     if($this->dn == "new"){
78       $this->is_new =TRUE;
79     }
80     $this->ui = get_userinfo();
81   }
84   /* Reload some attributes */
85   function get_object_attributes($object,$attributes)
86   {
87     $ldap = $this->config->get_ldap_link();
88     $ldap->cd($this->config->current['BASE']);
89     $ldap->cat($object['dn'],$attributes);
90     $tmp  = $ldap->fetch();
92     foreach($attributes as $attrs){
93       if(isset($tmp[$attrs][0])){
94         $var = $tmp[$attrs][0];
96         /* Check if we must decode some attributes */
97         if(in_array_ics($attrs,$this->sub64coded)){
98           $var = base64_decode($var);
99         }
101         /*  check if this is a binary entry */
102         if(in_array_ics($attrs,$this->subBinary)){
103           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
104         }
106         /* Fix slashes */
107         $var = addslashes($var);
108         $object[$attrs] = $var;
109       }
110     }
111     return($object);
112   }
115   function acl_base_for_current_object($dn)
116   {
117     if($dn == "new"){
118       if($this->dn == "new"){
119         $dn = session::get('CurrentMainBase');
120       }else{
121         $dn = $this->dn;
122       }
123     }
124     return($dn);
125   }
128   function getUsedFAItask($cn)
129   {
130     $ret = array();
131     foreach($this->SubObjects as $name => $class){
132       if($class['cn'] == $cn){
133         continue;
134       } 
135       if($class['status'] != "delete"){
136         $ret[$class['FAItask']] = $class['FAItask'];
137       }
138     }
139     return($ret);
140   }
142   function execute()
143   {
144     /* Call parent execute */
145     plugin::execute();
147     if($this->is_account && !$this->view_logged){
148       $this->view_logged = TRUE;
149       new log("view","fai/".get_class($this),$this->dn);
150     }
152     /* Fill templating stuff */
153     $smarty= get_smarty();
154     $display= "";
156     /* New Listhandling
157      */
158     $once = true;
159     foreach($_POST as $name => $value){
160       if(preg_match("/^editscript_/",$name)&&($once)){
161         $once = false;
162         $entry = preg_replace("/^editscript_/","",$name);
163         $entry = base64_decode(preg_replace("/_.*/","",$entry));
165         $obj  = $this->SubObjects[$entry];
166         if($obj['status'] == "FreshLoaded"){
167           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
168         }
170         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
171         $this->dialog->FAIstate = $this->FAIstate;
172         $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
173         $this->dialog->set_acl_category("fai");
175         session::set('objectinfo',$obj['dn']);
176         $this->dialog->parent = &$this;
177         $this->is_dialog=true;
178       }
179       if(preg_match("/^deletescript_/",$name)&&($once)){
180         $entry = preg_replace("/^deletescript_/","",$name);
181         $entry = base64_decode(preg_replace("/_.*/","",$entry));
183         $dn = $this->acl_base_for_current_object($this->SubObjects[$entry]['dn']);
184         $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry")  ;
186         if(preg_match("/d/",$acl)){
187           $once = false;
189           $status = $this->SubObjects[$entry]['status'];
190           if($status == "edited" || $status == "FreshLoaded"){
191             $this->SubObjects[$entry]['status']= "delete";
192           }else{
193             unset($this->SubObjects[$entry]);
194           }
195         }
196       }
197     }
199     /* File download requested */
200     if(isset($_GET['getFAIHook'])){
201       if(isset($this->SubObjects[base64_decode($_GET['getFAIHook'])])){
202         $obj = $this->SubObjects[base64_decode($_GET['getFAIHook'])];
203         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
204         send_binary_content(stripslashes($obj['FAIscript']),$obj['cn'].".FAIhook");
205       }
206     }
208     /* Edit entries via GET */
209     if(isset($_GET['act']) && isset($_GET['id'])){
210       if($_GET['act'] == "edit" && isset($this->SubObjects[base64_decode($_GET['id'])])){
211         $obj = $this->SubObjects[base64_decode($_GET['id'])];
212           if($obj['status'] == "FreshLoaded"){
213           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
214         }
215         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
216         $this->dialog->FAIstate = $this->FAIstate;
217         $this->dialog->acl = $this->acl;
218         session::set('objectinfo',$obj['dn']);
219         $this->dialog->parent = &$this;
220         $this->is_dialog=true;
221       }
222     }
224     ///// Ende new list handling
226     /* Add new sub object */
227     if(isset($_POST['AddSubObject'])){
229       $c_dn = $this->acl_base_for_current_object($this->dn);
230       $this->dialog= new $this->subClassName($this->config,"new");
231       $this->dialog->set_acl_base($c_dn);
232       $this->dialog->FAIstate = $this->FAIstate;
233       $this->dialog->set_acl_category("fai");
234       $this->dialog->parent = &$this;
235       $this->is_dialog=true;
236     }
239     /* Save Dialog */
240     if(isset($_POST['SaveSubObject'])){
242       /* Perform post check*/
243       $this->dialog->save_object();
245       /* Get messages */
246       $msgs = $this->dialog->check();
248       /* print errors */
249       if(count($msgs)>0){
250         foreach($msgs as $msg){
251           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
252         }
253       }else{
255         /* Get return object */
256         $obj = $this->dialog->save();
257         if(isset($obj['remove'])){
259           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
260   
261           /* Depending on status, set new status */
262           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
263             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
264           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
265             unset($this->SubObjects[$obj['remove']['from']]);
266           }
267           $obj['status'] = "new";
268           $this->SubObjects[$obj['remove']['to']] = $obj;
269           unset($this->SubObjects[$obj['remove']['to']]['remove']);
270         }else{
271           if($obj['status'] == "FreshLoaded"){
272             $obj['status'] = "edited";
273           }
274           $this->SubObjects[$obj['cn']]=$obj;
275         }
276         $this->is_dialog=false;
277         unset($this->dialog);
278         $this->dialog=FALSE;
279       }
280     }
282     /* Sort entries */
283     $tmp = $keys = array();
284     foreach($this->SubObjects as $key => $entry){
285       $keys[$key]=$key;
286     }
287     natcasesort($keys);
288     foreach($keys as $key){
289       $tmp[$key]=$this->SubObjects[$key];
290     }
291     $this->SubObjects = $tmp;
293     /* Cancel Dialog */
294     if(isset($_POST['CancelSubObject'])){
295       $this->is_dialog=false; 
296       unset($this->dialog);
297       $this->dialog=FALSE;
298     }
300     /* Print dialog if $this->dialog is set */
301     if(is_object($this->dialog)){
302       $this->dialog->save_object();
303       $display = $this->dialog->execute();
304       return($display);
305     }
309     /* Divlist            added 28.02.2006
310        Containing FAIscripts
311      */
313     $divlist = new divSelectBox("FAIhooks");
314     $divlist->setHeight(400);
316     foreach($this->getList(true) as $key => $name){
318       $dn= $this->acl_base_for_current_object($name['dn']);
319       $acl = $this->ui->get_permissions($dn,"fai/faiHookEntry")  ;
320       $act = "";
322       /* Check if this object is freezed, in this case hide the delete icon */
323       if(preg_match("/^freeze/", $this->FAIstate)){
324         $act .= "<input type='image' src='images/lists/edit.png'      name='editscript_%s'    title='"._("Edit")."' alt='"._("Edit")."'>";
325       }else{
326         $act .= "<input type='image' src='images/lists/edit.png'      name='editscript_%s'    title='"._("Edit")."' alt='"._("Edit")."'>";
327         if(preg_match("/d/",$acl)){
328           $act .="<input type='image' src='images/lists/trash.png' name='deletescript_%s'  title='"._("Delete")."' alt='"._("Delete")."'>";
329         }
330       }
332       /* Check if we are allowed to use the export button for this object */
333       $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","FAIscript")  ;
334       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
335         $down = "";
336       }else{
337         $down = "<a href='?plug=".$_GET['plug']."&getFAIHook=".base64_encode($key)."'>
338           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
339           </a>";
340       }
342       /* Check if we are allowed to view the object */
343       $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","cn")  ;
344       if(preg_match("/r/",$s_acl)){
346         $edit_link = "<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".base64_encode($key)."'>".$name['name']."</a>";
347         $divlist->AddEntry(array( array("string"=>$edit_link),
348               array("string"=>$down , "attach" => "style='width:20px;'"),
349               array("string"=>str_replace("%s",base64_encode($key),$act),
350                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
351       }
352     }
353     $smarty->assign("Entry_divlist",$divlist->DrawList());
354     /* Divlist creation complete
355      */
357     $smarty->assign("SubObjects",$this->getList());
359     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
360      * If we post the escaped strings they will be escaped again
361      */
362     foreach($this->attributes as $attrs){
363       if(get_magic_quotes_gpc()){
364         $smarty->assign($attrs,stripslashes($this->$attrs));
365       }else{
366         $smarty->assign($attrs,($this->$attrs));
367       }
368     }
370     $tmp = $this->plInfo();
371       
372     $c_dn = $this->acl_base_for_current_object($this->dn);
373     $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiHookEntry")) && !preg_match('/^freeze/', $this->FAIstate));
374     foreach($tmp['plProvidedAcls'] as $name => $translation){
375       $smarty->assign($name."ACL",$this->getacl($name));
376     }
378     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
379     return($display);
380   }
382   /* Generate listbox friendly SubObject list
383    */
384   function getList($use_dns=false){
385     $a_return=array();
386     foreach($this->SubObjects as $obj){
387       if($obj['status'] != "delete"){
389         $cn   = stripslashes($obj['cn']);
390         $desc = "";
392         if((isset($obj['description']))&&(!empty($obj['description']))){
393           $desc = " [".stripslashes($obj['description'])."]"; 
394         }
396         if($use_dns){
397           $a_return[$obj['cn']]['name']= $cn.$desc;
398           $a_return[$obj['cn']]['dn']= $obj['dn'];
399         }else{
400           $a_return[$obj['cn']] =  $cn.$desc;
401         }
402       }
403     }
404     return($a_return);
405   }
408   /* Delete me, and all my subtrees
409    */
410   function remove_from_parent()
411   {
412     if($this->acl_is_removeable()){
413       $ldap = $this->config->get_ldap_link();
414       $ldap->cd ($this->dn);
415       $release = $this->parent->parent->fai_release;
416       $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $this->dn);
417       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
418       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
420       foreach($this->SubObjects as $name => $obj){
421         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $obj['dn']);
422         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
423       }
424       $this->handle_post_events("remove");
425     }
426   }
429   /* Save data to object 
430    */
431   function save_object()
432   {
433     if((isset($_POST['FAIhook_posted'])) && !preg_match("/^freeze/", $this->FAIstate)){
434       plugin::save_object();
435       foreach($this->attributes as $attrs){
436         if(isset($_POST[$attrs])){
437           $this->$attrs = $_POST[$attrs];
438         }
439       }
440     }
441   }
444   /* Check supplied data */
445   function check()
446   {
447     /* Call common method to give check the hook */
448     $message= plugin::check();
450     /* Ensure that we do not overwrite an allready existing entry 
451      */
452     if($this->is_new){
453       $release = $this->parent->parent->fai_release;
454       $new_dn= 'cn='.$this->cn.",".get_ou('faihookou').get_ou('faiou').$release;
455       $res = faiManagement::check_class_name("FAIhook",$this->cn,$new_dn);
456       if(isset($res[$this->cn])){
457         $message[] = msgPool::duplicated(_("Name"));
458       }
459     }
461     return ($message);
462   }
465   /* Save to LDAP */
466   function save()
467   {
468     plugin::save();
470     $ldap = $this->config->get_ldap_link();
472     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
474     if($this->initially_was_account){
475       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
476     }else{
477       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
478     }
480     $ldap->cd($this->dn);
482     /* Prepare FAIscriptEntry to write it to ldap
483      * First sort array.
484      *  Because we must delete old entries first.
485      * After deletion, we perform add and modify 
486      */
487     $Objects = array();
489     /* We do not need to save untouched objects */
490     foreach($this->SubObjects as $name => $obj){
491       if($obj['status'] != "FreshLoaded"){
492         $Objects[$name] = $obj; 
493       }
494     }
496     foreach($Objects as $name => $obj){
498       foreach($this->sub64coded as $codeIt){
499         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
500       }
502       $tmp = array();
503       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
504       foreach($attributes as $attrs){
505         if(empty($obj[$attrs])){
506           $obj[$attrs] = array();
507         }
508         if(!is_array($obj[$attrs])){
509           $tmp[$attrs] = stripslashes($obj[$attrs]);
510         }else{
511           $tmp[$attrs] = $obj[$attrs];
512         }
513       }    
515       $tmp['objectClass'] = $this->subClasses;
517       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
519       if($obj['status']=="new"){
520         $ldap->cat($sub_dn,array("objectClass"));
521         if($ldap->count()){
522           $obj['status']="edited";
523         }
524       }
526       /* Tag object */
527       $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
529       if($obj['status'] == "delete"){
530         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
531         $this->handle_post_events("remove");
532       }elseif($obj['status'] == "edited"){
533         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
534         $this->handle_post_events("modify");
535       }elseif($obj['status']=="new"){
536         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
537         $this->handle_post_events("add");
538       }
539     }
540   }
543   function PrepareForCopyPaste($source)
544   {
545     plugin::PrepareForCopyPaste($source);
547     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
548      */
549     $ldap     = $this->config->get_ldap_link();
550     $ldap->cd ($source['dn']);
552     $attrs_to_search = $this->subAttributes;
553     $attrs_to_search[] = "FAIstate";
554     $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
556     while($object = $ldap->fetch()){
558       /* Skip objects, that are tagged as removed */
559       if(isset($object['FAIstate'][0])){
560         if(preg_match("/removed$/",$object['FAIstate'][0])){
561           continue;
562         }
563       }
565       /* Set status for save management */
566       $objects = array();
567       $objects['status']      = "edited";
568       $objects['dn']          = $object['dn'];
569       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
570       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
572       $this->SubObjects[$objects['cn']] = $objects;
573     }
574   }
577   /* Return plugin informations for acl handling */ 
578   static function plInfo()
579   {
580     return (array( 
581           "plShortName" => _("Hook"),
582           "plDescription" => _("FAI hook"),
583           "plSelfModify"  => FALSE,
584           "plDepends"     => array(),
585           "plPriority"    => 20,
586           "plSection"     => array("administration"),
587           "plCategory"    => array("fai"),
588           "plProvidedAcls" => array(
589             "cn"                => _("Name")." ("._("Read only").")",
590             "description"       => _("Description"))
591           ));
592   }
595   /*! \brief  Used for copy & paste.
596     Returns a HTML input mask, which allows to change the cn of this entry.
597     @param  Array   Array containing current status && a HTML template.
598    */
599   function getCopyDialog()
600   {
601     $vars = array("cn");
602     $smarty = get_smarty();
603     $smarty->assign("cn", htmlentities($this->cn));
604     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
605     $ret = array();
606     $ret['string'] = $str;
607     $ret['status'] = "";
608     return($ret);
609   }
612   /*! \brief  Used for copy & paste.
613     Some entries must be renamed to avaoid duplicate entries.
614    */
615   function saveCopyDialog()
616   {
617     if(isset($_POST['cn'])){
618       $this->cn = get_post('cn');
619     }
620   }
623 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
624 ?>