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{lc($_)} = $i; }
55 my (@result,%leftover);
56 $#result = $#$order; # preextend
58 my $key = lc(shift(@param));
60 if (exists $pos{$key}) {
61 $result[$pos{$key}] = shift(@param);
63 $leftover{$key} = shift(@param);
67 push (@result,make_attributes(\%leftover,1)) 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 lower 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);
88 $toencode =~ s{&}{&}gso;
89 $toencode =~ s{<}{<}gso;
90 $toencode =~ s{>}{>}gso;
91 $toencode =~ s{\"}{"}gso;
92 # Doesn't work. Can't work. forget it.
93 # $toencode =~ s{\x8b}{‹}gso;
94 # $toencode =~ s{\x9b}{›}gso;
98 # unescape URL-encoded data
100 shift() if ref($_[0]) || (defined $_[1] && $_[0] eq $CGI::DefaultClass);
101 my $todecode = shift;
102 return undef unless defined($todecode);
103 $todecode =~ tr/+/ /; # pluses become spaces
105 $todecode =~ s/%([0-9a-fA-F]{2})/chr $A2E[hex($1)]/ge;
107 $todecode =~ s/%([0-9a-fA-F]{2})/chr hex($1)/ge;
114 shift() if ref($_[0]) || (defined $_[1] && $_[0] eq $CGI::DefaultClass);
115 my $toencode = shift;
116 return undef unless defined($toencode);
117 $toencode=~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
121 # This internal routine creates date strings suitable for use in
122 # cookies and HTTP headers. (They differ, unfortunately.)
123 # Thanks to Mark Fisher for this.
125 my($time,$format) = @_;
128 my(@MON)=qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
129 my(@WDAY) = qw/Sun Mon Tue Wed Thu Fri Sat/;
131 # pass through preformatted dates for the sake of expire_calc()
132 $time = expire_calc($time);
133 return $time unless $time =~ /^\d+$/;
135 # make HTTP/cookie date string from GMT'ed time
136 # (cookies use '-' as date separator, HTTP uses ' ')
138 $sc = '-' if $format eq "cookie";
139 my($sec,$min,$hour,$mday,$mon,$year,$wday) = gmtime($time);
141 return sprintf("%s, %02d$sc%s$sc%04d %02d:%02d:%02d GMT",
142 $WDAY[$wday],$mday,$MON[$mon],$year,$hour,$min,$sec);
145 # This internal routine creates an expires time exactly some number of
146 # hours from the current time. It incorporates modifications from
156 # format for time can be in any of the forms...
157 # "now" -- expire immediately
158 # "+180s" -- in 180 seconds
159 # "+2m" -- in 2 minutes
160 # "+12h" -- in 12 hours
162 # "+3M" -- in 3 months
163 # "+2y" -- in 2 years
164 # "-3m" -- 3 minutes ago(!)
165 # If you don't supply one of these forms, we assume you are
166 # specifying the date yourself
168 if (!$time || (lc($time) eq 'now')) {
170 } elsif ($time=~/^\d+/) {
172 } elsif ($time=~/^([+-]?(?:\d+|\d*\.\d*))([mhdMy]?)/) {
173 $offset = ($mult{$2} || 1)*$1;
177 return (time+$offset);