From 8ffb2dced123cb60e7fb62a5f64c41e1cd811d6d Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Sat, 15 Aug 2015 14:00:58 +0200 Subject: [PATCH] Bug 14321: Switch to Upload in opac-retrieve Content-Type: text/plain; charset=utf-8 A last step to obsolete UploadedFiles.pm. Note that we now also check for the public flag. If that flag is not set, the uploaded file cannot be reached via the opac. This patch also the encode step to tools/upload for the http header that could include a filename with special chars (just as in opac-retrieve). Test plan: [1] Check downloading a file marked public in OPAC without logging in. If possible, include some special chars in the filename. [2] Check another file (not marked as public). Should not be possible. --- .../bootstrap/en/modules/opac-retrieve-file.tt | 6 ++++ opac/opac-retrieve-file.pl | 36 ++++++++++++++------ tools/upload.pl | 2 +- 3 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-retrieve-file.tt diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-retrieve-file.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-retrieve-file.tt new file mode 100644 index 0000000..581d9ea --- /dev/null +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-retrieve-file.tt @@ -0,0 +1,6 @@ +[%# This template is called only for a very simple error message %] + + +Your search [% IF hash %]for [% hash %][% END %] was not successful. + + diff --git a/opac/opac-retrieve-file.pl b/opac/opac-retrieve-file.pl index 419f2bd..b443a7c 100755 --- a/opac/opac-retrieve-file.pl +++ b/opac/opac-retrieve-file.pl @@ -19,19 +19,33 @@ use Modern::Perl; use CGI; +use Encode; +use C4::Auth; use C4::Context; -use C4::UploadedFiles; +use C4::Output; +use Koha::Upload; -my $input = new CGI; +my $input = CGI::->new; +my $hash = $input->param('id'); # historically called id (used in URLs?) -my $id = $input->param('id'); -my $file = C4::UploadedFiles::GetUploadedFile($id); -exit 1 if !$file || !-f $file->{filepath}; - -open my $fh, '<', $file->{filepath} or die "Can't open file: $!"; -print $input->header( C4::UploadedFiles::httpheaders( $file->{filename} )); -while(<$fh>) { - print $_; +my $upl = Koha::Upload->new({ public => 1 }); +my $rec = $upl->get({ hashvalue => $hash, filehandle => 1 }); +my $fh = $rec->{fh}; +if( !$rec || !$fh ) { + my ( $template, $user, $cookie ) = get_template_and_user({ + query => $input, + template_name => 'opac-retrieve-file.tt', + type => 'opac', + authnotrequired => 1, + }); + $template->param( hash => $hash ); + output_html_with_http_headers $input, $cookie, $template->output; +} else { + my @hdr = $upl->httpheaders( $rec->{name} ); + print Encode::encode_utf8( $input->header( @hdr ) ); + while( <$fh> ) { + print $_; + } + $fh->close; } -close $fh; diff --git a/tools/upload.pl b/tools/upload.pl index 96d74b1..47c28f5 100755 --- a/tools/upload.pl +++ b/tools/upload.pl @@ -89,7 +89,7 @@ if( $op eq 'new' ) { output_html_with_http_headers $input, $cookie, $template->output; } else { my @hdr = $upl->httpheaders( $rec->{name} ); - print $input->header( @hdr ); + print Encode::encode_utf8( $input->header( @hdr ) ); while( <$fh> ) { print $_; } -- 1.7.10.4