Code

Fixed removal of template and profile entries.
[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->setInstantDelete(false);
73     $this->templateList->setDeleteable(true);
74     $this->templateList->setEditable(true);
75     $this->templateList->setWidth("100%");
76     $this->templateList->setHeight("220px");
77     $this->templateList->setColspecs(array('*','18px'));
78     $this->templateList->setHeader(array(_("Name"),""));
79     $this->templateList->setDefaultSortColumn(0);
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         }
100         /*  check if this is a binary entry */
101         if(in_array_ics($attrs,$this->subBinary)){
102           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
103         }
104   
105         $object[$attrs] = $var;
106       }
107     }
108     return($object);
109   }
112   function execute()
113   {
114     /* Call parent execute */
115     plugin::execute();
117     if($this->is_account && !$this->view_logged){
118       $this->view_logged = TRUE;
119       new log("view","fai/".get_class($this),$this->dn);
120     }
122     /* Fill templating stuff */
123     $smarty= get_smarty();
124     $display= "";
126     /* New Listhandling
127      */
128     $this->templateList->save_object();
129     $action = $this->templateList->getAction();
130     if($action['action'] == 'delete'){
131         $id = $this->templateList->getKey($action['targets'][0]);
132         $obj  = $this->SubObjects[$id];
133         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
134         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
135         if(preg_match("/d/",$acl)){
136             $status = $this->SubObjects[$id]['status'];
137             if($status == "edited" || $status == "FreshLoaded"){
138                 $this->SubObjects[$id]['status']= "delete";
139             }else{
140                 unset($this->SubObjects[$id]);
141             }
142         }
143     }
144     if($action['action'] == 'edit'){
145         $id = $this->templateList->getKey($action['targets'][0]);
146         $obj  = $this->SubObjects[$id];
147         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
148         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
149         if(preg_match("/r/",$acl)){
150             if($obj['status'] == "FreshLoaded"){
151                 $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
152             }
153             $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
154             $this->dialog->set_acl_base($this->acl_base);
155             $this->dialog->FAIstate = $this->FAIstate;
156             $this->dialog->set_acl_category("fai");
158             set_object_info($obj['dn']);
159             $this->dialog->parent = &$this;
160             $this->is_dialog=true;
161         }
162     }
164     $once = true;
165     foreach($_POST as $name => $value){
166       if(preg_match("/^download_/",$name)&&($once) && !preg_match("/freeze/",$this->FAIstate)){
167         $once = false;
168         $entry = postDecode(preg_replace("/^download_/","",$name));
169         $obj  = $this->SubObjects[$entry];
170         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
171         if(!isset($obj['cn'])){
172           trigger_error("Something wen't wrong here!");
173         }else{
174           send_binary_content($obj['FAItemplateFile'],$obj['cn'].".FAItemplate");
175         }
176       } 
177     }
179     /* Edit entries via GET */
180     if(isset($_GET['act']) && isset($_GET['id'])){
181       if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
182         $obj = $this->SubObjects[$_GET['id']];
183         if($obj['status'] == "FreshLoaded"){
184           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
185         }
186         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
187         $this->dialog->FAIstate = $this->FAIstate;
188         $this->dialog->acl = $this->acl;
189         $this->dialog->set_acl_category("fai");
190         set_object_info($obj['dn']);
191         $this->dialog->parent = &$this;
192         $this->is_dialog=true;
193       }
194     }
196     /* Add new sub object */
197     if(isset($_POST['AddSubObject']) && !preg_match("/freeze/",$this->FAIstate)){
198       $acl_dn = $this->acl_base_for_current_object($this->dn);
199       $acl    = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
201       if(preg_match("/c/",$acl)){
202         $this->dialog= new $this->subClassName($this->config,"new");
203         $this->dialog->set_acl_base($this->acl_base);
204         $this->dialog->FAIstate = $this->FAIstate;
205         $this->dialog->set_acl_category("fai");
206         $this->dialog->parent = &$this;
207         $this->is_dialog=true;
208       }
209     }
211     if($this->dn != "new"){
212       set_object_info($this->dn);
213     }
215     /* Save Dialog */
216     if(isset($_POST['SaveSubObject']) && is_object($this->dialog)){
217       $this->dialog->save_object();
218       $msgs = $this->dialog->check();
219       if(count($msgs)>0){
220         foreach($msgs as $msg){
221           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
222         }
223       }else{
224         /* Get return object */
225         $obj = $this->dialog->save();
226         if(isset($obj['remove'])){
228           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
230           /* Depending on status, set new status */
231           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
232             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
233           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
234             unset($this->SubObjects[$obj['remove']['from']]);
235           }
236           $obj['status'] = "new";
237           $this->SubObjects[$obj['remove']['to']] = $obj;
238           unset($this->SubObjects[$obj['remove']['to']]['remove']);
239         }else{
240           if($obj['status'] == "FreshLoaded"){
241             $obj['status'] = "edited";
242           }
243           $this->SubObjects[$obj['cn']]=$obj;
244         }
245         $this->is_dialog=false;
246         unset($this->dialog);
247         $this->dialog=FALSE;
248       }
249     }
251     /* Sort entries */
252     $tmp = $keys = array();
253     foreach($this->SubObjects as $key => $entry){
254       $keys[$key]=$key;
255     }
256     natcasesort($keys);
257     foreach($keys as $key){
258       $tmp[$key]=$this->SubObjects[$key];
259     }
260     $this->SubObjects = $tmp;
262     /* Cancel Dialog */
263     if(isset($_POST['CancelSubObject'])){
264       $this->is_dialog=false; 
265       unset($this->dialog);
266       $this->dialog=FALSE;
267     }
269     /* Print dialog if $this->dialog is set */
270     if(is_object($this->dialog)){
271       $this->dialog->save_object();
272       $display = $this->dialog->execute();
273       return($display);
274     }
276      /* list Containing FAItemplates */
277     $dn  = $this->acl_base_for_current_object($this->dn);
278     $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry")  ;
279     $this->templateList->setAcl($acl,preg_match('/^freeze/', $this->FAIstate));
281     $tmp = $this->getList(true);
282     $data = $lData = array();
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       $smarty->assign($attrs,set_post($this->$attrs));
313     }
315     $dn = $this->acl_base_for_current_object($this->dn);
316     $smarty->assign("sub_object_is_addable", 
317             preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) && 
318             !preg_match("/freeze/",$this->FAIstate));
320     foreach($this->attributes as $attr){
321       $smarty->assign($attr."ACL",$this->getacl($attr, preg_match("/freeze/",$this->FAIstate)));
322     }
324     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
325     return($display);
326   }
329   function acl_base_for_current_object($dn)
330   {
331     if($dn == "new" || $dn == ""){
332       if($this->dn == "new"){
333         $dn = $dn= $this->parent->parent->acl_base;
334       }else{
335         $dn = $this->dn;
336       }
337     }
338     return($dn);
339   }
342   /* Generate listbox friendly SubObject list
343   */
344   function getList(){
345     $a_return=array();
346     foreach($this->SubObjects as $obj){
347       if($obj['status'] != "delete"){
348       
349         if((isset($obj['description']))&&(!empty($obj['description']))){
350           if(strlen($obj['description']) > 40){
351             $obj['description'] = substr($obj['description'],0,40)."...";
352           }
353           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
354         }else{
355           $a_return[$obj['cn']]= $obj['cn'];
356         }
357       }
358     }
359     return($a_return);
360   }
362   /* Delete me, and all my subtrees
363    */
364   function remove_from_parent()
365   {
366     if($this->acl_is_removeable()){
367       $ldap = $this->config->get_ldap_link();
368       $ldap->cd ($this->dn);
369       $release = $this->parent->parent->fai_release;
370       $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn, '/'))."/i", $release, $this->dn);
371       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
372       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
374       foreach($this->SubObjects as $name => $obj){
375         $use_dn = preg_replace("/".preg_quote(FAI::get_release_dn($this->dn), '/')."/i", $release, $obj['dn']);
376         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
377       }
378       $this->handle_post_events("remove");
379     }
380   }
383   /* Save data to object 
384    */
385   function save_object()
386   {
387     if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
388       plugin::save_object();
389     }
390   }
393   /* Check supplied data */
394   function check()
395   {
396     /* Call common method to give check the hook */
397     $message= plugin::check();
399     /* Ensure that we do not overwrite an allready existing entry 
400      */
401     if($this->is_new){
402       $release = $this->parent->parent->fai_release;
403       $new_dn= 'cn='.$this->cn.",".get_ou("faiTemplate", "faiTemplateRDN").get_ou("faiManagement", "faiBaseRDN").$release;
404       $res = faiManagement::check_class_name("FAItemplate",$this->cn,$new_dn);
405       if(isset($res[$this->cn])){
406         $message[] = msgPool::duplicated(_("Name"));
407       }
408     }
410     return ($message);
411   }
414   /* Save to LDAP */
415   function save()
416   {
417     plugin::save();
419     $ldap = $this->config->get_ldap_link();
421     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
423     if($this->initially_was_account){
424       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
425     }else{
426       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
427     }
429     /* Prepare FAIscriptEntry to write it to ldap
430      * First sort array.
431      *  Because we must delete old entries first.
432      * After deletion, we perform add and modify 
433      */
434     $Objects = array();
435     $tmp = $this->SubObjects;
436     foreach($tmp as $name => $obj){
437       if($obj['status'] == "FreshLoaded"){
438         unset($tmp[$name]);
439       }
440     }
442     foreach($tmp as $name => $obj){
443       if($obj['status'] == "delete"){
444         $Objects[$name] = $obj; 
445       }
446     }
447     foreach($tmp as $name => $obj){
448       if($obj['status'] != "delete"){
449         $Objects[$name] = $obj; 
450       }
451     }
453     foreach($Objects as $name => $obj){
455       foreach($this->sub64coded as $codeIt){
456         $obj[$codeIt]=base64_encode($obj[$codeIt]);
457       }
458       $tmp = array();
459       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
460       foreach($attributes as $attrs){
461         if(!isset($obj[$attrs]) || $obj[$attrs] == ""){
462           $obj[$attrs] = array();
463         }
464         $tmp[$attrs] =($obj[$attrs]);
465       }    
466         
467       $tmp['objectClass'] = $this->subClasses;
469       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
471       if($obj['status']=="new"){
472         $ldap->cat($sub_dn,array("objectClass"));
473         if($ldap->count()){
474           $obj['status']="edited";
475         }
476       }
478       /* Tag object */
479       $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
481       if($obj['status'] == "delete"){
482         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
483         $this->handle_post_events("remove");
484       }elseif($obj['status'] == "edited"){
485         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
486         $this->handle_post_events("modify");
487       }elseif($obj['status']=="new"){
488         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
489         $this->handle_post_events("add");
490       }
491     }
492   }
495   function PrepareForCopyPaste($source)
496   {
497     plugin::PrepareForCopyPaste($source);
499     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
500      */
501     $res = FAI::get_all_objects_for_given_base($source['dn'],"(&(objectClass=FAIclass)(objectClass=".$this->subClass."))");
502     foreach($res as $obj){
504       /* Skip not relevant objects */
505       if(!preg_match("/".preg_quote($source['dn'], '/')."$/i",$obj['dn'])) continue;
507       $objects = array();
508       $objects['status']      = "edited";
509       $objects['dn']          = $obj['dn'];
510       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
511       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
512       $this->SubObjects[$objects['cn']] = $objects;
513     }
514   }
515   
517   /* Return plugin informations for acl handling */ 
518   static function plInfo()
519   {
520     return (array( 
521           "plShortName" => _("Template"),
522           "plDescription" => _("FAI template"),
523           "plSelfModify"  => FALSE,
524           "plDepends"     => array(),
525           "plPriority"    => 24,
526           "plSection"     => array("administration"),
527           "plCategory"    => array("fai"),
528           "plProperties" =>
529           array(
530               array(
531                   "name"          => "faiTemplateRDN",
532                   "type"          => "rdn",
533                   "default"       => "ou=templates,",
534                   "description"   => _("RDN for FAI template storage."),
535                   "check"         => "gosaProperty::isRdn",
536                   "migrate"       => "migrate_faiTemplateRDN",
537                   "group"         => "plugin",
538                   "mandatory"     => TRUE)),
540           "plProvidedAcls" => array(
541               "cn"                => _("Name")." ("._("Read-only").")",
542             "description"       => _("Description"))
543           ));
544   }
547   /*! \brief  Used for copy & paste.
548     Returns a HTML input mask, which allows to change the cn of this entry.
549     @param  Array   Array containing current status && a HTML template.
550    */
551   function getCopyDialog()
552   {
553     $vars = array("cn");
554     $smarty = get_smarty();
555     $smarty->assign("cn", set_post($this->cn));
556     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
557     $ret = array();
558     $ret['string'] = $str;
559     $ret['status'] = "";
560     return($ret);
561   }
564   /*! \brief  Used for copy & paste.
565     Some entries must be renamed to avaoid duplicate entries.
566    */
567   function saveCopyDialog()
568   {
569     if(isset($_POST['cn'])){
570       $this->cn = get_post('cn');
571     }
572   }
575 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
576 ?>