Bug 23329: (follow-up) Use any instead of grep
[koha.git] / opac / tracklinks.pl
1 #!/usr/bin/perl
2
3 # script to log clicks on links to external urls
4
5 # Copyright 2012 Catalyst IT
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 use Modern::Perl;
22 use C4::Context;
23 use C4::Auth qw(checkauth);
24 use C4::Biblio;
25 use Koha::Items;
26 use Koha::Linktracker;
27 use CGI qw ( -utf8 );
28 use List::MoreUtils qw(any);
29
30 my $cgi = new CGI;
31 my $uri = $cgi->param('uri') || '';
32 my $biblionumber = $cgi->param('biblionumber') || 0;
33 my $itemnumber   = $cgi->param('itemnumber')   || 0;
34
35 my $tracker = Koha::Linktracker->new(
36     { trackingmethod => C4::Context->preference('TrackClicks') } );
37
38 if ($uri && ($biblionumber || $itemnumber) ) {
39     my $borrowernumber = 0;
40
41     # we have a uri and we want to track
42     if ( $tracker->trackingmethod() eq 'track' ) {
43         my ( $user, $cookie, $sessionID, $flags ) =
44           checkauth( $cgi, 1, {}, 'opac' );
45         my $userenv = C4::Context->userenv;
46
47         if (   defined($userenv)
48             && ref($userenv) eq 'HASH'
49             && $userenv->{number} )
50         {
51             $borrowernumber = $userenv->{number};
52         }
53
54         # get borrower info
55     }
56
57     my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
58     my $marc_urls = C4::Biblio::GetMarcUrls($record, C4::Context->preference('marcflavour'));
59     if ( ( any { $_ eq $uri } map { $_->{MARCURL} } @$marc_urls )
60         || Koha::Items->search( { itemnumber => $itemnumber, uri => $uri } )->count )
61     {
62         $tracker->trackclick(
63             {
64                 uri            => $uri,
65                 biblionumber   => $biblionumber,
66                 borrowernumber => $borrowernumber,
67                 itemnumber     => $itemnumber
68             }
69         ) if (   $tracker->trackingmethod() eq 'track' || $tracker->trackingmethod() eq 'anonymous' );
70         print $cgi->redirect($uri);
71         exit;
72     }
73 }
74
75 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");    # escape early
76 exit;