|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Frédérick Capovilla, 2011 - Libéo |
| 4 |
# |
| 5 |
# Show a list of all the files in the directory specified by the option |
| 6 |
# "publiclogdir" in koha-conf.xml so they can be downloaded by users with the |
| 7 |
# "reports" permission. |
| 8 |
# |
| 9 |
# This file is part of Koha. |
| 10 |
# |
| 11 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 12 |
# terms of the GNU General Public License as published by the Free Software |
| 13 |
# Foundation; either version 2 of the License, or (at your option) any later |
| 14 |
# version. |
| 15 |
# |
| 16 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 17 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 18 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 19 |
# |
| 20 |
# You should have received a copy of the GNU General Public License along |
| 21 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 22 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 |
|
| 24 |
use strict; |
| 25 |
use warnings; |
| 26 |
use C4::Auth; |
| 27 |
use CGI; |
| 28 |
use C4::Context; |
| 29 |
use C4::Output; |
| 30 |
use C4::Koha; |
| 31 |
use File::stat; |
| 32 |
|
| 33 |
my $input = new CGI; |
| 34 |
my $file_id = $input->param("id"); |
| 35 |
my $directory = C4::Context->config('publiclogdir'); |
| 36 |
|
| 37 |
my ($template, $borrowernumber, $cookie) |
| 38 |
= get_template_and_user({template_name => "reports/report_files.tt", |
| 39 |
query => $input, |
| 40 |
type => "intranet", |
| 41 |
authnotrequired => 0, |
| 42 |
flagsrequired => {reports => '*'}, |
| 43 |
}); |
| 44 |
|
| 45 |
unless($directory) { |
| 46 |
$template->param(error_no_publiclogdir => 1); |
| 47 |
} |
| 48 |
else { |
| 49 |
#Get the files list |
| 50 |
my @files_list; |
| 51 |
opendir(DIR, $directory); |
| 52 |
|
| 53 |
my $i=0; |
| 54 |
foreach my $filename (readdir(DIR)) { |
| 55 |
my $full_path = "$directory/$filename"; |
| 56 |
next if ($filename =~ /^\./ or -d $full_path); |
| 57 |
|
| 58 |
my $st = stat($full_path); |
| 59 |
push(@files_list, {name => $filename, |
| 60 |
date => scalar localtime($st->mtime), |
| 61 |
size => $st->size, |
| 62 |
id => $i}); |
| 63 |
$i++; |
| 64 |
} |
| 65 |
closedir(DIR); |
| 66 |
|
| 67 |
# If we received a file_id and it is valid, send the file to the browser |
| 68 |
if(defined $file_id and $file_id >= 0 and $file_id < scalar @files_list){ |
| 69 |
my $filename = $files_list[$file_id]->{name}; |
| 70 |
binmode STDOUT; |
| 71 |
# Open the selected file and send it to the browser |
| 72 |
print $input->header(-type => 'application/x-download', |
| 73 |
-name => "$filename", |
| 74 |
-Content_length => -s "$directory/$filename", |
| 75 |
-attachment => "$filename"); |
| 76 |
|
| 77 |
open FILE, "<:utf8", "$directory/$filename"; |
| 78 |
binmode FILE; |
| 79 |
|
| 80 |
my $buf; |
| 81 |
while(read(FILE, $buf, 65536)) { |
| 82 |
print $buf; |
| 83 |
} |
| 84 |
close FILE; |
| 85 |
|
| 86 |
exit(1); |
| 87 |
} |
| 88 |
else{ |
| 89 |
# Send the file list to the template |
| 90 |
$template->param(files_loop => \@files_list); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
output_html_with_http_headers $input, $cookie, $template->output; |