Bug 25728: Don't explode if no CAN_* var defined
[koha.git] / plugins / plugins-upload.pl
blob6ad52abccf84f317a92786cd5ac15c5db5cf82fa
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 Mojo::UserAgent;
24 use File::Copy;
25 use File::Temp;
27 use C4::Context;
28 use C4::Auth;
29 use C4::Output;
30 use C4::Members;
31 use C4::Debug;
32 use Koha::Plugins;
34 my $plugins_enabled = C4::Context->config("enable_plugins");
36 my $input = new CGI;
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
39 { template_name => ($plugins_enabled) ? "plugins/plugins-upload.tt" : "plugins/plugins-disabled.tt",
40 query => $input,
41 type => "intranet",
42 authnotrequired => 0,
43 flagsrequired => { plugins => 'manage' },
44 debug => 1,
48 my $uploadfilename = $input->param('uploadfile');
49 my $uploadfile = $input->upload('uploadfile');
50 my $uploadlocation = $input->param('uploadlocation');
51 my $op = $input->param('op') || q{};
53 my ( $tempfile, $tfh );
55 my %errors;
57 if ($plugins_enabled) {
58 if ( ( $op eq 'Upload' ) && ( $uploadfile || $uploadlocation ) ) {
59 my $plugins_dir = C4::Context->config("pluginsdir");
60 $plugins_dir = ref($plugins_dir) eq 'ARRAY' ? $plugins_dir->[0] : $plugins_dir;
62 my $dirname = File::Temp::tempdir( CLEANUP => 1 );
63 $debug and warn "dirname = $dirname";
65 my $filesuffix;
66 $filesuffix = $1 if $uploadfilename =~ m/(\..+)$/i;
67 ( $tfh, $tempfile ) = File::Temp::tempfile( SUFFIX => $filesuffix, UNLINK => 1 );
69 $debug and warn "tempfile = $tempfile";
71 $errors{'NOTKPZ'} = 1 if ( $uploadfilename !~ /\.kpz$/i );
72 $errors{'NOWRITETEMP'} = 1 unless ( -w $dirname );
73 $errors{'NOWRITEPLUGINS'} = 1 unless ( -w $plugins_dir );
75 if ( $uploadlocation ) {
76 my $ua = Mojo::UserAgent->new(max_redirects => 5);
77 my $tx = $ua->get($uploadlocation);
78 $tx->result->content->asset->move_to($tempfile);
79 } else {
80 $errors{'EMPTYUPLOAD'} = 1 unless ( length($uploadfile) > 0 );
83 if (%errors) {
84 $template->param( ERRORS => [ \%errors ] );
85 } else {
86 if ( $uploadfile ) {
87 while (<$uploadfile>) {
88 print $tfh $_;
90 close $tfh;
93 my $ae = Archive::Extract->new( archive => $tempfile, type => 'zip' );
94 unless ( $ae->extract( to => $plugins_dir ) ) {
95 warn "ERROR: " . $ae->error;
96 $errors{'UZIPFAIL'} = $uploadfilename;
97 $template->param( ERRORS => [ \%errors ] );
98 output_html_with_http_headers $input, $cookie, $template->output;
99 exit;
102 Koha::Plugins->new()->InstallPlugins();
104 } elsif ( ( $op eq 'Upload' ) && !$uploadfile && !$uploadlocation ) {
105 warn "Problem uploading file or no file uploaded.";
108 if ( ($uploadfile || $uploadlocation) && !%errors && !$template->param('ERRORS') ) {
109 print $input->redirect("/cgi-bin/koha/plugins/plugins-home.pl");
110 } else {
111 output_html_with_http_headers $input, $cookie, $template->output;
114 } else {
115 output_html_with_http_headers $input, $cookie, $template->output;