# -*- Mode: perl; indent-tabs-mode: nil -*- # # 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 Netscape Communications # Corporation. Portions created by Netscape are # Copyright (C) 1998 Netscape Communications Corporation. All # Rights Reserved. # # Contributor(s): Terry Weissman # Dan Mosedale # Contains some global routines used throughout the CGI scripts of Bugzilla. use diagnostics; use strict; # Shut up misguided -w warnings about "used only once". For some reason, # "use vars" chokes on me when I try it here. sub CGI_pl_sillyness { my $zz; $zz = %::FILENAME; $zz = %::dontchange; } use CGI::Carp qw(fatalsToBrowser); require 'globals.pl'; sub GeneratePersonInput { my ($field, $required, $def_value, $extraJavaScript) = (@_); $extraJavaScript ||= ""; if ($extraJavaScript ne "") { $extraJavaScript = "onChange=\"$extraJavaScript\""; } return ""; } sub GeneratePeopleInput { my ($field, $def_value) = (@_); return ""; } # Implementations of several of the below were blatently stolen from CGI.pm, # by Lincoln D. Stein. # Get rid of all the %xx encoding and the like from the given URL. sub url_decode { my ($todecode) = (@_); $todecode =~ tr/+/ /; # pluses become spaces $todecode =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge; return $todecode; } # Quotify a string, suitable for putting into a URL. sub url_quote { my($toencode) = (@_); $toencode=~s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg; return $toencode; } sub ParseUrlString { my ($buffer, $f, $m) = (@_); undef %$f; undef %$m; my %isnull; my $remaining = $buffer; while ($remaining ne "") { my $item; if ($remaining =~ /^([^&]*)&(.*)$/) { $item = $1; $remaining = $2; } else { $item = $remaining; $remaining = ""; } my $name; my $value; if ($item =~ /^([^=]*)=(.*)$/) { $name = $1; $value = url_decode($2); } else { $name = $item; $value = ""; } if ($value ne "") { if (defined $f->{$name}) { $f->{$name} .= $value; my $ref = $m->{$name}; push @$ref, $value; } else { $f->{$name} = $value; $m->{$name} = [$value]; } } else { $isnull{$name} = 1; } } if (defined %isnull) { foreach my $name (keys(%isnull)) { if (!defined $f->{$name}) { $f->{$name} = ""; $m->{$name} = []; } } } } sub ProcessFormFields { my ($buffer) = (@_); return ParseUrlString($buffer, \%::FORM, \%::MFORM); } sub ProcessMultipartFormFields { my ($boundary) = (@_); $boundary =~ s/^-*//; my $remaining = $ENV{"CONTENT_LENGTH"}; my $inheader = 1; my $itemname = ""; # open(DEBUG, ">debug") || die "Can't open debugging thing"; # print DEBUG "Boundary is '$boundary'\n"; while ($remaining > 0 && ($_ = )) { $remaining -= length($_); # print DEBUG "< $_"; if ($_ =~ m/^-*$boundary/) { # print DEBUG "Entered header\n"; $inheader = 1; $itemname = ""; next; } if ($inheader) { if (m/^\s*$/) { $inheader = 0; # print DEBUG "left header\n"; $::FORM{$itemname} = ""; } if (m/^Content-Disposition:\s*form-data\s*;\s*name\s*=\s*"([^\"]+)"/i) { $itemname = $1; # print DEBUG "Found itemname $itemname\n"; if (m/;\s*filename\s*=\s*"([^\"]+)"/i) { $::FILENAME{$itemname} = $1; } } next; } $::FORM{$itemname} .= $_; } delete $::FORM{""}; # Get rid of trailing newlines. foreach my $i (keys %::FORM) { chomp($::FORM{$i}); $::FORM{$i} =~ s/\r$//; } } # check and see if a given field exists, is non-empty, and is set to a # legal value. assume a browser bug and abort appropriately if not. # if $legalsRef is not passed, just check to make sure the value exists and # is non-NULL # sub CheckFormField (\%$;\@) { my ($formRef, # a reference to the form to check (a hash) $fieldname, # the fieldname to check $legalsRef # (optional) ref to a list of legal values ) = @_; if ( !defined $formRef->{$fieldname} || trim($formRef->{$fieldname}) eq "" || (defined($legalsRef) && lsearch($legalsRef, $formRef->{$fieldname})<0) ){ print "A legal $fieldname was not set; "; print Param("browserbugmessage"); PutFooter(); exit 0; } } # check and see if a given field is defined, and abort if not # sub CheckFormFieldDefined (\%$) { my ($formRef, # a reference to the form to check (a hash) $fieldname, # the fieldname to check ) = @_; if ( !defined $formRef->{$fieldname} ) { print "$fieldname was not defined; "; print Param("browserbugmessage"); PutFooter(); exit 0; } } # check and see if a given string actually represents a positive # integer, and abort if not. # sub CheckPosInt($) { my ($number) = @_; # the fieldname to check if ( $number !~ /^[1-9][0-9]*$/ ) { print "Received string \"$number\" when postive integer expected; "; print Param("browserbugmessage"); PutFooter(); exit 0; } } sub FormData { my ($field) = (@_); return $::FORM{$field}; } sub html_quote { my ($var) = (@_); $var =~ s/\&/\&/g; $var =~ s//\>/g; return $var; } sub value_quote { my ($var) = (@_); $var =~ s/\&/\&/g; $var =~ s//\>/g; $var =~ s/"/\"/g; $var =~ s/\n/\ /g; $var =~ s/\r/\ /g; return $var; } sub navigation_header { if (defined $::COOKIE{"BUGLIST"} && $::COOKIE{"BUGLIST"} ne "" && defined $::FORM{'id'}) { my @bugs = split(/:/, $::COOKIE{"BUGLIST"}); my $cur = lsearch(\@bugs, $::FORM{"id"}); print "Bug List: (@{[$cur + 1]} of @{[$#bugs + 1]})\n"; print "First\n"; print "Last\n"; if ($cur > 0) { print "Prev\n"; } else { print "Prev\n"; } if ($cur < $#bugs) { $::next_bug = $bugs[$cur + 1]; print "Next\n"; } else { print "Next\n"; } print qq{  Show list\n}; } print "     Query page\n"; print "     Enter new bug\n" } sub make_checkboxes { my ($src,$default,$isregexp,$name) = (@_); my $last = ""; my $capitalized = ""; my $popup = ""; my $found = 0; $default = "" if !defined $default; if ($src) { foreach my $item (@$src) { if ($item eq "-blank-" || $item ne $last) { if ($item eq "-blank-") { $item = ""; } $last = $item; $capitalized = $item; $capitalized =~ tr/A-Z/a-z/; $capitalized =~ s/^(.?)(.*)/\u$1$2/; if ($isregexp ? $item =~ $default : $default eq $item) { $popup .= "$capitalized
"; $found = 1; } else { $popup .= "$capitalized
"; } } } } if (!$found && $default ne "") { $popup .= "$default"; } return $popup; } # # make_selection_widget: creates an HTML selection widget from a list of text strings. # $groupname is the name of the setting (form value) that this widget will control # $src is the list of options # you can specify a $default value which is either a string or a regex pattern to match to # identify the default value # $capitalize lets you optionally capitalize the option strings; the default is the value # of Param("capitalizelists") # $multiple is 1 if several options are selectable (default), 0 otherwise. # $size is used for lists to control how many items are shown. The default is 7. A list of # size 1 becomes a popup menu. # $preferLists is 1 if selection lists should be used in favor of radio buttons and # checkboxes, and 0 otherwise. The default is the value of Param("preferlists"). # # The actual widget generated depends on the parameter settings: # # MULTIPLE PREFERLISTS SIZE RESULT # 0 (single) 0 =1 Popup Menu (normal for list of size 1) # 0 (single) 0 >1 Radio buttons # 0 (single) 1 =1 Popup Menu (normal for list of size 1) # 0 (single) 1 n>1 List of size n, single selection # 1 (multi) 0 n/a Check boxes; size ignored # 1 (multi) 1 n/a List of size n, multiple selection, of size n # sub make_selection_widget { my ($groupname,$src,$default,$isregexp,$multiple, $size, $capitalize, $preferLists) = (@_); my $last = ""; my $popup = ""; my $found = 0; my $displaytext = ""; $groupname = "" if !defined $groupname; $default = "" if !defined $default; $capitalize = Param("capitalizelists") if !defined $capitalize; $multiple = 1 if !defined $multiple; $preferLists = Param("preferlists") if !defined $preferLists; $size = 7 if !defined $size; my $type = "LIST"; if (!$preferLists) { if ($multiple) { $type = "CHECKBOX"; } else { if ($size > 1) { $type = "RADIO"; } } } if ($type eq "LIST") { $popup .= "$displaytext
"; } else { $popup .= "