7 CGI::Util - various utilities
12 use vars '$VERSION','@EXPORT_OK','@ISA','$EBCDIC','@A2E';
15 @EXPORT_OK = qw(rearrange make_attributes unescape escape expires);
19 $EBCDIC = "\t" ne "\011";
22 0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 21, 11, 12, 13, 14, 15,
23 16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,
24 64, 90,127,123, 91,108, 80,125, 77, 93, 92, 78,107, 96, 75, 97,
25 240,241,242,243,244,245,246,247,248,249,122, 94, 76,126,110,111,
26 124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
27 215,216,217,226,227,228,229,230,231,232,233,173,224,189, 95,109,
28 121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
29 151,152,153,162,163,164,165,166,167,168,169,192, 79,208,161, 7,
30 32, 33, 34, 35, 36, 37, 6, 23, 40, 41, 42, 43, 44, 9, 10, 27,
31 48, 49, 26, 51, 52, 53, 54, 8, 56, 57, 58, 59, 4, 20, 62,255,
32 65,170, 74,177,159,178,106,181,187,180,154,138,176,202,175,188,
33 144,143,234,250,190,160,182,179,157,218,155,139,183,184,185,171,
34 100,101, 98,102, 99,103,158,104,116,113,114,115,120,117,118,119,
35 172,105,237,238,235,239,236,191,128,253,254,251,252,186,174, 89,
36 68, 69, 66, 70, 67, 71,156, 72, 84, 81, 82, 83, 88, 85, 86, 87,
37 140, 73,205,206,203,207,204,225,112,221,222,219,220,141,142,223
41 # Smart rearrangement of parameters to allow named parameter
42 # calling. We do the rearangement if:
43 # the first parameter begins with a -
45 my($order,@param) = @_;
46 return () unless @param;
48 if (ref($param[0]) eq 'HASH') {
49 @param = %{$param[0]};
52 unless (defined($param[0]) && substr($param[0],0,1) eq '-');
55 # map parameters into positional indices
59 foreach (ref($_) eq 'ARRAY' ? @$_ : $_) { $pos{$_} = $i; }
63 my (@result,%leftover);
64 $#result = $#$order; # preextend
66 my $key = uc(shift(@param));
68 if (exists $pos{$key}) {
69 $result[$pos{$key}] = shift(@param);
71 $leftover{$key} = shift(@param);
75 push (@result,make_attributes(\%leftover)) if %leftover;
81 return () unless $attr && ref($attr) && ref($attr) eq 'HASH';
82 my $escape = shift || 0;
84 foreach (keys %{$attr}) {
86 $key=~s/^\-//; # get rid of initial - if present
87 $key=~tr/a-z_/A-Z-/; # parameters are upper case, use dashes
88 my $value = $escape ? simple_escape($attr->{$_}) : $attr->{$_};
89 push(@att,defined($attr->{$_}) ? qq/$key="$value"/ : qq/$key/);
95 return unless defined (my $toencode = shift);
97 if ($1 eq '<') { '<' }
98 elsif ($1 eq '>') { '>' }
99 elsif ($1 eq '&') { '&' }
100 elsif ($1 eq '"') { '"' }
101 elsif ($1 eq "\x8b") { '‹' }
102 elsif ($1 eq "\x9b") { '›' }
108 # unescape URL-encoded data
110 shift() if ref($_[0]) || (defined $_[1] && $_[0] eq $CGI::DefaultClass);
111 my $todecode = shift;
112 return undef unless defined($todecode);
113 $todecode =~ tr/+/ /; # pluses become spaces
115 $todecode =~ s/%([0-9a-fA-F]{2})/chr $A2E[hex($1)]/ge;
117 $todecode =~ s/%([0-9a-fA-F]{2})/chr hex($1)/ge;
124 shift() if ref($_[0]) || (defined $_[1] && $_[0] eq $CGI::DefaultClass);
125 my $toencode = shift;
126 return undef unless defined($toencode);
127 $toencode=~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
131 # This internal routine creates date strings suitable for use in
132 # cookies and HTTP headers. (They differ, unfortunately.)
133 # Thanks to Mark Fisher for this.
135 my($time,$format) = @_;
138 my(@MON)=qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
139 my(@WDAY) = qw/Sun Mon Tue Wed Thu Fri Sat/;
141 # pass through preformatted dates for the sake of expire_calc()
142 $time = expire_calc($time);
143 return $time unless $time =~ /^\d+$/;
145 # make HTTP/cookie date string from GMT'ed time
146 # (cookies use '-' as date separator, HTTP uses ' ')
148 $sc = '-' if $format eq "cookie";
149 my($sec,$min,$hour,$mday,$mon,$year,$wday) = gmtime($time);
151 return sprintf("%s, %02d$sc%s$sc%04d %02d:%02d:%02d GMT",
152 $WDAY[$wday],$mday,$MON[$mon],$year,$hour,$min,$sec);
155 # This internal routine creates an expires time exactly some number of
156 # hours from the current time. It incorporates modifications from
166 # format for time can be in any of the forms...
167 # "now" -- expire immediately
168 # "+180s" -- in 180 seconds
169 # "+2m" -- in 2 minutes
170 # "+12h" -- in 12 hours
172 # "+3M" -- in 3 months
173 # "+2y" -- in 2 years
174 # "-3m" -- 3 minutes ago(!)
175 # If you don't supply one of these forms, we assume you are
176 # specifying the date yourself
178 if (!$time || (lc($time) eq 'now')) {
180 } elsif ($time=~/^\d+/) {
182 } elsif ($time=~/^([+-]?(?:\d+|\d*\.\d*))([mhdMy]?)/) {
183 $offset = ($mult{$2} || 1)*$1;
187 return (time+$offset);