Line 0
Link Here
|
|
|
1 |
package Koha::Template::AutoEscaping; |
2 |
|
3 |
use Modern::Perl; |
4 |
|
5 |
use Template::Config; |
6 |
use parent ($Template::Config::STASH, 'Class::Data::Inheritable'); |
7 |
|
8 |
use Template::Stash::AutoEscaping::RawString; |
9 |
|
10 |
our $DEBUG = 0; |
11 |
|
12 |
sub new { |
13 |
my $class = shift; |
14 |
my $self = $class->SUPER::new(@_); |
15 |
$self->{method_for_raw} ||= 'raw'; |
16 |
$self->{method_for_escape} ||= 'escape'; |
17 |
$self->{_raw_string_class} ||= 'Template::Stash::AutoEscaping::RawString'; |
18 |
$self->{ignore_escape} ||= []; |
19 |
$self->{die_on_unescaped} ||= 0; |
20 |
|
21 |
foreach my $ops ($Template::Stash::SCALAR_OPS, $Template::Stash::LIST_OPS) |
22 |
{ |
23 |
$ops->{$self->{method_for_raw}} = sub { |
24 |
my $scalar = shift; |
25 |
return $self->{_raw_string_class}->new($scalar); |
26 |
}; |
27 |
|
28 |
$ops->{$self->{method_for_escape}} = sub { |
29 |
my $scalar = shift; |
30 |
return $self->{_raw_string_class}->new( |
31 |
$self->escape($scalar), |
32 |
); |
33 |
}; |
34 |
|
35 |
}; |
36 |
return $self; |
37 |
} |
38 |
|
39 |
sub get_raw_args { |
40 |
my ( $args, $escaped_class ) = @_; |
41 |
my $changed = 0; |
42 |
my @raw_args; |
43 |
for my $v (@{ $args }) { |
44 |
my $new_v; |
45 |
if ( ref $v eq $escaped_class ) { |
46 |
$changed = 1; |
47 |
$new_v = $v->[0]; |
48 |
} elsif (ref $v eq 'ARRAY') { |
49 |
$new_v = get_raw_args($v, $escaped_class); |
50 |
if ($new_v) { |
51 |
$changed = 1; |
52 |
} else { |
53 |
$new_v = $v; |
54 |
} |
55 |
} else { |
56 |
$new_v = $v; |
57 |
} |
58 |
push @raw_args, $new_v; |
59 |
} |
60 |
|
61 |
return unless $changed; |
62 |
return \@raw_args; |
63 |
} |
64 |
|
65 |
sub get { |
66 |
my ( $self, @args ) = @_; |
67 |
|
68 |
# note: hack for [% hash.${key} %] [% hash.item(key) %] |
69 |
# key expected raw string. |
70 |
if (ref $args[0] eq "ARRAY" && (scalar @{$args[0]} > 2)){ |
71 |
my $changed = get_raw_args($args[0], 'Koha::Template::AutoEscaping'); |
72 |
# retry by non-escaped args |
73 |
if ($changed) { |
74 |
$args[0] = $changed; |
75 |
return $self->get(@args); |
76 |
} |
77 |
} |
78 |
|
79 |
my ($var) = $self->SUPER::get(@args); |
80 |
if (ref $args[0] eq "ARRAY") { |
81 |
my $key = $args[0]->[0]; |
82 |
warn $key if $DEBUG; |
83 |
if (grep { $key eq $_ } @{ $self->{ignore_escape} }) { |
84 |
warn "ignore escape $key" if $DEBUG; |
85 |
return $var; |
86 |
} |
87 |
} |
88 |
|
89 |
my $ref = ref $var; |
90 |
# string |
91 |
if ((!$ref) and (length($var) > 0)) { |
92 |
return $self->escape($var); |
93 |
} |
94 |
# via .raw vmethod |
95 |
if ($ref eq $self->{_raw_string_class}) { |
96 |
return "$var"; |
97 |
} |
98 |
return $var; |
99 |
} |
100 |
|
101 |
# With %XML => xml_escape subroutine from Mojo |
102 |
#our %XML = ( |
103 |
# '&' => '&', |
104 |
# '<' => '<', |
105 |
# '>' => '>', |
106 |
# '"' => '"', |
107 |
# '\'' => ''' |
108 |
#); |
109 |
|
110 |
sub escape { |
111 |
my $self = shift; |
112 |
my $text = shift; |
113 |
for ($text) { |
114 |
s/&/&/g; |
115 |
s/</</g; |
116 |
s/>/>/g; |
117 |
s/"/"/g; |
118 |
s/'/'/g; |
119 |
} |
120 |
return $text; |
121 |
} |
122 |
|
123 |
1; |