| Lines 20-50
          use Modern::Perl;
      
      
        Link Here | 
        
          | 20 |  | 20 |  | 
        
          | 21 | use Archive::Extract; | 21 | use Archive::Extract; | 
        
          | 22 | use CGI qw ( -utf8 ); | 22 | use CGI qw ( -utf8 ); | 
            
              |  |  | 23 | use List::Util qw( any ); | 
        
          | 23 | use Mojo::UserAgent; | 24 | use Mojo::UserAgent; | 
        
          | 24 | use File::Temp; | 25 | use File::Temp; | 
        
          | 25 |  | 26 |  | 
        
          | 26 | use C4::Context; | 27 | use C4::Context; | 
          
            
              | 27 | use C4::Auth qw( get_template_and_user ); | 28 | use C4::Auth   qw( get_template_and_user ); | 
        
          | 28 | use C4::Output qw( output_html_with_http_headers ); | 29 | use C4::Output qw( output_html_with_http_headers ); | 
        
          | 29 | use C4::Members; | 30 | use C4::Members; | 
        
          | 30 | use Koha::Logger; | 31 | use Koha::Logger; | 
        
          | 31 | use Koha::Plugins; | 32 | use Koha::Plugins; | 
        
          | 32 |  | 33 |  | 
          
            
              | 33 | my $plugins_enabled = C4::Context->config("enable_plugins"); | 34 | my $plugins_enabled    = C4::Context->config("enable_plugins"); | 
            
              | 34 | my $browser_upload_enabled = C4::Context->config('enable_plugin_browser_upload'); | 35 | my $plugins_restricted = C4::Context->config("plugins_restricted"); | 
        
          | 35 |  | 36 |  | 
        
          | 36 | my $input = CGI->new; | 37 | my $input = CGI->new; | 
        
          | 37 |  | 38 |  | 
        
          | 38 | my ( $template, $loggedinuser, $cookie ) = get_template_and_user( | 39 | my ( $template, $loggedinuser, $cookie ) = get_template_and_user( | 
          
            
              | 39 |     {   template_name => ($plugins_enabled && $browser_upload_enabled) ? "plugins/plugins-upload.tt" : "plugins/plugins-disabled.tt", | 40 |     { | 
            
              |  |  | 41 |         template_name => $plugins_enabled ? "plugins/plugins-upload.tt" : "plugins/plugins-disabled.tt", | 
        
          | 40 |         query         => $input, | 42 |         query         => $input, | 
        
          | 41 |         type          => "intranet", | 43 |         type          => "intranet", | 
          
            
              | 42 |         flagsrequired   => { plugins => 'manage' }, | 44 |         flagsrequired => { plugins => 'manage' }, | 
        
          | 43 |     } | 45 |     } | 
        
          | 44 | ); | 46 | ); | 
        
          | 45 |  | 47 |  | 
          
            
              | 46 | if ($plugins_enabled){ | 48 | if ($plugins_enabled) { | 
            
              | 47 |     $template->param( browser_upload_enabled => $browser_upload_enabled ); | 49 |     $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; | 
        
          | 48 | } | 53 | } | 
        
          | 49 |  | 54 |  | 
        
          | 50 | my $uploadfilename = $input->param('uploadfile'); | 55 | my $uploadfilename = $input->param('uploadfile'); | 
  
    | Lines 56-62
          my ( $tempfile, $tfh );
      
      
        Link Here | 
        
          | 56 |  | 61 |  | 
        
          | 57 | my %errors; | 62 | my %errors; | 
        
          | 58 |  | 63 |  | 
          
            
              | 59 | if ($plugins_enabled && $browser_upload_enabled) { | 64 | if ($plugins_enabled) { | 
        
          | 60 |     if ( ( $op eq 'Upload' ) && ( $uploadfile || $uploadlocation ) ) { | 65 |     if ( ( $op eq 'Upload' ) && ( $uploadfile || $uploadlocation ) ) { | 
        
          | 61 |         my $plugins_dir = C4::Context->config("pluginsdir"); | 66 |         my $plugins_dir = C4::Context->config("pluginsdir"); | 
        
          | 62 |         $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; | 
  
    | Lines 67-88
          if ($plugins_enabled && $browser_upload_enabled) {
      
      
        Link Here | 
        
          | 67 |         $filesuffix = $1 if $uploadfilename =~ m/(\..+)$/i; | 72 |         $filesuffix = $1 if $uploadfilename =~ m/(\..+)$/i; | 
        
          | 68 |         ( $tfh, $tempfile ) = File::Temp::tempfile( SUFFIX => $filesuffix, UNLINK => 1 ); | 73 |         ( $tfh, $tempfile ) = File::Temp::tempfile( SUFFIX => $filesuffix, UNLINK => 1 ); | 
        
          | 69 |  | 74 |  | 
          
            
              | 70 |         $errors{'NOTKPZ'} = 1 if ( $uploadfilename !~ /\.kpz$/i ); | 75 |         $errors{'NOTKPZ'}         = 1 if ( $uploadfilename !~ /\.kpz$/i ); | 
        
          | 71 |         $errors{'NOWRITETEMP'}    = 1 unless ( -w $dirname ); | 76 |         $errors{'NOWRITETEMP'}    = 1 unless ( -w $dirname ); | 
        
          | 72 |         $errors{'NOWRITEPLUGINS'} = 1 unless ( -w $plugins_dir ); | 77 |         $errors{'NOWRITEPLUGINS'} = 1 unless ( -w $plugins_dir ); | 
        
          | 73 |  | 78 |  | 
          
            
              | 74 |         if ( $uploadlocation ) { | 79 |         if ($uploadlocation) { | 
            
              | 75 |             my $ua = Mojo::UserAgent->new(max_redirects => 5); | 80 |             my $do_get = 1; | 
            
              | 76 |             my $tx = $ua->get($uploadlocation); | 81 |             if ( $plugins_restricted ) { | 
            
              | 77 |             $tx->result->content->asset->move_to($tempfile); | 82 |                 my $repos = C4::Context->config('plugin_repos'); | 
            
              |  |  | 83 |  | 
            
              | 84 |                 # Fix data structure if only one repo defined | 
            
              | 85 |                 if ( ref($repos->{repo}) eq 'HASH' ) { | 
            
              | 86 |                     $repos = { repo => [ $repos->{repo} ] }; | 
            
              | 87 |                 } | 
            
              | 88 |  | 
            
              | 89 |                 $do_get = any { index($uploadlocation, $_->{org_name}) != -1 } @{ $repos->{repo} }; | 
            
              | 90 |             } | 
            
              | 91 |  | 
            
              | 92 |             if ( $do_get ) { | 
            
              | 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 |             } | 
        
          | 78 |         } else { | 99 |         } else { | 
          
            
              | 79 |             $errors{'EMPTYUPLOAD'}    = 1 unless ( length($uploadfile) > 0 ); | 100 |             $errors{'RESTRICTED'} = 1 unless ( !$plugins_restricted ); | 
            
              |  |  | 101 |             $errors{'EMPTYUPLOAD'} = 1 unless ( length($uploadfile) > 0 ); | 
        
          | 80 |         } | 102 |         } | 
        
          | 81 |  | 103 |  | 
        
          | 82 |         if (%errors) { | 104 |         if (%errors) { | 
        
          | 83 |             $template->param( ERRORS => [ \%errors ] ); | 105 |             $template->param( ERRORS => [ \%errors ] ); | 
        
          | 84 |         } else { | 106 |         } else { | 
          
            
              | 85 |             if ( $uploadfile ) { | 107 |             if ($uploadfile && !$plugins_restricted) { | 
        
          | 86 |                 while (<$uploadfile>) { | 108 |                 while (<$uploadfile>) { | 
        
          | 87 |                     print $tfh $_; | 109 |                     print $tfh $_; | 
        
          | 88 |                 } | 110 |                 } | 
  
    | Lines 104-110
          if ($plugins_enabled && $browser_upload_enabled) {
      
      
        Link Here | 
        
          | 104 |         warn "Problem uploading file or no file uploaded."; | 126 |         warn "Problem uploading file or no file uploaded."; | 
        
          | 105 |     } | 127 |     } | 
        
          | 106 |  | 128 |  | 
          
            
              | 107 |     if ( ($uploadfile || $uploadlocation) && !%errors && !$template->param('ERRORS') ) { | 129 |     if ( ( $uploadfile || $uploadlocation ) && !%errors && !$template->param('ERRORS') ) { | 
        
          | 108 |         print $input->redirect("/cgi-bin/koha/plugins/plugins-home.pl"); | 130 |         print $input->redirect("/cgi-bin/koha/plugins/plugins-home.pl"); | 
        
          | 109 |     } else { | 131 |     } else { | 
        
          | 110 |         output_html_with_http_headers $input, $cookie, $template->output; | 132 |         output_html_with_http_headers $input, $cookie, $template->output; | 
            
              | 111 | -  |  |  |