Code

FAI template
[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       $ldap     = $this->config->get_ldap_link();
52       $ldap->cd ($this->dn);
53     
54       $attrs_to_search = $this->subAttributes;
55       $attrs_to_search[] = "FAIstate";
56       $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
58       $data = array();
59       while($object = $ldap->fetch()){
60         $data[] = $object;
61       }
62       foreach($data as $object){
64         /* Skip objects, that are tagged as removed */
65         if(isset($object['FAIstate'][0])){
66           if(preg_match("/removed$/",$object['FAIstate'][0])){
67             continue;
68           }
69         }
71         /* Set status for save management */
72         $objects = array();
73         $objects['status']      = "FreshLoaded";
74         $objects['dn']          = $object['dn'];
75         $objects                = $this->get_object_attributes($objects,$this->subAttributes);
76         $this->SubObjects[$objects['cn']] = $objects;
77       }
78     }
79     $this->is_new = FALSE;
80     if($this->dn == "new"){
81       $this->is_new =TRUE;
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         }
103         $var = stripslashes($var);
105         /*  check if this is a binary entry */
106         if(in_array_ics($attrs,$this->subBinary)){
107           $var = $ldap->get_attribute($object['dn'], $attrs,$r_array=0);
108         }
109   
110         $object[$attrs] = $var;
111       }
112     }
113     return($object);
114   }
117   function execute()
118   {
119     /* Call parent execute */
120     plugin::execute();
122     if($this->is_account && !$this->view_logged){
123       $this->view_logged = TRUE;
124       new log("view","fai/".get_class($this),$this->dn);
125     }
127     /* Fill templating stuff */
128     $smarty= get_smarty();
129     $display= "";
131     /* New Listhandling
132      */
133     $once = true;
134     foreach($_POST as $name => $value){
135       if(preg_match("/^editscript_/",$name)&&($once)){
136         $once = false;
137         $entry = preg_replace("/^editscript_/","",$name);
138         $entry = base64_decode(preg_replace("/_.*/","",$entry));
139         $obj  = $this->SubObjects[$entry];
141         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
142         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
143         if(preg_match("/r/",$acl)){
144           if($obj['status'] == "FreshLoaded"){
145             $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
146           }
147           $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
148           $this->dialog->set_acl_base($this->acl_base);
149           $this->dialog->FAIstate = $this->FAIstate;
150           $this->dialog->set_acl_category("fai");
152           session::set('objectinfo',$obj['dn']);
153           $this->dialog->parent = &$this;
154           $this->is_dialog=true;
155         }
156       }
157       if(preg_match("/^deletescript_/",$name)&&($once) && !preg_match("/freeze/",$this->FAIstate)){
158         $once = false;
159         $entry = preg_replace("/^deletescript_/","",$name);
160         $entry = base64_decode(preg_replace("/_.*/","",$entry));
161         $obj  = $this->SubObjects[$entry];
163         $acl_dn = $this->acl_base_for_current_object($obj['dn']);
164         $acl = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
165         if(preg_match("/d/",$acl)){
166           $status = $this->SubObjects[$entry]['status'];
167           if($status == "edited" || $status == "FreshLoaded"){
168             $this->SubObjects[$entry]['status']= "delete";
169           }else{
170             unset($this->SubObjects[$entry]);
171           }
172         }
173       }
174     }
176     /* File download requested */
177     if(isset($_GET['getFAItemplate'])){
178       if(isset($this->SubObjects[$_GET['getFAItemplate']])){
179         $obj = $this->SubObjects[$_GET['getFAItemplate']];
180         $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
181         send_binary_content($obj['FAItemplateFile'],$obj['cn'].".FAItemplate");
182       }
183     }
185     /* Edit entries via GET */
186     if(isset($_GET['act']) && isset($_GET['id'])){
187       if($_GET['act'] == "edit" && isset($this->SubObjects[$_GET['id']])){
188         $obj = $this->SubObjects[$_GET['id']];
189           if($obj['status'] == "FreshLoaded"){
190           $obj  = $this->get_object_attributes($obj,$this->sub_Load_Later);
191         }
192         $this->dialog= new $this->subClassName($this->config,$this->dn,$obj);
193         $this->dialog->FAIstate = $this->FAIstate;
194         $this->dialog->acl = $this->acl;
195         session::set('objectinfo',$obj['dn']);
196         $this->dialog->parent = &$this;
197         $this->is_dialog=true;
198       }
199     }
201     /* Add new sub object */
202     if(isset($_POST['AddSubObject']) && !preg_match("/freeze/",$this->FAIstate)){
203       $acl_dn = "cn=dummy,".$this->acl_base_for_current_object($this->dn);
204       $acl    = $this->ui->get_permissions($acl_dn,"fai/faiTemplateEntry");
206       if(preg_match("/c/",$acl)){
207         $this->dialog= new $this->subClassName($this->config,"new");
208         $this->dialog->set_acl_base($this->acl_base);
209         $this->dialog->FAIstate = $this->FAIstate;
210         $this->dialog->set_acl_category("fai");
211         $this->dialog->parent = &$this;
212         $this->is_dialog=true;
213       }
214     }
216     if($this->dn != "new"){
217       session::set('objectinfo',$this->dn);
218     }
220     /* Save Dialog */
221     if(isset($_POST['SaveSubObject']) && is_object($this->dialog)){
222       $this->dialog->save_object();
223       $msgs = $this->dialog->check();
224       if(count($msgs)>0){
225         foreach($msgs as $msg){
226           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
227         }
228       }else{
229         /* Get return object */
230         $obj = $this->dialog->save();
231         if(isset($obj['remove'])){
233           $old_stat = $this->SubObjects[$obj['remove']['from']]['status'];
235           /* Depending on status, set new status */
236           if($old_stat == "edited" || $old_stat == "FreshLoaded"){
237             $this->SubObjects[$obj['remove']['from']]['status'] = "delete";
238           }elseif($this->SubObjects[$obj['remove']['from']]['status']=="new"){
239             unset($this->SubObjects[$obj['remove']['from']]);
240           }
241           $obj['status'] = "new";
242           $this->SubObjects[$obj['remove']['to']] = $obj;
243           unset($this->SubObjects[$obj['remove']['to']]['remove']);
244         }else{
245           if($obj['status'] == "FreshLoaded"){
246             $obj['status'] = "edited";
247           }
248           $this->SubObjects[$obj['cn']]=$obj;
249         }
250         $this->is_dialog=false;
251         unset($this->dialog);
252         $this->dialog=FALSE;
253       }
254     }
256     /* Sort entries */
257     $tmp = $keys = array();
258     foreach($this->SubObjects as $key => $entry){
259       $keys[$key]=$key;
260     }
261     natcasesort($keys);
262     foreach($keys as $key){
263       $tmp[$key]=$this->SubObjects[$key];
264     }
265     $this->SubObjects = $tmp;
267     /* Cancel Dialog */
268     if(isset($_POST['CancelSubObject'])){
269       $this->is_dialog=false; 
270       unset($this->dialog);
271       $this->dialog=FALSE;
272     }
274     /* Print dialog if $this->dialog is set */
275     if(is_object($this->dialog)){
276       $this->dialog->save_object();
277       $display = $this->dialog->execute();
278       return($display);
279     }
281      /* Divlist Containing FAItemplates */
282     $divlist = new divSelectBox("FAItemplates");
283     $divlist->setHeight(400);
285     $tmp = $this->getList(true);
286   
287     /* Create div list with all sub entries listed */
288     foreach($this->SubObjects as $key => $name){
290       /* Skip removed entries */ 
291       if($name['status'] == "delete") continue;
293       /* Get permissions */
294       $dn  = $this->acl_base_for_current_object($name['dn']);
295       $acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry")  ;
296       $act = "";
298       /* Hide delete icon if this object is freezed */
299       if(preg_match("/freeze/",$this->FAIstate)){
300         $act .= "<input type='image' src='images/lists/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
301       }else{
302         $act .= "<input type='image' src='images/lists/edit.png'      name='editscript_%s'    title='"._("edit")."' alt='"._("edit")."'>";
303         if(preg_match("/d/",$acl)){
304           $act .="<input type='image' src='images/lists/trash.png' name='deletescript_%s'  title='"._("delete")."' alt='"._("delete")."'>";
305         }
306       }
308       /* Check acls for download icon */
309       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","FAItemplateFile")  ;
310       if(($this->SubObjects[$key]['status'] == "new") || ($this->SubObjects[$key]['dn'] == "new") || !preg_match("/r/",$s_acl)){
311         $down = "";
312       }else{
313         $down = "<a href='?plug=".$_GET['plug']."&getFAItemplate=".$key."'>
314           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0>
315           </a>";
316       }
318       /* Check if we are allowed to view this object */
319       $s_acl = $this->ui->get_permissions($dn,"fai/faiTemplateEntry","cn")  ;
320       if(preg_match("/r/",$s_acl)){
322         $edit_link = "<a href='?plug=".$_GET['plug']."&amp;act=edit&amp;id=".$key."'>".$tmp[$key]."</a>";
323         $divlist->AddEntry(array( array("string"=> $edit_link), 
324               array("string"=>$down , "attach" => "style='width:20px;'"),
325               array("string"=>str_replace("%s",base64_encode($key),$act),
326                 "attach"=>"style='border-right: 0px;width:50px;text-align:right;'")));
327       }
328     }
329     $smarty->assign("Entry_divlist",$divlist->DrawList());
330     /* Divlist creation complete
331      */
333      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
334      * If we post the escaped strings they will be escaped again
335      */
336     foreach($this->attributes as $attrs){
337       if(get_magic_quotes_gpc()){
338         $smarty->assign($attrs,stripslashes($this->$attrs));
339       }else{
340         $smarty->assign($attrs,($this->$attrs));
341       }
342     }
344     $dn = $this->acl_base_for_current_object($this->dn);
345     $smarty->assign("sub_object_is_addable", 
346             preg_match("/c/",$this->ui->get_permissions($dn,"fai/faiTemplateEntry")) && 
347             !preg_match("/freeze/",$this->FAIstate));
349     foreach($this->attributes as $attr){
350       $smarty->assign($attr."ACL",$this->getacl($attr, preg_match("/freeze/",$this->FAIstate)));
351     }
353     $display.= $smarty->fetch(get_template_path('faiTemplate.tpl', TRUE));
354     return($display);
355   }
358   function acl_base_for_current_object($dn)
359   {
360     if($dn == "new"){
361       if($this->dn == "new"){
362         $dn = session::get('CurrentMainBase');
363       }else{
364         $dn = $this->dn;
365       }
366     }
367     return($dn);
368   }
371   /* Generate listbox friendly SubObject list
372   */
373   function getList(){
374     $a_return=array();
375     foreach($this->SubObjects as $obj){
376       if($obj['status'] != "delete"){
377       
378         if((isset($obj['description']))&&(!empty($obj['description']))){
379           if(strlen($obj['description']) > 40){
380             $obj['description'] = substr($obj['description'],0,40)."...";
381           }
382           $a_return[$obj['cn']]= $obj['cn']." [".$obj['description']."]";
383         }else{
384           $a_return[$obj['cn']]= $obj['cn'];
385         }
386       }
387     }
388     return($a_return);
389   }
391   /* Delete me, and all my subtrees
392    */
393   function remove_from_parent()
394   {
395     if($this->acl_is_removeable()){
396       $ldap = $this->config->get_ldap_link();
397       $ldap->cd ($this->dn);
398       $release = $this->parent->parent->fai_release;
399       $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $this->dn);
400       new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
401       FAI::prepare_to_save_FAI_object($use_dn,array(),true);
403       foreach($this->SubObjects as $name => $obj){
404         $use_dn = preg_replace("/".normalizePreg(FAI::get_release_dn($this->dn))."/i", $release, $obj['dn']);
405         FAI::prepare_to_save_FAI_object($use_dn,array(),true);
406       }
407       $this->handle_post_events("remove");
408     }
409   }
412   /* Save data to object 
413    */
414   function save_object()
415   {
416     if((isset($_POST['FAItemplate_posted'])) && (!preg_match("/freeze/",$this->FAIstate))){
417       plugin::save_object();
418     }
419   }
422   /* Check supplied data */
423   function check()
424   {
425     /* Call common method to give check the hook */
426     $message= plugin::check();
428     /* Ensure that we do not overwrite an allready existing entry 
429      */
430     if($this->is_new){
431       $release = $this->parent->parent->fai_release;
432       $new_dn= 'cn='.$this->cn.",".get_ou('faitemplateou').get_ou('faiou').$release;
433       $res = faiManagement::check_class_name("FAItemplate",$this->cn,$new_dn);
434       if(isset($res[$this->cn])){
435         $message[] = msgPool::duplicated(_("Name"));
436       }
437     }
439     return ($message);
440   }
443   /* Save to LDAP */
444   function save()
445   {
446     plugin::save();
448     $ldap = $this->config->get_ldap_link();
450     FAI::prepare_to_save_FAI_object($this->dn,$this->attrs);
452     if($this->initially_was_account){
453       new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
454     }else{
455       new log("create","fai/".get_class($this),$this->dn,$this->attributes);
456     }
458     /* Prepare FAIscriptEntry to write it to ldap
459      * First sort array.
460      *  Because we must delete old entries first.
461      * After deletion, we perform add and modify 
462      */
463     $Objects = array();
464     $tmp = $this->SubObjects;
465     foreach($tmp as $name => $obj){
466       if($obj['status'] == "FreshLoaded"){
467         unset($tmp[$name]);
468       }
469     }
471     foreach($tmp as $name => $obj){
472       if($obj['status'] == "delete"){
473         $Objects[$name] = $obj; 
474       }
475     }
476     foreach($tmp as $name => $obj){
477       if($obj['status'] != "delete"){
478         $Objects[$name] = $obj; 
479       }
480     }
482     foreach($Objects as $name => $obj){
484       foreach($this->sub64coded as $codeIt){
485         $obj[$codeIt]=base64_encode($obj[$codeIt]);
486       }
487       $tmp = array();
488       $attributes = array_merge($this->sub_Load_Later,$this->subAttributes);
489       foreach($attributes as $attrs){
490         if(empty($obj[$attrs])){
491           $obj[$attrs] = array();
492         }
493         $tmp[$attrs] =($obj[$attrs]);
494       }    
495         
496       $tmp['objectClass'] = $this->subClasses;
498       $sub_dn = "cn=".$obj['cn'].",".$this->dn;
500       if($obj['status']=="new"){
501         $ldap->cat($sub_dn,array("objectClass"));
502         if($ldap->count()){
503           $obj['status']="edited";
504         }
505       }
507       /* Tag object */
508       $this->tag_attrs($tmp, $sub_dn, $this->gosaUnitTag);
510       if($obj['status'] == "delete"){
511         FAI::prepare_to_save_FAI_object($sub_dn,array(),true);
512         $this->handle_post_events("remove");
513       }elseif($obj['status'] == "edited"){
514         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
515         $this->handle_post_events("modify");
516       }elseif($obj['status']=="new"){
517         FAI::prepare_to_save_FAI_object($sub_dn,$tmp);
518         $this->handle_post_events("add");
519       }
520     }
521   }
524   function PrepareForCopyPaste($source)
525   {
526     plugin::PrepareForCopyPaste($source);
528     /* Read all leaf objects of this object (For FAIscript this would be FAIscriptEntry)
529      */
530     $ldap     = $this->config->get_ldap_link();
531     $ldap->cd ($source['dn']);
533     $attrs_to_search = $this->subAttributes;
534     $attrs_to_search[] = "FAIstate";
535     $ldap->search("(&(objectClass=FAIclass)(objectClass=".$this->subClass."))",$attrs_to_search);
537     while($object = $ldap->fetch()){
539       /* Skip objects, that are tagged as removed */
540       if(isset($object['FAIstate'][0])){
541         if(preg_match("/removed$/",$object['FAIstate'][0])){
542           continue;
543         }
544       }
546       /* Set status for save management */
547       $objects = array();
548       $objects['status']      = "edited";
549       $objects['dn']          = $object['dn'];
550       $objects                = $this->get_object_attributes($objects,$this->subAttributes);
551       $objects                = $this->get_object_attributes($objects,$this->sub_Load_Later);
553       $this->SubObjects[$objects['cn']] = $objects;
554     }
555   }
556   
558   /* Return plugin informations for acl handling */ 
559   static function plInfo()
560   {
561     return (array( 
562           "plShortName" => _("Template"),
563           "plDescription" => _("FAI template"),
564           "plSelfModify"  => FALSE,
565           "plDepends"     => array(),
566           "plPriority"    => 24,
567           "plSection"     => array("administration"),
568           "plCategory"    => array("fai"),
569           "plProvidedAcls" => array(
570             "cn"                => _("Name")." ("._("Readonly").")",
571             "description"       => _("Description"))
572           ));
573   }
576   /*! \brief  Used for copy & paste.
577     Returns a HTML input mask, which allows to change the cn of this entry.
578     @param  Array   Array containing current status && a HTML template.
579    */
580   function getCopyDialog()
581   {
582     $vars = array("cn");
583     $smarty = get_smarty();
584     $smarty->assign("cn", htmlentities($this->cn));
585     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
586     $ret = array();
587     $ret['string'] = $str;
588     $ret['status'] = "";
589     return($ret);
590   }
593   /*! \brief  Used for copy & paste.
594     Some entries must be renamed to avaoid duplicate entries.
595    */
596   function saveCopyDialog()
597   {
598     if(isset($_POST['cn'])){
599       $this->cn = get_post('cn');
600     }
601   }
604 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
605 ?>