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