View | Details | Raw Unified | Return to bug 19799
Collapse All | Expand All

(-)a/Koha/Util.pm (+84 lines)
Line 0 Link Here
1
package Koha::Util;
2
3
# Copyright Rijksmuseum 2018
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 3 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
use Modern::Perl;
21
use C4::Context;
22
23
=head1 NAME
24
25
Koha::Util
26
27
=head1 SYNOPSIS
28
29
    use Koha::Util;
30
    my $cgi = CGI->new;
31
    my $referer = Koha::Util::localReferer($cgi);
32
33
=head1 DESCRIPTION
34
35
Utility class
36
37
=head1 FUNCTIONS
38
39
=head2 localReferer
40
41
    my $referer = Koha::Util::localReferer( $cgi, { fallback => '/', remove_language => 1 });
42
43
    If the referer is a local URI, return local path.
44
    Otherwise return fallback.
45
46
=cut
47
48
sub localReferer {
49
    my ( $cgi, $params ) = @_;
50
    my $referer = $cgi->referer;
51
    my $fallback = $params->{fallback} // '/';
52
    return $fallback if !$referer;
53
54
    my $opacbase = C4::Context->preference('OPACBaseURL');
55
    my $rv;
56
    if( $opacbase ) {
57
        # Check OPACBaseURL
58
        if( substr($referer, 0, length($opacbase)) eq $opacbase &&
59
            $referer =~ /\/cgi-bin\/koha\// )
60
        {
61
            $rv = substr( $referer, length($opacbase) );
62
            $rv =~ s/^\///;
63
            $rv = '/'.$rv;
64
        }
65
    } else {
66
        # Check $cgi->url; but note that protocol returned by CGI is not reliable under Plack
67
        my $cgibase = $cgi->url( -base => 1 );
68
        $cgibase =~ s/^https?://;
69
        if( $referer =~ /$cgibase(\/cgi-bin\/koha\/.*)$/ ) {
70
            $rv = $1;
71
        }
72
    }
73
    $rv =~ s/(?<=[?&])language=[\w-]+(&|$)// if $rv and $params->{remove_language};
74
    return $rv // $fallback;
75
}
76
77
1;
78
79
=head1 AUTHOR
80
81
    Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands
82
    Koha development team
83
84
=cut
(-)a/t/Koha/Util/localReferer.t (-1 / +58 lines)
Line 0 Link Here
0
- 
1
use Modern::Perl;
2
3
use Test::More tests => 1;
4
use Test::MockObject;
5
6
use t::lib::Mocks;
7
use Koha::Util;
8
9
subtest 'Tests for localReferer' => sub {
10
    plan tests => 10;
11
12
    my ( $referer, $base );
13
    my $cgi = Test::MockObject->new;
14
    $cgi->mock( 'referer', sub { $referer } );
15
    $cgi->mock( 'url', sub { $base } ); # base for opac-changelanguage
16
17
    # Start with filled OPACBaseIRL
18
    t::lib::Mocks::mock_preference('OPACBaseURL', 'https://koha.nl' );
19
    $referer = 'https://somewhere.com/myscript';
20
    is( Koha::Util::localReferer($cgi), '/', 'External referer' );
21
22
    my $search = '/cgi-bin/koha/opac-search.pl?q=perl';
23
    $referer = "https://koha.nl$search";
24
    is( Koha::Util::localReferer($cgi), $search, 'opac-search' );
25
26
    $referer = 'https://koha.nl/custom/stuff';
27
    is( Koha::Util::localReferer($cgi), '/', 'custom url' );
28
29
    # trailing backslash
30
    t::lib::Mocks::mock_preference('OPACBaseURL', 'http://koha.nl/' );
31
    $referer = "http://koha.nl$search";
32
    is( Koha::Util::localReferer($cgi), $search, 'opac-search, trailing backslash' );
33
34
    # no OPACBaseURL
35
    t::lib::Mocks::mock_preference('OPACBaseURL', '');
36
    $referer = 'https://somewhere.com/myscript';
37
    $base = 'http://koha.nl';
38
    is( Koha::Util::localReferer($cgi), '/', 'no opacbaseurl, external' );
39
40
    $referer = "https://koha.nl$search";
41
    $base = 'https://koha.nl';
42
    is( Koha::Util::localReferer($cgi), $search, 'no opacbaseurl, opac-search' );
43
    $base = 'http://koha.nl';
44
    is( Koha::Util::localReferer($cgi), $search, 'no opacbaseurl, opac-search, protocol diff' );
45
46
    # base contains https, referer http (this should be very unusual)
47
    # test remove_language
48
    $referer = "http://koha.nl$search&language=zz-ZZ&debug=1";
49
    $base = 'https://koha.nl';
50
    is( Koha::Util::localReferer($cgi, { remove_language => 1 }), $search.'&debug=1', 'no opacbaseurl, opac-search, protocol diff (base https)' );
51
52
    # custom script, test fallback parameter
53
    $referer = 'https://koha.nl/custom/stuff';
54
    $base = 'https://koha.nl';
55
    is( Koha::Util::localReferer($cgi, { fallback => 'ZZZ' }), 'ZZZ', 'no opacbaseurl, custom url, test fallback' );
56
    $base = 'http://koha.nl';
57
    is( Koha::Util::localReferer($cgi), '/', 'no opacbaseurl, custom url, protocol diff' );
58
};

Return to bug 19799