Bug 10694: (follow-up) fix various issues
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use Modern::Perl;
22 use C4::Context;
23 use C4::Auth qw(checkauth);
24 use Koha::Linktracker;
25 use CGI;
26
27 my $cgi = new CGI;
28 my $uri = $cgi->param('uri') || '';
29
30 my $tracker = Koha::Linktracker->new(
31     { trackingmethod => C4::Context->preference('TrackClicks') } );
32
33 if ($uri) {
34     if (   $tracker->trackingmethod() eq 'track'
35         || $tracker->trackingmethod() eq 'anonymous' )
36     {
37         my $borrowernumber = 0;
38
39         # we have a uri and we want to track
40         if ( $tracker->trackingmethod() eq 'track' ) {
41             my ( $user, $cookie, $sessionID, $flags ) =
42               checkauth( $cgi, 1, {}, 'opac' );
43             my $userenv = C4::Context->userenv;
44
45             if (   defined($userenv)
46                 && ref($userenv) eq 'HASH'
47                 && $userenv->{number} )
48             {
49                 $borrowernumber = $userenv->{number};
50             }
51
52             # get borrower info
53         }
54         my $biblionumber = $cgi->param('biblionumber') || 0;
55         my $itemnumber   = $cgi->param('itemnumber')   || 0;
56
57         $tracker->trackclick(
58             {
59                 uri            => $uri,
60                 biblionumber   => $biblionumber,
61                 borrowernumber => $borrowernumber,
62                 itemnumber     => $itemnumber
63             }
64         );
65         print $cgi->redirect($uri);
66     }
67     else {
68
69         # We have a valid url, but we shouldn't track it, just redirect
70         print $cgi->redirect($uri);
71     }
72 }
73 else {
74
75     # we shouldn't be here, bail out
76     print $cgi->redirect("/cgi-bin/koha/errors/404.pl");    # escape early
77     exit;
78 }