View | Details | Raw Unified | Return to bug 25672
Collapse All | Expand All

(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/plugins/plugins-upload.tt (-1 / +1 lines)
Lines 46-52 Link Here
46
                [% END %]
46
                [% END %]
47
                <form method="post" action="/cgi-bin/koha/plugins/plugins-upload.pl" enctype="multipart/form-data">
47
                <form method="post" action="/cgi-bin/koha/plugins/plugins-upload.pl" enctype="multipart/form-data">
48
                    <fieldset class="brief">
48
                    <fieldset class="brief">
49
                         <div class="hint"><strong>NOTE:</strong> Only KPZ file format is supported.</div>
49
                        <div class="hint"><strong>NOTE:</strong> Only KPZ file format is supported.</div>
50
                        <ol>
50
                        <ol>
51
                            <li>
51
                            <li>
52
                                <label for="uploadfile">Select the file to upload: </label><input type="file" id="uploadfile" name="uploadfile" accept=".kpz" />
52
                                <label for="uploadfile">Select the file to upload: </label><input type="file" id="uploadfile" name="uploadfile" accept=".kpz" />
(-)a/plugins/plugins-upload.pl (-59 / +55 lines)
Lines 45-137 my ( $template, $loggedinuser, $cookie ) = get_template_and_user( Link Here
45
    }
45
    }
