From 5f43478512a884243bb56f8eac2d8b4dc8211d96 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 11 May 2023 15:49:08 +0200 Subject: [PATCH] Bug 33047: Return 404 instead of 500 when biblio does not exist If the biblionumber or the itemnumber passed in parameter does not exist we should return 404 instead of exploding with a 500. Test plan: Attach cover images to biblio and items Notice that the UI is working correctly (staff and OPAC) Hit catalogue/image.pl and opac/opac-image.pl with non-existent biblionumber and imagenumber Notice that you now get 404 instead of 500 Signed-off-by: Owen Leonard Signed-off-by: Katrin Fischer Signed-off-by: Tomas Cohen Arazi --- catalogue/image.pl | 26 ++++++++++++++++---------- opac/opac-image.pl | 21 ++++++++++++++------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/catalogue/image.pl b/catalogue/image.pl index 62e1ebb7ca..41eebc019c 100755 --- a/catalogue/image.pl +++ b/catalogue/image.pl @@ -25,15 +25,14 @@ use Modern::Perl; -use CGI qw ( -utf8 ); #qw(:standard escapeHTML); +use CGI qw ( -utf8 ); use C4::Context; -use Koha::CoverImages; use Koha::Biblios; -use Koha::Exceptions; +use Koha::CoverImages; $| = 1; -my $data = CGI->new; +my $input = CGI->new; my $imagenumber; =head1 NAME @@ -60,15 +59,22 @@ imagenumber, a random image is selected. my ( $image ); if ( C4::Context->preference("LocalCoverImages") ) { - my $imagenumber = $data->param('imagenumber'); - my $biblionumber = $data->param('biblionumber'); + my $imagenumber = $input->param('imagenumber'); + my $biblionumber = $input->param('biblionumber'); if ( defined $imagenumber ) { - $imagenumber = $data->param('imagenumber'); + $imagenumber = $input->param('imagenumber'); $image = Koha::CoverImages->find($imagenumber); + unless ($image) { + print $input->redirect("/cgi-bin/koha/errors/404.pl"); + exit; + } } elsif ( defined $biblionumber ) { my $biblio = Koha::Biblios->find($biblionumber); - Koha::Exceptions::ObjectNotFound->throw( 'No bibliographic record for biblionumber ' . $biblionumber ) unless $biblio; + unless ($biblio) { + print $input->redirect("/cgi-bin/koha/errors/404.pl"); + exit; + } my $cover_images = $biblio->cover_images; if ( $cover_images->count ) { $image = $cover_images->next; @@ -79,11 +85,11 @@ if ( C4::Context->preference("LocalCoverImages") ) { $image ||= Koha::CoverImages->no_image; my $image_data = - $data->param('thumbnail') + $input->param('thumbnail') ? $image->thumbnail : $image->imagefile; -print $data->header( +print $input->header( -type => $image->mimetype, -expires => '+30m', -Content_Length => length($image_data) diff --git a/opac/opac-image.pl b/opac/opac-image.pl index 56cf552eb8..040bd2d7cb 100755 --- a/opac/opac-image.pl +++ b/opac/opac-image.pl @@ -32,7 +32,7 @@ use Koha::CoverImages; $| = 1; -my $data = CGI->new; +my $input = CGI->new; my $imagenumber; =head1 NAME @@ -59,15 +59,22 @@ imagenumber, a random image is selected. my ( $image ); if ( C4::Context->preference("OPACLocalCoverImages") ) { - my $imagenumber = $data->param('imagenumber'); - my $biblionumber = $data->param('biblionumber'); + my $imagenumber = $input->param('imagenumber'); + my $biblionumber = $input->param('biblionumber'); if ( defined $imagenumber ) { - $imagenumber = $data->param('imagenumber'); + $imagenumber = $input->param('imagenumber'); $image = Koha::CoverImages->find($imagenumber); + unless ($image) { + print $input->redirect("/cgi-bin/koha/errors/404.pl"); + exit; + } } elsif ( defined $biblionumber ) { my $biblio = Koha::Biblios->find($biblionumber); - Koha::Exceptions::ObjectNotFound->throw( 'No bibliographic record for biblionumber ' . $biblionumber ) unless $biblio; + unless ($biblio) { + print $input->redirect("/cgi-bin/koha/errors/404.pl"); + exit; + } my $cover_images = $biblio->cover_images; if ( $cover_images->count ) { $image = $cover_images->next; @@ -78,11 +85,11 @@ if ( C4::Context->preference("OPACLocalCoverImages") ) { $image ||= Koha::CoverImages->no_image; my $image_data = - $data->param('thumbnail') + $input->param('thumbnail') ? $image->thumbnail : $image->imagefile; -print $data->header( +print $input->header( -type => $image->mimetype, -expires => '+30m', -Content_Length => length($image_data) -- 2.39.5