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

(-)a/Koha/Util.pm (+87 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, staff => 1 });
42
43
    If the referer is a local URI, return local path.
44
    Otherwise return fallback.
45
    Optional parameters fallback, remove_language and staff. If you do not
46
    pass staff, opac is assumed.
47
48
=cut
49
50
sub localReferer {
51
    my ( $cgi, $params ) = @_;
52
    my $referer = $cgi->referer;
53
    my $fallback = $params->{fallback} // '/';
54
    my $staff = $params->{staff}; # no staff means OPAC
55
    return $fallback if !$referer;
56
57
    my $base = C4::Context->preference($staff ? 'staffClientBaseURL' : 'OPACBaseURL');
58
    my $rv;
59
60
    # Try ..BaseURL first, otherwise use CGI::url
61
    if( $base ) {
62
        if( substr($referer, 0, length($base)) eq $base &&
63
            $referer =~ /\/cgi-bin\/koha\// )
64
        {
65
            $rv = substr( $referer, length($base) );
66
            $rv =~ s/^\///;
67
            $rv = '/'.$rv;
68
        }
69
    } else {
70
        my $cgibase = $cgi->url( -base => 1 );
71
        $cgibase =~ s/^https?://;
72
        if( $referer =~ /$cgibase(\/cgi-bin\/koha\/.*)$/ ) {
73
            $rv = $1;
74
        }
75
    }
76
    $rv =~ s/(?<=[?&])language=[\w-]+(&|$)// if $rv and $params->{remove_language};
77
    return $rv // $fallback;
78
}
79
80
1;
81
82
=head1 AUTHOR
83
84
    Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands
85
    Koha development team
86
87
=cut
(-)a/t/Koha/Util/localReferer.t (-1 / +60 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 parameters remove_language. staff
48
    t::lib::Mocks::mock_preference('staffClientBaseURL', '' );
49
    $search = '/cgi-bin/koha/catalogue/search.pl?q=perl'; # staff
50
    $referer = "http://koha.nl:8080$search&language=zz-ZZ&debug=1";
51
    $base = 'https://koha.nl:8080';
52
    is( Koha::Util::localReferer($cgi, { remove_language => 1, staff => 1 }), $search.'&debug=1', 'no baseurl, staff search, protocol diff (base https)' );
53
54
    # custom script, test fallback parameter
55
    $referer = 'https://koha.nl/custom/stuff';
56
    $base = 'https://koha.nl';
57
    is( Koha::Util::localReferer($cgi, { fallback => 'ZZZ' }), 'ZZZ', 'no opacbaseurl, custom url, test fallback' );
58
    $base = 'http://koha.nl';
59
    is( Koha::Util::localReferer($cgi), '/', 'no opacbaseurl, custom url, protocol diff' );
60
};

Return to bug 21299