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