Bug 30477: Add new UNIMARC installer translation files
[koha.git] / plugins / plugins-upload.pl
1 #!/usr/bin/perl
2
3 #
4 # This file is part of Koha.
5 #
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.
10 #
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.
15 #
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>.
18
19 use Modern::Perl;
20
21 use Archive::Extract;
22 use CGI qw ( -utf8 );
23 use Mojo::UserAgent;
24 use File::Temp;
25
26 use C4::Context;
27 use C4::Auth qw( get_template_and_user );
28 use C4::Output qw( output_html_with_http_headers );
29 use C4::Members;
30 use Koha::Logger;
31 use Koha::Plugins;
32
33 my $plugins_enabled = C4::Context->config("enable_plugins");
34
35 my $input = CGI->new;
36
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         flagsrequired   => { plugins => 'manage' },
42     }
43 );
44
45 my $uploadfilename = $input->param('uploadfile');
46 my $uploadfile     = $input->upload('uploadfile');
47 my $uploadlocation = $input->param('uploadlocation');
48 my $op             = $input->param('op') || q{};
49
50 my ( $tempfile, $tfh );
51
52 my %errors;
53
54 if ($plugins_enabled) {
55     if ( ( $op eq 'Upload' ) && ( $uploadfile || $uploadlocation ) ) {
56         my $plugins_dir = C4::Context->config("pluginsdir");
57         $plugins_dir = ref($plugins_dir) eq 'ARRAY' ? $plugins_dir->[0] : $plugins_dir;
58
59         my $dirname = File::Temp::tempdir( CLEANUP => 1 );
60
61         my $filesuffix;
62         $filesuffix = $1 if $uploadfilename =~ m/(\..+)$/i;
63         ( $tfh, $tempfile ) = File::Temp::tempfile( SUFFIX => $filesuffix, UNLINK => 1 );
64
65         $errors{'NOTKPZ'} = 1 if ( $uploadfilename !~ /\.kpz$/i );
66         $errors{'NOWRITETEMP'}    = 1 unless ( -w $dirname );
67         $errors{'NOWRITEPLUGINS'} = 1 unless ( -w $plugins_dir );
68
69         if ( $uploadlocation ) {
70             my $ua = Mojo::UserAgent->new(max_redirects => 5);
71             my $tx = $ua->get($uploadlocation);
72             $tx->result->content->asset->move_to($tempfile);
73         } else {
74             $errors{'EMPTYUPLOAD'}    = 1 unless ( length($uploadfile) > 0 );
75         }
76
77         if (%errors) {
78             $template->param( ERRORS => [ \%errors ] );
79         } else {
80             if ( $uploadfile ) {
81                 while (<$uploadfile>) {
82                     print $tfh $_;
83                 }
84                 close $tfh;
85             }
86
87             my $ae = Archive::Extract->new( archive => $tempfile, type => 'zip' );
88             unless ( $ae->extract( to => $plugins_dir ) ) {
89                 warn "ERROR: " . $ae->error;
90                 $errors{'UZIPFAIL'} = $uploadfilename;
91                 $template->param( ERRORS => [ \%errors ] );
92                 output_html_with_http_headers $input, $cookie, $template->output;
93                 exit;
94             }
95
96             Koha::Plugins->new()->InstallPlugins();
97         }
98     } elsif ( ( $op eq 'Upload' ) && !$uploadfile && !$uploadlocation ) {
99         warn "Problem uploading file or no file uploaded.";
100     }
101
102     if ( ($uploadfile || $uploadlocation) && !%errors && !$template->param('ERRORS') ) {
103         print $input->redirect("/cgi-bin/koha/plugins/plugins-home.pl");
104     } else {
105         output_html_with_http_headers $input, $cookie, $template->output;
106     }
107
108 } else {
109     output_html_with_http_headers $input, $cookie, $template->output;
110 }