Bug 14321: Integrate Upload.pm into Koha
[koha.git] / tools / upload-file-progress.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 C4::UploadedFile;
30 use CGI::Cookie; # need to check cookies before
31                  # having CGI parse the POST request
32
33 my $flags_required = [
34      {circulate => 'circulate_remaining_permissions'},
35      {tools     => 'stage_marc_import'},
36      {tools     => 'upload_local_cover_images'},
37 ];
38
39 my %cookies = fetch CGI::Cookie;
40
41 my ($auth_status, $sessionID) = check_cookie_auth($cookies{'CGISESSID'}->value);
42
43 my $auth_failure = 1;
44 foreach my $flag_required ( @{$flags_required} ) {
45     if ( my $flags = haspermission( C4::Context->config('user'), $flag_required ) ) {
46         $auth_failure = 0 if $auth_status eq 'ok';
47     }
48 }
49
50 if ($auth_failure) {
51     my $reply = CGI->new("");
52     print $reply->header(-type => 'text/html');
53     print '{"progress":"0"}';
54     exit 0;
55 }
56
57 my $reported_progress = C4::UploadedFile->upload_progress($sessionID);
58
59 my $reply = CGI->new("");
60 print $reply->header(-type => 'text/html');
61 # response will be sent back as JSON
62 print '{"progress":"' . $reported_progress . '"}';