|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* UploadHandler.php - This class implements file upload with Ajax. |
|
5
|
|
|
* |
|
6
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
7
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
8
|
|
|
* @copyright 2017 Thierry Feuzeu <[email protected]> |
|
9
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
10
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
11
|
|
|
*/ |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
namespace Jaxon\Request\Handler; |
|
14
|
|
|
|
|
15
|
|
|
use Jaxon\Di\Container; |
|
16
|
|
|
use Jaxon\Exception\RequestException; |
|
17
|
|
|
use Jaxon\Response\Manager\ResponseManager; |
|
18
|
|
|
use Jaxon\Response\UploadResponse; |
|
19
|
|
|
use Jaxon\Utils\Translation\Translator; |
|
20
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
21
|
|
|
|
|
22
|
|
|
use Closure; |
|
23
|
|
|
use Exception; |
|
24
|
|
|
|
|
25
|
|
|
use function count; |
|
26
|
|
|
use function trim; |
|
27
|
|
|
|
|
28
|
|
|
class UploadHandler |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* DI container |
|
32
|
|
|
* |
|
33
|
|
|
* @var Container |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $di; |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The response manager |
|
39
|
|
|
* |
|
40
|
|
|
* @var ResponseManager |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $xResponseManager; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Translator |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $xTranslator; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The uploaded files copied in the user dir |
|
51
|
|
|
* |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $aUserFiles = []; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* The name of file containing upload data |
|
58
|
|
|
* |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $sTempFile = ''; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Is the current request an HTTP upload |
|
65
|
|
|
* |
|
66
|
|
|
* @var bool |
|
|
|
|
|
|
67
|
|
|
*/ |
|
68
|
|
|
protected $bIsAjaxRequest = true; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* The constructor |
|
72
|
|
|
* |
|
73
|
|
|
* @param Container $di |
|
|
|
|
|
|
74
|
|
|
* @param ResponseManager $xResponseManager |
|
|
|
|
|
|
75
|
|
|
* @param Translator $xTranslator |
|
|
|
|
|
|
76
|
|
|
*/ |
|
77
|
|
|
public function __construct(Container $di, ResponseManager $xResponseManager, Translator $xTranslator) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
$this->di = $di; |
|
|
|
|
|
|
80
|
|
|
$this->xResponseManager = $xResponseManager; |
|
81
|
|
|
$this->xTranslator = $xTranslator; |
|
|
|
|
|
|
82
|
|
|
} |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Set the uploaded file name sanitizer |
|
86
|
|
|
* |
|
87
|
|
|
* @param Closure $cSanitizer The closure |
|
|
|
|
|
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
|
|
public function sanitizer(Closure $cSanitizer) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->di->getUploadManager()->setNameSanitizer($cSanitizer); |
|
94
|
|
|
} |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get the uploaded files |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
|
|
public function files(): array |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->aUserFiles; |
|
104
|
|
|
} |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Inform this plugin that other plugin can process the current request |
|
108
|
|
|
* |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
|
|
public function isHttpUpload() |
|
112
|
|
|
{ |
|
113
|
|
|
$this->bIsAjaxRequest = false; |
|
114
|
|
|
} |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Check if the current request contains uploaded files |
|
118
|
|
|
* |
|
119
|
|
|
* @param ServerRequestInterface $xRequest |
|
|
|
|
|
|
120
|
|
|
* |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
|
|
public function canProcessRequest(ServerRequestInterface $xRequest): bool |
|
124
|
|
|
{ |
|
125
|
|
|
if(count($xRequest->getUploadedFiles()) > 0) |
|
126
|
|
|
{ |
|
127
|
|
|
return true; |
|
128
|
|
|
} |
|
129
|
|
|
$aBody = $xRequest->getParsedBody(); |
|
130
|
|
|
if(is_array($aBody)) |
|
131
|
|
|
{ |
|
132
|
|
|
return isset($aBody['jxnupl']); |
|
133
|
|
|
} |
|
134
|
|
|
$aParams = $xRequest->getQueryParams(); |
|
135
|
|
|
return isset($aParams['jxnupl']); |
|
136
|
|
|
} |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Read the upload temp file name from the HTTP request |
|
140
|
|
|
* |
|
141
|
|
|
* @param ServerRequestInterface $xRequest |
|
|
|
|
|
|
142
|
|
|
* |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
|
|
private function setTempFile(ServerRequestInterface $xRequest): bool |
|
146
|
|
|
{ |
|
147
|
|
|
$aBody = $xRequest->getParsedBody(); |
|
148
|
|
|
if(is_array($aBody)) |
|
149
|
|
|
{ |
|
150
|
|
|
$this->sTempFile = trim($aBody['jxnupl'] ?? ''); |
|
151
|
|
|
return $this->sTempFile !== ''; |
|
152
|
|
|
} |
|
153
|
|
|
$aParams = $xRequest->getQueryParams(); |
|
|
|
|
|
|
154
|
|
|
$this->sTempFile = trim($aParams['jxnupl'] ?? ''); |
|
155
|
|
|
return $this->sTempFile !== ''; |
|
156
|
|
|
} |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Process the uploaded files in the HTTP request |
|
160
|
|
|
* |
|
161
|
|
|
* @param ServerRequestInterface $xRequest |
|
|
|
|
|
|
162
|
|
|
* |
|
163
|
|
|
* @return bool |
|
164
|
|
|
* @throws RequestException |
|
165
|
|
|
*/ |
|
166
|
|
|
public function processRequest(ServerRequestInterface $xRequest): bool |
|
167
|
|
|
{ |
|
168
|
|
|
$xUploadManager = $this->di->getUploadManager(); |
|
169
|
|
|
if($this->setTempFile($xRequest)) |
|
170
|
|
|
{ |
|
171
|
|
|
// Ajax request following a normal HTTP upload. |
|
172
|
|
|
// Copy the previously uploaded files' location from the temp file. |
|
173
|
|
|
$this->aUserFiles = $xUploadManager->readFromTempFile($this->sTempFile); |
|
174
|
|
|
return true; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
if($this->bIsAjaxRequest) |
|
178
|
|
|
{ |
|
179
|
|
|
// Ajax request with upload. |
|
180
|
|
|
// Copy the uploaded files from the HTTP request. |
|
181
|
|
|
$this->aUserFiles = $xUploadManager->readFromHttpData($xRequest); |
|
182
|
|
|
return true; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
// For HTTP requests, save the files' location to a temp file, |
|
186
|
|
|
// and return a response with a reference to this temp file. |
|
187
|
|
|
try |
|
188
|
|
|
{ |
|
189
|
|
|
// Copy the uploaded files from the HTTP request, and create the temp file. |
|
190
|
|
|
$this->aUserFiles = $xUploadManager->readFromHttpData($xRequest); |
|
191
|
|
|
$sTempFile = $xUploadManager->saveToTempFile($this->aUserFiles); |
|
|
|
|
|
|
192
|
|
|
$this->xResponseManager->append(new UploadResponse($sTempFile)); |
|
193
|
|
|
} |
|
194
|
|
|
catch(Exception $e) |
|
195
|
|
|
{ |
|
196
|
|
|
$this->xResponseManager->append(new UploadResponse('', $e->getMessage())); |
|
197
|
|
|
} |
|
198
|
|
|
return true; |
|
199
|
|
|
} |
|
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
|