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>.
23 use C4::Auth qw( checkauth );
25 use C4::Output qw( output_error );
28 use Koha::Linktracker;
30 use List::MoreUtils qw( any );
33 my $uri = $cgi->param('uri') || '';
34 my $biblionumber = $cgi->param('biblionumber');;
35 my $itemnumber = $cgi->param('itemnumber');
36 $uri =~ s/^\s+|\s+$//g if $uri; # trim
38 my $tracking_method = C4::Context->preference('TrackClicks');
39 unless ( $tracking_method ) {
40 output_error( $cgi, '404' );
43 my $tracker = Koha::Linktracker->new(
44 { trackingmethod => $tracking_method } );
45 if ($uri && ($biblionumber || $itemnumber) ) {
48 # we have a uri and we want to track
49 if ( $tracker->trackingmethod() eq 'track' ) {
50 my ( $user, $cookie, $sessionID, $flags ) =
51 checkauth( $cgi, 1, {}, 'opac' );
52 my $userenv = C4::Context->userenv;
54 if ( defined($userenv)
55 && ref($userenv) eq 'HASH'
56 && $userenv->{number} )
58 $borrowernumber = $userenv->{number};
64 my $biblio = Koha::Biblios->find($biblionumber);
65 my $record = eval{ $biblio->metadata->record };
66 my $marc_urls = $record ? C4::Biblio::GetMarcUrls($record, C4::Context->preference('marcflavour')) : [];
67 my $search_crit = { uri => { -like => "%$uri%" } };
68 if( $itemnumber ) { # itemnumber is leading over biblionumber
69 $search_crit->{itemnumber} = $itemnumber;
70 } elsif( $biblionumber ) {
71 $search_crit->{biblionumber} = $biblionumber;
73 if ( ( any { $_ eq $uri } map { $_->{MARCURL} } @$marc_urls )
74 || Koha::Items->search( $search_crit )->count )
79 biblionumber => $biblionumber,
80 borrowernumber => $borrowernumber,
81 itemnumber => $itemnumber
83 ) if ( $tracker->trackingmethod() eq 'track' || $tracker->trackingmethod() eq 'anonymous' );
84 print $cgi->redirect($uri);
89 output_error( $cgi, '404' );