Code

Updated templates
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiTemplate.inc
1 <?php
3 class faiTemplate 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","FAItemplate");
14   /* Class name of the Ldap ObjectClass for the Sub Object */
15   var $subClass         = "FAItemplateEntry";
16   var $subClasses       = array("top","FAIclass","FAItemplateEntry");
18   /* Class name of the php class which allows us to edit a Sub Object */
19   var $subClassName     = "faiTemplateEntry";      
21   /* Attributes to initialise for each subObject */
22   var $subAttributes    = array("cn","description","FAItemplatePath","FAIowner","FAImode");
23   var $sub_Load_Later   = array("FAItemplateFile"); 
24   var $sub64coded       = array();
25   var $subBinary        = array("FAItemplateFile");
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
33   var $FAIstate         = "";
34   var $ui;
35   var $view_logged      = FALSE;
37   function faiTemplate (&$config, $dn= NULL)
38   {
39     /* Load Attributes */
40     plugin::plugin ($config, $dn);
42     /* If "dn==new" we try to create a new entry
43      * Else we must read all objects from ldap which belong to this entry.
44      * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
45      */
46     if($dn != "new"){
47       $this->dn =$dn;
49       /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
50        */
51       $res = FAI::get_all_objects_for_given_base($this->dn,"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
52       foreach($res as $obj){
54         /* Skip not relevant objects */
55         if(!preg_match("/".preg_quote($this->dn, '/')."$/i",$obj['dn'])) continue;
57         $objects = array();
58         $objects['status']      = "FreshLoaded";
59         $objects['dn']          = $obj['dn'];
60         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
61         $this->SubObjects[$objects['cn']] = $objects;
62       }
63     }
64     $this->is_new = FALSE;
65     if($this->dn == "new"){
66       $this->is_new =TRUE;
67     }
68     $this->ui = get_userinfo();
69   }
72   /* Reload some attributes */
73   function get_object_attributes($object,$attributes)
74   {
75     $ldap = $this->config->get_ldap_link();
76     $ldap->cd($this->config->current['BASE']);
77     $ldap->cat($object['dn'],$attributes);
78     $tmp  = $ldap->fetch();
80     foreach($attributes as $attrs){
81       if(isset($tmp[$attrs][0])){
82         $var = $tmp[$attrs][0];
84         /* Check if we must decode some attributes */
85         if(in_array_ics($attrs,$this->sub64coded)){
86           $var = base64_decode($var);
87         }
88         $var = stripslashes($var);
90         /*  check if this is a binary entry */
91         if(in_array_ics($attrs,$this->subBinary)){
92           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
93         }
94   
95         $object[$attrs] = $var;
96       }
97     }
98     return($object);
99   }
102   function execute()
103   {
104     /* Call parent execute */
105     plugin::execute();
107     if($this->is_account && !$this->view_logged){
108       $this->view_logged = TRUE;
109       new log("view","fai/".get_class($this),$this->dn);
110     }
112     /* Fill templating stuff */
113     $smarty= get_smarty();
114     $display= "";
116     /* New Listhandling
117      */
118     $once = true;
119     foreach($_POST as $name => $value){
120       if(preg_match("/^editscript_/",$name)&&($once)){
121         $once = false;
122         $entry = preg_replace("/^editscript_/","",$name);
123         $entry = postDecode(preg_replace("/^editscript_/","",$name));
124         $obj  = $this->SubObjects[$entry];
126         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
127         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
128         if(preg_match("/r/",$acl)){
129           if($obj['status'] == "FreshLoaded"){
130             $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
131           }
132           $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
133           $this->dialog->set_acl_base($this->acl_base);
134           $this->dialog->FAIstate = $this->FAIstate;
135           $this->dialog->set_acl_category("fai");
137           set_object_info($obj['dn']);
138           $this->dialog->parent = &$this;
139           $this->is_dialog=true;
140         }
141       }
142       if(preg_match("/^deletescript_/",$name)&&($once) && !preg_match("/freeze/",$this->FAIstate)){
143         $once = false;
144         $entry = preg_replace("/^deletescript_/","",$name);
145         $entry = postDecode(preg_replace("/^deletescript_/","",$name));
146         $obj  = $this->SubObjects[$entry];
148         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
149         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
150         if(preg_match("/d/",$acl)){
151           $status = $this->SubObjects[$entry]['status'];
152           if($status == "edited" || $status == "FreshLoaded"){
153             $this->SubObjects[$entry]['status']= "delete";
154           }else{
155             unset($this->SubObjects[$entry]);
156           }
157         }
158       }
160       if(preg_match("/^download_/",$name)&&($once) && !preg_match("/freeze/",$this->FAIstate)){
161         $once = false;
162         $entry = postDecode(preg_replace("/^download_/","",$name));
163         $obj  = $this->SubObjects[$entry];
164         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
165         if(!isset($obj['cn'])){
166           trigger_error("Something wen't wrong here!");
167         }else{
168           send_binary_content($obj['FAItemplateFile'],$obj['cn'].".FAItemplate");
169         }
170       } 
171     }
173     /* Edit entries via GET */
174     if(isset($_GET['act']) && isset($_GET['id'])){
175       if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
176         $obj = $this->SubObjects[$_GET['id']];
177         if($obj['status'] == "FreshLoaded"){
178           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
179         }
180         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
181         $this->dialog->FAIstate = $this->FAIstate;
182         $this->dialog->acl = $this->acl;
183         $this->dialog->set_acl_category("fai");
184         set_object_info($obj['dn']);
185         $this->dialog->parent = &$this;
186         $this->is_dialog=true;
187       }
188     }
190     /* Add new sub object */
191     if(isset($_POST['AddSubObject']) && !preg_match("/freeze/",$this->FAIstate)){
192       $acl_dn = $this->acl_base_for_current_object($this->dn);
193       $acl    = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
195       if(preg_match("/c/",$acl)){
196         $this->dialog= new $this->subClassName($this->config,"new");
197         $this->dialog->set_acl_base($this->acl_base);
198         $this->dialog->FAIstate = $this->FAIstate;
199         $this->dialog->set_acl_category("fai");
200         $this->dialog->parent = &$this;
201         $this->is_dialog=true;
202       }
203     }
205     if($this->dn != "new"){
206       set_object_info($this->dn);
207     }
209     /* Save Dialog */
210     if(isset($_POST['SaveSubObject']) && is_object($this->dialog)){
211       $this->dialog->save_object();
212       $msgs = $this->dialog->check();
213       if(count($msgs)>0){
214         foreach($msgs as $msg){
215           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
216         }
217       }else{
218         /* Get return object */
219         $obj = $this->dialog->save();
220         if(isset($obj['remove'])){
222           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
224           /* Depending on status, set new status */
225           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
226             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
227           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
228             unset($this->SubObjects[$obj['remove']['from']]);
229           }
230           $obj['status'] = "new";
231           $this->SubObjects[$obj['remove']['to']] = $obj;
232           unset($this->SubObjects[$obj['remove']['to']]['remove']);
233         }else{
234           if($obj['status'] == "FreshLoaded"){
235             $obj['status'] = "edited";
236           }
237           $this->SubObjects[$obj['cn']]=$obj;
238         }
239         $this->is_dialog=false;
240         unset($this->dialog);
241         $this->dialog=FALSE;
242       }
243     }
245     /* Sort entries */
246     $tmp = $keys = array();
247     foreach($this->SubObjects as $key => $entry){
248       $keys[$key]=$key;
249     }
250     natcasesort($keys);
251     foreach($keys as $key){
252       $tmp[$key]=$this->SubObjects[$key];
253     }
254     $this->SubObjects = $tmp;
256     /* Cancel Dialog */
257     if(isset($_POST['CancelSubObject'])){
258       $this->is_dialog=false; 
259       unset($this->dialog);
260       $this->dialog=FALSE;
261     }
263     /* Print dialog if $this->dialog is set */
264     if(is_object($this->dialog)){
265       $this->dialog->save_object();
266       $display = $this->dialog->execute();
267       return($display);
268     }
270      /* Divlist Containing FAItemplates */
271     $divlist = new divSelectBox("FAItemplates");
272     $divlist->setHeight(400);
274     $tmp = $this->getList(true);
275   
276     /* Create div list with all sub entries listed */
277     foreach($this->SubObjects as $key => $name){
279       /* Skip removed entries */ 
280       if($name['status'] == "delete") continue;
282       /* Get permissions */
283       $dn  = $this->acl_base_for_current_object($name['dn']);
284       $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry")  ;
285       $act = "";
287       /* Hide delete icon if this object is freezed */
288       if(preg_match("/freeze/",$this->FAIstate)){
289         $act .= image('images/lists/edit.png','editscript_%s', msgPool::editButton());
290       }else{
291         $act .= image('images/lists/edit.png','editscript_%s', msgPool::editButton());
292         if(preg_match("/d/",$acl)){
293           $act .= image('images/lists/trash.png','deletescript_%s', msgPool::delButton());
294         }
295       }
297       /* Check acls for download icon */
298       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile")  ;
299       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
300         $down = "";
301       }else{
302         $down = image('images/save.png','download_'.postEncode($key), _("Download"));
303       }
305       /* Check if we are allowed to view this object */
306       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn")  ;
307       if(preg_match("/r/",$s_acl)){
309         $edit_link = "<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".$key."'>".$tmp[$key]."</a>";
310         $divlist->AddEntry(array( array("string"=> $edit_link), 
311               array("string"=>$down , "attach" => "style='width:20px;'"),
312               array("string"=>str_replace("%s",postEncode($key),$act),
313                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
314       }
315     }
316     $smarty->assign("Entry_divlist",$divlist->DrawList());
317     /* Divlist creation complete
318      */
320      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
321      * If we post the escaped strings they will be escaped again
322      */
323     foreach($this->attributes as $attrs){
324       if(get_magic_quotes_gpc()){
325         $smarty->assign($attrs,stripslashes($this->$attrs));
326       }else{
327         $smarty->assign($attrs,($this->$attrs));
328       }
329     }
331     $dn = $this->acl_base_for_current_object($this->dn);
332     $smarty->assign("sub_object_is_addable", 
333             preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) && 
334             !preg_match("/freeze/",$this->FAIstate));
336     foreach($this->attributes as $attr){
337       $smarty->assign($attr."ACL",$this->getacl($attr, preg_match("/freeze/",$this->FAIstate)));
338     }
340     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
341     return($display);
342   }
345   function acl_base_for_current_object($dn)
346   {
347     if($dn == "new" || $dn == ""){
348       if($this->dn == "new"){
349         $dn = $dn= $this->parent->parent->acl_base;
350       }else{
351         $dn = $this->dn;
352       }
353     }
354     return($dn);
355   }
358   /* Generate listbox friendly SubObject list
359   */
360   function getList(){
361     $a_return=array();
362     foreach($this->SubObjects as $obj){
363       if($obj['status'] != "delete"){
364       
365         if((isset($obj['description']))&&(!empty($obj['description']))){
366           if(strlen($obj['description']) > 40){
367             $obj['description'] = substr($obj['description'],0,40)."...";
368           }
369           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
370         }else{
371           $a_return[$obj['cn']]= $obj['cn'];
372         }
373       }
374     }
375     return($a_return);
376   }
378   /* Delete me, and all my subtrees
379    */
380   function remove_from_parent()
381   {
382     if($this->acl_is_removeable()){
383       $ldap = $this->config->get_ldap_link();
384       $ldap->cd ($this->dn);
385       $release = $this->parent->parent->fai_release;
386       $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn, '/'))."/i", $release, $this->dn);
387       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
388       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
390       foreach($this->SubObjects as $name => $obj){
391         $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $obj['dn']);
392         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
393       }
394       $this->handle_post_events("remove");
395     }
396   }
399   /* Save data to object 
400    */
401   function save_object()
402   {
403     if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
404       plugin::save_object();
405     }
406   }
409   /* Check supplied data */
410   function check()
411   {
412     /* Call common method to give check the hook */
413     $message= plugin::check();
415     /* Ensure that we do not overwrite an allready existing entry 
416      */
417     if($this->is_new){
418       $release = $this->parent->parent->fai_release;
419       $new_dn= 'cn='.$this->cn.",".get_ou('faiTemplateRDN').get_ou('faiBaseRDN').$release;
420       $res = faiManagement::check_class_name("FAItemplate",$this->cn,$new_dn);
421       if(isset($res[$this->cn])){
422         $message[] = msgPool::duplicated(_("Name"));
423       }
424     }
426     return ($message);
427   }
430   /* Save to LDAP */
431   function save()
432   {
433     plugin::save();
435     $ldap = $this->config->get_ldap_link();
437     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
439     if($this->initially_was_account){
440       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
441     }else{
442       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
443     }
445     /* Prepare FAIscriptEntry to write it to ldap
446      * First sort array.
447      *  Because we must delete old entries first.
448      * After deletion, we perform add and modify 
449      */
450     $Objects = array();
451     $tmp = $this->SubObjects;
452     foreach($tmp as $name => $obj){
453       if($obj['status'] == "FreshLoaded"){
454         unset($tmp[$name]);
455       }
456     }
458     foreach($tmp as $name => $obj){
459       if($obj['status'] == "delete"){
460         $Objects[$name] = $obj; 
461       }
462     }
463     foreach($tmp as $name => $obj){
464       if($obj['status'] != "delete"){
465         $Objects[$name] = $obj; 
466       }
467     }
469     foreach($Objects as $name => $obj){
471       foreach($this->sub64coded as $codeIt){
472         $obj[$codeIt]=base64_encode($obj[$codeIt]);
473       }
474       $tmp = array();
475       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
476       foreach($attributes as $attrs){
477         if(!isset($obj[$attrs]) || $obj[$attrs] == ""){
478           $obj[$attrs] = array();
479         }
480         $tmp[$attrs] =($obj[$attrs]);
481       }    
482         
483       $tmp['objectClass'] = $this->subClasses;
485       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
487       if($obj['status']=="new"){
488         $ldap->cat($sub_dn,array("objectClass"));
489         if($ldap->count()){
490           $obj['status']="edited";
491         }
492       }
494       /* Tag object */
495       $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
497       if($obj['status'] == "delete"){
498         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
499         $this->handle_post_events("remove");
500       }elseif($obj['status'] == "edited"){
501         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
502         $this->handle_post_events("modify");
503       }elseif($obj['status']=="new"){
504         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
505         $this->handle_post_events("add");
506       }
507     }
508   }
511   function PrepareForCopyPaste($source)
512   {
513     plugin::PrepareForCopyPaste($source);
515     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
516      */
517     $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
518     foreach($res as $obj){
520       /* Skip not relevant objects */
521       if(!preg_match("/".preg_quote($source['dn'], '/')."$/i",$obj['dn'])) continue;
523       $objects = array();
524       $objects['status']      = "edited";
525       $objects['dn']          = $obj['dn'];
526       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
527       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
528       $this->SubObjects[$objects['cn']] = $objects;
529     }
530   }
531   
533   /* Return plugin informations for acl handling */ 
534   static function plInfo()
535   {
536     return (array( 
537           "plShortName" => _("Template"),
538           "plDescription" => _("FAI template"),
539           "plSelfModify"  => FALSE,
540           "plDepends"     => array(),
541           "plPriority"    => 24,
542           "plSection"     => array("administration"),
543           "plCategory"    => array("fai"),
544           "plProvidedAcls" => array(
545             "cn"                => _("Name")." ("._("Readonly").")",
546             "description"       => _("Description"))
547           ));
548   }
551   /*! \brief  Used for copy & paste.
552     Returns a HTML input mask, which allows to change the cn of this entry.
553     @param  Array   Array containing current status && a HTML template.
554    */
555   function getCopyDialog()
556   {
557     $vars = array("cn");
558     $smarty = get_smarty();
559     $smarty->assign("cn", htmlentities($this->cn));
560     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
561     $ret = array();
562     $ret['string'] = $str;
563     $ret['status'] = "";
564     return($ret);
565   }
568   /*! \brief  Used for copy & paste.
569     Some entries must be renamed to avaoid duplicate entries.
570    */
571   function saveCopyDialog()
572   {
573     if(isset($_POST['cn'])){
574       $this->cn = get_post('cn');
575     }
576   }
579 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
580 ?>