46
);
46
);
47
47
48
if ($plugins_enabled) {
48
my $uploadlocation = $input->param('uploadlocation');
49
50
# Early exists if uploads are not enabled direct upload attempted when uploads are restricted
51
if (!$plugins_enabled) {
52
    output_html_with_http_headers $input, $cookie, $template->output;
53
} elsif ( $plugins_restricted && !$uploadlocation ) {
49
    $template->param( plugins_restricted => $plugins_restricted );
54
    $template->param( plugins_restricted => $plugins_restricted );
50
} else {
51
    # Exit early if uploads are not enabled
52
    output_html_with_http_headers $input, $cookie, $template->output;
55
    output_html_with_http_headers $input, $cookie, $template->output;
53
}
56
}
54
57
55
my $uploadfilename = $input->param('uploadfile');
58
my $uploadfilename = $input->param('uploadfile');
56
my $uploadfile     = $input->upload('uploadfile');
59
my $uploadfile     = $input->upload('uploadfile');
57
my $uploadlocation = $input->param('uploadlocation');
58
my $op             = $input->param('op') || q{};
60
my $op             = $input->param('op') || q{};
59
61
60
my ( $tempfile, $tfh );
62
my ( $tempfile, $tfh );
61
63
62
my %errors;
64
my %errors;
63
65
64
if ($plugins_enabled) {
66
if ( ( $op eq 'Upload' ) && ( $uploadfile || $uploadlocation ) ) {
65
    if ( ( $op eq 'Upload' ) && ( $uploadfile || $uploadlocation ) ) {
67
    my $plugins_dir = C4::Context->config("pluginsdir");
66
        my $plugins_dir = C4::Context->config("pluginsdir");
68
    $plugins_dir = ref($plugins_dir) eq 'ARRAY' ? $plugins_dir->[0] : $plugins_dir;
67
        $plugins_dir = ref($plugins_dir) eq 'ARRAY' ? $plugins_dir->[0] : $plugins_dir;
68
69
69
        my $dirname = File::Temp::tempdir( CLEANUP => 1 );
70
    my $dirname = File::Temp::tempdir( CLEANUP => 1 );
70
71
71
        my $filesuffix;
72
    my $filesuffix;
72
        $filesuffix = $1 if $uploadfilename =~ m/(\..+)$/i;
73
    $filesuffix = $1 if $uploadfilename =~ m/(\..+)$/i;
73
        ( $tfh, $tempfile ) = File::Temp::tempfile( SUFFIX => $filesuffix, UNLINK => 1 );
74
    ( $tfh, $tempfile ) = File::Temp::tempfile( SUFFIX => $filesuffix, UNLINK => 1 );
74
75
75
        $errors{'NOTKPZ'}         = 1 if ( $uploadfilename !~ /\.kpz$/i );
76
    $errors{'NOTKPZ'}         = 1 if ( $uploadfilename !~ /\.kpz$/i );
76
        $errors{'NOWRITETEMP'}    = 1 unless ( -w $dirname );
77
    $errors{'NOWRITETEMP'}    = 1 unless ( -w $dirname );
77
        $errors{'NOWRITEPLUGINS'} = 1 unless ( -w $plugins_dir );
78
    $errors{'NOWRITEPLUGINS'} = 1 unless ( -w $plugins_dir );
78
79
79
        if ($uploadlocation) {
80
    if ($uploadlocation) {
80
            my $do_get = 1;
81
        my $do_get = 1;
81
            if ( $plugins_restricted ) {
82
        if ($plugins_restricted) {
82
                my $repos = C4::Context->config('plugin_repos');
83
            my $repos = C4::Context->config('plugin_repos');
83
84
84
                # Fix data structure if only one repo defined
85
            # Fix data structure if only one repo defined
85
                if ( ref($repos->{repo}) eq 'HASH' ) {
86
            if ( ref( $repos->{repo} ) eq 'HASH' ) {
86
                    $repos = { repo => [ $repos->{repo} ] };
87
                $repos = { repo => [ $repos->{repo} ] };
87
                }
88
89
                $do_get = any { index($uploadlocation, $_->{org_name}) != -1 } @{ $repos->{repo} };
90
            }
88
            }
91
89
92
            if ( $do_get ) {
90
            $do_get = any { index( $uploadlocation, $_->{org_name} ) != -1 } @{ $repos->{repo} };
93
                my $ua = Mojo::UserAgent->new( max_redirects => 5 );
94
                my $tx = $ua->get($uploadlocation);
95
                $tx->result->content->asset->move_to($tempfile);
96
            } else {
97
                $errors{'RESTRICTED'} = 1;
98
            }
99
        } else {
100
            $errors{'RESTRICTED'} = 1 unless ( !$plugins_restricted );
101
            $errors{'EMPTYUPLOAD'} = 1 unless ( length($uploadfile) > 0 );
102
        }
91
        }
103
92
104
        if (%errors) {
93
        if ($do_get) {
105
            $template->param( ERRORS => [ \%errors ] );
94
            my $ua = Mojo::UserAgent->new( max_redirects => 5 );
95
            my $tx = $ua->get($uploadlocation);
96
            $tx->result->content->asset->move_to($tempfile);
106
        } else {
97
        } else {
107
            if ($uploadfile && !$plugins_restricted) {
98
            $errors{'RESTRICTED'} = 1;
108
                while (<$uploadfile>) {
99
        }
109
                    print $tfh $_;
100
    } else {
110
                }
101
        $errors{'RESTRICTED'}  = 1 unless ( !$plugins_restricted );
111
                close $tfh;
102
        $errors{'EMPTYUPLOAD'} = 1 unless ( length($uploadfile) > 0 );
112
            }
103
    }
113
104
114
            my $ae = Archive::Extract->new( archive => $tempfile, type => 'zip' );
105
    if (%errors) {
115
            unless ( $ae->extract( to => $plugins_dir ) ) {
106
        $template->param( ERRORS => [ \%errors ] );
116
                warn "ERROR: " . $ae->error;
107
    } else {
117
                $errors{'UZIPFAIL'} = $uploadfilename;
108
        if ( $uploadfile && !$plugins_restricted ) {
118
                $template->param( ERRORS => [ \%errors ] );
109
            while (<$uploadfile>) {
119
                output_html_with_http_headers $input, $cookie, $template->output;
110
                print $tfh $_;
120
                exit;
121
            }
111
            }
112
            close $tfh;
113
        }
122
114
123
            Koha::Plugins->new()->InstallPlugins();
115
        my $ae = Archive::Extract->new( archive => $tempfile, type => 'zip' );
116
        unless ( $ae->extract( to => $plugins_dir ) ) {
117
            warn "ERROR: " . $ae->error;
118
            $errors{'UZIPFAIL'} = $uploadfilename;
119
            $template->param( ERRORS => [ \%errors ] );
120
            output_html_with_http_headers $input, $cookie, $template->output;
121
            exit;
124
        }
122
        }
125
    } elsif ( ( $op eq 'Upload' ) && !$uploadfile && !$uploadlocation ) {
126
        warn "Problem uploading file or no file uploaded.";
127
    }
128
123
129
    if ( ( $uploadfile || $uploadlocation ) && !%errors && !$template->param('ERRORS') ) {
124
        Koha::Plugins->new()->InstallPlugins();
130
        print $input->redirect("/cgi-bin/koha/plugins/plugins-home.pl");
131
    } else {
132
        output_html_with_http_headers $input, $cookie, $template->output;
133
    }
125
    }
126
} elsif ( ( $op eq 'Upload' ) && !$uploadfile && !$uploadlocation ) {
127
    warn "Problem uploading file or no file uploaded.";
128
}
134
129
130
if ( ( $uploadfile || $uploadlocation ) && !%errors && !$template->param('ERRORS') ) {
131
    print $input->redirect("/cgi-bin/koha/plugins/plugins-home.pl");
135
} else {
132
} else {
136
    output_html_with_http_headers $input, $cookie, $template->output;
133
    output_html_with_http_headers $input, $cookie, $template->output;
137
}
134
}
138
- 

Return to bug 25672