Bug 36511: Some scripts missing a dependency following Bug 24879
[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 );
29 use C4::Context;
30 use C4::Auth qw( check_cookie_auth );
31 use Koha::Biblios;
32 use Koha::CoverImages;
33
34 $| = 1;
35
36 my $input = CGI->new;
37 my ($auth_status) =
38     check_cookie_auth( $input->cookie('CGISESSID'), { catalogue => 1 } );
39 if ( $auth_status ne "ok" ) {
40     print $input->header( -type => 'text/plain', -status => '403 Forbidden' );
41     exit 0;
42 }
43
44 my $imagenumber;
45
46 =head1 NAME
47
48 image.pl - Script for retrieving and formatting local cover images for display
49
50 =head1 SYNOPSIS
51
52 <img src="image.pl?imagenumber=X" />
53 <img src="image.pl?biblionumber=X" />
54 <img src="image.pl?imagenumber=X&thumbnail=1" />
55 <img src="image.pl?biblionumber=X&thumbnail=1" />
56
57 =head1 DESCRIPTION
58
59 This script, when called from within HTML and passed a valid imagenumber or
60 biblionumber, will retrieve the image data associated with that biblionumber
61 if one exists, format it in proper HTML format and pass it back to be displayed.
62 If the parameter thumbnail has been provided, a thumbnail will be returned
63 rather than the full-size image. When a biblionumber is provided rather than an
64 imagenumber, a random image is selected.
65
66 =cut
67
68 my ( $image );
69 if ( C4::Context->preference("LocalCoverImages") ) {
70     my $imagenumber = $input->param('imagenumber');
71     my $biblionumber = $input->param('biblionumber');
72     if ( defined $imagenumber ) {
73         $imagenumber = $input->param('imagenumber');
74         $image = Koha::CoverImages->find($imagenumber);
75         unless ($image) {
76             print $input->redirect("/cgi-bin/koha/errors/404.pl");
77             exit;
78         }
79     }
80     elsif ( defined $biblionumber ) {
81         my $biblio = Koha::Biblios->find($biblionumber);
82         unless ($biblio) {
83             print $input->redirect("/cgi-bin/koha/errors/404.pl");
84             exit;
85         }
86         my $cover_images = $biblio->cover_images;
87         if ( $cover_images->count ) {
88             $image = $cover_images->next;
89         }
90     }
91 }
92
93 $image ||= Koha::CoverImages->no_image;
94
95 my $image_data =
96     $input->param('thumbnail')
97   ? $image->thumbnail
98   : $image->imagefile;
99
100 print $input->header(
101     -type            => $image->mimetype,
102     -expires         => '+30m',
103     -Content_Length  => length($image_data)
104 ), $image_data;
105
106 =head1 AUTHOR
107
108 Chris Nighswonger cnighswonger <at> foundations <dot> edu
109
110 modified for local cover images by Koustubha Kale kmkale <at> anantcorp <dot> com
111
112 =cut