Code

Updated copy & paste
[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       while($object = $ldap->fetch()){
63        /* Skip objects, that are tagged as removed */
64         if(isset($object['FAIstate'][0])){
65           if(preg_match("/removed$/",$object['FAIstate'][0])){
66             continue;
67           }
68         }
70         /* Set status for save management */
71         $objects = array();
72         $objects['status']      = "FreshLoaded";
73         $objects['dn']          = $object['dn'];
74         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
75         $this->SubObjects[$objects['cn']] = $objects;
76       }
77     }
78     $this->ui = get_userinfo();
79   }
82   /* Reload some attributes */
83   function get_object_attributes($object,$attributes)
84   {
85     $ldap = $this->config->get_ldap_link();
86     $ldap->cd($this->config->current['BASE']);
87     $ldap->cat($object['dn'],$attributes);
88     $tmp  = $ldap->fetch();
90     foreach($attributes as $attrs){
91       if(isset($tmp[$attrs][0])){
92         $var = $tmp[$attrs][0];
94         /* Check if we must decode some attributes */
95         if(in_array_ics($attrs,$this->sub64coded)){
96           $var = base64_decode($var);
97         }
99         /*  check if this is a binary entry */
100         if(in_array_ics($attrs,$this->subBinary)){
101           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
102         }
104         /* Fix slashes */
105         $var = addslashes($var);
106         $object[$attrs] = $var;
107       }
108     }
109     return($object);
110   }
113   function acl_base_for_current_object($dn)
114   {
115     if($dn == "new"){
116       if($this->dn == "new"){
117         $dn = session::get('CurrentMainBase');
118       }else{
119         $dn = $this->dn;
120       }
121     }
122     return($dn);
123   }
126   function getUsedFAItask($cn)
127   {
128     $ret = array();
129     foreach($this->SubObjects as $name => $class){
130       if($class['cn'] == $cn){
131         continue;
132       } 
133       if($class['status'] != "delete"){
134         $ret[$class['FAItask']] = $class['FAItask'];
135       }
136     }
137     return($ret);
138   }
140   function execute()
141   {
142     /* Call parent execute */
143     plugin::execute();
145     if($this->is_account && !$this->view_logged){
146       $this->view_logged = TRUE;
147       new log("view","fai/".get_class($this),$this->dn);
148     }
150     /* Fill templating stuff */
151     $smarty= get_smarty();
152     $display= "";
154     /* New Listhandling
155      */
156     $once = true;
157     foreach($_POST as $name => $value){
158       if(preg_match("/^editscript_/",$name)&&($once)){
159         $once = false;
160         $entry = preg_replace("/^editscript_/","",$name);
161         $entry = base64_decode(preg_replace("/_.*/","",$entry));
163         $obj  = $this->SubObjects[$entry];
164         if($obj['status'] == "FreshLoaded"){
165           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
166         }
168         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
169         $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
170         $this->dialog->set_acl_category("fai");
172         session::set('objectinfo',$obj['dn']);
173         $this->dialog->parent = &$this;
174         $this->is_dialog=true;
175       }
176       if(preg_match("/^deletescript_/",$name)&&($once)){
177         $entry = preg_replace("/^deletescript_/","",$name);
178         $entry = base64_decode(preg_replace("/_.*/","",$entry));
180         $dn = $this->acl_base_for_current_object($this->SubObjects[$entry]['dn']);
181         $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry")  ;
183         if(preg_match("/d/",$acl)){
184           $once = false;
186           $status = $this->SubObjects[$entry]['status'];
187           if($status == "edited" || $status == "FreshLoaded"){
188             $this->SubObjects[$entry]['status']= "delete";
189           }else{
190             unset($this->SubObjects[$entry]);
191           }
192         }
193       }
194     }
196     /* File download requested */
197     if(isset($_GET['getFAIHook'])){
198       if(isset($this->SubObjects[$_GET['getFAIHook']])){
199         $obj = $this->SubObjects[$_GET['getFAIHook']];
200         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
201         send_binary_content($obj['FAIscript'],$obj['cn'].".FAIhook");
202       }
203     }
205     /* Edit entries via GET */
206     if(isset($_GET['act']) && isset($_GET['id'])){
207       if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
208         $obj = $this->SubObjects[$_GET['id']];
209           if($obj['status'] == "FreshLoaded"){
210           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
211         }
212         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
213         $this->dialog->acl = $this->acl;
214         session::set('objectinfo',$obj['dn']);
215         $this->dialog->parent = &$this;
216         $this->is_dialog=true;
217       }
218     }
220     ///// Ende new list handling
222     /* Add new sub object */
223     if(isset($_POST['AddSubObject'])){
225       $c_dn = $this->acl_base_for_current_object($this->dn);
226       $this->dialog= new $this->subClassName($this->config,"new");
227       $this->dialog->set_acl_base($c_dn);
228       $this->dialog->set_acl_category("fai");
229       $this->dialog->parent = &$this;
230       $this->is_dialog=true;
231     }
234     /* Save Dialog */
235     if(isset($_POST['SaveSubObject'])){
237       /* Perform post check*/
238       $this->dialog->save_object();
240       /* Get messages */
241       $msgs = $this->dialog->check();
243       /* print errors */
244       if(count($msgs)>0){
245         foreach($msgs as $msg){
246           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
247         }
248       }else{
250         /* Get return object */
251         $obj = $this->dialog->save();
252         if(isset($obj['remove'])){
254           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
255   
256           /* Depending on status, set new status */
257           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
258             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
259           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
260             unset($this->SubObjects[$obj['remove']['from']]);
261           }
262           $obj['status'] = "new";
263           $this->SubObjects[$obj['remove']['to']] = $obj;
264           unset($this->SubObjects[$obj['remove']['to']]['remove']);
265         }else{
266           if($obj['status'] == "FreshLoaded"){
267             $obj['status'] = "edited";
268           }
269           $this->SubObjects[$obj['cn']]=$obj;
270         }
271         $this->is_dialog=false;
272         unset($this->dialog);
273         $this->dialog=FALSE;
274       }
275     }
277     /* Sort entries */
278     $tmp = $keys = array();
279     foreach($this->SubObjects as $key => $entry){
280       $keys[$key]=$key;
281     }
282     natcasesort($keys);
283     foreach($keys as $key){
284       $tmp[$key]=$this->SubObjects[$key];
285     }
286     $this->SubObjects = $tmp;
288     /* Cancel Dialog */
289     if(isset($_POST['CancelSubObject'])){
290       $this->is_dialog=false; 
291       unset($this->dialog);
292       $this->dialog=FALSE;
293     }
295     /* Print dialog if $this->dialog is set */
296     if(is_object($this->dialog)){
297       $this->dialog->save_object();
298       $display = $this->dialog->execute();
299       return($display);
300     }
304     /* Divlist            added 28.02.2006
305        Containing FAIscripts
306      */
308     $divlist = new divSelectBox("FAIhooks");
309     $divlist->setHeight(400);
311     foreach($this->getList(true) as $key => $name){
313       $dn= $this->acl_base_for_current_object($name['dn']);
314       $acl = $this->ui->get_permissions($dn,"fai/faiHookEntry")  ;
315       $act = "";
317       /* Check if this object is freezed, in this case hide the delete icon */
318       if($this->FAIstate == "freeze"){
319         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("Edit")."' alt='"._("Edit")."'>";
320       }else{
321         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("Edit")."' alt='"._("Edit")."'>";
322         if(preg_match("/d/",$acl)){
323           $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("Delete")."' alt='"._("Delete")."'>";
324         }
325       }
327       /* Check if we are allowed to use the export button for this object */
328       $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","FAIscript")  ;
329       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
330         $down = "";
331       }else{
332         $down = "<a href='?plug=".$_GET['plug']."&getFAIHook=".$key."'>
333           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
334           </a>";
335       }
337       /* Check if we are allowed to view the object */
338       $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","cn")  ;
339       if(preg_match("/r/",$s_acl)){
341         $edit_link = "<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".$key."'>".$name['name']."</a>";
342         $divlist->AddEntry(array( array("string"=>$edit_link),
343               array("string"=>$down , "attach" => "style='width:20px;'"),
344               array("string"=>str_replace("%s",base64_encode($key),$act),
345                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
346       }
347     }
348     $smarty->assign("Entry_divlist",$divlist->DrawList());
349     /* Divlist creation complete
350      */
352     $smarty->assign("SubObjects",$this->getList());
354     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
355      * If we post the escaped strings they will be escaped again
356      */
357     foreach($this->attributes as $attrs){
358       if(get_magic_quotes_gpc()){
359         $smarty->assign($attrs,stripslashes($this->$attrs));
360       }else{
361         $smarty->assign($attrs,($this->$attrs));
362       }
363     }
365     $tmp = $this->plInfo();
366       
367     $c_dn = $this->acl_base_for_current_object($this->dn);
368     $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiHookEntry")) && $this->FAIstate!="freeze");
369     foreach($tmp['plProvidedAcls'] as $name => $translation){
370       $smarty->assign($name."ACL",$this->getacl($name));
371     }
373     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
374     return($display);
375   }
377   /* Generate listbox friendly SubObject list
378    */
379   function getList($use_dns=false){
380     $a_return=array();
381     foreach($this->SubObjects as $obj){
382       if($obj['status'] != "delete"){
383         if($use_dns){
384           if((isset($obj['description']))&&(!empty($obj['description']))){
385             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
386           }else{
387             $a_return[$obj['cn']]['name']= $obj['cn'];
388           }
389           $a_return[$obj['cn']]['dn']= $obj['dn'];
390         }else{
391           if((isset($obj['description']))&&(!empty($obj['description']))){
392             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
393           }else{
394             $a_return[$obj['cn']]= $obj['cn'];
395           }
396         }
397       }
398     }
399     return($a_return);
400   }
403   /* Delete me, and all my subtrees
404    */
405   function remove_from_parent()
406   {
407     $ldap = $this->config->get_ldap_link();
408     $ldap->cd ($this->dn);
410     $faifilter = session::get('faifilter');
411     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
412     if($faifilter['branch'] == "main"){
413       $use_dn = $this->dn;
414     }
416     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
418     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
420     foreach($this->SubObjects as $name => $obj){
421       $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
422       if($faifilter['branch'] == "main"){
423         $use_dn = $obj['dn'];
424       }
425       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
426     }
427     $this->handle_post_events("remove");    
428   }
431   /* Save data to object 
432    */
433   function save_object()
434   {
435     if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
436       plugin::save_object();
437       foreach($this->attributes as $attrs){
438         if(isset($_POST[$attrs])){
439           $this->$attrs = $_POST[$attrs];
440         }
441       }
442     }
443   }
446   /* Check supplied data */
447   function check()
448   {
449     /* Call common method to give check the hook */
450     $message= plugin::check();
452     return ($message);
453   }
456   /* Save to LDAP */
457   function save()
458   {
459     plugin::save();
461     $ldap = $this->config->get_ldap_link();
463     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
464     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/hook with dn '%s' failed."),$this->dn));
466     if($this->initially_was_account){
467       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
468     }else{
469       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
470     }
472     $ldap->cd($this->dn);
474     /* Prepare FAIscriptEntry to write it to ldap
475      * First sort array.
476      *  Because we must delete old entries first.
477      * After deletion, we perform add and modify 
478      */
479     $Objects = array();
481     /* We do not need to save untouched objects */
482     foreach($this->SubObjects as $name => $obj){
483       if($obj['status'] == "FreshLoaded"){
484         unset($this->SubObjects[$name]);
485       }
486     }
488     foreach($this->SubObjects as $name => $obj){
489       if($obj['status'] == "delete"){
490         $Objects[$name] = $obj; 
491       }
492     }
493     foreach($this->SubObjects as $name => $obj){
494       if($obj['status'] != "delete"){
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 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
599 ?>