Code

Reverted Last commits, I've accidentally replaced to much.
[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();
70     // Prepare lists
71     $this->templateList = new sortableListing();
72     $this->templateList->setDeleteable(true);
73     $this->templateList->setEditable(true);
74     $this->templateList->setWidth("100%");
75     $this->templateList->setHeight("120px");
76     $this->templateList->setColspecs(array('200px','*'));
77     $this->templateList->setHeader(array(_("Name"),_("Download")));
78     $this->templateList->setDefaultSortColumn(1);
80   }
83   /* Reload some attributes */
84   function get_object_attributes($object,$attributes)
85   {
86     $ldap = $this->config->get_ldap_link();
87     $ldap->cd($this->config->current['BASE']);
88     $ldap->cat($object['dn'],$attributes);
89     $tmp  = $ldap->fetch();
91     foreach($attributes as $attrs){
92       if(isset($tmp[$attrs][0])){
93         $var = $tmp[$attrs][0];
95         /* Check if we must decode some attributes */
96         if(in_array_ics($attrs,$this->sub64coded)){
97           $var = base64_decode($var);
98         }
99         $var = stripslashes($var);
101         /*  check if this is a binary entry */
102         if(in_array_ics($attrs,$this->subBinary)){
103           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
104         }
105   
106         $object[$attrs] = $var;
107       }
108     }
109     return($object);
110   }
113   function execute()
114   {
115     /* Call parent execute */
116     plugin::execute();
118     if($this->is_account && !$this->view_logged){
119       $this->view_logged = TRUE;
120       new log("view","fai/".get_class($this),$this->dn);
121     }
123     /* Fill templating stuff */
124     $smarty= get_smarty();
125     $display= "";
127     /* New Listhandling
128      */
129     $this->templateList->save_object();
130     $action = $this->templateList->getAction();
131     if($action['action'] == 'delete'){
132         $id = $this->templateList->getKey($action['targets'][0]);
133         $obj  = $this->SubObjects[$id];
134         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
135         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
136         if(preg_match("/d/",$acl)){
137             $status = $this->SubObjects[$id]['status'];
138             if($status == "edited" || $status == "FreshLoaded"){
139                 $this->SubObjects[$id]['status']= "delete";
140             }else{
141                 unset($this->SubObjects[$id]);
142             }
143         }
144     }
145     if($action['action'] == 'edit'){
146         $id = $this->templateList->getKey($action['targets'][0]);
147         $obj  = $this->SubObjects[$id];
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("/r/",$acl)){
151             if($obj['status'] == "FreshLoaded"){
152                 $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
153             }
154             $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
155             $this->dialog->set_acl_base($this->acl_base);
156             $this->dialog->FAIstate = $this->FAIstate;
157             $this->dialog->set_acl_category("fai");
159             set_object_info($obj['dn']);
160             $this->dialog->parent = &$this;
161             $this->is_dialog=true;
162         }
163     }
165     $once = true;
166     foreach($_POST as $name => $value){
167       if(preg_match("/^download_/",$name)&&($once) && !preg_match("/freeze/",$this->FAIstate)){
168         $once = false;
169         $entry = postDecode(preg_replace("/^download_/","",$name));
170         $obj  = $this->SubObjects[$entry];
171         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
172         if(!isset($obj['cn'])){
173           trigger_error("Something wen't wrong here!");
174         }else{
175           send_binary_content($obj['FAItemplateFile'],$obj['cn'].".FAItemplate");
176         }
177       } 
178     }
180     /* Edit entries via GET */
181     if(isset($_GET['act']) && isset($_GET['id'])){
182       if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
183         $obj = $this->SubObjects[$_GET['id']];
184         if($obj['status'] == "FreshLoaded"){
185           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
186         }
187         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
188         $this->dialog->FAIstate = $this->FAIstate;
189         $this->dialog->acl = $this->acl;
190         $this->dialog->set_acl_category("fai");
191         set_object_info($obj['dn']);
192         $this->dialog->parent = &$this;
193         $this->is_dialog=true;
194       }
195     }
197     /* Add new sub object */
198     if(isset($_POST['AddSubObject']) && !preg_match("/freeze/",$this->FAIstate)){
199       $acl_dn = $this->acl_base_for_current_object($this->dn);
200       $acl    = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
202       if(preg_match("/c/",$acl)){
203         $this->dialog= new $this->subClassName($this->config,"new");
204         $this->dialog->set_acl_base($this->acl_base);
205         $this->dialog->FAIstate = $this->FAIstate;
206         $this->dialog->set_acl_category("fai");
207         $this->dialog->parent = &$this;
208         $this->is_dialog=true;
209       }
210     }
212     if($this->dn != "new"){
213       set_object_info($this->dn);
214     }
216     /* Save Dialog */
217     if(isset($_POST['SaveSubObject']) && is_object($this->dialog)){
218       $this->dialog->save_object();
219       $msgs = $this->dialog->check();
220       if(count($msgs)>0){
221         foreach($msgs as $msg){
222           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
223         }
224       }else{
225         /* Get return object */
226         $obj = $this->dialog->save();
227         if(isset($obj['remove'])){
229           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
231           /* Depending on status, set new status */
232           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
233             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
234           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
235             unset($this->SubObjects[$obj['remove']['from']]);
236           }
237           $obj['status'] = "new";
238           $this->SubObjects[$obj['remove']['to']] = $obj;
239           unset($this->SubObjects[$obj['remove']['to']]['remove']);
240         }else{
241           if($obj['status'] == "FreshLoaded"){
242             $obj['status'] = "edited";
243           }
244           $this->SubObjects[$obj['cn']]=$obj;
245         }
246         $this->is_dialog=false;
247         unset($this->dialog);
248         $this->dialog=FALSE;
249       }
250     }
252     /* Sort entries */
253     $tmp = $keys = array();
254     foreach($this->SubObjects as $key => $entry){
255       $keys[$key]=$key;
256     }
257     natcasesort($keys);
258     foreach($keys as $key){
259       $tmp[$key]=$this->SubObjects[$key];
260     }
261     $this->SubObjects = $tmp;
263     /* Cancel Dialog */
264     if(isset($_POST['CancelSubObject'])){
265       $this->is_dialog=false; 
266       unset($this->dialog);
267       $this->dialog=FALSE;
268     }
270     /* Print dialog if $this->dialog is set */
271     if(is_object($this->dialog)){
272       $this->dialog->save_object();
273       $display = $this->dialog->execute();
274       return($display);
275     }
277      /* list Containing FAItemplates */
278     $dn  = $this->acl_base_for_current_object($this->dn);
279     $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry")  ;
280     $this->templateList->setAcl($acl,preg_match('/^freeze/', $this->FAIstate));
282     $tmp = $this->getList(true);
283     foreach($this->SubObjects as $key => $name){
285       /* Skip removed entries */ 
286       if($name['status'] == "delete") continue;
288       $act = "";
290       /* Check acls for download icon */
291       $dn  = $this->acl_base_for_current_object($name['dn']);
292       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile")  ;
293       if(($this->SubObjects[$key]['status'] == "new") || 
294               ($this->SubObjects[$key]['dn'] == "new") || 
295               !preg_match("/r/",$s_acl)){
296         $down = "";
297       }else{
298         $down = image('images/save.png','download_'.postEncode($key), _("Download"));
299       }
300       $data[$key] = $key;
301       $lData[$key] = array('data' => array($name['cn'], $down));
302     }
304     $this->templateList->setListData($data,$lData);
305     $this->templateList->update();
306     $smarty->assign("Entry_listing",$this->templateList->render());
308      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
309      * If we post the escaped strings they will be escaped again
310      */
311     foreach($this->attributes as $attrs){
312       if(get_magic_quotes_gpc()){
313         $smarty->assign($attrs,stripslashes($this->$attrs));
314       }else{
315         $smarty->assign($attrs,($this->$attrs));
316       }
317     }
319     $dn = $this->acl_base_for_current_object($this->dn);
320     $smarty->assign("sub_object_is_addable", 
321             preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) && 
322             !preg_match("/freeze/",$this->FAIstate));
324     foreach($this->attributes as $attr){
325       $smarty->assign($attr."ACL",$this->getacl($attr, preg_match("/freeze/",$this->FAIstate)));
326     }
328     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
329     return($display);
330   }
333   function acl_base_for_current_object($dn)
334   {
335     if($dn == "new" || $dn == ""){
336       if($this->dn == "new"){
337         $dn = $dn= $this->parent->parent->acl_base;
338       }else{
339         $dn = $this->dn;
340       }
341     }
342     return($dn);
343   }
346   /* Generate listbox friendly SubObject list
347   */
348   function getList(){
349     $a_return=array();
350     foreach($this->SubObjects as $obj){
351       if($obj['status'] != "delete"){
352       
353         if((isset($obj['description']))&&(!empty($obj['description']))){
354           if(strlen($obj['description']) > 40){
355             $obj['description'] = substr($obj['description'],0,40)."...";
356           }
357           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
358         }else{
359           $a_return[$obj['cn']]= $obj['cn'];
360         }
361       }
362     }
363     return($a_return);
364   }
366   /* Delete me, and all my subtrees
367    */
368   function remove_from_parent()
369   {
370     if($this->acl_is_removeable()){
371       $ldap = $this->config->get_ldap_link();
372       $ldap->cd ($this->dn);
373       $release = $this->parent->parent->fai_release;
374       $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn, '/'))."/i", $release, $this->dn);
375       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
376       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
378       foreach($this->SubObjects as $name => $obj){
379         $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $obj['dn']);
380         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
381       }
382       $this->handle_post_events("remove");
383     }
384   }
387   /* Save data to object 
388    */
389   function save_object()
390   {
391     if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
392       plugin::save_object();
393     }
394   }
397   /* Check supplied data */
398   function check()
399   {
400     /* Call common method to give check the hook */
401     $message= plugin::check();
403     /* Ensure that we do not overwrite an allready existing entry 
404      */
405     if($this->is_new){
406       $release = $this->parent->parent->fai_release;
407       $new_dn= 'cn='.$this->cn.",".get_ou('faiTemplateRDN').get_ou('faiBaseRDN').$release;
408       $res = faiManagement::check_class_name("FAItemplate",$this->cn,$new_dn);
409       if(isset($res[$this->cn])){
410         $message[] = msgPool::duplicated(_("Name"));
411       }
412     }
414     return ($message);
415   }
418   /* Save to LDAP */
419   function save()
420   {
421     plugin::save();
423     $ldap = $this->config->get_ldap_link();
425     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
427     if($this->initially_was_account){
428       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
429     }else{
430       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
431     }
433     /* Prepare FAIscriptEntry to write it to ldap
434      * First sort array.
435      *  Because we must delete old entries first.
436      * After deletion, we perform add and modify 
437      */
438     $Objects = array();
439     $tmp = $this->SubObjects;
440     foreach($tmp as $name => $obj){
441       if($obj['status'] == "FreshLoaded"){
442         unset($tmp[$name]);
443       }
444     }
446     foreach($tmp as $name => $obj){
447       if($obj['status'] == "delete"){
448         $Objects[$name] = $obj; 
449       }
450     }
451     foreach($tmp as $name => $obj){
452       if($obj['status'] != "delete"){
453         $Objects[$name] = $obj; 
454       }
455     }
457     foreach($Objects as $name => $obj){
459       foreach($this->sub64coded as $codeIt){
460         $obj[$codeIt]=base64_encode($obj[$codeIt]);
461       }
462       $tmp = array();
463       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
464       foreach($attributes as $attrs){
465         if(!isset($obj[$attrs]) || $obj[$attrs] == ""){
466           $obj[$attrs] = array();
467         }
468         $tmp[$attrs] =($obj[$attrs]);
469       }    
470         
471       $tmp['objectClass'] = $this->subClasses;
473       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
475       if($obj['status']=="new"){
476         $ldap->cat($sub_dn,array("objectClass"));
477         if($ldap->count()){
478           $obj['status']="edited";
479         }
480       }
482       /* Tag object */
483       $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
485       if($obj['status'] == "delete"){
486         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
487         $this->handle_post_events("remove");
488       }elseif($obj['status'] == "edited"){
489         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
490         $this->handle_post_events("modify");
491       }elseif($obj['status']=="new"){
492         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
493         $this->handle_post_events("add");
494       }
495     }
496   }
499   function PrepareForCopyPaste($source)
500   {
501     plugin::PrepareForCopyPaste($source);
503     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
504      */
505     $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
506     foreach($res as $obj){
508       /* Skip not relevant objects */
509       if(!preg_match("/".preg_quote($source['dn'], '/')."$/i",$obj['dn'])) continue;
511       $objects = array();
512       $objects['status']      = "edited";
513       $objects['dn']          = $obj['dn'];
514       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
515       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
516       $this->SubObjects[$objects['cn']] = $objects;
517     }
518   }
519   
521   /* Return plugin informations for acl handling */ 
522   static function plInfo()
523   {
524     return (array( 
525           "plShortName" => _("Template"),
526           "plDescription" => _("FAI template"),
527           "plSelfModify"  => FALSE,
528           "plDepends"     => array(),
529           "plPriority"    => 24,
530           "plSection"     => array("administration"),
531           "plCategory"    => array("fai"),
532           "plProperties" =>
533           array(
534               array(
535                   "name"          => "faiTemplateRDN",
536                   "type"          => "rdn",
537                   "default"       => "ou=templates,",
538                   "description"   => "The 'faiTemplateRDN' statement defines the location where new fai-templates will be created. The default is 'ou=templates,'.",
539                   "check"         => "gosaProperty::isRdn",
540                   "migrate"       => "",
541                   "group"         => "plugin",
542                   "mandatory"     => FALSE)),
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 ?>