]> mj.ucw.cz Git - libucw.git/blob - lib/perl/CGI.pm
Better avoid the brain-dead encoding " " as "+" when generating URL's.
[libucw.git] / lib / perl / CGI.pm
1 #       Poor Man's CGI Module for Perl
2 #
3 #       (c) 2002 Martin Mares <mj@ucw.cz>
4 #
5 #       This software may be freely distributed and used according to the terms
6 #       of the GNU Lesser General Public License.
7
8 package Sherlock::CGI;
9
10 use strict;
11 use warnings;
12
13 BEGIN {
14         # The somewhat hairy Perl export mechanism
15         use Exporter();
16         our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
17         $VERSION = 1.0;
18         @ISA = qw(Exporter);
19         @EXPORT = qw(&html_escape &url_escape &self_ref &self_form);
20         @EXPORT_OK = qw();
21         %EXPORT_TAGS = ();
22 }
23
24 sub url_escape($) {
25         my $x = shift @_;
26         $x =~ s/([^-\$_.!*'(),0-9A-Za-z\x80-\xff])/"%".unpack('H2',$1)/ge;
27         return $x;
28 }
29
30 sub html_escape($) {
31         my $x = shift @_;
32         $x =~ s/&/&amp;/g;
33         $x =~ s/</&lt;/g;
34         $x =~ s/>/&gt;/g;
35         $x =~ s/"/&quot;/g;
36         return $x;
37 }
38
39 our $arg_table;
40
41 sub parse_arg_string($) {
42         my ($s) = @_;
43         $s =~ s/\s+//;
44         foreach $_ (split /[&:]/,$s) {
45                 (/^([^=]+)=(.*)$/) or next;
46                 my $arg = $arg_table->{$1} or next;
47                 $_ = $2;
48                 s/\+/ /g;
49                 s/%(..)/pack("c",hex $1)/eg;
50                 s/(\r|\n|\t)/ /g;
51                 s/^\s+//;
52                 s/\s+$//;
53                 if (my $rx = $arg->{'check'}) {
54                         if (!/^$rx$/) { $_ = $arg->{'default'}; }
55                 }
56                 ${$arg->{'var'}} = $_;
57         }
58 }
59
60 sub parse_args($) {
61         $arg_table = shift @_;
62         foreach my $a (values %$arg_table) {
63                 defined($a->{'default'}) or $a->{'default'}="";
64                 ${$a->{'var'}} = $a->{'default'};
65         }
66         defined $ENV{"GATEWAY_INTERFACE"} or die "Not called as a CGI script";
67         my $method = $ENV{"REQUEST_METHOD"};
68         if ($method eq "GET") {
69                 parse_arg_string($ENV{"QUERY_STRING"});
70         } elsif ($method eq "POST") {
71                 if ($ENV{"CONTENT_TYPE"} =~ /^application\/x-www-form-urlencoded\b/i) {
72                         while (<STDIN>) {
73                                 chomp;
74                                 parse_arg_string($_);
75                         }
76                 } else {
77                         return "Unknown content type for POST data";
78                 }
79         } else {
80                 return "Unknown request method";
81         }
82 }
83
84 sub make_out_args($) {
85         my ($overrides) = @_;
86         my $out = {};
87         foreach my $name (keys %$arg_table) {
88                 my $arg = $arg_table->{$name};
89                 defined $arg->{'pass'} && !$arg->{'pass'} && !exists $overrides->{$name} && next;
90                 my $value;
91                 if (!defined($value = $overrides->{$name})) {
92                         if (exists $overrides->{$name}) {
93                                 $value = $arg->{'default'};
94                         } else {
95                                 $value = ${$arg->{'var'}};
96                         }
97                 }
98                 if ($value ne $arg->{'default'}) {
99                         $out->{$name} = $value;
100                 }
101         }
102         return $out;
103 }
104
105 sub self_ref(@) {
106         my %h = @_;
107         my $out = make_out_args(\%h);
108         return "?" . join(':', map { "$_=" . url_escape($out->{$_}) } sort keys %$out);
109 }
110
111 sub self_form(@) {
112         my %h = @_;
113         my $out = make_out_args(\%h);
114         return join('', map { "<input type=hidden name=$_ value='" . html_escape($out->{$_}) . "'>\n" } sort keys %$out);
115 }
116
117 1;  # OK