line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*-perl-*- |
2
|
|
|
|
|
|
|
# Creation date: 2003-09-01 22:23:46 |
3
|
|
|
|
|
|
|
# Authors: Don |
4
|
|
|
|
|
|
|
# Change log: |
5
|
|
|
|
|
|
|
# $Id: UploadFile.pm,v 1.6 2004/10/24 10:33:08 don Exp $ |
6
|
|
|
|
|
|
|
|
7
|
6
|
|
|
6
|
|
36
|
use strict; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
309
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
{ package CGI::Utils::UploadFile; |
10
|
|
|
|
|
|
|
|
11
|
6
|
|
|
6
|
|
35
|
use vars qw($VERSION); |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
1326
|
|
12
|
|
|
|
|
|
|
$VERSION = do { my @r=(q$Revision: 1.6 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r }; |
13
|
|
|
|
|
|
|
|
14
|
6
|
|
|
6
|
|
146
|
use vars qw($FH_COUNT $Have_File_Temp $Open_Flags); |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
418
|
|
15
|
|
|
|
|
|
|
$FH_COUNT = 0; |
16
|
|
|
|
|
|
|
|
17
|
6
|
|
|
6
|
|
32
|
use Fcntl (); |
|
6
|
|
|
|
|
37
|
|
|
6
|
|
|
|
|
323
|
|
18
|
|
|
|
|
|
|
|
19
|
6
|
|
|
6
|
|
14750
|
use overload '""' => '_asString', cmp => '_compareAsString', fallback => 1; |
|
6
|
|
|
|
|
8862
|
|
|
6
|
|
|
|
|
40
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
BEGIN { |
22
|
6
|
|
|
6
|
|
1174
|
local $SIG{__DIE__} = sub {}; |
|
6
|
|
|
|
|
1151
|
|
23
|
6
|
|
|
|
|
186
|
local $SIG{__WARN__} = sub {}; |
|
0
|
|
|
|
|
0
|
|
24
|
6
|
|
|
|
|
534
|
$Have_File_Temp = eval 'require File::Temp; 1'; |
25
|
6
|
|
|
|
|
101
|
$Open_Flags = Fcntl::O_RDWR()|Fcntl::O_CREAT(); |
26
|
|
|
|
|
|
|
# Fcntl::O_EXCL(); - leave this out for now cuz it breaks File::Temp usage |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# idea taken from File::Temp |
29
|
6
|
50
|
|
|
|
41
|
unless ($^O eq 'MacOS') { |
30
|
6
|
|
|
|
|
12
|
my $bit = 0; |
31
|
6
|
50
|
|
|
|
522
|
$Open_Flags |= $bit if eval '$bit = Fcntl::O_TEMPORARY()'; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub new { |
36
|
0
|
|
|
0
|
0
|
|
my ($proto, $name) = @_; |
37
|
6
|
|
|
6
|
|
54
|
no strict 'refs'; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
7407
|
|
38
|
0
|
|
|
|
|
|
(my $safe_name = $name) =~ s/([^a-zA-Z0-9_])/sprintf("%%%02x", ord($1))/eg; |
|
0
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
$FH_COUNT++; |
40
|
0
|
|
|
|
|
|
my $sub_name = "fh" . $FH_COUNT . "_" . $safe_name; |
41
|
0
|
|
|
|
|
|
my $ref = \*{"CGI::Utils::UploadFile::$sub_name"}; |
|
0
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
my $self = bless $ref, $proto; |
43
|
0
|
0
|
|
|
|
|
return wantarray ? ($self, $sub_name) : $self; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub new_from_handle { |
47
|
0
|
|
|
0
|
0
|
|
my ($proto, $file_name, $old_fh) = @_; |
48
|
0
|
|
|
|
|
|
my ($fh, $name_space) = $proto->new($file_name); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# dup the old file handle |
51
|
0
|
|
|
|
|
|
open($fh, ">&", $old_fh); |
52
|
0
|
|
|
|
|
|
return $fh; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub new_tmpfile { |
56
|
0
|
|
|
0
|
0
|
|
my ($proto, $file_name) = @_; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
my ($fh, $name_space) = $proto->new($file_name); |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
my $tmp_file = ''; |
61
|
|
|
|
|
|
|
|
62
|
0
|
0
|
|
|
|
|
if ($Have_File_Temp) { |
63
|
0
|
|
|
|
|
|
my $tmp_fh = File::Temp->new(UNLINK => 0); |
64
|
0
|
|
|
|
|
|
$tmp_file = $tmp_fh->filename; |
65
|
|
|
|
|
|
|
} else { |
66
|
0
|
|
|
|
|
|
my $tmp_dir = "/tmp"; |
67
|
0
|
|
|
|
|
|
$tmp_file = $tmp_dir . |
68
|
|
|
|
|
|
|
"/_cgi_utils_" . sprintf("%x%x%x", 10000 + int rand(10000), time(), $$); |
69
|
0
|
|
|
|
|
|
for my $i (1 .. 20) { |
70
|
0
|
0
|
|
|
|
|
last unless -e $tmp_file; |
71
|
0
|
|
|
|
|
|
$tmp_file = $tmp_dir . |
72
|
|
|
|
|
|
|
"/_cgi_utils_" . sprintf("%x%x%x", 10000 + int rand(10000), time(), $$); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
0
|
0
|
|
|
|
|
sysopen($fh, $tmp_file, $Open_Flags, 0600) |
77
|
|
|
|
|
|
|
or return undef; |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
unlink $tmp_file; |
80
|
0
|
|
|
|
|
|
delete $CGI::Utils::UploadFile::{$name_space}; |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
return $fh; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub filename { |
86
|
0
|
|
|
0
|
0
|
|
return shift()->_asString; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub _asString { |
90
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
(my $safe_name = $$self) =~ s/^.+::fh\d+_([^:]+)$/$1/; |
93
|
0
|
|
|
|
|
|
$safe_name =~ s/%([a-f0-9]{2})/chr(hex($1))/eg; |
|
0
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
return $safe_name; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub _compareAsString { |
98
|
0
|
|
|
0
|
|
|
my ($self, $val) = @_; |
99
|
0
|
|
|
|
|
|
return "$self" cmp $val; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub DESTROY { |
103
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
104
|
0
|
|
|
|
|
|
close $self; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
__END__ |