From: lpsolit%gmail.com <> Date: Thu, 25 Aug 2005 21:02:39 +0000 (+0000) Subject: Bug 208761: Move GetFormat() from globals.pl into Bugzilla::Template - Patch by Frédé... X-Git-Tag: bugzilla-2.21.1~80 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2a5664ad1abf679b9e50a6c409902ce2ef638cc5;p=thirdparty%2Fbugzilla.git Bug 208761: Move GetFormat() from globals.pl into Bugzilla::Template - Patch by Frédéric Buclin r=wicked a=justdave --- diff --git a/Bugzilla/Template.pm b/Bugzilla/Template.pm index 711144a6fd..52a1bf150f 100644 --- a/Bugzilla/Template.pm +++ b/Bugzilla/Template.pm @@ -25,12 +25,14 @@ # Tobias Burnus # Myk Melez # Max Kanat-Alexander +# Frédéric Buclin package Bugzilla::Template; use strict; +use Bugzilla::Constants; use Bugzilla::Config qw(:DEFAULT $templatedir $datadir); use Bugzilla::Util; use Bugzilla::User; @@ -132,7 +134,6 @@ sub getTemplateIncludePath { @usedlanguages)]; } -# Write the header for non yet templatized .cgi files. sub put_header { my $self = shift; ($vars->{'title'}, $vars->{'h1'}, $vars->{'h2'}) = (@_); @@ -142,13 +143,51 @@ sub put_header { $vars->{'header_done'} = 1; } -# Write the footer for non yet templatized .cgi files. sub put_footer { my $self = shift; $self->process("global/footer.html.tmpl", $vars) || ThrowTemplateError($self->error()); } +sub get_format { + my $self = shift; + my ($template, $format, $ctype) = @_; + + $ctype ||= 'html'; + $format ||= ''; + + # Security - allow letters and a hyphen only + $ctype =~ s/[^a-zA-Z\-]//g; + $format =~ s/[^a-zA-Z\-]//g; + trick_taint($ctype); + trick_taint($format); + + $template .= ($format ? "-$format" : ""); + $template .= ".$ctype.tmpl"; + + # Now check that the template actually exists. We only want to check + # if the template exists; any other errors (eg parse errors) will + # end up being detected later. + eval { + $self->context->template($template); + }; + # This parsing may seem fragile, but its OK: + # http://lists.template-toolkit.org/pipermail/templates/2003-March/004370.html + # Even if it is wrong, any sort of error is going to cause a failure + # eventually, so the only issue would be an incorrect error message + if ($@ && $@->info =~ /: not found$/) { + ThrowUserError('format_not_found', {'format' => $format, + 'ctype' => $ctype}); + } + + # Else, just return the info + return + { + 'template' => $template, + 'extension' => $ctype, + 'ctype' => Bugzilla::Constants::contenttypes->{$ctype} + }; +} ############################################################################### # Templatization Code @@ -449,12 +488,19 @@ __END__ =head1 NAME -Bugzilla::Template - Wrapper arround the Template Toolkit C