page.cgi 2.44 KB
Newer Older
1
#!/usr/bin/perl -wT
2 3 4
# 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/.
5
#
6 7
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
8 9 10

###############################################################################
# This CGI is a general template display engine. To display templates using it,
11
# put them in the "pages" subdirectory of en/default, call them
12 13
# "foo.<ctype>.tmpl" and use the URL page.cgi?id=foo.<ctype> , where <ctype> is
# a content-type, e.g. html.
14 15
###############################################################################

16
use 5.10.1;
17
use strict;
18
use lib qw(. lib);
19 20

use Bugzilla;
21
use Bugzilla::Error;
22
use Bugzilla::Hook;
23 24 25 26 27 28 29 30
use Bugzilla::Search::Quicksearch;

###############
# Subroutines #
###############

# For quicksearch.html.
sub quicksearch_field_names {
31
    my $fields = Bugzilla::Search::Quicksearch->FIELD_MAP;
32 33 34 35 36 37 38 39 40 41 42 43 44 45
    my %fields_reverse;
    # Put longer names before shorter names.
    my @nicknames = sort { length($b) <=> length($a) } (keys %$fields);
    foreach my $nickname (@nicknames) {
        my $db_field = $fields->{$nickname};
        $fields_reverse{$db_field} ||= [];
        push(@{ $fields_reverse{$db_field} }, $nickname);
    }
    return \%fields_reverse;
}

###############
# Main Script #
###############
46

47
Bugzilla->login();
48

49
my $cgi = Bugzilla->cgi;
50
my $template = Bugzilla->template;
51

52 53
my $id = $cgi->param('id');
if ($id) {
54 55 56
    # Be careful not to allow directory traversal.
    if ($id =~ /\.\./) {
        # two dots in a row is bad
57
        ThrowUserError("bad_page_cgi_id", { "page_id" => $id });
58 59 60
    }
    # Split into name and ctype.
    $id =~ /^([\w\-\/\.]+)\.(\w+)$/;
61 62
    if (!$2) {
        # if this regexp fails to match completely, something bad came in
63
        ThrowUserError("bad_page_cgi_id", { "page_id" => $id });
64
    }
65

66 67 68
    my %vars = ( 
      quicksearch_field_names => \&quicksearch_field_names,
    );
69
    Bugzilla::Hook::process('page_before_template', 
70 71
                            { page_id => $id, vars => \%vars });

72
    my $format = $template->get_format("pages/$1", undef, $2);
73
    
74
    $cgi->param('id', $id);
75 76

    print $cgi->header($format->{'ctype'});
77

78
    $template->process("$format->{'template'}", \%vars)
79 80 81 82 83
      || ThrowTemplateError($template->error());
}
else {
    ThrowUserError("no_page_specified");
}