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   13886 use strict;
  20         44  
  20         1046  
19 20     20   109 use Encode;
  20         44  
  20         2516  
20 20     20   160 use XAO::Utils;
  20         43  
  20         1176  
21 20     20   118 use XAO::Objects;
  20         35  
  20         19357  
22              
23             ###############################################################################
24              
25             sub new ($%) {
26 133     133 0 16727 my $proto=shift;
27 133         335 my $args=get_args(\@_);
28              
29 133         1231 my $cgi;
30 133 100       536 if($args->{'cgi'}) {
    100          
    50          
31 2         5 $cgi=$args->{'cgi'};
32             }
33             elsif($args->{'query'}) {
34 51         17722 require CGI;
35 51         521510 $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         472 require CGI;
43 80         253 $cgi=CGI->new();
44             }
45              
46 133 50       55056 $cgi || die "Cannot create proxied CGI instance";
47              
48 133         380 my $self={
49             cgi => $cgi,
50             };
51              
52 133   33     949 bless $self,ref($proto) || $proto;
53             }
54              
55             ###############################################################################
56              
57             our $AUTOLOAD;
58              
59             sub AUTOLOAD {
60 457     457   3383 my $self=shift;
61 457 50       1389 return undef if !$self->{'cgi'};
62 457         1381 my @mpath=split('::',$AUTOLOAD);
63 457         886 my $method=$mpath[$#mpath];
64 457         1307 my $code=$self->{'cgi'}->can($method);
65 457 100       1246 if(!$code) {
66 100 50       2446 return if $method eq 'DESTROY';
67 0         0 die "No method $method on $self->{'cgi'}";
68             }
69 357         890 return $code->($self->{'cgi'},@_);
70             }
71              
72             ###############################################################################
73              
74             sub can {
75 124     124 0 438 my ($self,$method)=@_;
76 124   100     774 return $self->SUPER::can($method) || $self->{'cgi'}->can($method);
77             }
78              
79             ###############################################################################
80              
81             sub cookie ($@) {
82 58     58 0 13563 my $self=shift;
83 58 100       129 if(@_) {
84 50         360 my @c1=caller(1);
85 50 50 33     350 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         219 return $self->{'cgi'}->cookie(@_);
91             }
92              
93             ###############################################################################
94              
95             sub set_param_charset($$) {
96 124     124 0 279 my ($self,$charset)=@_;
97              
98 124         231 my $old=$self->{'xao_param_charset'};
99 124         289 $self->{'xao_param_charset'}=$charset;
100              
101 124         285 return $old;
102             }
103              
104             ###############################################################################
105              
106             sub get_param_charset($$) {
107 2     2 0 54 my $self=shift;
108 2         12 return $self->{'xao_param_charset'};
109             }
110              
111             ###############################################################################
112              
113             sub param ($;$) {
114 76     76 0 5314 my $self=shift;
115              
116 76         111 my $charset=$self->{'xao_param_charset'};
117              
118 76 100       156 if(!$charset) {
119 41 100       66 if(wantarray) {
120 24         110 return $self->{'cgi'}->multi_param(@_);
121             }
122             else {
123 17         45 return $self->{'cgi'}->param(@_);
124             }
125             }
126             else {
127 35 50       62 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         97 my $value=$self->{'cgi'}->param(@_);
134 35 50       2014 return ref($value) ? $value : Encode::decode($charset,$value);
135             }
136             }
137             }
138              
139             ###############################################################################
140              
141             sub Vars ($) {
142 3     3 0 522 my $self=shift;
143              
144 3         6 my $charset=$self->{'xao_param_charset'};
145              
146 3 100       14 if(!$charset) {
147 1         13 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         11 my %vb=$self->{'cgi'}->Vars();
155 2         507 my %vu;
156 2         8 foreach my $param (keys %vb) {
157 8         386 $vu{Encode::decode($charset,$param)}=Encode::decode($charset,$vb{$param});
158             }
159              
160 2 100       126 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__