File Coverage

blib/lib/XAO/DO/CGI.pm
Criterion Covered Total %
statement 63 73 86.3
branch 23 34 67.6
condition 5 9 55.5
subroutine 12 13 92.3
pod 0 8 0.0
total 103 137 75.1


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             XAO::DO::CGI - CGI interface for XAO::Web
4              
5             =head1 DESCRIPTION
6              
7             This is an extension of the standard CGI package that overrides its param()
8             method. If the current site has a 'charset' parameter in siteconfig then
9             parameters received from CGI are decoded from that charset into Perl
10             native unicode strings.
11              
12             =over
13              
14             =cut
15              
16             ###############################################################################
17             package XAO::DO::CGI;
18 20     20   20048 use strict;
  20         49  
  20         1193  
19 20     20   184 use Encode;
  20         41  
  20         3840  
20 20     20   250 use XAO::Utils;
  20         40  
  20         1799  
21 20     20   156 use XAO::Objects;
  20         54  
  20         25653  
22              
23             ###############################################################################
24              
25             sub new ($%) {
26 133     133 0 20735 my $proto=shift;
27 133         490 my $args=get_args(\@_);
28              
29 133         2679 my $cgi;
30 133 100       757 if($args->{'cgi'}) {
    100          
    50          
31 2         6 $cgi=$args->{'cgi'};
32             }
33             elsif($args->{'query'}) {
34 51         90858 require CGI;
35 51         1194343 $cgi=CGI->new($args->{'query'});
36             }
37             elsif($args->{'no_cgi'}) {
38 0         0 require CGI;
39 0         0 $cgi=CGI->new('foo=bar');
40             }
41             else {
42 80         830 require CGI;
43 80         380 $cgi=CGI->new();
44             }
45              
46 133 50       73952 $cgi || die "Cannot create proxied CGI instance";
47              
48 133         526 my $self={
49             cgi => $cgi,
50             };
51              
52 133   33     1347 bless $self,ref($proto) || $proto;
53             }
54              
55             ###############################################################################
56              
57             our $AUTOLOAD;
58              
59             sub AUTOLOAD {
60 457     457   4227 my $self=shift;
61 457 50       1370 return undef if !$self->{'cgi'};
62 457         1668 my @mpath=split('::',$AUTOLOAD);
63 457         1057 my $method=$mpath[$#mpath];
64 457         1818 my $code=$self->{'cgi'}->can($method);
65 457 100       1072 if(!$code) {
66 100 50       4483 return if $method eq 'DESTROY';
67 0         0 die "No method $method on $self->{'cgi'}";
68             }
69 357         1119 return $code->($self->{'cgi'},@_);
70             }
71              
72             ###############################################################################
73              
74             sub can {
75 124     124 0 456 my ($self,$method)=@_;
76 124   100     1160 return $self->SUPER::can($method) || $self->{'cgi'}->can($method);
77             }
78              
79             ###############################################################################
80              
81             sub cookie ($@) {
82 58     58 0 15234 my $self=shift;
83 58 100       149 if(@_) {
84 50         339 my @c1=caller(1);
85 50 50 33     366 if(!@c1 || $c1[3]!~/get_cookie/) {
86 0         0 my @c0=caller(0);
87 0 0       0 eprint "Using CGI::cookie() method is deprecated, consider switching to \$config->get_cookie() in ".join(':',map { $_ || '' } ($c0[1],$c0[2]));
  0         0  
88             }
89             }
90 58         259 return $self->{'cgi'}->cookie(@_);
91             }
92              
93             ###############################################################################
94              
95             sub set_param_charset($$) {
96 124     124 0 332 my ($self,$charset)=@_;
97              
98 124         316 my $old=$self->{'xao_param_charset'};
99 124         356 $self->{'xao_param_charset'}=$charset;
100              
101 124         316 return $old;
102             }
103              
104             ###############################################################################
105              
106             sub get_param_charset($$) {
107 2     2 0 58 my $self=shift;
108 2         14 return $self->{'xao_param_charset'};
109             }
110              
111             ###############################################################################
112              
113             sub param ($;$) {
114 76     76 0 1611 my $self=shift;
115              
116 76         223 my $charset=$self->{'xao_param_charset'};
117              
118 76 100       242 if(!$charset) {
119 41 100       128 if(wantarray) {
120 24         80 return $self->{'cgi'}->multi_param(@_);
121             }
122             else {
123 17         66 return $self->{'cgi'}->param(@_);
124             }
125             }
126             else {
127 35 50       73 if(wantarray) {
128             return map {
129 0 0       0 ref($_) ? $_ : Encode::decode($charset,$_)
130 0         0 } $self->{'cgi'}->multi_param(@_);
131             }
132             else {
133 35         139 my $value=$self->{'cgi'}->param(@_);
134 35 50       2658 return ref($value) ? $value : Encode::decode($charset,$value);
135             }
136             }
137             }
138              
139             ###############################################################################
140              
141             sub Vars ($) {
142 3     3 0 337 my $self=shift;
143              
144 3         14 my $charset=$self->{'xao_param_charset'};
145              
146 3 100       19 if(!$charset) {
147 1         19 return $self->{'cgi'}->Vars();
148             }
149              
150             # There is a known incompatibility in this implementation -- our
151             # hash is never tied and cannot be used to modify the CGI object
152             # values.
153             #
154 2         18 my %vb=$self->{'cgi'}->Vars();
155 2         561 my %vu;
156 2         10 foreach my $param (keys %vb) {
157 8         358 $vu{Encode::decode($charset,$param)}=Encode::decode($charset,$vb{$param});
158             }
159              
160 2 100       144 return wantarray ? %vu : \%vu;
161             }
162              
163             ###############################################################################
164              
165             sub multi_param ($;$) {
166 0     0 0   my $self=shift;
167 0           return $self->param(@_);
168             }
169              
170             ###############################################################################
171             1;
172             __END__