]> git.koha-community.org Git - koha.git/blob - tools/access_files.pl
Bug 11317: Add a way to access files from the intranet
[koha.git] / tools / access_files.pl
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 # "access_dir" in koha-conf.xml so they can be downloaded by users with the
7 # "access_files" 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 3 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 Modern::Perl;
25
26 use C4::Auth;
27 use CGI;
28 use C4::Context;
29 use C4::Output;
30 use C4::Koha;
31 use File::stat qw(stat);
32 use Digest::MD5 qw(md5_hex);
33 use Encode;
34
35 my $input = new CGI;
36 my $file_id = $input->param("id");
37 my $access_dir = C4::Context->config('access_dir');
38 my @directories = $access_dir ? (ref $access_dir ? @{$access_dir} : ($access_dir)) : ();
39
40 my ($template, $borrowernumber, $cookie)
41     = get_template_and_user({template_name => "tools/access_files.tt",
42                 query => $input,
43                 type => "intranet",
44                 authnotrequired => 0,
45                 flagsrequired => { tools => 'access_files' },
46                 });
47
48 unless(@directories) {
49     $template->param(error_no_dir => 1);
50 }
51 else {
52     #Get the files list
53     my @files_list;
54     foreach my $dir(@directories){
55         opendir(DIR, $dir);
56         foreach my $filename (readdir(DIR)) {
57             my $full_path = "$dir/$filename";
58             my $id = md5_hex($full_path);
59             next if ($filename =~ /^\./ or -d $full_path);
60
61             # Make sure the filename is unicode-friendly
62             my $decoded_filename = decode('utf8', $filename);
63             my $st = stat("$dir/$decoded_filename");
64
65             my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime($st->mtime);
66             my $dt=DateTime->new(year      => $year + 1900,
67                                   month    => $mon + 1,
68                                   day      => $mday,
69                                   hour     => $hour,
70                                   minute   => $min,
71                             );
72             push(@files_list, {name => $decoded_filename,
73                                access_dir => $dir,
74                                date =>Koha::DateUtils::output_pref($dt),
75                                size => $st->size,
76                                id   => $id});
77         }
78         closedir(DIR);
79     }
80
81     my %files_hash = map { $_->{id} => $_ } @files_list;
82     # If we received a file_id and it is valid, send the file to the browser
83     if(defined $file_id and exists $files_hash{$file_id} ){
84         my $filename = $files_hash{$file_id}->{name};
85         my $dir = $files_hash{$file_id}->{access_dir};
86         binmode STDOUT;
87         # Open the selected file and send it to the browser
88         print $input->header(-type => 'application/x-download',
89                              -name => "$filename",
90                              -Content_length => -s "$dir/$filename",
91                              -attachment => "$filename");
92
93         my $fh;
94         open $fh, "<:encoding(UTF-8)", "$dir/$filename";
95         binmode $fh;
96
97         my $buf;
98         while(read($fh, $buf, 65536)) {
99             print $buf;
100         }
101         close $fh;
102
103         exit(0);
104     }
105     else{
106         # Send the file list to the template
107         $template->param(files_loop => \@files_list);
108     }
109 }
110
111 output_html_with_http_headers $input, $cookie, $template->output;