Bug 32482: (follow-up) Add markup comments
[koha.git] / opac / opac-patron-image.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2009 LibLime
4 # Parts copyright 2012 Athens County Public Libraries
5 #
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::Members;
23 use CGI qw ( -utf8 );
24 use CGI::Cookie;  # need to check cookies before having CGI parse the POST request
25 use C4::Auth qw( check_cookie_auth );
26 use Koha::Patron::Images;
27
28 my $query = CGI->new;
29
30 unless (C4::Context->preference('OPACpatronimages')) {
31     print $query->header(status => '403 Forbidden - displaying patron images in the OPAC not enabled');
32     exit;
33 }
34
35 my ($auth_status) = check_cookie_auth( $query->cookie('CGISESSID') );
36 if( $auth_status ne 'ok' ) {
37     print CGI::header( '-status' => '401' );
38     exit 0;
39 }
40
41 my $userenv = C4::Context->userenv;
42 my $patron_image = $userenv ? Koha::Patron::Images->find( $userenv->{number} ) : undef;
43
44 if ($patron_image) {
45     print $query->header(
46         -type           => $patron_image->mimetype,
47         -Content_Length => length( $patron_image->imagefile )
48       ),
49       $patron_image->imagefile;
50 } else {
51     print $query->header(status => '404 patron image not found');
52 }