Code

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