Bug 16047 [Follow-up] Software error on deleting a group with no category code
[koha.git] / tools / upload-file.pl
blob19942141b0ab0bae5f9af559d15be1cfc51cb79e
1 #!/usr/bin/perl
3 # Copyright (C) 2007 LibLime
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use CGI::Cookie;
24 use Encode;
25 use JSON;
26 use URI::Escape;
28 use C4::Context;
29 use C4::Auth qw/check_cookie_auth haspermission/;
30 use Koha::Upload;
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 # has been created.
40 my $flags_required = [
41 {circulate => 'circulate_remaining_permissions'},
42 {tools => 'stage_marc_import'},
43 {tools => 'upload_local_cover_images'}
46 my %cookies = CGI::Cookie->fetch;
47 my $sid = $cookies{'CGISESSID'}->value;
49 my $auth_failure = 1;
50 my ( $auth_status, $sessionID ) = check_cookie_auth( $sid );
51 my $uid = C4::Auth::get_session($sid)->param('id');
52 foreach my $flag_required ( @{$flags_required} ) {
53 if ( my $flags = haspermission( $uid, $flag_required ) ) {
54 $auth_failure = 0 if $auth_status eq 'ok';
58 if ($auth_failure) {
59 send_reply( 'denied' );
60 exit 0;
63 my $upload = Koha::Upload->new( upload_pars($ENV{QUERY_STRING}) );
64 if( !$upload || !$upload->cgi || !$upload->count ) {
65 # not one upload succeeded
66 send_reply( 'failed', undef, $upload? $upload->err: undef );
67 } else {
68 # in case of multiple uploads, at least one got through
69 send_reply( 'done', $upload->result, $upload->err );
71 exit 0;
73 sub send_reply { # response will be sent back as JSON
74 my ( $upload_status, $data, $error ) = @_;
75 my $reply = CGI->new("");
76 print $reply->header( -type => 'text/html', -charset => 'UTF-8' );
77 print JSON::encode_json({
78 status => $upload_status,
79 fileid => $data,
80 errors => $error,
81 });
84 sub upload_pars { # this sub parses QUERY_STRING in order to build the
85 # parameter hash for Koha::Upload
86 my ( $qstr ) = @_;
87 $qstr = Encode::decode_utf8( uri_unescape( $qstr ) );
88 # category could include a utf8 character
89 my $rv = {};
90 foreach my $p ( qw[public category temp] ) {
91 if( $qstr =~ /(^|&)$p=(\w+)(&|$)/ ) {
92 $rv->{$p} = $2;
95 return $rv;