blib/lib/CGI/UploadEasy.pm

filter: Statementstmt 22.4% 20 / 89 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 46.1% 6 / 13 Podpod 100% 4 / 4 totaltotal 17.6% 30 / 170 CC 28 SCAR 45.1
CC
28
Cov
14%
_argscheck
182.0
_upload
110.0
new
30.0
CRAP 91.2
↑ Summary
1 package CGI::UploadEasy;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use CGI 2.76;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 $Carp::CarpLevel = 1;
11  
12 1 our $VERSION = '1.00';
13 # $Id: UploadEasy.pm,v 1.8 2009/02/01 21:04:22 gunnarh Exp $
14  
15 =head1 NAME
16  
17 CGI::UploadEasy - Facilitate file uploads
18  
19 =head1 SYNOPSIS
20  
21 use CGI::UploadEasy;
22 my $ue = CGI::UploadEasy->new(-uploaddir => '/path/to/upload/dir');
23 my $cgi = $ue->cgiobject;
24 my $info = $ue->fileinfo;
25  
26 =head1 DESCRIPTION
27  
28 C<CGI::UploadEasy> is a wrapper around, and relies heavily on, L<CGI.pm|CGI>. Its
29 purpose is to provide a simple interface to the upload functionality of C<CGI.pm>.
30  
31 At creation of the C<CGI::UploadEasy> object, the module saves one or more files
32 from a file upload request in the upload directory, and information about uploaded
33 files is made available via the B<fileinfo()> method. C<CGI::UploadEasy> performs
34 a number of tests, which limit the risk that you encounter difficulties when
35 developing a file upload application.
36  
37 =head2 Methods
38  
39 =cut
40  
41 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
42 0 my $class = shift;
43 0 my $self = {
44 maxsize => 1000,
45 &_argscheck,
46 };
47  
48 0 $CGI::POST_MAX = $self->{maxsize} * 1024;
49 0 $CGI::DISABLE_UPLOADS = 0;
50 0 ▶ $CGITempFile::TMPDIRECTORY = $self->{tempdir} if $self->{tempdir};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tempdir"} 0 0 0
51 0 $self->{cgi} = CGI->new;
52 0 ▶ if ( my $status = $self->{cgi}->cgi_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $status = $self->{"cgi"}->cgi_error) 0 0 0
53 0 _error($self, $status, "Post too large: Maxsize $self->{maxsize} KiB exceeded.");
54 }
55  
56 0 ▶ if ( $ENV{REQUEST_METHOD} eq 'POST' and $ENV{CONTENT_TYPE} !~ /^multipart\/form-data\b/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"REQUEST_METHOD"} eq "POST" and not $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i) 0 0 0
Condition0%0/3
$ENV{"REQUEST_METHOD"} eq "POST" and not $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"REQUEST_METHOD"} eq "POST" and not $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i
$ENV{"REQUEST_METHOD"} eq "POST" not $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i
Truth table0%0/3
A and not(B)
A= $ENV{"REQUEST_METHOD"} eq "POST"B= not $ENV{"CONTENT_TYPE"} =~ m[^multipart/form-data\b]i
ABresult
0X0
100
111
57 0 _error($self, '400 Bad Request', 'The content-type at file uploads shall be '
58 . "'multipart/form-data'.<br />\nMake sure that the 'FORM' tag includes the "
59 . 'attribute: enctype=&quot;multipart/form-data&quot;');
60 }
61  
62 0 $self->{files} = _upload($self);
63  
64 0 bless $self, $class;
65 }
66  
67 =over 4
68  
69 =item B<my $ue = CGI::UploadEasy-E<gt>new( -uploaddir =E<gt> $dir [ , -maxsize =E<gt> $kibibytes, ... ] )>
70  
71 The B<new()> constructor takes hash style arguments. The following arguments are
72 recognized:
73  
74 =over 4
75  
76 =item B<-uploaddir>
77  
78 Specifying the upload directory is mandatory.
79  
80 =item B<-tempdir>
81  
82 To control which directory will be used for temporary files, set the -tempdir
83 argument.
84  
85 =item B<-maxsize>
86  
87 Specifies the maximum size in KiB (kibibytes) of a POST request data set.
88 Default limit is 1,000 KiB. To disable this ceiling for POST requests, set a
89 negative -maxsize value.
90  
91 =back
92  
93 =back
94  
95 =cut
96  
97 0 ▶sub cgiobject {
Subroutine cgiobject: not called CC=1 SCAR=6.9
Pod documented
98 0 my $self = shift;
99 0 $self->{cgi};
100 }
101  
102 =over 4
103  
104 =item B<$ue-E<gt>cgiobject>
105  
106 Returns a reference to the C<CGI> object that C<CGI::UploadEasy> uses internally,
107 which gives access to all the L<CGI.pm|CGI> methods.
108  
109 If you prefer the function-oriented style, you can import a set of methods
110 instead. Example:
111  
112 use CGI qw/:standard/;
113 print header;
114  
115 =back
116  
117 =cut
118  
119 0 ▶sub fileinfo {
Subroutine fileinfo: not called CC=2 SCAR=17.9
Pod documented
120 0 my $self = shift;
121 0 ▶ if ( @_ ) { croak "The 'fileinfo' method does not take arguments" }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
122 0 $self->{files};
123 }
124  
125 =over 4
126  
127 =item B<$ue-E<gt>fileinfo>
128  
129 Returns a reference to a 'hash of hashes' with info about uploaded files. The info
130 may be of use for a result page and/or an email notification, and it lets you use
131 e.g. MIME type and file size as criteria for how to further process the files.
132  
133 =back
134  
135 =cut
136  
137 0 ▶sub otherparam {
Subroutine otherparam: not called CC=2 SCAR=17.9
Pod documented
138 0 my $self = shift;
139 0 ▶ if ( @_ ) { croak "The 'otherparam' method does not take arguments",
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
140 "--use CGI.pm's 'param' method to access values" }
141 0 my $cgi = $self->{cgi};
142 0 grep ! ref $cgi->param($_), $cgi->param;
143 }
144  
145 =over 4
146  
147 =item B<$ue-E<gt>otherparam>
148  
149 The B<otherparam()> method returns a list of parameter names besides the names
150 of the file select controls that were used for file uploads. To access the values,
151 use L<CGI.pm|CGI>'s B<param()> method.
152  
153 =back
154  
155 =cut
156  
157 0 ▶sub _argscheck {
Subroutine _argscheck: not called CC=13 SCAR=52.0
158 0 my %args;
159 0 my %names = (
160 -uploaddir => 'uploaddir',
161 -tempdir => 'tempdir',
162 -maxsize => 'maxsize',
163 );
164 0 local $Carp::CarpLevel = 2;
165  
166 0 ▶ @_ % 2 == 0 and @_ > 0 or croak 'One or more name=>argument pairs are ',
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 0 and @_ > 0 or croak("One or more name=>argument pairs are ", "expected at the creation of the CGI::UploadEasy object") 0 0 0
Condition0%0/3
@_ % 2 == 0 and @_ > 0
!ll&&!rl&&r
000
MC/DC0%0/2
@_ % 2 == 0 and @_ > 0
@_ % 2 == 0 @_ > 0
Truth table0%0/3
A and B
A= @_ % 2 == 0B= @_ > 0
ABresult
0X0
100
111
167 'expected at the creation of the CGI::UploadEasy object';
168  
169 0 ▶ while ( my $arg = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift()) 0 0 0
170 0 my $name = lc $arg;
171 0 ▶ $names{$name} or croak "Unknown argument: '$arg'";
Branch: 1 branch
BranchTrueFalseTotal
$names{$name} or croak("Unknown argument: '${arg}'") 0 0 0
172 0 $args{ $names{$name} } = shift;
173 }
174 0 ▶ $args{uploaddir} or croak "The compulsory argument '-uploaddir' is missing";
Branch: 1 branch
BranchTrueFalseTotal
$args{"uploaddir"} or croak("The compulsory argument '-uploaddir' is missing") 0 0 0
175  
176 0 for my $dir ( @args{ grep exists $args{$_}, qw/uploaddir tempdir/ } ) {
177 0 ▶ -d $dir or croak "Can't find any directory '$dir'";
Branch: 1 branch
BranchTrueFalseTotal
-d $dir or croak("Can't find any directory '${dir}'") 0 0 0
178 0 ▶ -r $dir and -w _ and -x _ or croak 'The user this script runs as ',
Branch: 1 branch
BranchTrueFalseTotal
-r $dir and -w _ and -x _ or croak("The user this script runs as ", "does not have write access to '${dir}'") 0 0 0
Condition0%0/6
-r $dir and -w _ and -x _
!ll&&!rl&&r
000
-r $dir and -w _
!ll&&!rl&&r
000
MC/DC0%0/3
-r $dir and -w _ and -x _
-r $dir -w _ -x _
Truth table0%0/4
A and B and C
A= -r $dirB= -w _C= -x _
ABCresult
0XX0
10X0
1100
1111
179 "does not have write access to '$dir'";
180 }
181 0 ▶ $args{maxsize} and $args{maxsize} !~ /^-?\d+$/
Branch: 1 branch
BranchTrueFalseTotal
$args{"maxsize"} and not $args{"maxsize"} =~ /^-?\d+$/ and croak("The '-maxsize' argument shall be an integer") 0 0 0
Condition0%0/3
$args{"maxsize"} and not $args{"maxsize"} =~ /^-?\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"maxsize"} and not $args{"maxsize"} =~ /^-?\d+$/
$args{"maxsize"} not $args{"maxsize"} =~ /^-?\d+$/
Truth table0%0/3
A and not(B)
A= $args{"maxsize"}B= not $args{"maxsize"} =~ /^-?\d+$/
ABresult
0X0
100
111
182 and croak "The '-maxsize' argument shall be an integer";
183  
184 0 %args;
185 }
186  
187 0 ▶sub _upload {
Subroutine _upload: not called CC=10 SCAR=47.0
188 0 my $self = shift;
189 0 my $cgi = $self->{cgi};
190 0 my %files;
191  
192 0 for my $TEMP ( map $cgi->upload($_), $cgi->param ) {
193 0 ( my $name = $TEMP ) =~ s#.*[\]:\\/]##;
194 0 ▶ $name =~ tr/ /_/ unless $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "MSWin32" 0 0 0
195 0 $name =~ tr/-+@a-zA-Z0-9. /_/cs;
196 0 ($name) = $name =~ /^([-+@\w. ]+)$/;
197 0 my $path = File::Spec->catfile( $self->{uploaddir}, $name );
198  
199 # don't overwrite file with same name
200 0 my $i = 2;
201 0 while (1) {
202 0 ▶ last unless -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 0 0 0
203 0 $name =~ s/([^.]+?)(?:_\d+)?(\.|$)/$1_$i$2/;
204 0 $path = File::Spec->catfile( $self->{uploaddir}, $name );
205 0 $i++;
206 }
207  
208 0 my ($cntrname) = $cgi->uploadInfo($TEMP)->{'Content-Disposition'} =~ /\bname="([^"]+)"/;
209 $files{$name} = {
210 ctrlname => $cntrname,
211 0 mimetype => $cgi->uploadInfo($TEMP)->{'Content-Type'},
212 };
213  
214 0 ▶ open my $OUT, '>', $path or die "Couldn't open file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $OUT, ">", $path or die "Couldn't open file: $!" 0 0 0
215 0 ▶ if ( $files{$name}{mimetype} =~ /^text\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($files{$name}{'mimetype'} =~ /^text\b/) { } 0 0 0
216 0 binmode $TEMP, ':crlf';
217 0 ▶ print $OUT $_ while <$TEMP>;
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $TEMP) and do { print $OUT $_ } 0 0 0
218 } else {
219 0 binmode $OUT, ':raw';
220 0 ▶ while ( read $TEMP, my $buffer, 1024 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $TEMP, my $buffer, 1024) 0 0 0
221 0 print $OUT $buffer;
222 }
223 }
224 0 ▶ close $TEMP or die $!; # so the temporary file gets deleted
Branch: 1 branch
BranchTrueFalseTotal
close $TEMP or die $! 0 0 0
225 0 ▶ close $OUT or die $!; # so file size can be grabbed below
Branch: 1 branch
BranchTrueFalseTotal
close $OUT or die $! 0 0 0
226  
227 0 $files{$name}{bytes} = -s $path;
228 }
229  
230 0 \%files;
231 }
232  
233 0 ▶sub _error {
Subroutine _error: not called CC=1 SCAR=6.9
234 0 my ($self, $status, $msg) = @_;
235 0 my $cgi = $self->{cgi};
236 0 print $cgi->header(-status => $status),
237 $cgi->start_html(-title => "Error $status"),
238 $cgi->h1('Error'),
239 $cgi->tt($msg),
240 $cgi->end_html;
241 0 exit 1;
242 }
243  
244 1 1;
245  
246 __END__
↑ Summary