|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EventEspresso\core\entities\forms; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class UploadedFile |
|
9
|
|
|
* |
|
10
|
|
|
* A file that was uploaded using an HTML form. |
|
11
|
|
|
* See https://secure.php.net/manual/en/features.file-upload.post-method.php |
|
12
|
|
|
* |
|
13
|
|
|
* @package Event Espresso |
|
14
|
|
|
* @author Mike Nelson |
|
15
|
|
|
* @since $VID:$ |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
class UploadedFile |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string original name on the client machine |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $name; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string mime type |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $type; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var int in bytes |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $size; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string local filepath to the temporary file |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $tmp_name; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string one of https://secure.php.net/manual/en/features.file-upload.errors.php |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $error; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct($php_file_info) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->name = $this->extractFromArrayOrThrowException($php_file_info, 'name'); |
|
48
|
|
|
try { |
|
49
|
|
|
$this->type = $this->extractFromArrayOrThrowException($php_file_info, 'type'); |
|
50
|
|
|
} catch (InvalidArgumentException $e) { |
|
51
|
|
|
// The browser must have not provided the mimetype, oh well. |
|
52
|
|
|
$this->type = 'text/html'; |
|
53
|
|
|
} |
|
54
|
|
|
$this->size = $this->extractFromArrayOrThrowException($php_file_info, 'size', 'int'); |
|
55
|
|
|
$this->tmp_name = $this->extractFromArrayOrThrowException($php_file_info, 'tmp_name'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Makes sure the input has the desired key and casts it as the appropriate type. |
|
60
|
|
|
* @since $VID:$ |
|
61
|
|
|
* @param $php_file_info_array |
|
62
|
|
|
* @param $desired_key |
|
63
|
|
|
* @param string $cast_as "string" or "int" |
|
64
|
|
|
* @return int|string |
|
65
|
|
|
* @throws InvalidArgumentException |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function extractFromArrayOrThrowException($php_file_info_array, $desired_key, $cast_as = 'string') |
|
68
|
|
|
{ |
|
69
|
|
|
if (!isset($php_file_info_array[$desired_key])) { |
|
70
|
|
|
throw new InvalidArgumentException( |
|
71
|
|
|
sprintf( |
|
72
|
|
|
esc_html__('PHP Upload data for a file was missing the key %1$s', 'event_espresso'), |
|
73
|
|
|
$desired_key |
|
74
|
|
|
) |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
$raw_value = $php_file_info_array[$desired_key]; |
|
78
|
|
|
switch ($cast_as) { |
|
79
|
|
|
case 'int': |
|
80
|
|
|
return (int)$raw_value; |
|
81
|
|
|
case 'string': |
|
82
|
|
|
default: |
|
83
|
|
|
return (string)$raw_value; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getName() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->name; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getType() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->type; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return int |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getSize() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->size; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getTmpName() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->tmp_name; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getError() |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->error; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @since $VID:$ |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
|
|
public function __toString() |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->getTmpName(); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
// End of file UploadedFile.php |
|
137
|
|
|
// Location: EventEspresso\core\entities\forms/UploadedFile.php |
|
138
|
|
|
|