On Sun, 1 Jun 2003, Andy Wardley wrote:

> That's strange, it still works for me, even like this.

[snip]

> Maybe there is something else in your code that is causing the problem?

yes, it works. But I cannot explain what happened: I experimented a 
little, and then moved to "include" staff. Suddenly I saw my error message 
in the right place. Sorry for the complaint. 


The callback is working, after redefining Template::Provider::_load
Maybe there is a better way...


#redefine Template::Provider::_load to check includes
use Template;
use Template::Provider;

sub Template::Provider::_load {
  my ($self, $name, $alias) = @_;
  my ($data, $error);
  my $tolerant = $self->{ TOLERANT };
  my $now = time;
  local $/ = undef;    # slurp files in one go
  local *FH;

  $alias = $name unless defined $alias or ref $name;
  $self->debug("_load($name, ", defined $alias ? $alias : '<no alias>', 
               ')') if $self->{ DEBUG };

  LOAD: {
    if (ref $name eq 'SCALAR') {
      # $name can be a SCALAR reference to the input text...
      $data = {
        name => defined $alias ? $alias : 'input text',
        text => $$name,
        time => $now,
        load => 0,
      };
    }
    elsif (ref $name) {
      # ...or a GLOB or file handle...
      my $text = <$name>;
      $data = {
        name => defined $alias ? $alias : 'input file handle',
        text => $text,
        time => $now,
        load => 0,
       };
    } elsif (-f $name) {
      if (defined($self->{PARAMS}->{LOAD_ACCESS}) and 
        ref($self->{PARAMS}->{LOAD_ACCESS}) eq "CODE" and  
        $data = &{$self->{PARAMS}->{LOAD_ACCESS}}($name, $self)) {
         $error = Template::Constants::STATUS_ERROR;
      } else {
        if (open(FH, $name)) {
          my $text = <FH>;
          $data = {
            name => $alias,
            text => $text,
            time => (stat $name)[9],
            load => $now,
          };
        } elsif ($tolerant) {
          ($data, $error) = (undef, Template::Constants::STATUS_DECLINED);
        } else {
          $data  = "$alias: $!";
          $error = Template::Constants::STATUS_ERROR;
        }
      }
    } else {
      ($data, $error) = (undef, Template::Constants::STATUS_DECLINED);
    }
  }
  return ($data, $error);
}


# this generates the callback for control access 
sub access_control {
  my ($origusername, $origcontext)  = @_;
  return sub {
    my ($file, $opt) = @_;
    my $username = $origusername; # do not trust what is in $context, can be altered
    my $context = $origcontext;   # just in case
    return "cannot include file outside this wiki" 
      unless $file =~ s/^$commonDir\///; 
    my ($what, $web, $topic ) = split /\//, $file;
    # data and pub should come from TWiki.cfg...
    return "cannot include but topics or attachments" 
      unless $what =~ s/data|pub//; 
    $topic =~ s/\.txt$// if $what eq "data";
    return "forbidden" 
      unless TWiki::Access::checkAccessPermission("VIEW", $username, "",
        $topic, $web);  
    return 0;
 }
} 

############################################
#and this is the call


  use Template;
  use Template::Provider::Allow;
  use CGI::Carp qw(fatalsToBrowser);;
  use CGI;
  use DBI; 

  # I provide a guest access to DB
  my $dbh = DBI->connect("dbi:mysql:$TWiki::LogTable",
    $TWiki::GuestUser,$TWiki::GuestPasswd) or die $db_errstr;

 
  $context = {GuestDbh=>$dbh};

  my $allow    = Template::Provider::Allow->new(qw( Lcalc DBI ));
  my $plugins  = Template::Plugins->new();

  my $config = {
    INCLUDE_PATH => ["$pubDir/$thePathInfo", "$dataDir/$webName", 
      $pubDir,$dataDir],
    INTERPOLATE  => 0, 
    POST_CHOMP   => 1,
    EVAL_PERL    => 0,             
    START_TAG    => '\[\+',   
    END_TAG    => '\+\]',    
    OUTPUT_PATH => "$pubDir/$thePathInfo",
    LOAD_PLUGINS => [ $allow, $plugins ],
    LOAD_ACCESS => access_control($userName, $context),
  };

  $template = Template->new($config) or die "cannot start Template";    
  
-- 
Franco Bagnoli (franchino) <[EMAIL PROTECTED]> ([EMAIL PROTECTED])
virtual location: Dipartimento di Energetica "S. Stecco"
real location: Dip. Matematica Applicata "G. Sansone", Universita' Firenze,
Via S. Marta, 3 I-50139 Firenze, Italy. Tel. +39 0554796422, fax: +39 055471787
GPG Key fingerprint = 169D 9EA5 8FD3 7EDA E43A  9830 255F BCEC 0D63 3728


_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to