1
# -*- Mode: perl; indent-tabs-mode: nil -*-
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/
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.
13
# The Original Code is the Bugzilla Bug Tracking System.
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
20
# Contributor(s): Terry Weissman <terry@mozilla.org>
21
# Dan Mosedale <dmose@mozilla.org>
22
# Jacob Steenhagen <jake@bugzilla.org>
23
# Bradley Baetz <bbaetz@student.usyd.edu.au>
24
# Christopher Aillon <christopher@aillon.com>
26
package Bugzilla::Util;
30
use base qw(Exporter);
31
@Bugzilla::Util::EXPORT = qw(is_tainted trick_taint detaint_natural
33
html_quote url_quote value_quote xml_quote
40
# This is from the perlsec page, slightly modifed to remove a warning
42
# This function makes use of the fact that the presence of
43
# tainted data anywhere within an expression renders the
44
# entire expression tainted.
45
# Don't ask me how it works...
47
return not eval { my $foo = join('',@_), kill 0; 1; };
52
Carp::confess("Undef to trick_taint") unless defined $_[0];
53
my ($match) = $_[0] =~ /^(.*)$/s;
55
return (defined($_[0]));
59
my ($match) = $_[0] =~ /^(\d+)$/;
61
return (defined($_[0]));
65
my ($match) = $_[0] =~ /^([-+]?\d+)$/;
67
# Remove any leading plus sign.
68
if (defined($_[0]) && $_[0] =~ /^\+(\d+)$/) {
71
return (defined($_[0]));
76
$var =~ s/\&/\&/g;
79
$var =~ s/\"/\"/g;
83
# This orignally came from CGI.pm, by Lincoln D. Stein
85
my ($toencode) = (@_);
86
$toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
91
my ($toencode) = (@_);
93
$toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("&#x%x;",ord($1))/eg;
99
$var =~ s/\&/\&/g;
102
$var =~ s/\"/\"/g;
103
# See bug http://bugzilla.mozilla.org/show_bug.cgi?id=4928 for
104
# explanaion of why bugzilla does this linebreak substitution.
105
# This caused form submission problems in mozilla (bug 22983, 32000).
106
$var =~ s/\r\n/\
/g;
107
$var =~ s/\n\r/\
/g;
108
$var =~ s/\r/\
/g;
109
$var =~ s/\n/\
/g;
115
$var =~ s/\&/\&/g;
118
$var =~ s/\"/\"/g;
119
$var =~ s/\'/\'/g;
124
my ($list,$item) = (@_);
126
foreach my $i (@$list) {
137
foreach my $val (@_) {
138
$max = $val if $val > $max;
145
foreach my $val (@_) {
146
$min = $val if $val < $min;
163
my ($year, $month, $day, $hour, $min, $sec);
164
if ($time =~ m/^\d{14}$/) {
165
# We appear to have a timestamp direct from MySQL
166
$year = substr($time,0,4);
167
$month = substr($time,4,2);
168
$day = substr($time,6,2);
169
$hour = substr($time,8,2);
170
$min = substr($time,10,2);
172
elsif ($time =~ m/^(\d{4})[-\.](\d{2})[-\.](\d{2}) (\d{2}):(\d{2})(:(\d{2}))?$/) {
181
warn "Date/Time format ($time) unrecogonzied";
185
$time = "$year-$month-$day $hour:$min";
189
$time .= " " . &::Param('timezone') if &::Param('timezone');
200
Bugzilla::Util - Generic utility functions for bugzilla
206
# Functions for dealing with variable tainting
207
$rv = is_tainted($var);
209
detaint_natural($var);
210
detaint_signed($var);
212
# Functions for quoting
218
# Functions for searching
219
$loc = lsearch(\@arr, $val);
220
$val = max($a, $b, $c);
221
$val = min($a, $b, $c);
223
# Functions for trimming variables
224
$val = trim(" abc ");
226
# Functions for formatting time
231
This package contains various utility functions which do not belong anywhere
234
B<It is not intended as a general dumping group for something which
235
people feel might be useful somewhere, someday>. Do not add methods to this
236
package unless it is intended to be used for a significant number of files,
237
and it does not belong anywhere else.
241
This package provides several types of routines:
245
Several functions are available to deal with tainted variables. B<Use these
246
with care> to avoid security holes.
252
Determines whether a particular variable is tainted
254
=item C<trick_taint($val)>
256
Tricks perl into untainting a particular variable.
258
Use trick_taint() when you know that there is no way that the data
259
in a scalar can be tainted, but taint mode still bails on it.
261
B<WARNING!! Using this routine on data that really could be tainted defeats
262
the purpose of taint mode. It should only be used on variables that have been
263
sanity checked in some way and have been determined to be OK.>
265
=item C<detaint_natural($num)>
267
This routine detaints a natural number. It returns a true value if the
268
value passed in was a valid natural number, else it returns false. You
269
B<MUST> check the result of this routine to avoid security holes.
271
=item C<detaint_signed($num)>
273
This routine detaints a signed integer. It returns a true value if the
274
value passed in was a valid signed integer, else it returns false. You
275
B<MUST> check the result of this routine to avoid security holes.
281
Some values may need to be quoted from perl. However, this should in general
282
be done in the template where possible.
286
=item C<html_quote($val)>
288
Returns a value quoted for use in HTML, with &, E<lt>, E<gt>, and E<34> being
289
replaced with their appropriate HTML entities.
291
=item C<url_quote($val)>
293
Quotes characters so that they may be included as part of a url.
295
=item C<css_class_quote($val)>
297
Quotes characters so that they may be used as CSS class names. Spaces
298
are replaced by underscores.
300
=item C<value_quote($val)>
302
As well as escaping html like C<html_quote>, this routine converts newlines
303
into 
, suitable for use in html attributes.
305
=item C<xml_quote($val)>
307
This is similar to C<html_quote>, except that ' is escaped to '. This
308
is kept separate from html_quote partly for compatibility with previous code
309
(for ') and partly for future handling of non-ASCII characters.
315
Functions for searching within a set of values.
319
=item C<lsearch($list, $item)>
321
Returns the position of C<$item> in C<$list>. C<$list> must be a list
324
If the item is not in the list, returns -1.
326
=item C<max($a, $b, ...)>
328
Returns the maximum from a set of values.
330
=item C<min($a, $b, ...)>
332
Returns the minimum from a set of values.
342
Removes any leading or trailing whitespace from a string. This routine does not
343
modify the existing string.
347
=head2 Formatting Time
351
=item C<format_time($time)>
353
Takes a time and appends the timezone as defined in editparams.cgi. This routine
354
will be expanded in the future to adjust for user preferences regarding what
355
timezone to display times in. In the future, it may also allow for the time to be
356
shown in different formats.