]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki.pm
bugfix
[git.ikiwiki.info.git] / IkiWiki.pm
index 1327e4db50495f0b2b02ba0ab08eb1b55445bec6..8bae6b72f8f8f212b5114e059f470e4607214446 100644 (file)
@@ -1653,57 +1653,67 @@ sub saveindex () {
 
 sub template_file ($) {
        my $name=shift;
-
-       my $template=srcfile("templates/$name", 1);
-       return $template if defined $template;
        
-       foreach my $dir ($config{templatedir},
-                        "$installdir/share/ikiwiki/templates") {
-               return "$dir/$name" if -e "$dir/$name";
+       my $tpage=($name =~ /^\//) ? $name : "templates/$name";
+       if ($name !~ /\.tmpl$/ && exists $pagesources{$tpage}) {
+               $tpage=$pagesources{$tpage};
+               $name.=".tmpl";
+       }
+
+       my $template=srcfile($tpage, 1);
+       if (! defined $template) {
+               $name=~s:/::; # avoid path traversal
+               foreach my $dir ($config{templatedir},
+                                "$installdir/share/ikiwiki/templates") {
+                       if (-e "$dir/$name") {
+                               $template="$dir/$name";
+                               last;
+                       }
+               }
        }
-       return;
-}
 
-sub template_params (@) {
-       filter => sub {
-               my $text_ref = shift;
-               ${$text_ref} = decode_utf8(${$text_ref});
-       },
-       loop_context_vars => 1,
-       die_on_bad_params => 0,
-       @_,
-       no_includes => 1,
-}
-
-sub template ($;@) {
-       require HTML::Template;
-       return HTML::Template->new(template_params(
-               filename => template_file(shift),
-               @_
-       ));
+       if (defined $template) {        
+               return $template, $tpage if wantarray;
+               return $template;
+       }
+       return;
 }
 
 sub template_depends ($$;@) {
        my $name=shift;
        my $page=shift;
-
-       if (defined $page) {
-               add_depends($page, "templates/$name");
+       
+       my ($filename, $tpage)=template_file($name);
+       if (defined $page && defined $tpage) {
+               add_depends($page, $tpage);
        }
-       my $filename=template_file($name);
+
+       return unless defined $filename;
 
        require HTML::Template;
-       return HTML::Template->new(template_params(
+       return HTML::Template->new(
+               filter => sub {
+                       my $text_ref = shift;
+                       ${$text_ref} = decode_utf8(${$text_ref});
+               },
+               loop_context_vars => 1,
+               die_on_bad_params => 0,
                filename => $filename,
-               @_
-       ));
+               @_,
+               no_includes => 1,
+       );
+}
+
+sub template ($;@) {
+       template_depends(shift, undef, @_);
 }
 
 sub misctemplate ($$;@) {
        my $title=shift;
        my $pagebody=shift;
        
-       my $template=template("misc.tmpl",
+       my $template=template("misc.tmpl");
+       $template->param(
                title => $title,
                indexlink => indexlink(),
                wikiname => $config{wikiname},