| 1 | package CGI::Upload; | ||||||||||||||||||||||||||||
| 2 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use File::MMagic; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use HTTP::BrowserDetect; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use vars qw/ $AUTOLOAD $VERSION @ISA @EXPORT_OK /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 10 | require Exporter; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 10 | @ISA = qw/ Exporter /; | |||||||||||||||||||||||||||
| 16 | 10 | @EXPORT_OK = qw/ file_handle file_name file_type mime_magic mime_type query /; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 10 | $VERSION = '1.13'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub AUTOLOAD { | ||||||||||||||||||||||||||||
| 22 | 19 | ▶ | my ( $self, $param ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # Parse method name from $AUTOLOAD variable | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 19 | my $property = $AUTOLOAD; | |||||||||||||||||||||||||||
| 27 | 19 | $property =~ s/.*:://; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 19 | my @properties = qw/ file_handle file_name file_type mime_type /; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 19 | ▶ | unless ( grep { $property eq $_ } @properties ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 4 | croak( __PACKAGE__, '->AUTOLOAD : Unsupported object method within module - ', $property ); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Return undef if the requested parameter does not exist within | ||||||||||||||||||||||||||||
| 36 | # CGI object | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 15 | my $cgi = $self->query; | |||||||||||||||||||||||||||
| 39 | 15 | ▶ | return unless defined $cgi->param( $param ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # The determination of all information about the uploaded file is | ||||||||||||||||||||||||||||
| 42 | # performed by a private subroutine called _handle_file - This subroutine | ||||||||||||||||||||||||||||
| 43 | # returns a hash of all information determined about the uploaded file | ||||||||||||||||||||||||||||
| 44 | # which is be cached for subsequent requests. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 10 | ▶ | $self->{'_CACHE'}->{$param} = $self->_handle_file( $param ) unless exists $self->{'_CACHE'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Return the requested property of the uploaded file | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 10 | return $self->{'_CACHE'}->{$param}->{$property}; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | ▶ | sub DESTROY {} | |||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub _handle_file { | ||||||||||||||||||||||||||||
| 58 | 5 | ▶ | my ( $self, $param ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_handle_file: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||
| 59 | 5 | my $cgi = $self->query; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Determine and set the appropriate file system parsing routines for the | ||||||||||||||||||||||||||||
| 62 | # uploaded path name based upon the HTTP client header information. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 5 | my $client_os = $^O; | |||||||||||||||||||||||||||
| 65 | 5 | my $browser = HTTP::BrowserDetect->new; | |||||||||||||||||||||||||||
| 66 | 5 | ▶ | $client_os = 'MSWin32' if $browser->windows; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 5 | ▶ | $client_os = 'MacOS' if $browser->mac; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 5 | fileparse_set_fstype($client_os); | |||||||||||||||||||||||||||
| 69 | 5 | my @file = fileparse( scalar($cgi->param( $param )), '\.[^.]*' ); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Return an undefined value if the file name cannot be parsed from the | ||||||||||||||||||||||||||||
| 72 | # file field form parameter. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 5 | ▶ | return unless $file[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Determine whether binary mode is required in the handling of uploaded | ||||||||||||||||||||||||||||
| 77 | # files - | ||||||||||||||||||||||||||||
| 78 | # Binary mode is deemed to be required when we (the server) are running one one | ||||||||||||||||||||||||||||
| 79 | # of these platforms: for Windows, OS/2 and VMS | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 5 | my $binmode = $^O =~ /OS2|VMS|Win|DOS|Cygwin/i; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Pass uploaded file into temporary file handle - This is somewhat | ||||||||||||||||||||||||||||
| 84 | # redundant given the temporary file generation within CGI.pm, however is | ||||||||||||||||||||||||||||
| 85 | # included to reduce dependence upon the CGI.pm module. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 5 | my $buffer; | |||||||||||||||||||||||||||
| 88 | 5 | my $fh = IO::File->new_tmpfile; | |||||||||||||||||||||||||||
| 89 | 5 | ▶ | binmode( $fh ) if $binmode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # it seems that in CGI::Simple for every call to ->upload it somehow resets | ||||||||||||||||||||||||||||
| 93 | # the file handle. or I don't really know what is the problem with this code: | ||||||||||||||||||||||||||||
| 94 | # while ( read( $cgi->upload( $param ) , $buffer, 1024 ) ) { | ||||||||||||||||||||||||||||
| 95 | 5 | my $ourfh = $cgi->upload( $param ); | |||||||||||||||||||||||||||
| 96 | 5 | ▶ | while ( read( $ourfh , $buffer, 1024 ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 5 | $fh->write( $buffer, length( $buffer ) ); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # Hold temporary file open, move file pointer to start - As the temporary | ||||||||||||||||||||||||||||
| 101 | # file handle returned by the IO::File::new_tmpfile method is only | ||||||||||||||||||||||||||||
| 102 | # accessible via this handle, the file handle must be held open for all | ||||||||||||||||||||||||||||
| 103 | # operations. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 5 | $fh->seek( 0, 0 ); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # Retrieve the MIME magic file, if this has been defined, and construct | ||||||||||||||||||||||||||||
| 108 | # the File::MMagic object for the identification of the MIME type of the | ||||||||||||||||||||||||||||
| 109 | # uploaded file. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 5 | my $mime_magic = $self->mime_magic; | |||||||||||||||||||||||||||
| 112 | 5 | ▶ | my $magic = length $mime_magic ? File::MMagic->new( $mime_magic ) : File::MMagic->new; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 5 | my $properties = { | |||||||||||||||||||||||||||
| 115 | 'file_handle' => $fh, | ||||||||||||||||||||||||||||
| 116 | 'file_name' => $file[0] . $file[2], | ||||||||||||||||||||||||||||
| 117 | 'file_type' => lc substr( $file[2], 1 ), | ||||||||||||||||||||||||||||
| 118 | 'mime_type' => $magic->checktype_filehandle($fh) | ||||||||||||||||||||||||||||
| 119 | }; | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # Hold temporary file open, move file pointer to start - As the temporary | ||||||||||||||||||||||||||||
| 122 | # file handle returned by the IO::File::new_tmpfile method is only | ||||||||||||||||||||||||||||
| 123 | # accessible via this handle, the file handle must be held open for all | ||||||||||||||||||||||||||||
| 124 | # operations. | ||||||||||||||||||||||||||||
| 125 | # | ||||||||||||||||||||||||||||
| 126 | # The importance of this operation here is due to the MIME type | ||||||||||||||||||||||||||||
| 127 | # identification routine of File::MMagic on the open file handle | ||||||||||||||||||||||||||||
| 128 | # (File::MMagic->checktype_filehandle), which may or may not reset the | ||||||||||||||||||||||||||||
| 129 | # file pointer following its operation. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 5 | $fh->seek( 0, 0 ); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 5 | return $properties; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub mime_magic { | ||||||||||||||||||||||||||||
| 138 | 5 | ▶ | my ( $self, $magic ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
mime_magic: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # If a filename is passed to this subroutine as an argument, this filename | ||||||||||||||||||||||||||||
| 141 | # is taken to be the file containing file MIME types and magic numbers | ||||||||||||||||||||||||||||
| 142 | # which File::MMagic uses for determining file MIME types. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 5 | ▶ | $self->{'_MIME'} = $magic if defined $magic; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 5 | return $self->{'_MIME'}; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | sub new { | ||||||||||||||||||||||||||||
| 150 | 8 | ▶ | my ( $class, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=22.5
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 8 | ▶ | if ($args and 'HASH' ne ref $args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args and "HASH" ne ref $args
MC/DC100%2/2
$args and "HASH" ne ref $args
$args
"HASH" ne ref $args
Truth table100%3/3
A and B
A= $argsB= "HASH" ne ref $args
| |||||||||||||||||||||||||||||
| 153 | 1 | croak( __PACKAGE__, 'Argument to new should be a HASH reference'); | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | 7 | my $query; | |||||||||||||||||||||||||||
| 156 | 7 | my $module = "CGI"; # default module is CGI.pm if for nothing else for backword compatibility | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 7 | ▶ | if ($args and $args->{query}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args and $args->{"query"}
MC/DC50%1/2
$args and $args->{"query"}
$args
$args->{"query"}
Truth table66%2/3
A and B
A= $argsB= $args->{"query"}
| |||||||||||||||||||||||||||||
| 159 | 3 | $module = $args->{query}; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 7 | ▶ | if (ref $module) { # an object was passed to us | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 1 | $query = $module; | |||||||||||||||||||||||||||
| 164 | 1 | $module = ref $module; | |||||||||||||||||||||||||||
| 165 | } else { # assuming a name of a module was passed to us | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | # load the requested module | ||||||||||||||||||||||||||||
| 168 | 6 | (my $file = $module) =~ s{::}{/}g; | |||||||||||||||||||||||||||
| 169 | 6 | $file .= ".pm"; | |||||||||||||||||||||||||||
| 170 | 6 | require $file; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 5 | ▶ | if ("CGI::Simple" eq $module) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | $CGI::Simple::DISABLE_UPLOADS = 0; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 5 | $query = new $module; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 6 | ▶ | if ($module eq "CGI::Simple" and $CGI::Simple::VERSION < '0.075') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$module eq "CGI::Simple" and $CGI::Simple::VERSION < "0.075"
MC/DC0%0/2
$module eq "CGI::Simple" and $CGI::Simple::VERSION < "0.075"
$module eq "CGI::Simple"
$CGI::Simple::VERSION < "0.075"
Truth table33%1/3
A and B
A= $module eq "CGI::Simple"B= $CGI::Simple::VERSION < "0.075"
| |||||||||||||||||||||||||||||
| 180 | 0 | die "CGI::Simple must be at least version 0.075\n"; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 6 | my $self = bless { | |||||||||||||||||||||||||||
| 184 | # '_CACHE' => {}, | ||||||||||||||||||||||||||||
| 185 | '_CGI' => $query, | ||||||||||||||||||||||||||||
| 186 | '_MIME' => '' | ||||||||||||||||||||||||||||
| 187 | }, $class; | ||||||||||||||||||||||||||||
| 188 | 6 | return $self; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | sub query { | ||||||||||||||||||||||||||||
| 193 | 20 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
query: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 194 | 20 | return $self->{'_CGI'}; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 10 | 1; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | __END__ | ||||||||||||||||||||||||||||