|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* FileUpload.php - This class handles HTTP file upload. |
|
5
|
|
|
* |
|
6
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
7
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
8
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
9
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Jaxon\Request\Support; |
|
13
|
|
|
|
|
14
|
|
|
use Closure; |
|
15
|
|
|
|
|
16
|
|
|
class FileUpload |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
use \Jaxon\Features\Validator; |
|
19
|
|
|
use \Jaxon\Features\Translator; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* A user defined function to transform uploaded file names |
|
23
|
|
|
* |
|
24
|
|
|
* @var Closure |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $cNameSanitizer = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Filter uploaded file name |
|
30
|
|
|
* |
|
31
|
|
|
* @param Closure $cNameSanitizer The closure which filters filenames |
|
|
|
|
|
|
32
|
|
|
* |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
|
public function setNameSanitizer(Closure $cNameSanitizer) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->cNameSanitizer = $cNameSanitizer; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Check uploaded files |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $aFiles The uploaded files |
|
|
|
|
|
|
44
|
|
|
* |
|
45
|
|
|
* @return void |
|
46
|
|
|
* @throws \Jaxon\Exception\Error |
|
47
|
|
|
*/ |
|
48
|
|
|
private function checkFiles(array $aFiles) |
|
49
|
|
|
{ |
|
50
|
|
|
foreach($aFiles as $sVarName => $aVarFiles) |
|
51
|
|
|
{ |
|
52
|
|
|
foreach($aVarFiles as $aFile) |
|
53
|
|
|
{ |
|
54
|
|
|
// Verify upload result |
|
55
|
|
|
if($aFile['error'] != 0) |
|
56
|
|
|
{ |
|
57
|
|
|
throw new \Jaxon\Exception\Error($this->trans('errors.upload.failed', $aFile)); |
|
58
|
|
|
} |
|
59
|
|
|
// Verify file validity (format, size) |
|
60
|
|
|
if(!$this->validateUploadedFile($sVarName, $aFile)) |
|
61
|
|
|
{ |
|
62
|
|
|
throw new \Jaxon\Exception\Error($this->getValidatorMessage()); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get a file from upload entry |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $sVarName The corresponding variable |
|
|
|
|
|
|
72
|
|
|
* @param array $aVarFiles An entry in the PHP $_FILES array |
|
|
|
|
|
|
73
|
|
|
* @param integer $nPosition The postion of the file to be processed |
|
|
|
|
|
|
74
|
|
|
* |
|
75
|
|
|
* @return null|array |
|
76
|
|
|
*/ |
|
77
|
|
|
private function getUploadedFile($sVarName, array $aVarFiles, $nPosition) |
|
78
|
|
|
{ |
|
79
|
|
|
if(!$aVarFiles['name'][$nPosition]) |
|
80
|
|
|
{ |
|
81
|
|
|
return null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// Filename without the extension |
|
85
|
|
|
$sFilename = pathinfo($aVarFiles['name'][$nPosition], PATHINFO_FILENAME); |
|
86
|
|
|
if(($this->cNameSanitizer)) |
|
87
|
|
|
{ |
|
88
|
|
|
$sFilename = (string)call_user_func_array($this->cNameSanitizer, [$sFilename, $sVarName]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return [ |
|
92
|
|
|
'name' => $aVarFiles['name'][$nPosition], |
|
93
|
|
|
'type' => $aVarFiles['type'][$nPosition], |
|
94
|
|
|
'tmp_name' => $aVarFiles['tmp_name'][$nPosition], |
|
95
|
|
|
'error' => $aVarFiles['error'][$nPosition], |
|
96
|
|
|
'size' => $aVarFiles['size'][$nPosition], |
|
97
|
|
|
'filename' => $sFilename, |
|
98
|
|
|
'extension' => pathinfo($aVarFiles['name'][$nPosition], PATHINFO_EXTENSION), |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Read uploaded files info from HTTP request data |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getUploadedFiles() |
|
108
|
|
|
{ |
|
109
|
|
|
// Check validity of the uploaded files |
|
110
|
|
|
$aUploadedFiles = []; |
|
111
|
|
|
foreach($_FILES as $sVarName => $aVarFiles) |
|
112
|
|
|
{ |
|
113
|
|
|
// If there is only one file, transform each entry into an array, |
|
114
|
|
|
// so the same processing for multiple files can be applied. |
|
115
|
|
|
if(!is_array($aVarFiles['name'])) |
|
116
|
|
|
{ |
|
117
|
|
|
$aVarFiles['name'] = [$aVarFiles['name']]; |
|
|
|
|
|
|
118
|
|
|
$aVarFiles['type'] = [$aVarFiles['type']]; |
|
|
|
|
|
|
119
|
|
|
$aVarFiles['tmp_name'] = [$aVarFiles['tmp_name']]; |
|
120
|
|
|
$aVarFiles['error'] = [$aVarFiles['error']]; |
|
|
|
|
|
|
121
|
|
|
$aVarFiles['size'] = [$aVarFiles['size']]; |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$nFileCount = count($aVarFiles['name']); |
|
125
|
|
|
for($i = 0; $i < $nFileCount; $i++) |
|
126
|
|
|
{ |
|
127
|
|
|
$aUploadedFile = $this->getUploadedFile($sVarName, $aVarFiles, $i); |
|
128
|
|
|
if(is_array($aUploadedFile)) |
|
129
|
|
|
{ |
|
130
|
|
|
if(!array_key_exists($sVarName, $aUploadedFiles)) |
|
131
|
|
|
{ |
|
132
|
|
|
$aUploadedFiles[$sVarName] = []; |
|
133
|
|
|
} |
|
134
|
|
|
$aUploadedFiles[$sVarName][] = $aUploadedFile; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
// Check uploaded files validity |
|
140
|
|
|
$this->checkFiles($aUploadedFiles); |
|
141
|
|
|
|
|
142
|
|
|
return $aUploadedFiles; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|