1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\request\files; |
4
|
|
|
|
5
|
|
|
use finfo; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class FileSubmission |
10
|
|
|
* |
11
|
|
|
* All the info about a file from $_FILES (except we determine mimetype ourselves, because what's in $_FILES isn't |
12
|
|
|
* reliable), but put together onto one object with a few helpers. |
13
|
|
|
* FilesDataHandler takes care of creating these from $_FILES. |
14
|
|
|
* |
15
|
|
|
* @package Event Espresso |
16
|
|
|
* @author Mike Nelson |
17
|
|
|
* @since 4.9.80.p |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
class FileSubmission implements FileSubmissionInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string original name on the client machine |
24
|
|
|
*/ |
25
|
|
|
protected $name; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string mime type |
29
|
|
|
*/ |
30
|
|
|
protected $type; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string file extension |
34
|
|
|
*/ |
35
|
|
|
protected $extension; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var int in bytes |
39
|
|
|
*/ |
40
|
|
|
protected $size; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string local filepath to the temporary file |
44
|
|
|
*/ |
45
|
|
|
protected $tmp_file; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var int one of UPLOAD_ERR_OK, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE or other values |
49
|
|
|
* although those aren't expected. |
50
|
|
|
*/ |
51
|
|
|
protected $error_code; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* FileSubmission constructor. |
55
|
|
|
* @param $name |
56
|
|
|
* @param $tmp_file |
57
|
|
|
* @param $size |
58
|
|
|
* @param null $error_code |
59
|
|
|
* @throws InvalidArgumentException |
60
|
|
|
*/ |
61
|
|
|
public function __construct($name, $tmp_file, $size, $error_code = null) |
62
|
|
|
{ |
63
|
|
|
$this->name = basename($name); |
64
|
|
|
$scheme = parse_url($tmp_file, PHP_URL_SCHEME); |
65
|
|
|
if (in_array($scheme, ['http', 'https'])) { |
66
|
|
|
// Wait a minute- just local filepaths please, no URL schemes allowed! |
67
|
|
|
throw new InvalidArgumentException( |
68
|
|
|
sprintf( |
69
|
|
|
// @codingStandardsIgnoreStart |
70
|
|
|
esc_html__('The scheme ("%1$s") on the temporary file ("%2$s") indicates is located elsewhere, that’s not ok!', 'event_espresso'), |
71
|
|
|
// @codingStandardsIgnoreEnd |
72
|
|
|
$scheme, |
73
|
|
|
$tmp_file |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
$this->tmp_file = (string) $tmp_file; |
78
|
|
|
$this->size = (int) $size; |
79
|
|
|
$this->error_code = (int) $error_code; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getName() |
86
|
|
|
{ |
87
|
|
|
return $this->name; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Gets the file's mime type |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function getType() |
95
|
|
|
{ |
96
|
|
|
if (!$this->type) { |
97
|
|
|
$this->type = $this->determineType(); |
98
|
|
|
} |
99
|
|
|
return $this->type; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @since 4.9.80.p |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
protected function determineType() |
107
|
|
|
{ |
108
|
|
|
if (!$this->getTmpFile()) { |
109
|
|
|
return ''; |
110
|
|
|
} |
111
|
|
|
$finfo = new finfo(FILEINFO_MIME_TYPE); |
112
|
|
|
return $finfo->file($this->getTmpFile()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Gets the file's extension. |
117
|
|
|
* @since 4.9.80.p |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getExtension() |
121
|
|
|
{ |
122
|
|
|
if (!$this->extension) { |
123
|
|
|
$this->extension = $this->determineExtension(); |
124
|
|
|
} |
125
|
|
|
return $this->extension; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Determine's the file's extension given the temporary file. |
130
|
|
|
* @since 4.9.80.p |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
protected function determineExtension() |
134
|
|
|
{ |
135
|
|
|
$position_of_period = strrpos($this->getName(), '.'); |
136
|
|
|
if ($position_of_period === false) { |
137
|
|
|
return ''; |
138
|
|
|
} |
139
|
|
|
return mb_substr( |
140
|
|
|
$this->getName(), |
141
|
|
|
$position_of_period + 1 |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Gets the size of the file |
147
|
|
|
* @return int |
148
|
|
|
*/ |
149
|
|
|
public function getSize() |
150
|
|
|
{ |
151
|
|
|
return $this->size; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Gets the path to the temporary file which was uploaded. |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function getTmpFile() |
159
|
|
|
{ |
160
|
|
|
return $this->tmp_file; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @since 4.9.80.p |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
public function __toString() |
168
|
|
|
{ |
169
|
|
|
return $this->getName(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Gets the error code PHP reported for the file upload. |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
public function getErrorCode() |
177
|
|
|
{ |
178
|
|
|
return $this->error_code; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
// End of file FileSubmission.php |
182
|
|
|
// Location: EventEspresso\core\services\request\files/FileSubmission.php |
183
|
|
|
|