Fix authentication problem in previous commit; userenv->{} should not be used before...
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21
22 # standard or CPAN modules used
23 use IO::File;
24 use CGI;
25 use CGI::Session;
26 use C4::Context;
27 use C4::Auth qw/check_cookie_auth/;
28 use CGI::Cookie; # need to check cookies before
29                  # having CGI parse the POST request
30 use C4::UploadedFile;
31
32 # upload-file.pl must authenticate the user
33 # before processing the POST request,
34 # and quickly bounce if the user is
35 # not authorized.  Consequently, unlike
36 # most of the other CGI scripts, upload-file.pl
37 # requires that the session cookie already
38 # have been created.
39
40 my %cookies = fetch CGI::Cookie;
41 my ($auth_status, $sessionID) = check_cookie_auth($cookies{'CGISESSID'}->value, { tools => '*' });
42 if ($auth_status ne "ok") {
43     $auth_status = 'denied' if $auth_status eq 'failed';
44     send_reply($auth_status, "");
45     exit 0;
46 }
47
48 my $uploaded_file = C4::UploadedFile->new($sessionID);
49 unless (defined $uploaded_file) {
50     # FIXME - failed to create file for some reason
51     send_reply('failed', '');
52     exit 0;
53 }
54 $uploaded_file->max_size($ENV{'CONTENT_LENGTH'}); # may not be the file size, exactly
55
56 my $first_chunk = 1;
57
58 my $query;
59 $query = new CGI \&upload_hook;
60 $uploaded_file->done();
61 send_reply('done', $uploaded_file->id());
62
63 # FIXME - if possible, trap signal caused by user cancelling upload
64 # 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.
65 exit 0;
66
67 sub upload_hook {
68     my ($file_name, $buffer, $bytes_read, $session) = @_;
69     $uploaded_file->stash(\$buffer, $bytes_read);
70     if ($first_chunk) {
71         $uploaded_file->name($file_name);
72         $first_chunk = 0;
73     }
74 }
75
76 sub send_reply {
77     my ($upload_status, $fileid) = @_;
78
79     my $reply = CGI->new("");
80     print $reply->header(-type => 'text/html');
81     # response will be sent back as JSON
82     print "{ status: '$upload_status', fileid: '$fileid' }";
83 }