|
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\Request\Upload\UploadManager; |
|
16
|
|
|
use Jaxon\Response\ResponseManager; |
|
17
|
|
|
use Jaxon\Response\UploadResponse; |
|
18
|
|
|
use Jaxon\Utils\Translation\Translator; |
|
19
|
|
|
use Jaxon\Exception\RequestException; |
|
20
|
|
|
|
|
21
|
|
|
use Closure; |
|
22
|
|
|
use Exception; |
|
23
|
|
|
|
|
24
|
|
|
use function count; |
|
25
|
|
|
use function trim; |
|
26
|
|
|
|
|
27
|
|
|
class UploadHandler |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* The response manager |
|
31
|
|
|
* |
|
32
|
|
|
* @var ResponseManager |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $xResponseManager; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* HTTP file upload manager |
|
38
|
|
|
* |
|
39
|
|
|
* @var UploadManager |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $xUploadManager = null; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var Translator |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $xTranslator; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The uploaded files copied in the user dir |
|
50
|
|
|
* |
|
51
|
|
|
* @var array |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $aUserFiles = []; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The name of file containing upload data |
|
57
|
|
|
* |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $sTempFile = ''; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Is the current request an HTTP upload |
|
64
|
|
|
* |
|
65
|
|
|
* @var bool |
|
|
|
|
|
|
66
|
|
|
*/ |
|
67
|
|
|
protected $bIsAjaxRequest = true; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* The constructor |
|
71
|
|
|
* |
|
72
|
|
|
* @param UploadManager $xUploadManager HTTP file upload manager |
|
|
|
|
|
|
73
|
|
|
* @param Translator $xTranslator |
|
|
|
|
|
|
74
|
|
|
* @param ResponseManager $xResponseManager |
|
|
|
|
|
|
75
|
|
|
*/ |
|
76
|
|
|
public function __construct(UploadManager $xUploadManager, Translator $xTranslator, ResponseManager $xResponseManager) |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$this->xResponseManager = $xResponseManager; |
|
79
|
|
|
$this->xUploadManager = $xUploadManager; |
|
|
|
|
|
|
80
|
|
|
$this->xTranslator = $xTranslator; |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
if(isset($_POST['jxnupl'])) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->sTempFile = trim($_POST['jxnupl']); |
|
85
|
|
|
} |
|
86
|
|
|
elseif(isset($_GET['jxnupl'])) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->sTempFile = trim($_GET['jxnupl']); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Set the uploaded file name sanitizer |
|
94
|
|
|
* |
|
95
|
|
|
* @param Closure $cSanitizer The closure |
|
|
|
|
|
|
96
|
|
|
* |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
|
|
public function sanitizer(Closure $cSanitizer) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->xUploadManager->setNameSanitizer($cSanitizer); |
|
102
|
|
|
} |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get the uploaded files |
|
106
|
|
|
* |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
public function files(): array |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->aUserFiles; |
|
112
|
|
|
} |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Inform this plugin that other plugin can process the current request |
|
116
|
|
|
* |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
|
|
public function isHttpUpload() |
|
120
|
|
|
{ |
|
121
|
|
|
$this->bIsAjaxRequest = false; |
|
122
|
|
|
} |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Check if the current request contains uploaded files |
|
126
|
|
|
* |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
|
|
public function canProcessRequest(): bool |
|
130
|
|
|
{ |
|
131
|
|
|
return (count($_FILES) > 0 || ($this->sTempFile)); |
|
132
|
|
|
} |
|
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Process the uploaded files in the HTTP request |
|
136
|
|
|
* |
|
137
|
|
|
* @return bool |
|
138
|
|
|
* @throws RequestException |
|
139
|
|
|
*/ |
|
140
|
|
|
public function processRequest(): bool |
|
141
|
|
|
{ |
|
142
|
|
|
if(($this->sTempFile)) |
|
143
|
|
|
{ |
|
144
|
|
|
// Ajax request following a normal HTTP upload. |
|
145
|
|
|
// Copy the previously uploaded files' location from the temp file. |
|
146
|
|
|
$this->aUserFiles = $this->xUploadManager->readFromTempFile($this->sTempFile); |
|
147
|
|
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
// Ajax or Http request with upload; copy the uploaded files. |
|
151
|
|
|
$this->aUserFiles = $this->xUploadManager->readFromHttpData(); |
|
152
|
|
|
|
|
153
|
|
|
// For Ajax requests, there is nothing else to do here. |
|
154
|
|
|
if($this->bIsAjaxRequest) |
|
155
|
|
|
{ |
|
156
|
|
|
return true; |
|
157
|
|
|
} |
|
158
|
|
|
// For HTTP requests, save the files' location to a temp file, |
|
159
|
|
|
// and return a response with a reference to this temp file. |
|
160
|
|
|
$xResponse = new UploadResponse(); |
|
161
|
|
|
try |
|
162
|
|
|
{ |
|
163
|
|
|
$sTempFile = $this->xUploadManager->saveToTempFile($this->aUserFiles); |
|
164
|
|
|
$xResponse->setUploadedFile($sTempFile); |
|
165
|
|
|
} |
|
166
|
|
|
catch(Exception $e) |
|
167
|
|
|
{ |
|
168
|
|
|
$xResponse->setErrorMessage($e->getMessage()); |
|
169
|
|
|
} |
|
170
|
|
|
$this->xResponseManager->append($xResponse); |
|
171
|
|
|
return true; |
|
172
|
|
|
} |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|