Code

f1fec85fcca50681474f10bb2c4d8609f81ac458
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiHook.inc
1 <?php
3 class faiHook extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary      = "Manage server basic objects";
7   var $cli_description  = "Some longer text\nfor help";
8   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account   = TRUE;
13   /* Attributes for this Object */
14   var $attributes       = array("cn","description");
16   /* ObjectClasses for this Object*/
17   var $objectclasses    = array("top","FAIclass","FAIhook");
19   /* Class name of the Ldap ObjectClass for the Sub Object */
20   var $subClass         = "FAIhookEntry";
21   var $subClasses       = array("top","FAIclass","FAIhookEntry");
23   /* Class name of the php class which allows us to edit a Sub Object */
24   var $subClassName     = "faiHookEntry";      
26   /* Attributes to initialise for each subObject */
27   var $subAttributes    = array("cn","description","FAItask"); 
28   var $sub_Load_Later   = array("FAIscript");
29   var $sub64coded       = array();
30   var $subBinary        = array("FAIscript");
32   /* Specific attributes */
33   var $cn               = "";       // The class name for this object
34   var $description      = "";       // The description for this set of partitions
35   var $is_dialog        = false;    // specifies which buttons will be shown to save or abort
36   var $SubObjects       = array();  // All leafobjects of this object
37   var $view_logged      = FALSE;
38   var $FAIstate         = "";  
39   var $ui;
41   function faiHook (&$config, $dn= NULL)
42   {
43     /* Load Attributes */
44     plugin::plugin ($config, $dn);
46     /* If "dn==new" we try to create a new entry
47      * Else we must read all objects from ldap which belong to this entry.
48      */
49     if($dn != "new"){
50       $this->dn =$dn;
51     
52       /* Get FAIstate
53        */
54       if(isset($this->attrs['FAIstate'][0])){
55         $this->FAIstate = $this->attrs['FAIstate'][0];
56       }
58       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
59        */
60       $ldap     = $this->config->get_ldap_link();
61       $ldap->cd ($this->dn);
62       $attrs_to_search = $this->subAttributes;
63       $attrs_to_search[] = "FAIstate";
64       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))", $attrs_to_search);
66       while($object = $ldap->fetch()){
68        /* Skip objects, that are tagged as removed */
69         if(isset($object['FAIstate'][0])){
70           if(preg_match("/removed$/",$object['FAIstate'][0])){
71             continue;
72           }
73         }
75         /* Set status for save management */
76         $objects = array();
77         $objects['status']      = "FreshLoaded";
78         $objects['dn']          = $object['dn'];
79         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
80         $this->SubObjects[$objects['cn']] = $objects;
81       }
82     }
83     $this->ui = get_userinfo();
84   }
87   /* Reload some attributes */
88   function get_object_attributes($object,$attributes)
89   {
90     $ldap = $this->config->get_ldap_link();
91     $ldap->cd($this->config->current['BASE']);
92     $ldap->cat($object['dn'],$attributes);
93     $tmp  = $ldap->fetch();
95     foreach($attributes as $attrs){
96       if(isset($tmp[$attrs][0])){
97         $var = $tmp[$attrs][0];
99         /* Check if we must decode some attributes */
100         if(in_array_ics($attrs,$this->sub64coded)){
101           $var = base64_decode($var);
102         }
104         /*  check if this is a binary entry */
105         if(in_array_ics($attrs,$this->subBinary)){
106           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
107         }
109         /* Fix slashes */
110         $var = addslashes($var);
111         $object[$attrs] = $var;
112       }
113     }
114     return($object);
115   }
118   function acl_base_for_current_object($dn)
119   {
120     if($dn == "new"){
121       if($this->dn == "new"){
122         $dn = session::get('CurrentMainBase');
123       }else{
124         $dn = $this->dn;
125       }
126     }
127     return($dn);
128   }
131   function getUsedFAItask($cn)
132   {
133     $ret = array();
134     foreach($this->SubObjects as $name => $class){
135       if($class['cn'] == $cn){
136         continue;
137       } 
138       if($class['status'] != "delete"){
139         $ret[$class['FAItask']] = $class['FAItask'];
140       }
141     }
142     return($ret);
143   }
145   function execute()
146   {
147     /* Call parent execute */
148     plugin::execute();
150     if($this->is_account && !$this->view_logged){
151       $this->view_logged = TRUE;
152       new log("view","fai/".get_class($this),$this->dn);
153     }
155     /* Fill templating stuff */
156     $smarty= get_smarty();
157     $display= "";
159     /* New Listhandling
160      */
161     $once = true;
162     foreach($_POST as $name => $value){
163       if(preg_match("/^editscript_/",$name)&&($once)){
164         $once = false;
165         $entry = preg_replace("/^editscript_/","",$name);
166         $entry = base64_decode(preg_replace("/_.*/","",$entry));
168         $obj  = $this->SubObjects[$entry];
169         if($obj['status'] == "FreshLoaded"){
170           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
171         }
173         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
174         $this->dialog->set_acl_base($this->acl_base_for_current_object($obj['dn']));
175         $this->dialog->set_acl_category("fai");
177         session::set('objectinfo',$obj['dn']);
178         $this->dialog->parent = &$this;
179         $this->is_dialog=true;
180       }
181       if(preg_match("/^deletescript_/",$name)&&($once)){
182         $entry = preg_replace("/^deletescript_/","",$name);
183         $entry = base64_decode(preg_replace("/_.*/","",$entry));
185         $dn = $this->acl_base_for_current_object($this->SubObjects[$entry]['dn']);
186         $acl = $this->ui -> get_permissions($dn,"fai/faiScriptEntry")  ;
188         if(preg_match("/d/",$acl)){
189           $once = false;
191           $status = $this->SubObjects[$entry]['status'];
192           if($status == "edited" || $status == "FreshLoaded"){
193             $this->SubObjects[$entry]['status']= "delete";
194           }else{
195             unset($this->SubObjects[$entry]);
196           }
197         }
198       }
199     }
201     /* File download requested */
202     if(isset($_GET['getFAIHook'])){
203       if(isset($this->SubObjects[$_GET['getFAIHook']])){
204         $obj = $this->SubObjects[$_GET['getFAIHook']];
205         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
206         send_binary_content($obj['FAIscript'],$obj['cn'].".FAIhook");
207       }
208     }
210     /* Edit entries via GET */
211     if(isset($_GET['act']) && isset($_GET['id'])){
212       if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
213         $obj = $this->SubObjects[$_GET['id']];
214           if($obj['status'] == "FreshLoaded"){
215           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
216         }
217         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
218         $this->dialog->acl = $this->acl;
219         session::set('objectinfo',$obj['dn']);
220         $this->dialog->parent = &$this;
221         $this->is_dialog=true;
222       }
223     }
225     ///// Ende new list handling
227     /* Add new sub object */
228     if(isset($_POST['AddSubObject'])){
230       $c_dn = $this->acl_base_for_current_object($this->dn);
231       $this->dialog= new $this->subClassName($this->config,"new");
232       $this->dialog->set_acl_base($c_dn);
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           print_red($msg);
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($this->FAIstate == "freeze"){
324         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
325       }else{
326         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
327         if(preg_match("/d/",$acl)){
328           $act .="<input type='image' src='images/edittrash.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=".$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=".$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")) && $this->FAIstate!="freeze");
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"){
388         if($use_dns){
389           if((isset($obj['description']))&&(!empty($obj['description']))){
390             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
391           }else{
392             $a_return[$obj['cn']]['name']= $obj['cn'];
393           }
394           $a_return[$obj['cn']]['dn']= $obj['dn'];
395         }else{
396           if((isset($obj['description']))&&(!empty($obj['description']))){
397             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
398           }else{
399             $a_return[$obj['cn']]= $obj['cn'];
400           }
401         }
402       }
403     }
404     return($a_return);
405   }
408   /* Delete me, and all my subtrees
409    */
410   function remove_from_parent()
411   {
412     $ldap = $this->config->get_ldap_link();
413     $ldap->cd ($this->dn);
415     $faifilter = session::get('faifilter');
416     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
417     if($faifilter['branch'] == "main"){
418       $use_dn = $this->dn;
419     }
421     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
423     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
425     foreach($this->SubObjects as $name => $obj){
426       $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
427       if($faifilter['branch'] == "main"){
428         $use_dn = $obj['dn'];
429       }
430       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
431     }
432     $this->handle_post_events("remove");    
433   }
436   /* Save data to object 
437    */
438   function save_object()
439   {
440     if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
441       plugin::save_object();
442       foreach($this->attributes as $attrs){
443         if(isset($_POST[$attrs])){
444           $this->$attrs = $_POST[$attrs];
445         }
446       }
447     }
448   }
451   /* Check supplied data */
452   function check()
453   {
454     /* Call common method to give check the hook */
455     $message= plugin::check();
457     return ($message);
458   }
461   /* Save to LDAP */
462   function save()
463   {
464     plugin::save();
466     $ldap = $this->config->get_ldap_link();
468     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
469     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/hook with dn '%s' failed."),$this->dn));
471     if($this->initially_was_account){
472       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
473     }else{
474       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
475     }
477     $ldap->cd($this->dn);
479     /* Prepare FAIscriptEntry to write it to ldap
480      * First sort array.
481      *  Because we must delete old entries first.
482      * After deletion, we perform add and modify 
483      */
484     $Objects = array();
486     /* We do not need to save untouched objects */
487     foreach($this->SubObjects as $name => $obj){
488       if($obj['status'] == "FreshLoaded"){
489         unset($this->SubObjects[$name]);
490       }
491     }
493     foreach($this->SubObjects as $name => $obj){
494       if($obj['status'] == "delete"){
495         $Objects[$name] = $obj; 
496       }
497     }
498     foreach($this->SubObjects as $name => $obj){
499       if($obj['status'] != "delete"){
500         $Objects[$name] = $obj; 
501       }
502     }
504     foreach($Objects as $name => $obj){
506       foreach($this->sub64coded as $codeIt){
507         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
508       }
510       $tmp = array();
511       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
512       foreach($attributes as $attrs){
513         if(empty($obj[$attrs])){
514           $obj[$attrs] = array();
515         }
516         if(!is_array($obj[$attrs])){
517           $tmp[$attrs] = stripslashes($obj[$attrs]);
518         }else{
519           $tmp[$attrs] = $obj[$attrs];
520         }
521       }    
523       $tmp['objectClass'] = $this->subClasses;
525       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
527       if($obj['status']=="new"){
528         $ldap->cat($sub_dn,array("objectClass"));
529         if($ldap->count()){
530           $obj['status']="edited";
531         }
532       }
534       /* Tag object */
535       $this->tag_attrs(&$tmp, $sub_dn, $this->gosaUnitTag);
537       if($obj['status'] == "delete"){
538         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
539         $this->handle_post_events("remove");
540       }elseif($obj['status'] == "edited"){
541         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
542         $this->handle_post_events("modify");
543       }elseif($obj['status']=="new"){
544         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
545         $this->handle_post_events("add");
546       }
547     }
548   }
551   function PrepareForCopyPaste($source)
552   {
553     plugin::PrepareForCopyPaste($source);
555     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
556      */
557     $ldap     = $this->config->get_ldap_link();
558     $ldap->cd ($source['dn']);
560     $attrs_to_search = $this->subAttributes;
561     $attrs_to_search[] = "FAIstate";
562     $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
564     while($object = $ldap->fetch()){
566       /* Skip objects, that are tagged as removed */
567       if(isset($object['FAIstate'][0])){
568         if(preg_match("/removed$/",$object['FAIstate'][0])){
569           continue;
570         }
571       }
573       /* Set status for save management */
574       $objects = array();
575       $objects['status']      = "edited";
576       $objects['dn']          = $object['dn'];
577       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
578       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
580       $this->SubObjects[$objects['cn']] = $objects;
581     }
582   }
585   /* Return plugin informations for acl handling */ 
586   static function plInfo()
587   {
588     return (array( 
589           "plShortName" => _("Hook"),
590           "plDescription" => _("FAI hook"),
591           "plSelfModify"  => FALSE,
592           "plDepends"     => array(),
593           "plPriority"    => 20,
594           "plSection"     => array("administration"),
595           "plCategory"    => array("fai"),
596           "plProvidedAcls" => array(
597             "cn"                => _("Name")." ("._("Read only").")",
598             "description"       => _("Description"))
599           ));
600   }
603 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
604 ?>