Code

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