ZofCMS/Plugin/FileUpload.pm

filter: Statementstmt 13.7% 11 / 80 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 3 / 6 Podpod 0.0% 0 / 2 totaltotal 10.0% 14 / 140 CC 25 SCAR 57.9
CC
25
Cov
9%
_process_upload
462.0
process
30.0
new
2.0
CRAP 328.6
« ZofCMS/Plugin/FormChecker.pm ↑ Summary ZofCMS/Plugin/DBI.pm »
1 package App::ZofCMS::Plugin::FileUpload;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '1.001010'; # VERSION
7  
8 1 ▶use File::Spec::Functions (qw/catfile splitpath/);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶sub new { bless {}, shift }
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
11  
12 0 ▶sub process {
Subroutine process: not called CC=5 SCAR=34.0
Pod undocumented
13 0 my ( $self, $template, $query, $config ) = @_;
14  
15 return
16 0 ▶ unless $template->{file_upload};
Branch: 1 branch
BranchTrueFalseTotal
unless $template->{"file_upload"} 0 0 0
17  
18 0 my $uploads = delete $template->{file_upload};
19  
20 0 ▶ $uploads = [ $uploads ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $uploads eq "ARRAY" 0 0 0
21 unless ref $uploads eq 'ARRAY';
22  
23 0 ▶ my $upload_counter = @$uploads == 1 ? '' : 0;
Branch: 1 branch
BranchTrueFalseTotal
@$uploads == 1 ? : 0 0 0
24 0 for my $upload ( @$uploads ) {
25 0 $self->_process_upload(
26 $template,
27 $query,
28 $config,
29 $upload_counter++,
30 $upload,
31 );
32 }
33  
34 0 return 1;
35 }
36  
37 0 ▶sub _process_upload {
Subroutine _process_upload: not called CC=21 SCAR=61.4
38 0 my ( $self, $template, $query, $config, $upload_counter, $upload ) = @_;
39  
40 0 my $error_key = "upload_error$upload_counter";
41 0 my $filename_key = "upload_filename$upload_counter";
42 0 my $success_key = "upload_success$upload_counter";
43  
44 0 $upload = {
45 query => 'zofcms_upload',
46 path => 'zofcms_upload',
47 name => '[rand]',
48  
49 %$upload,
50 };
51  
52 0 my $cgi = $config->cgi;
53 0 my $remote_filename = $cgi->param( $upload->{query} );
54  
55 0 ▶ if ( not $remote_filename and $cgi->cgi_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $remote_filename and $cgi->cgi_error) 0 0 0
Condition0%0/3
not $remote_filename and $cgi->cgi_error
!ll&&!rl&&r
000
MC/DC0%0/2
not $remote_filename and $cgi->cgi_error
not $remote_filename $cgi->cgi_error
Truth table0%0/3
not(A) and B
A= not $remote_filenameB= $cgi->cgi_error
ABresult
0X0
100
111
56 0 $template->{t}{ $error_key } = $cgi->cgi_error;
57 0 return;
58 }
59  
60 return
61 0 ▶ unless defined $remote_filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $remote_filename 0 0 0
62  
63 ( $upload->{ext} ) = $remote_filename =~ /[^.]+([.].+)$/
64 0 ▶ unless defined $upload->{ext};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $upload->{"ext"} 0 0 0
65  
66 $upload->{ext} = ''
67 0 ▶ unless defined $upload->{ext};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $upload->{"ext"} 0 0 0
68  
69 0 ▶ if ( ref $upload->{name} eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $upload->{"name"} eq "CODE") 0 0 0
70 0 $upload->{name} = $upload->{name}->( $template, $query, $config );
71 }
72  
73 0 ▶ if ( $upload->{name} eq '[rand]' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($upload->{'name'} eq '[rand]') { } 0 0 0
elsif (ref $upload->{'name'} eq 'SCALAR') { } 0 0 0
elsif (ref $upload->{'name'} eq 'SCALAR') { } 0 0 0
74 UNIQUE_NAME: {
75 0 $upload->{name} = catfile(
76 $upload->{path},
77 0 do { my $x = rand() . time(); $x =~ tr/.//d; $x }
78 . $upload->{ext}
79 0 );
80 0 ▶ redo UNIQUE_NAME if -e $upload->{name};
Branch: 1 branch
BranchTrueFalseTotal
if -e $upload->{"name"} 0 0 0
81 }
82 }
83 elsif ( ref $upload->{name} eq 'SCALAR' ) {
84 0 $upload->{name} = ( splitpath $remote_filename )[-1];
85 0 $upload->{name} =~ s/(?<=[^.])[.].+$//;
86 $upload->{name} = catfile(
87 $upload->{path},
88 $upload->{name} . $upload->{ext}
89 0 );
90 }
91 elsif ( ref $upload->{name} eq 'SCALAR' ) {
92 $upload->{name} = catfile(
93 $upload->{path},
94 ( $remote_filename =~ /([^.]+)(?:[.].+)?$/)[0] . $upload->{ext}
95 0 );
96 }
97 else {
98 $upload->{name} = catfile(
99 $upload->{path},
100 $upload->{name} . $upload->{ext}
101 0 );
102 }
103  
104 0 ▶ if ( $upload->{name_filter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upload->{"name_filter"}) 0 0 0
105 0 $upload->{name} =~ s/$upload->{name_filter}//g;
106 }
107  
108 0 my $upload_info = $cgi->uploadInfo( $remote_filename );
109  
110 0 ▶ if ( defined $upload->{content_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $upload->{"content_type"}) 0 0 0
111 $upload->{content_type} = [ $upload->{content_type} ]
112 0 ▶ unless ref $upload->{content_type} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $upload->{"content_type"} eq "ARRAY" 0 0 0
113  
114 0 ▶ unless ( grep { $upload_info->{'Content-Type'} eq $_ }
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$upload_info->{"Content-Type"} eq $_;} @{$$upload{"content_type"};}) 0 0 0
115 0 @{ $upload->{content_type} }
116 ) {
117 0 $template->{t}{ $error_key } = 'Invalid file type';
118 0 return;
119 }
120 }
121  
122 0 my $fh = $cgi->upload( $upload->{query} );
123  
124 0 ▶ if ( not $fh and $cgi->cgi_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fh and $cgi->cgi_error) 0 0 0
Condition0%0/3
not $fh and $cgi->cgi_error
!ll&&!rl&&r
000
MC/DC0%0/2
not $fh and $cgi->cgi_error
not $fh $cgi->cgi_error
Truth table0%0/3
not(A) and B
A= not $fhB= $cgi->cgi_error
ABresult
0X0
100
111
125 0 $template->{t}{ $error_key } = $cgi->cgi_error;
126 0 return;
127 }
128  
129 return
130 0 ▶ unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
131  
132 0 my $fh_out;
133 0 ▶ unless ( open $fh_out, '>', $upload->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh_out, ">", $upload->{"name"}) 0 0 0
134 0 $template->{t}{ $error_key } = "Failed to open local file [$!]";
135 0 return;
136 }
137  
138 0 seek $fh, 0, 0;
139 0 binmode $fh;
140 0 binmode $fh_out;
141  
142 {
143 0 local $/ = \1024;
144 0 ▶ while ( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
145 0 print $fh_out $_;
146 }
147 }
148 0 close $fh;
149 0 close $fh_out;
150  
151 0 ▶ if ( ref $upload->{on_success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $upload->{"on_success"}) 0 0 0
152 $upload->{on_success}->(
153 0 $upload->{name}, $template, $query, $config,
154 );
155 }
156  
157 0 $template->{t}{ $success_key } = 1;
158 0 $template->{t}{ $filename_key } = $upload->{name};
159 0 return 1;
160 }
161  
162 1 1;
163 __END__
« ZofCMS/Plugin/FormChecker.pm ↑ Summary ZofCMS/Plugin/DBI.pm »