]> mj.ucw.cz Git - libucw.git/blob - lib/perl/CGI.pm
Patch to allow processing of multiple occurences of the same argument.
[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                 if (${$arg->{'var'}} eq $arg->{'default'}) {
57                         ${$arg->{'var'}} = $_;
58                 } else {
59                         ${$arg->{'var'}} .= "&".$_;
60                 }
61         }
62 }
63
64 sub parse_args($) {
65         $arg_table = shift @_;
66         foreach my $a (values %$arg_table) {
67                 defined($a->{'default'}) or $a->{'default'}="";
68                 ${$a->{'var'}} = $a->{'default'};
69         }
70         defined $ENV{"GATEWAY_INTERFACE"} or die "Not called as a CGI script";
71         my $method = $ENV{"REQUEST_METHOD"};
72         if ($method eq "GET") {
73                 parse_arg_string($ENV{"QUERY_STRING"});
74         } elsif ($method eq "POST") {
75                 if ($ENV{"CONTENT_TYPE"} =~ /^application\/x-www-form-urlencoded\b/i) {
76                         while (<STDIN>) {
77                                 chomp;
78                                 parse_arg_string($_);
79                         }
80                 } else {
81                         return "Unknown content type for POST data";
82                 }
83         } else {
84                 return "Unknown request method";
85         }
86 }
87
88 sub make_out_args($) {
89         my ($overrides) = @_;
90         my $out = {};
91         foreach my $name (keys %$arg_table) {
92                 my $arg = $arg_table->{$name};
93                 defined $arg->{'pass'} && !$arg->{'pass'} && !exists $overrides->{$name} && next;
94                 my $value;
95                 if (!defined($value = $overrides->{$name})) {
96                         if (exists $overrides->{$name}) {
97                                 $value = $arg->{'default'};
98                         } else {
99                                 $value = ${$arg->{'var'}};
100                         }
101                 }
102                 if ($value ne $arg->{'default'}) {
103                         $out->{$name} = $value;
104                 }
105         }
106         return $out;
107 }
108
109 sub self_ref(@) {
110         my %h = @_;
111         my $out = make_out_args(\%h);
112         return "?" . join(':', map { "$_=" . url_escape($out->{$_}) } sort keys %$out);
113 }
114
115 sub self_form(@) {
116         my %h = @_;
117         my $out = make_out_args(\%h);
118         return join('', map { "<input type=hidden name=$_ value='" . html_escape($out->{$_}) . "'>\n" } sort keys %$out);
119 }
120
121 1;  # OK