@@ -135,12 +135,12 @@ discard block |
||
135 | 135 | */ |
136 | 136 | public function canProcessRequest(ServerRequestInterface $xRequest): bool |
137 | 137 | { |
138 | - if(count($xRequest->getUploadedFiles()) > 0) |
|
138 | + if (count($xRequest->getUploadedFiles()) > 0) |
|
139 | 139 | { |
140 | 140 | return true; |
141 | 141 | } |
142 | 142 | $aBody = $xRequest->getParsedBody(); |
143 | - if(is_array($aBody)) |
|
143 | + if (is_array($aBody)) |
|
144 | 144 | { |
145 | 145 | return isset($aBody['jxnupl']); |
146 | 146 | } |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | private function setTempFile(ServerRequestInterface $xRequest): bool |
159 | 159 | { |
160 | 160 | $aBody = $xRequest->getParsedBody(); |
161 | - if(is_array($aBody)) |
|
161 | + if (is_array($aBody)) |
|
162 | 162 | { |
163 | 163 | $this->sTempFile = trim($aBody['jxnupl'] ?? ''); |
164 | 164 | return $this->sTempFile !== ''; |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | */ |
179 | 179 | public function processRequest(ServerRequestInterface $xRequest): bool |
180 | 180 | { |
181 | - if($this->setTempFile($xRequest)) |
|
181 | + if ($this->setTempFile($xRequest)) |
|
182 | 182 | { |
183 | 183 | // Ajax request following a normal HTTP upload. |
184 | 184 | // Copy the previously uploaded files' location from the temp file. |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | return true; |
187 | 187 | } |
188 | 188 | |
189 | - if($this->bIsAjaxRequest) |
|
189 | + if ($this->bIsAjaxRequest) |
|
190 | 190 | { |
191 | 191 | // Ajax request with upload. |
192 | 192 | // Copy the uploaded files from the HTTP request. |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | $sTempFile = $this->xUploadManager->saveToTempFile($this->aUserFiles); |
204 | 204 | $this->xResponseManager->append(new UploadResponse($this->xPsr17Factory, $sTempFile)); |
205 | 205 | } |
206 | - catch(Exception $e) |
|
206 | + catch (Exception $e) |
|
207 | 207 | { |
208 | 208 | $this->xResponseManager->append(new UploadResponse($this->xPsr17Factory, '', $e->getMessage())); |
209 | 209 | } |
@@ -202,8 +202,7 @@ |
||
202 | 202 | $this->aUserFiles = $this->xUploadManager->readFromHttpData($xRequest); |
203 | 203 | $sTempFile = $this->xUploadManager->saveToTempFile($this->aUserFiles); |
204 | 204 | $this->xResponseManager->append(new UploadResponse($this->xPsr17Factory, $sTempFile)); |
205 | - } |
|
206 | - catch(Exception $e) |
|
205 | + } catch(Exception $e) |
|
207 | 206 | { |
208 | 207 | $this->xResponseManager->append(new UploadResponse($this->xPsr17Factory, '', $e->getMessage())); |
209 | 208 | } |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | */ |
28 | 28 | function register(Container $di, bool $bForce = false) |
29 | 29 | { |
30 | - if(!$bForce && $di->h(UploadHandler::class)) |
|
30 | + if (!$bForce && $di->h(UploadHandler::class)) |
|
31 | 31 | { |
32 | 32 | return; |
33 | 33 | } |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | { |
74 | 74 | $di = jaxon()->di(); |
75 | 75 | $sEventListenerKey = UploadHandler::class . '\\ConfigListener'; |
76 | - if($di->h($sEventListenerKey)) |
|
76 | + if ($di->h($sEventListenerKey)) |
|
77 | 77 | { |
78 | 78 | return; |
79 | 79 | } |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | public function onChange(Config $xConfig, string $sName) |
87 | 87 | { |
88 | 88 | $sConfigKey = 'core.upload.enabled'; |
89 | - if(($sName === $sConfigKey || $sName === '') && $xConfig->getOption($sConfigKey)) |
|
89 | + if (($sName === $sConfigKey || $sName === '') && $xConfig->getOption($sConfigKey)) |
|
90 | 90 | { |
91 | 91 | register(jaxon()->di()); |
92 | 92 | } |
@@ -80,8 +80,7 @@ discard block |
||
80 | 80 | { |
81 | 81 | // Local file system adapter |
82 | 82 | $this->registerAdapter('local', function(string $sRootDir, $xOptions) { |
83 | - return empty($xOptions) ? new LocalFilesystemAdapter($sRootDir) : |
|
84 | - new LocalFilesystemAdapter($sRootDir, $xOptions); |
|
83 | + return empty($xOptions) ? new LocalFilesystemAdapter($sRootDir) : new LocalFilesystemAdapter($sRootDir, $xOptions); |
|
85 | 84 | }); |
86 | 85 | |
87 | 86 | // In memory file system adapter |
@@ -144,7 +143,7 @@ discard block |
||
144 | 143 | public function filesystem(string $sField = ''): Filesystem |
145 | 144 | { |
146 | 145 | $sField = trim($sField); |
147 | - if(isset($this->aFilesystems[$sField])) |
|
146 | + if (isset($this->aFilesystems[$sField])) |
|
148 | 147 | { |
149 | 148 | return $this->aFilesystems[$sField]; |
150 | 149 | } |
@@ -154,18 +153,18 @@ discard block |
||
154 | 153 | $sRootDir = $this->xConfigManager->getOption('upload.default.dir', ''); |
155 | 154 | $aOptions = $this->xConfigManager->getOption('upload.default.options'); |
156 | 155 | $sConfigKey = "upload.files.$sField"; |
157 | - if($sField !== '' && $this->xConfigManager->hasOption($sConfigKey)) |
|
156 | + if ($sField !== '' && $this->xConfigManager->hasOption($sConfigKey)) |
|
158 | 157 | { |
159 | 158 | $sStorage = $this->xConfigManager->getOption("$sConfigKey.storage", $sStorage); |
160 | 159 | $sRootDir = $this->xConfigManager->getOption("$sConfigKey.dir", $sRootDir); |
161 | 160 | $aOptions = $this->xConfigManager->getOption("$sConfigKey.options", $aOptions); |
162 | 161 | } |
163 | 162 | |
164 | - if(!is_string($sRootDir)) |
|
163 | + if (!is_string($sRootDir)) |
|
165 | 164 | { |
166 | 165 | throw new RequestException($this->xTranslator->trans('errors.upload.dir')); |
167 | 166 | } |
168 | - if(!isset($this->aAdapters[$sStorage])) |
|
167 | + if (!isset($this->aAdapters[$sStorage])) |
|
169 | 168 | { |
170 | 169 | throw new RequestException($this->xTranslator->trans('errors.upload.adapter')); |
171 | 170 | } |
@@ -154,13 +154,13 @@ discard block |
||
154 | 154 | try |
155 | 155 | { |
156 | 156 | $xFilesystem->createDirectory($sUploadDir); |
157 | - if($xFilesystem->visibility($sUploadDir) !== Visibility::PUBLIC) |
|
157 | + if ($xFilesystem->visibility($sUploadDir) !== Visibility::PUBLIC) |
|
158 | 158 | { |
159 | 159 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
160 | 160 | } |
161 | 161 | return $sUploadDir; |
162 | 162 | } |
163 | - catch(FilesystemException $e) |
|
163 | + catch (FilesystemException $e) |
|
164 | 164 | { |
165 | 165 | jaxon()->logger()->error('Filesystem error', ['message' => $e->getMessage()]); |
166 | 166 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
@@ -204,14 +204,14 @@ discard block |
||
204 | 204 | private function makeUploadedFile(UploadedFile $xHttpFile, string $sUploadDir, string $sField): File |
205 | 205 | { |
206 | 206 | // Check the uploaded file validity |
207 | - if($xHttpFile->getError()) |
|
207 | + if ($xHttpFile->getError()) |
|
208 | 208 | { |
209 | 209 | throw new RequestException($this->xTranslator->trans('errors.upload.failed', ['name' => $sField])); |
210 | 210 | } |
211 | 211 | |
212 | 212 | // Filename without the extension. Needs to be sanitized. |
213 | 213 | $sName = pathinfo($xHttpFile->getClientFilename(), PATHINFO_FILENAME); |
214 | - if($this->cNameSanitizer !== null) |
|
214 | + if ($this->cNameSanitizer !== null) |
|
215 | 215 | { |
216 | 216 | $sName = (string)call_user_func($this->cNameSanitizer, $sName, $sField, $this->sUploadFieldId); |
217 | 217 | } |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | // Set the user file data |
220 | 220 | $xFile = File::fromHttpFile($this->xFileStorage->filesystem($sField), $xHttpFile, $sUploadDir, $sName); |
221 | 221 | // Verify file validity (format, size) |
222 | - if(!$this->xValidator->validateUploadedFile($sField, $xFile)) |
|
222 | + if (!$this->xValidator->validateUploadedFile($sField, $xFile)) |
|
223 | 223 | { |
224 | 224 | throw new RequestException($this->xValidator->getErrorMessage()); |
225 | 225 | } |
@@ -244,16 +244,16 @@ discard block |
||
244 | 244 | |
245 | 245 | $aUserFiles = []; |
246 | 246 | $this->aAllFiles = []; // A flat list of all uploaded files |
247 | - foreach($aTempFiles as $sField => $aFiles) |
|
247 | + foreach ($aTempFiles as $sField => $aFiles) |
|
248 | 248 | { |
249 | 249 | $aUserFiles[$sField] = []; |
250 | 250 | // Get the path to the upload dir |
251 | 251 | $sUploadDir = $this->getUploadDir($sField); |
252 | - if(!is_array($aFiles)) |
|
252 | + if (!is_array($aFiles)) |
|
253 | 253 | { |
254 | 254 | $aFiles = [$aFiles]; |
255 | 255 | } |
256 | - foreach($aFiles as $xHttpFile) |
|
256 | + foreach ($aFiles as $xHttpFile) |
|
257 | 257 | { |
258 | 258 | $aUserFiles[$sField][] = $this->makeUploadedFile($xHttpFile, $sUploadDir, $sField); |
259 | 259 | } |
@@ -261,12 +261,12 @@ discard block |
||
261 | 261 | // Copy the uploaded files from the temp dir to the user dir |
262 | 262 | try |
263 | 263 | { |
264 | - foreach($this->aAllFiles as $aFiles) |
|
264 | + foreach ($this->aAllFiles as $aFiles) |
|
265 | 265 | { |
266 | 266 | $aFiles['user']->filesystem()->write($aFiles['user']->path(), $aFiles['temp']->getStream()); |
267 | 267 | } |
268 | 268 | } |
269 | - catch(FilesystemException $e) |
|
269 | + catch (FilesystemException $e) |
|
270 | 270 | { |
271 | 271 | jaxon()->logger()->error('Filesystem error', ['message' => $e->getMessage()]); |
272 | 272 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
@@ -286,10 +286,10 @@ discard block |
||
286 | 286 | { |
287 | 287 | // Convert uploaded file to an array |
288 | 288 | $aFiles = []; |
289 | - foreach($aUserFiles as $sField => $aFieldFiles) |
|
289 | + foreach ($aUserFiles as $sField => $aFieldFiles) |
|
290 | 290 | { |
291 | 291 | $aFiles[$sField] = []; |
292 | - foreach($aFieldFiles as $aFieldFile) |
|
292 | + foreach ($aFieldFiles as $aFieldFile) |
|
293 | 293 | { |
294 | 294 | $aFiles[$sField][] = $aFieldFile->toTempData(); |
295 | 295 | } |
@@ -301,7 +301,7 @@ discard block |
||
301 | 301 | { |
302 | 302 | $this->xFileStorage->filesystem()->write($sUploadDir . $sTempFile . '.json', json_encode($aFiles)); |
303 | 303 | } |
304 | - catch(FilesystemException $e) |
|
304 | + catch (FilesystemException $e) |
|
305 | 305 | { |
306 | 306 | jaxon()->logger()->error('Filesystem error', ['message' => $e->getMessage()]); |
307 | 307 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
@@ -320,7 +320,7 @@ discard block |
||
320 | 320 | private function getUploadTempFile(string $sTempFile): string |
321 | 321 | { |
322 | 322 | // Verify file name validity |
323 | - if(!$this->xValidator->validateTempFileName($sTempFile)) |
|
323 | + if (!$this->xValidator->validateTempFileName($sTempFile)) |
|
324 | 324 | { |
325 | 325 | throw new RequestException($this->xTranslator->trans('errors.upload.invalid')); |
326 | 326 | } |
@@ -328,13 +328,13 @@ discard block |
||
328 | 328 | $sUploadTempFile = $sUploadDir . $sTempFile . '.json'; |
329 | 329 | try |
330 | 330 | { |
331 | - if($this->xFileStorage->filesystem()->visibility($sUploadTempFile) !== Visibility::PUBLIC) |
|
331 | + if ($this->xFileStorage->filesystem()->visibility($sUploadTempFile) !== Visibility::PUBLIC) |
|
332 | 332 | { |
333 | 333 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
334 | 334 | } |
335 | 335 | return $sUploadTempFile; |
336 | 336 | } |
337 | - catch(FilesystemException $e) |
|
337 | + catch (FilesystemException $e) |
|
338 | 338 | { |
339 | 339 | jaxon()->logger()->error('Filesystem error', ['message' => $e->getMessage()]); |
340 | 340 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
@@ -358,15 +358,15 @@ discard block |
||
358 | 358 | { |
359 | 359 | $aFiles = json_decode($xFileSystem->read($sUploadTempFile), true); |
360 | 360 | } |
361 | - catch(FilesystemException $e) |
|
361 | + catch (FilesystemException $e) |
|
362 | 362 | { |
363 | 363 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
364 | 364 | } |
365 | 365 | $aUserFiles = []; |
366 | - foreach($aFiles as $sField => $aFieldFiles) |
|
366 | + foreach ($aFiles as $sField => $aFieldFiles) |
|
367 | 367 | { |
368 | 368 | $aUserFiles[$sField] = []; |
369 | - foreach($aFieldFiles as $aFieldFile) |
|
369 | + foreach ($aFieldFiles as $aFieldFile) |
|
370 | 370 | { |
371 | 371 | $aUserFiles[$sField][] = File::fromTempFile($this->xFileStorage->filesystem($sField), $aFieldFile); |
372 | 372 | } |
@@ -375,7 +375,7 @@ discard block |
||
375 | 375 | { |
376 | 376 | $xFileSystem->delete($sUploadTempFile); |
377 | 377 | } |
378 | - catch(FilesystemException $e) |
|
378 | + catch (FilesystemException $e) |
|
379 | 379 | { |
380 | 380 | jaxon()->logger()->warning('Filesystem error', ['message' => $e->getMessage()]); |
381 | 381 | } |
@@ -159,8 +159,7 @@ discard block |
||
159 | 159 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
160 | 160 | } |
161 | 161 | return $sUploadDir; |
162 | - } |
|
163 | - catch(FilesystemException $e) |
|
162 | + } catch(FilesystemException $e) |
|
164 | 163 | { |
165 | 164 | jaxon()->logger()->error('Filesystem error', ['message' => $e->getMessage()]); |
166 | 165 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
@@ -265,8 +264,7 @@ discard block |
||
265 | 264 | { |
266 | 265 | $aFiles['user']->filesystem()->write($aFiles['user']->path(), $aFiles['temp']->getStream()); |
267 | 266 | } |
268 | - } |
|
269 | - catch(FilesystemException $e) |
|
267 | + } catch(FilesystemException $e) |
|
270 | 268 | { |
271 | 269 | jaxon()->logger()->error('Filesystem error', ['message' => $e->getMessage()]); |
272 | 270 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
@@ -300,8 +298,7 @@ discard block |
||
300 | 298 | try |
301 | 299 | { |
302 | 300 | $this->xFileStorage->filesystem()->write($sUploadDir . $sTempFile . '.json', json_encode($aFiles)); |
303 | - } |
|
304 | - catch(FilesystemException $e) |
|
301 | + } catch(FilesystemException $e) |
|
305 | 302 | { |
306 | 303 | jaxon()->logger()->error('Filesystem error', ['message' => $e->getMessage()]); |
307 | 304 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
@@ -333,8 +330,7 @@ discard block |
||
333 | 330 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
334 | 331 | } |
335 | 332 | return $sUploadTempFile; |
336 | - } |
|
337 | - catch(FilesystemException $e) |
|
333 | + } catch(FilesystemException $e) |
|
338 | 334 | { |
339 | 335 | jaxon()->logger()->error('Filesystem error', ['message' => $e->getMessage()]); |
340 | 336 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
@@ -357,8 +353,7 @@ discard block |
||
357 | 353 | try |
358 | 354 | { |
359 | 355 | $aFiles = json_decode($xFileSystem->read($sUploadTempFile), true); |
360 | - } |
|
361 | - catch(FilesystemException $e) |
|
356 | + } catch(FilesystemException $e) |
|
362 | 357 | { |
363 | 358 | throw new RequestException($this->xTranslator->trans('errors.upload.access')); |
364 | 359 | } |
@@ -374,8 +369,7 @@ discard block |
||
374 | 369 | try |
375 | 370 | { |
376 | 371 | $xFileSystem->delete($sUploadTempFile); |
377 | - } |
|
378 | - catch(FilesystemException $e) |
|
372 | + } catch(FilesystemException $e) |
|
379 | 373 | { |
380 | 374 | jaxon()->logger()->warning('Filesystem error', ['message' => $e->getMessage()]); |
381 | 375 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | { |
98 | 98 | $xDefault = $this->xConfigManager->getOption('upload.default.' . $sProperty); |
99 | 99 | $aAllowed = $this->xConfigManager->getOption('upload.files.' . $sVarName . '.' . $sProperty, $xDefault); |
100 | - if(is_array($aAllowed) && !in_array($sValue, $aAllowed)) |
|
100 | + if (is_array($aAllowed) && !in_array($sValue, $aAllowed)) |
|
101 | 101 | { |
102 | 102 | $this->sErrorMessage = $this->xTranslator->trans('errors.upload.' . $sField, [$sField => $sValue]); |
103 | 103 | return false; |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | { |
119 | 119 | $xDefault = $this->xConfigManager->getOption('upload.default.' . $sProperty, 0); |
120 | 120 | $nSize = $this->xConfigManager->getOption('upload.files.' . $sVarName . '.' . $sProperty, $xDefault); |
121 | - if($nSize > 0 && ( |
|
121 | + if ($nSize > 0 && ( |
|
122 | 122 | ($sProperty == 'max-size' && $nFileSize > $nSize) || |
123 | 123 | ($sProperty == 'min-size' && $nFileSize < $nSize))) |
124 | 124 | { |
@@ -140,25 +140,25 @@ discard block |
||
140 | 140 | { |
141 | 141 | $this->sErrorMessage = ''; |
142 | 142 | // Verify the file extension |
143 | - if(!$this->validateFileProperty($sVarName, $xFile->type(), 'types', 'type')) |
|
143 | + if (!$this->validateFileProperty($sVarName, $xFile->type(), 'types', 'type')) |
|
144 | 144 | { |
145 | 145 | return false; |
146 | 146 | } |
147 | 147 | |
148 | 148 | // Verify the file extension |
149 | - if(!$this->validateFileProperty($sVarName, $xFile->extension(), 'extensions', 'extension')) |
|
149 | + if (!$this->validateFileProperty($sVarName, $xFile->extension(), 'extensions', 'extension')) |
|
150 | 150 | { |
151 | 151 | return false; |
152 | 152 | } |
153 | 153 | |
154 | 154 | // Verify the max size |
155 | - if(!$this->validateFileSize($sVarName, $xFile->size(), 'max-size')) |
|
155 | + if (!$this->validateFileSize($sVarName, $xFile->size(), 'max-size')) |
|
156 | 156 | { |
157 | 157 | return false; |
158 | 158 | } |
159 | 159 | |
160 | 160 | // Verify the min size |
161 | - if(!$this->validateFileSize($sVarName, $xFile->size(), 'min-size')) |
|
161 | + if (!$this->validateFileSize($sVarName, $xFile->size(), 'min-size')) |
|
162 | 162 | { |
163 | 163 | return false; |
164 | 164 | } |
@@ -101,8 +101,7 @@ |
||
101 | 101 | public function getOutput(): string |
102 | 102 | { |
103 | 103 | $sResult = json_encode(($this->sUploadedFile) ? |
104 | - ['code' => 'success', 'upl' => $this->sUploadedFile] : |
|
105 | - ['code' => 'error', 'msg' => $this->sErrorMessage]) . ';'; |
|
104 | + ['code' => 'success', 'upl' => $this->sUploadedFile] : ['code' => 'error', 'msg' => $this->sErrorMessage]) . ';'; |
|
106 | 105 | $sConsoleLog = array_reduce($this->aDebugMessages, function($sJsLog, $sMessage) { |
107 | 106 | return "$sJsLog\n\t" . 'console.log("' . addslashes($sMessage) . '");'; |
108 | 107 | }, ''); |