Bug 22898: Fix failure since bug 19618
[koha.git] / plugins / plugins-upload.pl
blobc849942c47a682923f5e47ab0848e12676631ffc
1 #!/usr/bin/perl
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 use Modern::Perl;
21 use Archive::Extract;
22 use CGI qw ( -utf8 );
23 use File::Copy;
24 use File::Temp;
26 use C4::Context;
27 use C4::Auth;
28 use C4::Output;
29 use C4::Members;
30 use C4::Debug;
31 use Koha::Plugins;
33 my $plugins_enabled = C4::Context->preference('UseKohaPlugins') && C4::Context->config("enable_plugins");
35 my $input = new CGI;
37 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38 { template_name => ($plugins_enabled) ? "plugins/plugins-upload.tt" : "plugins/plugins-disabled.tt",
39 query => $input,
40 type => "intranet",
41 authnotrequired => 0,
42 flagsrequired => { plugins => 'manage' },
43 debug => 1,
47 my $uploadfilename = $input->param('uploadfile');
48 my $uploadfile = $input->upload('uploadfile');
49 my $op = $input->param('op') || q{};
51 my ( $total, $handled, @counts, $tempfile, $tfh );
53 my %errors;
55 if ($plugins_enabled) {
56 if ( ( $op eq 'Upload' ) && $uploadfile ) {
57 my $plugins_dir = C4::Context->config("pluginsdir");
58 $plugins_dir = ref($plugins_dir) eq 'ARRAY' ? $plugins_dir->[0] : $plugins_dir;
60 my $dirname = File::Temp::tempdir( CLEANUP => 1 );
61 $debug and warn "dirname = $dirname";
63 my $filesuffix;
64 $filesuffix = $1 if $uploadfilename =~ m/(\..+)$/i;
65 ( $tfh, $tempfile ) = File::Temp::tempfile( SUFFIX => $filesuffix, UNLINK => 1 );
67 $debug and warn "tempfile = $tempfile";
69 $errors{'NOTKPZ'} = 1 if ( $uploadfilename !~ /\.kpz$/i );
70 $errors{'NOWRITETEMP'} = 1 unless ( -w $dirname );
71 $errors{'NOWRITEPLUGINS'} = 1 unless ( -w $plugins_dir );
72 $errors{'EMPTYUPLOAD'} = 1 unless ( length($uploadfile) > 0 );
74 if (%errors) {
75 $template->param( ERRORS => [ \%errors ] );
76 } else {
77 while (<$uploadfile>) {
78 print $tfh $_;
80 close $tfh;
82 my $ae = Archive::Extract->new( archive => $tempfile, type => 'zip' );
83 unless ( $ae->extract( to => $plugins_dir ) ) {
84 warn "ERROR: " . $ae->error;
85 $errors{'UZIPFAIL'} = $uploadfilename;
86 $template->param( ERRORS => [ \%errors ] );
87 output_html_with_http_headers $input, $cookie, $template->output;
88 exit;
91 Koha::Plugins->new()->InstallPlugins();
93 } elsif ( ( $op eq 'Upload' ) && !$uploadfile ) {
94 warn "Problem uploading file or no file uploaded.";
97 if ( $uploadfile && !%errors && !$template->param('ERRORS') ) {
98 print $input->redirect("/cgi-bin/koha/plugins/plugins-home.pl");
99 } else {
100 output_html_with_http_headers $input, $cookie, $template->output;
103 } else {
104 output_html_with_http_headers $input, $cookie, $template->output;