Bug 36575: Return correct patron when there is a shared userid / cardnumber
[koha.git] / catalogue / image.pl
1 #!/usr/bin/perl
2 #
3 # Copyright (C) 2011 C & P Bibliography Services
4 # Jared Camins-Esakov <jcamins@cpbibliograpy.com>
5 #
6 # based on patronimage.pl
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 #
23 #
24 #
25
26 use Modern::Perl;
27
28 use CGI qw ( -utf8 );    #qw(:standard escapeHTML);
29 use C4::Context;
30 use C4::Auth qw( check_cookie_auth );
31 use Koha::Biblios;
32 use Koha::CoverImages;
33 use Koha::Biblios;
34 use Koha::Exceptions;
35
36 $| = 1;
37
38 my $data  = CGI->new;
39 my ($auth_status) =
40     check_cookie_auth( $data->cookie('CGISESSID'), { catalogue => 1 } );
41 if ( $auth_status ne "ok" ) {
42     print $data->header( -type => 'text/plain', -status => '403 Forbidden' );
43     exit 0;
44 }
45
46 my $imagenumber;
47
48 =head1 NAME
49
50 image.pl - Script for retrieving and formatting local cover images for display
51
52 =head1 SYNOPSIS
53
54 <img src="image.pl?imagenumber=X" />
55 <img src="image.pl?biblionumber=X" />
56 <img src="image.pl?imagenumber=X&thumbnail=1" />
57 <img src="image.pl?biblionumber=X&thumbnail=1" />
58
59 =head1 DESCRIPTION
60
61 This script, when called from within HTML and passed a valid imagenumber or
62 biblionumber, will retrieve the image data associated with that biblionumber
63 if one exists, format it in proper HTML format and pass it back to be displayed.
64 If the parameter thumbnail has been provided, a thumbnail will be returned
65 rather than the full-size image. When a biblionumber is provided rather than an
66 imagenumber, a random image is selected.
67
68 =cut
69
70 my ( $image );
71 if ( C4::Context->preference("LocalCoverImages") ) {
72     my $imagenumber = $data->param('imagenumber');
73     my $biblionumber = $data->param('biblionumber');
74     if ( defined $imagenumber ) {
75         $imagenumber = $data->param('imagenumber');
76         $image = Koha::CoverImages->find($imagenumber);
77     }
78     elsif ( defined $biblionumber ) {
79         my $biblio = Koha::Biblios->find($biblionumber);
80         Koha::Exceptions::ObjectNotFound->throw( 'No bibliographic record for biblionumber ' . $biblionumber ) unless $biblio;
81         my $cover_images = $biblio->cover_images;
82         if ( $cover_images->count ) {
83             $image = $cover_images->next;
84         }
85     }
86 }
87
88 $image ||= Koha::CoverImages->no_image;
89
90 my $image_data =
91     $data->param('thumbnail')
92   ? $image->thumbnail
93   : $image->imagefile;
94
95 print $data->header(
96     -type            => $image->mimetype,
97     -expires         => '+30m',
98     -Content_Length  => length($image_data)
99 ), $image_data;
100
101 =head1 AUTHOR
102
103 Chris Nighswonger cnighswonger <at> foundations <dot> edu
104
105 modified for local cover images by Koustubha Kale kmkale <at> anantcorp <dot> com
106
107 =cut