Bug 7804 - Add Koha Plugin System
[koha-equinox.git] / plugins / plugins-upload.pl
1 #!/usr/bin/perl
2 #
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use strict;
19 use warnings;
20
21 use Archive::Extract;
22 use File::Temp;
23 use File::Copy;
24 use CGI;
25
26 use C4::Context;
27 use C4::Auth;
28 use C4::Output;
29 use C4::Members;
30 use C4::Debug;
31 use Koha::Plugins;
32
33 die("Koha plugins are disabled!")
34   unless C4::Context->preference('UseKohaPlugins');
35
36 my $input = new CGI;
37
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
39     {   template_name   => "plugins/plugins-upload.tmpl",
40         query           => $input,
41         type            => "intranet",
42         authnotrequired => 0,
43         flagsrequired   => { plugins => 'manage' },
44         debug           => 1,
45     }
46 );
47
48 my $uploadfilename = $input->param('uploadfile');
49 my $uploadfile     = $input->upload('uploadfile');
50 my $op             = $input->param('op');
51
52 my ( $total, $handled, @counts, $tempfile, $tfh );
53
54 my %errors;
55
56 if ( ( $op eq 'Upload' ) && $uploadfile ) {
57     my $plugins_dir = C4::Context->config("pluginsdir");
58
59     my $dirname = File::Temp::tempdir( CLEANUP => 1 );
60     $debug and warn "dirname = $dirname";
61
62     my $filesuffix;
63     $filesuffix = $1 if $uploadfilename =~ m/(\..+)$/i;
64     ( $tfh, $tempfile ) = File::Temp::tempfile( SUFFIX => $filesuffix, UNLINK => 1 );
65
66     $debug and warn "tempfile = $tempfile";
67
68     $errors{'NOTKPZ'} = 1 if ( $uploadfilename !~ /\.kpz$/i );
69     $errors{'NOWRITETEMP'}    = 1 unless ( -w $dirname );
70     $errors{'NOWRITEPLUGINS'} = 1 unless ( -w $plugins_dir );
71     $errors{'EMPTYUPLOAD'}    = 1 unless ( length($uploadfile) > 0 );
72
73     if (%errors) {
74         $template->param( ERRORS => [ \%errors ] );
75     } else {
76         while (<$uploadfile>) {
77             print $tfh $_;
78         }
79         close $tfh;
80
81         my $ae = Archive::Extract->new( archive => $tempfile, type => 'zip' );
82         unless ( $ae->extract( to => $plugins_dir ) ) {
83             warn "ERROR: " . $ae->error;
84             $errors{'UZIPFAIL'} = $uploadfilename;
85             $template->param( ERRORS => [ \%errors ] );
86             output_html_with_http_headers $input, $cookie, $template->output;
87             exit;
88         }
89     }
90 } elsif ( ( $op eq 'Upload' ) && !$uploadfile ) {
91     warn "Problem uploading file or no file uploaded.";
92 }
93
94 if ( $uploadfile && !%errors && !$template->param('ERRORS') ) {
95     print $input->redirect("/cgi-bin/koha/plugins/plugins-home.pl");
96 } else {
97     output_html_with_http_headers $input, $cookie, $template->output;
98 }