Bug 13663: Fix permissions in upload-file.pl and upload-file-progress.pl
[koha.git] / tools / upload-file.pl
1 #!/usr/bin/perl
2
3 # Copyright (C) 2007 LibLime
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22
23 # standard or CPAN modules used
24 use IO::File;
25 use CGI qw ( -utf8 );
26 use CGI::Session;
27 use C4::Context;
28 use C4::Auth qw/check_cookie_auth haspermission/;
29 use CGI::Cookie; # need to check cookies before
30                  # having CGI parse the POST request
31 use C4::UploadedFile;
32
33 # upload-file.pl must authenticate the user
34 # before processing the POST request,
35 # and quickly bounce if the user is
36 # not authorized.  Consequently, unlike
37 # most of the other CGI scripts, upload-file.pl
38 # requires that the session cookie already
39 # have been created.
40
41 my $flags_required = [
42                 {circulate => 'circulate_remaining_permissions'},
43                 {tools => 'stage_marc_import'},
44                 {tools => 'upload_local_cover_images'}
45 ];
46
47 my %cookies = fetch CGI::Cookie;
48
49 my $auth_failure = 1;
50 my ($auth_status, $sessionID) = check_cookie_auth($cookies{'CGISESSID'}->value);
51 foreach my $flag_required (@{ $flags_required}) {
52                 if (my $flags = haspermission(C4::Context->config('user'), $flag_required)) {
53                                 $auth_failure = 0 if $auth_status eq 'ok';
54                 }
55 }
56
57 if ($auth_failure) {
58     $auth_status = 'denied' if $auth_status eq 'failed';
59     send_reply($auth_status, "");
60     exit 0;
61 }
62
63 our $uploaded_file = C4::UploadedFile->new($sessionID);
64 unless (defined $uploaded_file) {
65     # FIXME - failed to create file for some reason
66     send_reply('failed', '');
67     exit 0;
68 }
69 $uploaded_file->max_size($ENV{'CONTENT_LENGTH'}); # may not be the file size, exactly
70
71 my $query;
72 $query = new CGI \&upload_hook;
73 $uploaded_file->done();
74 send_reply('done', $uploaded_file->id());
75
76 # FIXME - if possible, trap signal caused by user cancelling upload
77 # FIXME - something is wrong during cleanup: \t(in cleanup) Can't call method "commit" on unblessed reference at /usr/local/share/perl/5.8.8/CGI/Session/Driver/DBI.pm line 130 during global destruction.
78 exit 0;
79
80 sub upload_hook {
81     my ($file_name, $buffer, $bytes_read, $session) = @_;
82     $uploaded_file->stash(\$buffer, $bytes_read);
83     if ( ! $uploaded_file->name && $file_name ) { # save name on first chunk
84         $uploaded_file->name($file_name);
85     }
86 }
87
88 sub send_reply {
89     my ($upload_status, $fileid) = @_;
90
91     my $reply = CGI->new("");
92     print $reply->header(-type => 'text/html');
93     # response will be sent back as JSON
94     print '{"status":"' . $upload_status . '","fileid":"' . $fileid . '"}';
95 }