|
Line 0
Link Here
|
|
|
1 |
package Koha::Template::Plugin::EscapeURI; |
| 2 |
|
| 3 |
# Copyright Prosentient Systems 2015 |
| 4 |
|
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
use Template::Plugin::Filter; |
| 22 |
use base qw( Template::Plugin::Filter ); |
| 23 |
|
| 24 |
#This plugin is based on the original uri filter from version 2.26 of Template::Filters |
| 25 |
|
| 26 |
our $UNSAFE_SPEC = { |
| 27 |
RFC2732 => q{A-Za-z0-9\-_.!~*'()}, |
| 28 |
RFC3986 => q{A-Za-z0-9\-\._~"}, #This is actually a regression as per https://github.com/abw/Template2/issues/13 |
| 29 |
TRUE_RFC3986 => q{A-Za-z0-9\-\._~}, #http://tools.ietf.org/html/rfc3986#section-2.3 |
| 30 |
}; |
| 31 |
our $UNSAFE_CHARS = $UNSAFE_SPEC->{ TRUE_RFC3986 }; |
| 32 |
our $URI_REGEX; |
| 33 |
our $URI_ESCAPES; |
| 34 |
|
| 35 |
sub init { |
| 36 |
my $self = shift; |
| 37 |
my $name = $self->{ _CONFIG }->{ name } || 'escape_uri'; |
| 38 |
#Install a named filter so that we can use "| escape_uri" instead of "| $EscapeURI" |
| 39 |
$self->install_filter($name); |
| 40 |
return $self; |
| 41 |
} |
| 42 |
|
| 43 |
sub uri_escapes { |
| 44 |
return { |
| 45 |
map { ( chr($_), sprintf("%%%02X", $_) ) } (0..255), |
| 46 |
}; |
| 47 |
} |
| 48 |
|
| 49 |
sub filter { |
| 50 |
my ( $self, $text, $args, $config ) = @_; |
| 51 |
return "" unless $text; |
| 52 |
|
| 53 |
$URI_REGEX ||= qr/([^$UNSAFE_CHARS])/; |
| 54 |
$URI_ESCAPES ||= uri_escapes(); |
| 55 |
|
| 56 |
if ($] >= 5.008 && utf8::is_utf8($text)) { |
| 57 |
utf8::encode($text); |
| 58 |
} |
| 59 |
|
| 60 |
$text =~ s/$URI_REGEX/$URI_ESCAPES->{$1}/eg; |
| 61 |
return $text; |
| 62 |
} |
| 63 |
|
| 64 |
1; |