Line 0
Link Here
|
0 |
- |
1 |
package Koha::CGI; |
|
|
2 |
|
3 |
# Copyright 2013 Universidad Nacional de Cordoba |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 2 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
=head1 NAME |
21 |
|
22 |
Koha::CGI - class to wrap the CGI module |
23 |
|
24 |
=head1 SYNOPSIS |
25 |
|
26 |
Object-oriented class that encapsulates the CGI module use. Its goal is |
27 |
overloading the 'param' method so it decodes UTF-8 data by default. |
28 |
|
29 |
It provides a way to avoid decoding, using a the method param_utf8_nodecode. |
30 |
|
31 |
=head1 DESCRIPTION |
32 |
|
33 |
CGI module wrapper. |
34 |
|
35 |
=cut |
36 |
|
37 |
use Modern::Perl; |
38 |
|
39 |
use Carp; |
40 |
use Encode qw/decode/; |
41 |
use Try::Tiny; |
42 |
|
43 |
use parent 'CGI'; |
44 |
|
45 |
=head2 param |
46 |
|
47 |
my $param = Koha::CGI->param($paramname); |
48 |
|
49 |
Read $paramname from the CGI parameters. It decodes UTF-8 data by default. |
50 |
|
51 |
=cut |
52 |
|
53 |
sub param { |
54 |
my ($self,@p) = @_; |
55 |
my @result = map { _decode_utf8_param($_); } $self->SUPER::param(@p); |
56 |
|
57 |
return wantarray ? @result : $result[0]; |
58 |
} |
59 |
|
60 |
=head2 param_utf8_nodecode |
61 |
|
62 |
my $param = Koha::CGI->param_utf8_nodecode($paramname); |
63 |
|
64 |
Read $paramname from the CGI parameters. It doesn't decode UTF-8 data |
65 |
by default. |
66 |
|
67 |
=cut |
68 |
|
69 |
sub param_utf8_nodecode { |
70 |
my ($self,@p) = @_; |
71 |
my @result = $self->SUPER::param(@p); |
72 |
|
73 |
return wantarray ? @result : $result[0]; |
74 |
} |
75 |
|
76 |
=head2 _decode_utf8_param (internal) |
77 |
|
78 |
Internal method that decodes UTF-8 data. It carps if exception |
79 |
raises during decoding. |
80 |
|
81 |
=cut |
82 |
|
83 |
sub _decode_utf8_param { |
84 |
my ($self,$param) = @_; |
85 |
|
86 |
try { |
87 |
$param = decode( 'UTF-8', $param, Encode::FB_CROAK ); |
88 |
} catch { |
89 |
carp "Error decoding: $_"; |
90 |
} |
91 |
|
92 |
return $param; |
93 |
} |
94 |
|
95 |
1; |
96 |
__END__ |
97 |
|
98 |
=head1 SEE ALSO |
99 |
|
100 |
CGI.pm |
101 |
|
102 |
=head1 AUTHORS |
103 |
|
104 |
Tomas Cohen Arazi <tomascohen at gmail dot com> |
105 |
|
106 |
=cut |