CGI.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. # -*- Mode: perl; indent-tabs-mode: nil -*-
  2. #
  3. # The contents of this file are subject to the Mozilla Public
  4. # License Version 1.1 (the "License"); you may not use this file
  5. # except in compliance with the License. You may obtain a copy of
  6. # the License at http://www.mozilla.org/MPL/
  7. #
  8. # Software distributed under the License is distributed on an "AS
  9. # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10. # implied. See the License for the specific language governing
  11. # rights and limitations under the License.
  12. #
  13. # The Original Code is the Bugzilla Bug Tracking System.
  14. #
  15. # The Initial Developer of the Original Code is Netscape Communications
  16. # Corporation. Portions created by Netscape are
  17. # Copyright (C) 1998 Netscape Communications Corporation. All
  18. # Rights Reserved.
  19. #
  20. # Contributor(s): Bradley Baetz <bbaetz@student.usyd.edu.au>
  21. # Byron Jones <bugzilla@glob.com.au>
  22. # Marc Schumann <wurblzap@gmail.com>
  23. use strict;
  24. package Bugzilla::CGI;
  25. BEGIN {
  26. if ($^O =~ /MSWin32/i) {
  27. # Help CGI find the correct temp directory as the default list
  28. # isn't Windows friendly (Bug 248988)
  29. $ENV{'TMPDIR'} = $ENV{'TEMP'} || $ENV{'TMP'} || "$ENV{'WINDIR'}\\TEMP";
  30. }
  31. }
  32. use CGI qw(-no_xhtml -oldstyle_urls :private_tempfiles :unique_headers SERVER_PUSH);
  33. use base qw(CGI);
  34. use Bugzilla::Constants;
  35. use Bugzilla::Error;
  36. use Bugzilla::Util;
  37. # We need to disable output buffering - see bug 179174
  38. $| = 1;
  39. # Ignore SIGTERM and SIGPIPE - this prevents DB corruption. If the user closes
  40. # their browser window while a script is running, the web server sends these
  41. # signals, and we don't want to die half way through a write.
  42. $::SIG{TERM} = 'IGNORE';
  43. $::SIG{PIPE} = 'IGNORE';
  44. # CGI.pm uses AUTOLOAD, but explicitly defines a DESTROY sub.
  45. # We need to do so, too, otherwise perl dies when the object is destroyed
  46. # and we don't have a DESTROY method (because CGI.pm's AUTOLOAD will |die|
  47. # on getting an unknown sub to try to call)
  48. sub DESTROY {
  49. my $self = shift;
  50. $self->SUPER::DESTROY(@_);
  51. };
  52. sub new {
  53. my ($invocant, @args) = @_;
  54. my $class = ref($invocant) || $invocant;
  55. my $self = $class->SUPER::new(@args);
  56. # Make sure our outgoing cookie list is empty on each invocation
  57. $self->{Bugzilla_cookie_list} = [];
  58. # Send appropriate charset
  59. $self->charset(Bugzilla->params->{'utf8'} ? 'UTF-8' : '');
  60. # Redirect to urlbase/sslbase if we are not viewing an attachment.
  61. if (use_attachbase() && i_am_cgi()) {
  62. my $cgi_file = $self->url('-path_info' => 0, '-query' => 0, '-relative' => 1);
  63. $cgi_file =~ s/\?$//;
  64. my $urlbase = Bugzilla->params->{'urlbase'};
  65. my $sslbase = Bugzilla->params->{'sslbase'};
  66. my $path_regexp = $sslbase ? qr/^(\Q$urlbase\E|\Q$sslbase\E)/ : qr/^\Q$urlbase\E/;
  67. if ($cgi_file ne 'attachment.cgi' && $self->self_url !~ /$path_regexp/) {
  68. $self->redirect_to_urlbase;
  69. }
  70. }
  71. # Check for errors
  72. # All of the Bugzilla code wants to do this, so do it here instead of
  73. # in each script
  74. my $err = $self->cgi_error;
  75. if ($err) {
  76. # Note that this error block is only triggered by CGI.pm for malformed
  77. # multipart requests, and so should never happen unless there is a
  78. # browser bug.
  79. print $self->header(-status => $err);
  80. # ThrowCodeError wants to print the header, so it grabs Bugzilla->cgi
  81. # which creates a new Bugzilla::CGI object, which fails again, which
  82. # ends up here, and calls ThrowCodeError, and then recurses forever.
  83. # So don't use it.
  84. # In fact, we can't use templates at all, because we need a CGI object
  85. # to determine the template lang as well as the current url (from the
  86. # template)
  87. # Since this is an internal error which indicates a severe browser bug,
  88. # just die.
  89. die "CGI parsing error: $err";
  90. }
  91. return $self;
  92. }
  93. # We want this sorted plus the ability to exclude certain params
  94. sub canonicalise_query {
  95. my ($self, @exclude) = @_;
  96. # Reconstruct the URL by concatenating the sorted param=value pairs
  97. my @parameters;
  98. foreach my $key (sort($self->param())) {
  99. # Leave this key out if it's in the exclude list
  100. next if lsearch(\@exclude, $key) != -1;
  101. my $esc_key = url_quote($key);
  102. foreach my $value ($self->param($key)) {
  103. if (defined($value)) {
  104. my $esc_value = url_quote($value);
  105. push(@parameters, "$esc_key=$esc_value");
  106. }
  107. }
  108. }
  109. return join("&", @parameters);
  110. }
  111. sub clean_search_url {
  112. my $self = shift;
  113. # Delete any empty URL parameter
  114. my @cgi_params = $self->param;
  115. foreach my $param (@cgi_params) {
  116. if (defined $self->param($param) && $self->param($param) eq '') {
  117. $self->delete($param);
  118. $self->delete("${param}_type");
  119. }
  120. # Boolean Chart stuff is empty if it's "noop"
  121. if ($param =~ /\d-\d-\d/ && defined $self->param($param)
  122. && $self->param($param) eq 'noop')
  123. {
  124. $self->delete($param);
  125. }
  126. }
  127. # Delete certain parameters if the associated parameter is empty.
  128. $self->delete('bugidtype') if !$self->param('bug_id');
  129. $self->delete('emailtype1') if !$self->param('email1');
  130. $self->delete('emailtype2') if !$self->param('email2');
  131. }
  132. # Overwrite to ensure nph doesn't get set, and unset HEADERS_ONCE
  133. sub multipart_init {
  134. my $self = shift;
  135. # Keys are case-insensitive, map to lowercase
  136. my %args = @_;
  137. my %param;
  138. foreach my $key (keys %args) {
  139. $param{lc $key} = $args{$key};
  140. }
  141. # Set the MIME boundary and content-type
  142. my $boundary = $param{'-boundary'} || '------- =_aaaaaaaaaa0';
  143. delete $param{'-boundary'};
  144. $self->{'separator'} = "\r\n--$boundary\r\n";
  145. $self->{'final_separator'} = "\r\n--$boundary--\r\n";
  146. $param{'-type'} = SERVER_PUSH($boundary);
  147. # Note: CGI.pm::multipart_init up to v3.04 explicitly set nph to 0
  148. # CGI.pm::multipart_init v3.05 explicitly sets nph to 1
  149. # CGI.pm's header() sets nph according to a param or $CGI::NPH, which
  150. # is the desired behaviour.
  151. return $self->header(
  152. %param,
  153. ) . "WARNING: YOUR BROWSER DOESN'T SUPPORT THIS SERVER-PUSH TECHNOLOGY." . $self->multipart_end;
  154. }
  155. # Have to add the cookies in.
  156. sub multipart_start {
  157. my $self = shift;
  158. my %args = @_;
  159. # CGI.pm::multipart_start doesn't honour its own charset information, so
  160. # we do it ourselves here
  161. if (defined $self->charset() && defined $args{-type}) {
  162. # Remove any existing charset specifier
  163. $args{-type} =~ s/;.*$//;
  164. # and add the specified one
  165. $args{-type} .= '; charset=' . $self->charset();
  166. }
  167. my $headers = $self->SUPER::multipart_start(%args);
  168. # Eliminate the one extra CRLF at the end.
  169. $headers =~ s/$CGI::CRLF$//;
  170. # Add the cookies. We have to do it this way instead of
  171. # passing them to multpart_start, because CGI.pm's multipart_start
  172. # doesn't understand a '-cookie' argument pointing to an arrayref.
  173. foreach my $cookie (@{$self->{Bugzilla_cookie_list}}) {
  174. $headers .= "Set-Cookie: ${cookie}${CGI::CRLF}";
  175. }
  176. $headers .= $CGI::CRLF;
  177. return $headers;
  178. }
  179. # Override header so we can add the cookies in
  180. sub header {
  181. my $self = shift;
  182. # Add the cookies in if we have any
  183. if (scalar(@{$self->{Bugzilla_cookie_list}})) {
  184. if (scalar(@_) == 1) {
  185. # if there's only one parameter, then it's a Content-Type.
  186. # Since we're adding parameters we have to name it.
  187. unshift(@_, '-type' => shift(@_));
  188. }
  189. unshift(@_, '-cookie' => $self->{Bugzilla_cookie_list});
  190. }
  191. return $self->SUPER::header(@_) || "";
  192. }
  193. # CGI.pm is not utf8-aware and passes data as bytes instead of UTF-8 strings.
  194. sub param {
  195. my $self = shift;
  196. if (Bugzilla->params->{'utf8'} && scalar(@_) == 1) {
  197. if (wantarray) {
  198. return map { _fix_utf8($_) } $self->SUPER::param(@_);
  199. }
  200. else {
  201. return _fix_utf8(scalar $self->SUPER::param(@_));
  202. }
  203. }
  204. return $self->SUPER::param(@_);
  205. }
  206. sub _fix_utf8 {
  207. my $input = shift;
  208. # The is_utf8 is here in case CGI gets smart about utf8 someday.
  209. utf8::decode($input) if defined $input && !utf8::is_utf8($input);
  210. return $input;
  211. }
  212. # The various parts of Bugzilla which create cookies don't want to have to
  213. # pass them around to all of the callers. Instead, store them locally here,
  214. # and then output as required from |header|.
  215. sub send_cookie {
  216. my $self = shift;
  217. # Move the param list into a hash for easier handling.
  218. my %paramhash;
  219. my @paramlist;
  220. my ($key, $value);
  221. while ($key = shift) {
  222. $value = shift;
  223. $paramhash{$key} = $value;
  224. }
  225. # Complain if -value is not given or empty (bug 268146).
  226. if (!exists($paramhash{'-value'}) || !$paramhash{'-value'}) {
  227. ThrowCodeError('cookies_need_value');
  228. }
  229. # Add the default path and the domain in.
  230. $paramhash{'-path'} = Bugzilla->params->{'cookiepath'};
  231. $paramhash{'-domain'} = Bugzilla->params->{'cookiedomain'}
  232. if Bugzilla->params->{'cookiedomain'};
  233. # Move the param list back into an array for the call to cookie().
  234. foreach (keys(%paramhash)) {
  235. unshift(@paramlist, $_ => $paramhash{$_});
  236. }
  237. push(@{$self->{'Bugzilla_cookie_list'}}, $self->cookie(@paramlist));
  238. }
  239. # Cookies are removed by setting an expiry date in the past.
  240. # This method is a send_cookie wrapper doing exactly this.
  241. sub remove_cookie {
  242. my $self = shift;
  243. my ($cookiename) = (@_);
  244. # Expire the cookie, giving a non-empty dummy value (bug 268146).
  245. $self->send_cookie('-name' => $cookiename,
  246. '-expires' => 'Tue, 15-Sep-1998 21:49:00 GMT',
  247. '-value' => 'X');
  248. }
  249. # Redirect to https if required
  250. sub require_https {
  251. my ($self, $url) = @_;
  252. # Do not create query string if data submitted via XMLRPC
  253. # since we want the data to be resubmitted over POST method.
  254. my $query = Bugzilla->usage_mode == USAGE_MODE_WEBSERVICE ? 0 : 1;
  255. # XMLRPC clients (SOAP::Lite at least) requires 301 to redirect properly
  256. # and do not work with 302.
  257. my $status = Bugzilla->usage_mode == USAGE_MODE_WEBSERVICE ? 301 : 302;
  258. if (defined $url) {
  259. $url .= $self->url('-path_info' => 1, '-query' => $query, '-relative' => 1);
  260. } else {
  261. $url = $self->self_url;
  262. $url =~ s/^http:/https:/i;
  263. }
  264. print $self->redirect(-location => $url, -status => $status);
  265. # When using XML-RPC with mod_perl, we need the headers sent immediately.
  266. $self->r->rflush if $ENV{MOD_PERL};
  267. exit;
  268. }
  269. # Redirect to the urlbase version of the current URL.
  270. sub redirect_to_urlbase {
  271. my $self = shift;
  272. my $path = $self->url('-path_info' => 1, '-query' => 1, '-relative' => 1);
  273. print $self->redirect('-location' => correct_urlbase() . $path);
  274. exit;
  275. }
  276. 1;
  277. __END__
  278. =head1 NAME
  279. Bugzilla::CGI - CGI handling for Bugzilla
  280. =head1 SYNOPSIS
  281. use Bugzilla::CGI;
  282. my $cgi = new Bugzilla::CGI();
  283. =head1 DESCRIPTION
  284. This package inherits from the standard CGI module, to provide additional
  285. Bugzilla-specific functionality. In general, see L<the CGI.pm docs|CGI> for
  286. documention.
  287. =head1 CHANGES FROM L<CGI.PM|CGI>
  288. Bugzilla::CGI has some differences from L<CGI.pm|CGI>.
  289. =over 4
  290. =item C<cgi_error> is automatically checked
  291. After creating the CGI object, C<Bugzilla::CGI> automatically checks
  292. I<cgi_error>, and throws a CodeError if a problem is detected.
  293. =back
  294. =head1 ADDITIONAL FUNCTIONS
  295. I<Bugzilla::CGI> also includes additional functions.
  296. =over 4
  297. =item C<canonicalise_query(@exclude)>
  298. This returns a sorted string of the parameters, suitable for use in a url.
  299. Values in C<@exclude> are not included in the result.
  300. =item C<send_cookie>
  301. This routine is identical to the cookie generation part of CGI.pm's C<cookie>
  302. routine, except that it knows about Bugzilla's cookie_path and cookie_domain
  303. parameters and takes them into account if necessary.
  304. This should be used by all Bugzilla code (instead of C<cookie> or the C<-cookie>
  305. argument to C<header>), so that under mod_perl the headers can be sent
  306. correctly, using C<print> or the mod_perl APIs as appropriate.
  307. To remove (expire) a cookie, use C<remove_cookie>.
  308. =item C<remove_cookie>
  309. This is a wrapper around send_cookie, setting an expiry date in the past,
  310. effectively removing the cookie.
  311. As its only argument, it takes the name of the cookie to expire.
  312. =item C<require_https($baseurl)>
  313. This routine redirects the client to a different location using the https protocol.
  314. If the client is using XMLRPC, it will not retain the QUERY_STRING since XMLRPC uses POST.
  315. It takes an optional argument which will be used as the base URL. If $baseurl
  316. is not provided, the current URL is used.
  317. =item C<redirect_to_urlbase>
  318. Redirects from the current URL to one prefixed by the urlbase parameter.
  319. =back
  320. =head1 SEE ALSO
  321. L<CGI|CGI>, L<CGI::Cookie|CGI::Cookie>