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