Code

fb64e5d3c56c4e18dc73e245477d5335459b6bcb
[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     /* Edit entries via GET */
202     if(isset($_GET['act']) && isset($_GET['id'])){
203       if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
204         $obj = $this->SubObjects[$_GET['id']];
205           if($obj['status'] == "FreshLoaded"){
206           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
207         }
208         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
209         $this->dialog->acl = $this->acl;
210         session::set('objectinfo',$obj['dn']);
211         $this->dialog->parent = &$this;
212         $this->is_dialog=true;
213       }
214     }
216     ///// Ende new list handling
218     /* Add new sub object */
219     if(isset($_POST['AddSubObject'])){
221       $c_dn = $this->acl_base_for_current_object($this->dn);
222       $this->dialog= new $this->subClassName($this->config,"new");
223       $this->dialog->set_acl_base($c_dn);
224       $this->dialog->set_acl_category("fai");
225       $this->dialog->parent = &$this;
226       $this->is_dialog=true;
227     }
230     /* Save Dialog */
231     if(isset($_POST['SaveSubObject'])){
233       /* Perform post check*/
234       $this->dialog->save_object();
236       /* Get messages */
237       $msgs = $this->dialog->check();
239       /* print errors */
240       if(count($msgs)>0){
241         foreach($msgs as $msg){
242           print_red($msg);
243         }
244       }else{
246         /* Get return object */
247         $obj = $this->dialog->save();
248         if(isset($obj['remove'])){
250           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
251   
252           /* Depending on status, set new status */
253           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
254             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
255           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
256             unset($this->SubObjects[$obj['remove']['from']]);
257           }
258           $obj['status'] = "new";
259           $this->SubObjects[$obj['remove']['to']] = $obj;
260           unset($this->SubObjects[$obj['remove']['to']]['remove']);
261         }else{
262           if($obj['status'] == "FreshLoaded"){
263             $obj['status'] = "edited";
264           }
265           $this->SubObjects[$obj['cn']]=$obj;
266         }
267         $this->is_dialog=false;
268         unset($this->dialog);
269         $this->dialog=FALSE;
270       }
271     }
273     /* Sort entries */
274     $tmp = $keys = array();
275     foreach($this->SubObjects as $key => $entry){
276       $keys[$key]=$key;
277     }
278     natcasesort($keys);
279     foreach($keys as $key){
280       $tmp[$key]=$this->SubObjects[$key];
281     }
282     $this->SubObjects = $tmp;
284     /* Cancel Dialog */
285     if(isset($_POST['CancelSubObject'])){
286       $this->is_dialog=false; 
287       unset($this->dialog);
288       $this->dialog=FALSE;
289     }
291     /* Print dialog if $this->dialog is set */
292     if(is_object($this->dialog)){
293       $this->dialog->save_object();
294       $display = $this->dialog->execute();
295       return($display);
296     }
300     /* Divlist            added 28.02.2006
301        Containing FAIscripts
302      */
304     $divlist = new divSelectBox("FAIhooks");
305     $divlist->setHeight(400);
307     foreach($this->getList(true) as $key => $name){
309       $dn= $this->acl_base_for_current_object($name['dn']);
310       $acl = $this->ui->get_permissions($dn,"fai/faiHookEntry")  ;
311       $act = "";
313       /* Check if this object is freezed, in this case hide the delete icon */
314       if($this->FAIstate == "freeze"){
315         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
316       }else{
317         $act .= "<input type='image' src='images/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
318         if(preg_match("/d/",$acl)){
319           $act .="<input type='image' src='images/edittrash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
320         }
321       }
323       /* Check if we are allowed to use the export button for this object */
324       $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","FAIscript")  ;
325       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
326         $down = "";
327       }else{
328         $down = "<a href='getFAIscript.php?id=".base64_encode($name['dn'])."'>
329           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
330           </a>";
331       }
333       /* Check if we are allowed to view the object */
334       $s_acl = $this->ui->get_permissions($dn,"fai/faiHookEntry","cn")  ;
335       if(preg_match("/r/",$s_acl)){
337         $edit_link = "<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".$key."'>".$name['name']."</a>";
338         $divlist->AddEntry(array( array("string"=>$edit_link),
339               array("string"=>$down , "attach" => "style='width:20px;'"),
340               array("string"=>str_replace("%s",base64_encode($key),$act),
341                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
342       }
343     }
344     $smarty->assign("Entry_divlist",$divlist->DrawList());
345     /* Divlist creation complete
346      */
348     $smarty->assign("SubObjects",$this->getList());
350     /* Magic quotes GPC, escapes every ' " \, to solve some security risks
351      * If we post the escaped strings they will be escaped again
352      */
353     foreach($this->attributes as $attrs){
354       if(get_magic_quotes_gpc()){
355         $smarty->assign($attrs,stripslashes($this->$attrs));
356       }else{
357         $smarty->assign($attrs,($this->$attrs));
358       }
359     }
361     $tmp = $this->plInfo();
362       
363     $c_dn = $this->acl_base_for_current_object($this->dn);
364     $smarty->assign("sub_object_is_addable", preg_match("/c/",$this->ui->get_permissions($c_dn,"fai/faiHookEntry")) && $this->FAIstate!="freeze");
365     foreach($tmp['plProvidedAcls'] as $name => $translation){
366       $smarty->assign($name."ACL",$this->getacl($name));
367     }
369     $display.= $smarty->fetch(get_template_path('faiHook.tpl', TRUE));
370     return($display);
371   }
373   /* Generate listbox friendly SubObject list
374    */
375   function getList($use_dns=false){
376     $a_return=array();
377     foreach($this->SubObjects as $obj){
378       if($obj['status'] != "delete"){
379         if($use_dns){
380           if((isset($obj['description']))&&(!empty($obj['description']))){
381             $a_return[$obj['cn']]['name']= $obj['cn']." [".stripslashes($obj['description'])."]";
382           }else{
383             $a_return[$obj['cn']]['name']= $obj['cn'];
384           }
385           $a_return[$obj['cn']]['dn']= $obj['dn'];
386         }else{
387           if((isset($obj['description']))&&(!empty($obj['description']))){
388             $a_return[$obj['cn']]= $obj['cn']." [".stripslashes($obj['description'])."]";
389           }else{
390             $a_return[$obj['cn']]= $obj['cn'];
391           }
392         }
393       }
394     }
395     return($a_return);
396   }
399   /* Delete me, and all my subtrees
400    */
401   function remove_from_parent()
402   {
403     $ldap = $this->config->get_ldap_link();
404     $ldap->cd ($this->dn);
406     $faifilter = session::get('faifilter');
407     $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $this->dn);
408     if($faifilter['branch'] == "main"){
409       $use_dn = $this->dn;
410     }
412     FAI::prepare_to_save_FAI_object($use_dn,array(),true);
414     new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
416     foreach($this->SubObjects as $name => $obj){
417       $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $faifilter['branch'], $obj['dn']);
418       if($faifilter['branch'] == "main"){
419         $use_dn = $obj['dn'];
420       }
421       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
422     }
423     $this->handle_post_events("remove");    
424   }
427   /* Save data to object 
428    */
429   function save_object()
430   {
431     if((isset($_POST['FAIhook_posted'])) && ($this->FAIstate != "freeze")){
432       plugin::save_object();
433       foreach($this->attributes as $attrs){
434         if(isset($_POST[$attrs])){
435           $this->$attrs = $_POST[$attrs];
436         }
437       }
438     }
439   }
442   /* Check supplied data */
443   function check()
444   {
445     /* Call common method to give check the hook */
446     $message= plugin::check();
448     return ($message);
449   }
452   /* Save to LDAP */
453   function save()
454   {
455     plugin::save();
457     $ldap = $this->config->get_ldap_link();
459     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
460     show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/hook with dn '%s' failed."),$this->dn));
462     if($this->initially_was_account){
463       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
464     }else{
465       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
466     }
468     $ldap->cd($this->dn);
470     /* Prepare FAIscriptEntry to write it to ldap
471      * First sort array.
472      *  Because we must delete old entries first.
473      * After deletion, we perform add and modify 
474      */
475     $Objects = array();
477     /* We do not need to save untouched objects */
478     foreach($this->SubObjects as $name => $obj){
479       if($obj['status'] == "FreshLoaded"){
480         unset($this->SubObjects[$name]);
481       }
482     }
484     foreach($this->SubObjects as $name => $obj){
485       if($obj['status'] == "delete"){
486         $Objects[$name] = $obj; 
487       }
488     }
489     foreach($this->SubObjects as $name => $obj){
490       if($obj['status'] != "delete"){
491         $Objects[$name] = $obj; 
492       }
493     }
495     foreach($Objects as $name => $obj){
497       foreach($this->sub64coded as $codeIt){
498         $obj[$codeIt]=base64_encode(stripslashes($obj[$codeIt]));
499       }
501       $tmp = array();
502       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
503       foreach($attributes as $attrs){
504         if(empty($obj[$attrs])){
505           $obj[$attrs] = array();
506         }
507         if(!is_array($obj[$attrs])){
508           $tmp[$attrs] = stripslashes($obj[$attrs]);
509         }else{
510           $tmp[$attrs] = $obj[$attrs];
511         }
512       }    
514       $tmp['objectClass'] = $this->subClasses;
516       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
518       if($obj['status']=="new"){
519         $ldap->cat($sub_dn,array("objectClass"));
520         if($ldap->count()){
521           $obj['status']="edited";
522         }
523       }
525       /* Tag object */
526       $this->tag_attrs(&$tmp, $sub_dn, $this->gosaUnitTag);
528       if($obj['status'] == "delete"){
529         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
530         $this->handle_post_events("remove");
531       }elseif($obj['status'] == "edited"){
532         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
533         $this->handle_post_events("modify");
534       }elseif($obj['status']=="new"){
535         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
536         $this->handle_post_events("add");
537       }
538     }
539   }
542   function PrepareForCopyPaste($source)
543   {
544     plugin::PrepareForCopyPaste($source);
546     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
547      */
548     $ldap     = $this->config->get_ldap_link();
549     $ldap->cd ($source['dn']);
551     $attrs_to_search = $this->subAttributes;
552     $attrs_to_search[] = "FAIstate";
553     $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
555     while($object = $ldap->fetch()){
557       /* Skip objects, that are tagged as removed */
558       if(isset($object['FAIstate'][0])){
559         if(preg_match("/removed$/",$object['FAIstate'][0])){
560           continue;
561         }
562       }
564       /* Set status for save management */
565       $objects = array();
566       $objects['status']      = "edited";
567       $objects['dn']          = $object['dn'];
568       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
569       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
571       $this->SubObjects[$objects['cn']] = $objects;
572     }
573   }
576   /* Return plugin informations for acl handling */ 
577   static function plInfo()
578   {
579     return (array( 
580           "plShortName" => _("Hook"),
581           "plDescription" => _("FAI hook"),
582           "plSelfModify"  => FALSE,
583           "plDepends"     => array(),
584           "plPriority"    => 20,
585           "plSection"     => array("administration"),
586           "plCategory"    => array("fai"),
587           "plProvidedAcls" => array(
588             "cn"                => _("Name")." ("._("Read only").")",
589             "description"       => _("Description"))
590           ));
591   }
594 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
595 ?>