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