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