Bug 20710: Update MARC21 frameworks to Update 26 (April 2018)
[koha.git] / opac / tracklinks.pl
blobb5743fe69949e1c022de424c57f594973621b10c
1 #!/usr/bin/perl
3 # script to log clicks on links to external urls
5 # Copyright 2012 Catalyst IT
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Modern::Perl;
22 use C4::Context;
23 use C4::Auth qw(checkauth);
24 use C4::Biblio;
25 use Koha::Linktracker;
26 use CGI qw ( -utf8 );
28 my $cgi = new CGI;
29 my $uri = $cgi->param('uri') || '';
31 my $tracker = Koha::Linktracker->new(
32 { trackingmethod => C4::Context->preference('TrackClicks') } );
34 if ($uri) {
35 if ( $tracker->trackingmethod() eq 'track'
36 || $tracker->trackingmethod() eq 'anonymous' )
38 my $borrowernumber = 0;
40 # we have a uri and we want to track
41 if ( $tracker->trackingmethod() eq 'track' ) {
42 my ( $user, $cookie, $sessionID, $flags ) =
43 checkauth( $cgi, 1, {}, 'opac' );
44 my $userenv = C4::Context->userenv;
46 if ( defined($userenv)
47 && ref($userenv) eq 'HASH'
48 && $userenv->{number} )
50 $borrowernumber = $userenv->{number};
53 # get borrower info
55 my $biblionumber = $cgi->param('biblionumber') || 0;
56 my $itemnumber = $cgi->param('itemnumber') || 0;
58 my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
59 my $marc_urls = C4::Biblio::GetMarcUrls($record, C4::Context->preference('marcflavour'));
60 if ( grep { $_ eq $uri } map { $_->{MARCURL} } @$marc_urls ) {
61 $tracker->trackclick(
63 uri => $uri,
64 biblionumber => $biblionumber,
65 borrowernumber => $borrowernumber,
66 itemnumber => $itemnumber
69 print $cgi->redirect($uri);
70 exit;
73 else {
75 # We have a valid url, but we shouldn't track it, just redirect
76 print $cgi->redirect($uri);
77 exit;
81 print $cgi->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
82 exit;