From bcfde177dbb9f3748f59af067357e5902335ca46 Mon Sep 17 00:00:00 2001 From: "mkanat%kerio.com" <> Date: Thu, 14 Jul 2005 13:01:34 +0000 Subject: Bug 283989: CGI.pl global init code should be moved to Bugzilla::CGI Patch By Max Kanat-Alexander r=wurblzap, a=justdave --- Bugzilla.pm | 53 +++++++++++++++++++++++++++++ Bugzilla/CGI.pm | 9 +++++ Bugzilla/Util.pm | 16 +++++++++ CGI.pl | 26 -------------- template/en/default/global/message.txt.tmpl | 26 ++++++++++++++ 5 files changed, 104 insertions(+), 26 deletions(-) create mode 100644 template/en/default/global/message.txt.tmpl diff --git a/Bugzilla.pm b/Bugzilla.pm index 0deb6e16e..d3b1a5970 100644 --- a/Bugzilla.pm +++ b/Bugzilla.pm @@ -33,6 +33,59 @@ use Bugzilla::Constants; use Bugzilla::DB; use Bugzilla::Template; use Bugzilla::User; +use Bugzilla::Error; +use Bugzilla::Util; + +use File::Basename; + +##################################################################### +# Constants +##################################################################### + +# Scripts that are not stopped by shutdownhtml being in effect. +use constant SHUTDOWNHTML_EXEMPT => [ + 'doeditparams.cgi', + 'editparams.cgi', + 'checksetup.pl', +]; + +##################################################################### +# Global Code +##################################################################### + +# If Bugzilla is shut down, do not allow anything to run, just display a +# message to the user about the downtime. Scripts listed in +# SHUTDOWNHTML_EXEMPT are exempt from this message. +# +# This code must go here. It cannot go anywhere in Bugzilla::CGI, because +# it uses Template, and that causes various dependency loops. +if (Param("shutdownhtml") + && lsearch(SHUTDOWNHTML_EXEMPT, basename($0)) == -1) +{ + my $template = Bugzilla->template; + my $vars = {}; + $vars->{'message'} = 'shutdown'; + # Generate and return a message about the downtime, appropriately + # for if we're a command-line script or a CGI sript. + my $extension; + if (i_am_cgi() && (!Bugzilla->cgi->param('format') + || Bugzilla->cgi->param('format') eq 'html')) { + $extension = 'html'; + } + else { + $extension = 'txt'; + } + print Bugzilla->cgi->header() if i_am_cgi(); + my $t_output; + $template->process("global/message.$extension.tmpl", $vars, \$t_output) + || ThrowTemplateError($template->error); + print $t_output . "\n"; + exit; +} + +##################################################################### +# Subroutines and Methods +##################################################################### my $_template; sub template { diff --git a/Bugzilla/CGI.pm b/Bugzilla/CGI.pm index 6f5a6f6d7..c2d61780f 100644 --- a/Bugzilla/CGI.pm +++ b/Bugzilla/CGI.pm @@ -25,9 +25,18 @@ use strict; package Bugzilla::CGI; +BEGIN { + if ($^O =~ /MSWin32/i) { + # Help CGI find the correct temp directory as the default list + # isn't Windows friendly (Bug 248988) + $ENV{'TMPDIR'} = $ENV{'TEMP'} || $ENV{'TMP'} || "$ENV{'WINDIR'}\\TEMP"; + } +} + use CGI qw(-no_xhtml -oldstyle_urls :private_tempfiles :unique_headers SERVER_PUSH); use base qw(CGI); +use CGI::Carp qw(fatalsToBrowser); use Bugzilla::Error; use Bugzilla::Util; diff --git a/Bugzilla/Util.pm b/Bugzilla/Util.pm index 83c9bf7d3..1ac25d1aa 100644 --- a/Bugzilla/Util.pm +++ b/Bugzilla/Util.pm @@ -33,6 +33,7 @@ use base qw(Exporter); detaint_signed html_quote url_quote value_quote xml_quote css_class_quote + i_am_cgi lsearch max min diff_arrays diff_strings trim wrap_comment find_wrap_point @@ -130,6 +131,12 @@ sub xml_quote { return $var; } +sub i_am_cgi () { + # I use SERVER_SOFTWARE because it's required to be + # defined for all requests in the CGI spec. + return exists $ENV{'SERVER_SOFTWARE'} ? 1 : 0; +} + sub lsearch { my ($list,$item) = (@_); my $count = 0; @@ -376,6 +383,9 @@ Bugzilla::Util - Generic utility functions for bugzilla value_quote($var); xml_quote($var); + # Functions that tell you about your environment + my $is_cgi = i_am_cgi(); + # Functions for searching $loc = lsearch(\@arr, $val); $val = max($a, $b, $c); @@ -480,6 +490,12 @@ This is similar to C, except that ' is escaped to '. This is kept separate from html_quote partly for compatibility with previous code (for ') and partly for future handling of non-ASCII characters. +=item C + +Tells you whether or not you are being run as a CGI script in a web +server. For example, it would return false if the caller is running +in a command-line script. + =back =head2 Searching diff --git a/CGI.pl b/CGI.pl index 73df77695..78ef20fef 100644 --- a/CGI.pl +++ b/CGI.pl @@ -32,14 +32,6 @@ use lib "."; # use Carp; # for confess -BEGIN { - if ($^O =~ /MSWin32/i) { - # Help CGI find the correct temp directory as the default list - # isn't Windows friendly (Bug 248988) - $ENV{'TMPDIR'} = $ENV{'TEMP'} || $ENV{'TMP'} || "$ENV{'WINDIR'}\\TEMP"; - } -} - use Bugzilla::Util; use Bugzilla::Config; use Bugzilla::Constants; @@ -60,28 +52,10 @@ sub CGI_pl_sillyness { $zz = $::buffer; } -use CGI::Carp qw(fatalsToBrowser); - require 'globals.pl'; use vars qw($template $vars); -# If Bugzilla is shut down, do not go any further, just display a message -# to the user about the downtime. (do)editparams.cgi is exempted from -# this message, of course, since it needs to be available in order for -# the administrator to open Bugzilla back up. -if (Param("shutdownhtml") && $0 !~ m:(^|[\\/])(do)?editparams\.cgi$:) { - $::vars->{'message'} = "shutdown"; - - # Return the appropriate HTTP response headers. - print Bugzilla->cgi->header(); - - # Generate and return an HTML message about the downtime. - $::template->process("global/message.html.tmpl", $::vars) - || ThrowTemplateError($::template->error()); - exit; -} - # Implementations of several of the below were blatently stolen from CGI.pm, # by Lincoln D. Stein. diff --git a/template/en/default/global/message.txt.tmpl b/template/en/default/global/message.txt.tmpl new file mode 100644 index 000000000..07ee05b0a --- /dev/null +++ b/template/en/default/global/message.txt.tmpl @@ -0,0 +1,26 @@ +[%# 1.0@bugzilla.org %] +[%# The contents of this file are subject to the Mozilla Public + # License Version 1.1 (the "License"); you may not use this file + # except in compliance with the License. You may obtain a copy of + # the License at http://www.mozilla.org/MPL/ + # + # Software distributed under the License is distributed on an "AS + # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + # implied. See the License for the specific language governing + # rights and limitations under the License. + # + # The Original Code is the Bugzilla Bug Tracking System. + # + # The Initial Developer of the Original Code is Max Kanat-Alexander. + # Portions created by Max Kanat-Alexander are Copyright (C) 2005 + # Max Kanat-Alexander. All Rights Reserved. + # + # Contributor(s): Max Kanat-Alexander + #%] + +[% PROCESS global/variables.none.tmpl %] + +[%# Yes, this may show some HTML. But it's the best we + # can do at the moment. %] +[% PROCESS global/messages.html.tmpl %] +[% message %] -- cgit v1.2.3-24-g4f1b