summaryrefslogtreecommitdiffstats
path: root/Bugzilla/Template/PreloadProvider.pm
blob: 2588b1a7913581d8e9f2a58ea81f22e02e34af96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

# This exists to implement the template-before_process hook.
package Bugzilla::Template::PreloadProvider;

use 5.10.1;
use strict;
use warnings;

use base qw(Template::Provider);

use File::Find ();
use Cwd ();
use File::Spec;
use Template::Constants qw( STATUS_ERROR );
use Template::Document;
use Template::Config;

use Bugzilla::Util qw(trick_taint);

sub _init {
    my $self = shift;
    $self->SUPER::_init(@_);

    my $path   = $self->{INCLUDE_PATH};
    my $cache  = $self->{_BZ_CACHE} = {};
    my $search = $self->{_BZ_SEARCH} = {};

    foreach my $template_dir (@$path) {
        $template_dir = Cwd::realpath($template_dir);
        my $wanted = sub {
            my ( $name, $dir ) = ($File::Find::name, $File::Find::dir);
            if ( $name =~ /\.tmpl$/ ) {
                my $key = $name;
                $key =~ s/^\Q$template_dir\///;
                unless ($search->{$key}) {
                    $search->{$key} = $name;
                }
                trick_taint($name);
                my $data = {
                    name => $key,
                    text => do {
                        open my $fh, '<:utf8', $name or die "cannot open $name";
                        local $/ = undef;
                        scalar <$fh>; # $fh is closed it goes out of scope
                    },
                    time => (stat($name))[9],
                };
                trick_taint($data->{text}) if $data->{text};
                $cache->{$name} = $self->_bz_compile($data) or die "compile error: $name";
            }
        };
        File::Find::find( { wanted => $wanted, no_chdir => 1 }, $template_dir );
    }

    return $self;
}

sub fetch {
    my ($self, $name, $prefix) = @_;
    my $file;
    if (File::Spec->file_name_is_absolute($name)) {
        $file = $name;
    }
    elsif ($name =~ m#^\./#) {
        $file = File::Spec->rel2abs($name);
    }
    else {
        $file = $self->{_BZ_SEARCH}{$name};
    }

    if (not $file) {
        return ("cannot find file - $name ($file)", STATUS_ERROR);
    }

    if ($self->{_BZ_CACHE}{$file}) {
        return ($self->{_BZ_CACHE}{$file}, undef);
    }
    else {
        return ("unknown file - $file", STATUS_ERROR);
    }
}

sub _bz_compile {
    my ($self, $data) = @_;

    my $parser = $self->{PARSER} ||= Template::Config->parser( $self->{PARAMS} )
        || return ( Template::Config->error(), STATUS_ERROR );

    # discard the template text - we don't need it any more
    my $text = delete $data->{text};

    # call parser to compile template into Perl code
    if (my $parsedoc = $parser->parse($text, $data)) {
        $parsedoc->{METADATA} = {
            'name' => $data->{name},
            'modtime' => $data->{time},
            %{ $parsedoc->{METADATA} },
        };

        return Template::Document->new($parsedoc);
    }
}

1;