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   14455 use strict;
  20         45  
  20         708  
19 20     20   125 use Encode;
  20         62  
  20         2758  
20 20     20   142 use XAO::Utils;
  20         36  
  20         1105  
21 20     20   132 use XAO::Objects;
  20         38  
  20         19806  
22              
23             ###############################################################################
24              
25             sub new ($%) {
26 133     133 0 17058 my $proto=shift;
27 133         381 my $args=get_args(\@_);
28              
29 133         1227 my $cgi;
30 133 100       569 if($args->{'cgi'}) {
    100          
    50          
31 2         8 $cgi=$args->{'cgi'};
32             }
33             elsif($args->{'query'}) {
34 51         17480 require CGI;
35 51         534525 $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         477 require CGI;
43 80         245 $cgi=CGI->new();
44             }
45              
46 133 50       58645 $cgi || die "Cannot create proxied CGI instance";
47              
48 133         373 my $self={
49             cgi => $cgi,
50             };
51              
52 133   33     945 bless $self,ref($proto) || $proto;
53             }
54              
55             ###############################################################################
56              
57             our $AUTOLOAD;
58              
59             sub AUTOLOAD {
60 457     457   3607 my $self=shift;
61 457 50       998 return undef if !$self->{'cgi'};
62 457         1373 my @mpath=split('::',$AUTOLOAD);
63 457         878 my $method=$mpath[$#mpath];
64 457         1419 my $code=$self->{'cgi'}->can($method);
65 457 100       925 if(!$code) {
66 100 50       2491 return if $method eq 'DESTROY';
67 0         0 die "No method $method on $self->{'cgi'}";
68             }
69 357         881 return $code->($self->{'cgi'},@_);
70             }
71              
72             ###############################################################################
73              
74             sub can {
75 124     124 0 405 my ($self,$method)=@_;
76 124   100     828 return $self->SUPER::can($method) || $self->{'cgi'}->can($method);
77             }
78              
79             ###############################################################################
80              
81             sub cookie ($@) {
82 58     58 0 13271 my $self=shift;
83 58 100       134 if(@_) {
84 50         356 my @c1=caller(1);
85 50 50 33     407 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         231 return $self->{'cgi'}->cookie(@_);
91             }
92              
93             ###############################################################################
94              
95             sub set_param_charset($$) {
96 124     124 0 246 my ($self,$charset)=@_;
97              
98 124         217 my $old=$self->{'xao_param_charset'};
99 124         260 $self->{'xao_param_charset'}=$charset;
100              
101 124         259 return $old;
102             }
103              
104             ###############################################################################
105              
106             sub get_param_charset($$) {
107 2     2 0 47 my $self=shift;
108 2         12 return $self->{'xao_param_charset'};
109             }
110              
111             ###############################################################################
112              
113             sub param ($;$) {
114 76     76 0 1465 my $self=shift;
115              
116 76         125 my $charset=$self->{'xao_param_charset'};
117              
118 76 100       163 if(!$charset) {
119 41 100       81 if(wantarray) {
120 24         54 return $self->{'cgi'}->multi_param(@_);
121             }
122             else {
123 17         49 return $self->{'cgi'}->param(@_);
124             }
125             }
126             else {
127 35 50       67 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         104 my $value=$self->{'cgi'}->param(@_);
134 35 50       2028 return ref($value) ? $value : Encode::decode($charset,$value);
135             }
136             }
137             }
138              
139             ###############################################################################
140              
141             sub Vars ($) {
142 3     3 0 457 my $self=shift;
143              
144 3         11 my $charset=$self->{'xao_param_charset'};
145              
146 3 100       14 if(!$charset) {
147 1         12 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         13 my %vb=$self->{'cgi'}->Vars();
155 2         491 my %vu;
156 2         12 foreach my $param (keys %vb) {
157 8         422 $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__