Bug 30897: Add option to disable automated restart
[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 List::Util qw( any );
24 use Mojo::UserAgent;
25 use File::Temp;
26
27 use C4::Context;
28 use C4::Auth   qw( get_template_and_user );
29 use C4::Output qw( output_html_with_http_headers );
30 use C4::Members;
31 use Koha::Logger;
32 use Koha::Plugins;
33
34 my $plugins_enabled    = C4::Context->config("enable_plugins");
35 my $plugins_restricted = C4::Context->config("plugins_restricted");
36 my $plugins_restart    = C4::Context->config("plugins_restart");
37
38 my $input = CGI->new;
39 my $uploadlocation = $input->param('uploadlocation');
40
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {
43         template_name => ( !$plugins_enabled || $plugins_restricted && !$uploadlocation )
44         ? 'plugins/plugins-disabled.tt'
45         : 'plugins/plugins-upload.tt',
46         query         => $input,
47         type          => "intranet",
48         flagsrequired => { plugins => 'manage' },
49     }
50 );
51 $template->param( plugins_restart => $plugins_restart );
52
53 # Early exist if uploads are not enabled direct upload attempted when uploads are restricted
54 if (!$plugins_enabled) {
55     output_html_with_http_headers $input, $cookie, $template->output;
56     exit;
57 } elsif ( $plugins_restricted && !$uploadlocation ) {
58     $template->param( plugins_restricted => $plugins_restricted );
59     output_html_with_http_headers $input, $cookie, $template->output;
60     exit;
61 }
62
63 my $uploadfilename = $input->param('uploadfile');
64 my $uploadfile     = $input->upload('uploadfile');
65 my $op             = $input->param('op') || q{};
66
67 my ( $tempfile, $tfh );
68
69 my %errors;
70
71 if ( ( $op eq 'cud-Upload' ) && ( $uploadfile || $uploadlocation ) ) {
72     my $plugins_dir = C4::Context->config("pluginsdir");
73     $plugins_dir = ref($plugins_dir) eq 'ARRAY' ? $plugins_dir->[0] : $plugins_dir;
74
75     my $dirname = File::Temp::tempdir( CLEANUP => 1 );
76
77     my $filesuffix;
78     $filesuffix = $1 if $uploadfilename =~ m/(\..+)$/i;
79     ( $tfh, $tempfile ) = File::Temp::tempfile( SUFFIX => $filesuffix, UNLINK => 1 );
80
81     $errors{'NOTKPZ'}         = 1 if ( $uploadfilename !~ /\.kpz$/i );
82     $errors{'NOWRITETEMP'}    = 1 unless ( -w $dirname );
83     $errors{'NOWRITEPLUGINS'} = 1 unless ( -w $plugins_dir );
84
85     if ($uploadlocation) {
86         my $do_get = 1;
87         if ($plugins_restricted) {
88             my $repos = C4::Context->config('plugin_repos');
89
90             # Fix data structure if only one repo defined
91             if ( ref( $repos->{repo} ) eq 'HASH' ) {
92                 $repos = { repo => [ $repos->{repo} ] };
93             }
94
95             $do_get = any { index( $uploadlocation, $_->{org_name} ) != -1 } @{ $repos->{repo} };
96         }
97
98         if ($do_get) {
99             my $ua = Mojo::UserAgent->new( max_redirects => 5 );
100             my $tx = $ua->get($uploadlocation);
101             $tx->result->content->asset->move_to($tempfile);
102         } else {
103             $errors{'RESTRICTED'} = 1;
104         }
105     } else {
106         $errors{'RESTRICTED'}  = 1 unless ( !$plugins_restricted );
107         $errors{'EMPTYUPLOAD'} = 1 unless ( length($uploadfile) > 0 );
108     }
109
110     if (%errors) {
111         $template->param( ERRORS => [ \%errors ] );
112     } else {
113         if ( $uploadfile && !$plugins_restricted ) {
114             while (<$uploadfile>) {
115                 print $tfh $_;
116             }
117             close $tfh;
118         }
119
120         my $ae = Archive::Extract->new( archive => $tempfile, type => 'zip' );
121         unless ( $ae->extract( to => $plugins_dir ) ) {
122             warn "ERROR: " . $ae->error;
123             $errors{'UZIPFAIL'} = $uploadfilename;
124             $template->param( ERRORS => [ \%errors ] );
125             output_html_with_http_headers $input, $cookie, $template->output;
126             exit;
127         }
128
129         # Install plugins; verbose not needed, we redirect to plugins-home.
130         # FIXME There is no good way to verify the install below; we need an
131         # individual plugin install.
132         Koha::Plugins->new->InstallPlugins( { verbose => 0 } );
133     }
134 } elsif ( ( $op eq 'cud-Upload' ) && !$uploadfile && !$uploadlocation ) {
135     warn "Problem uploading file or no file uploaded.";
136 }
137
138 if ( ( $uploadfile || $uploadlocation ) && !%errors && !$template->param('ERRORS') ) {
139     print $input->redirect("/cgi-bin/koha/plugins/plugins-home.pl");
140 } else {
141     output_html_with_http_headers $input, $cookie, $template->output;
142 }
143
144 exit;