1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.5.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Http\Request; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Upload\File as FileUpload; |
18
|
|
|
use Quantum\Libraries\Storage\FileSystem; |
19
|
|
|
use Quantum\Exceptions\HttpException; |
20
|
|
|
use Quantum\Environment\Server; |
21
|
|
|
use Quantum\Di\Di; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Trait Params |
25
|
|
|
* @package Quantum\Http\Request |
26
|
|
|
*/ |
27
|
|
|
trait Params |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Gets the GET params |
32
|
|
|
* @return array|null |
33
|
|
|
*/ |
34
|
|
|
private static function getParams(): ?array |
35
|
|
|
{ |
36
|
|
|
$getParams = []; |
37
|
|
|
|
38
|
|
|
if (!empty($_GET)) { |
39
|
|
|
$getParams = filter_input_array(INPUT_GET, FILTER_DEFAULT); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $getParams; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Gets the POST params |
47
|
|
|
* @return array|null |
48
|
|
|
*/ |
49
|
|
|
private static function postParams(): ?array |
50
|
|
|
{ |
51
|
|
|
$postParams = []; |
52
|
|
|
|
53
|
|
|
if (!empty($_POST)) { |
54
|
|
|
$postParams = filter_input_array(INPUT_POST, FILTER_DEFAULT); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $postParams; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Parses the raw input parameters sent via PUT, PATCH or DELETE methods |
62
|
|
|
* @return array[] |
63
|
|
|
* @throws \Quantum\Exceptions\DiException |
64
|
|
|
* @throws \Quantum\Exceptions\HttpException |
65
|
|
|
* @throws \ReflectionException |
66
|
|
|
*/ |
67
|
|
|
private static function parsedParams(): array |
68
|
|
|
{ |
69
|
|
|
$parsedParams = [ |
70
|
|
|
'params' => [], |
71
|
|
|
'files' => [] |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
if (in_array(self::$__method, ['PUT', 'PATCH', 'DELETE'])) { |
75
|
|
|
|
76
|
|
|
$rawInput = file_get_contents('php://input'); |
77
|
|
|
|
78
|
|
|
switch (self::$server->contentType(true)) { |
79
|
|
|
case self::CONTENT_FORM_DATA: |
|
|
|
|
80
|
|
|
$parsedParams = self::parseRawInput($rawInput); |
81
|
|
|
break; |
82
|
|
|
case self::CONTENT_JSON_PAYLOAD: |
|
|
|
|
83
|
|
|
$parsedParams['params'] = json_decode($rawInput, true); |
84
|
|
|
break; |
85
|
|
|
case self::CONTENT_URL_ENCODED: |
|
|
|
|
86
|
|
|
parse_str(urldecode($rawInput), $result); |
87
|
|
|
$parsedParams['params'] = $result; |
88
|
|
|
break; |
89
|
|
|
default: |
90
|
|
|
throw HttpException::contentTypeNotSupported(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $parsedParams; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Parses the raw input |
99
|
|
|
* @param string $rawInput |
100
|
|
|
* @return array[] |
101
|
|
|
* @throws \Quantum\Exceptions\DiException |
102
|
|
|
* @throws \ReflectionException |
103
|
|
|
*/ |
104
|
|
|
private static function parseRawInput(string $rawInput): array |
105
|
|
|
{ |
106
|
|
|
$boundary = self::getBoundary(); |
107
|
|
|
|
108
|
|
|
if ($boundary) { |
109
|
|
|
$blocks = self::getBlocks($boundary, $rawInput); |
110
|
|
|
|
111
|
|
|
return self::processBlocks($blocks); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return [ |
115
|
|
|
'params' => [], |
116
|
|
|
'files' => [] |
117
|
|
|
]; |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Gets the boundary |
123
|
|
|
* @return string|null |
124
|
|
|
*/ |
125
|
|
|
private static function getBoundary(): ?string |
126
|
|
|
{ |
127
|
|
|
$contentType = (new Server)->contentType(); |
128
|
|
|
|
129
|
|
|
if (!$contentType) { |
130
|
|
|
return null; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
preg_match('/boundary=(.*)$/', $contentType, $match); |
134
|
|
|
|
135
|
|
|
if (!count($match)) { |
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $match[1]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Gets the blocks |
144
|
|
|
* @param string $boundary |
145
|
|
|
* @param string $rawInput |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
private static function getBlocks(string $boundary, string $rawInput): array |
149
|
|
|
{ |
150
|
|
|
$result = preg_split("/-+$boundary/", $rawInput); |
151
|
|
|
array_pop($result); |
152
|
|
|
return $result; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Process blocks |
157
|
|
|
* @param array $blocks |
158
|
|
|
* @return array |
159
|
|
|
* @throws \Quantum\Exceptions\DiException |
160
|
|
|
* @throws \ReflectionException |
161
|
|
|
*/ |
162
|
|
|
private static function processBlocks(array $blocks): array |
163
|
|
|
{ |
164
|
|
|
$params = []; |
165
|
|
|
$files = []; |
166
|
|
|
|
167
|
|
|
foreach ($blocks as $id => $block) { |
168
|
|
|
if (empty($block)) { |
169
|
|
|
continue; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (strpos($block, 'filename') !== false) { |
173
|
|
|
list($nameParam, $file) = self::getParsedFile($block); |
174
|
|
|
|
175
|
|
|
$arrayParam = self::arrayParam($nameParam); |
176
|
|
|
|
177
|
|
|
if (is_array($arrayParam)) { |
178
|
|
|
list($name, $key) = $arrayParam; |
179
|
|
|
|
180
|
|
|
if ($key === '') { |
181
|
|
|
$files[$name][] = $file; |
182
|
|
|
} else { |
183
|
|
|
$files[$name][$key] = $file; |
184
|
|
|
} |
185
|
|
|
} else { |
186
|
|
|
$files[$nameParam] = $file; |
187
|
|
|
} |
188
|
|
|
} else if (strpos($block, 'application/octet-stream') !== false) { |
189
|
|
|
$params = array_merge($params, self::getParsedStream($block)); |
190
|
|
|
} else { |
191
|
|
|
$params = array_merge($params, self::getParsedParameter($block)); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return [ |
196
|
|
|
'params' => $params, |
197
|
|
|
'files' => $files |
198
|
|
|
]; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string $block |
203
|
|
|
* @return array|string[] |
204
|
|
|
*/ |
205
|
|
|
private static function getParsedStream(string $block): array |
206
|
|
|
{ |
207
|
|
|
preg_match('/name=\"([^\"]*)\".*stream[\n|\r]+([^\n\r].*)?$/s', $block, $match); |
208
|
|
|
|
209
|
|
|
return [$match[1] => $match[2] ?: '']; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Gets the parsed file |
214
|
|
|
* @param string $block |
215
|
|
|
* @return array|\Quantum\Libraries\Upload\File[] |
216
|
|
|
* @throws \Quantum\Exceptions\DiException |
217
|
|
|
* @throws \ReflectionException |
218
|
|
|
*/ |
219
|
|
|
private static function getParsedFile(string $block): array |
220
|
|
|
{ |
221
|
|
|
list($name, $filename, $type, $content) = self::parseFileData($block); |
222
|
|
|
|
223
|
|
|
$fs = Di::get(FileSystem::class); |
224
|
|
|
|
225
|
|
|
$tempName = tempnam(sys_get_temp_dir(), 'qt_'); |
226
|
|
|
|
227
|
|
|
$fs->put($tempName, $content); |
228
|
|
|
|
229
|
|
|
$file = new FileUpload([ |
230
|
|
|
'name' => $filename, |
231
|
|
|
'type' => $type, |
232
|
|
|
'tmp_name' => $tempName, |
233
|
|
|
'error' => UPLOAD_ERR_OK, |
234
|
|
|
'size' => $fs->size($tempName), |
235
|
|
|
]); |
236
|
|
|
|
237
|
|
|
register_shutdown_function(function () use ($fs, $tempName) { |
238
|
|
|
$fs->remove($tempName); |
239
|
|
|
}); |
240
|
|
|
|
241
|
|
|
return [$name, $file]; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Parses the file string |
246
|
|
|
* @param string $block |
247
|
|
|
* @return array |
248
|
|
|
*/ |
249
|
|
|
private static function parseFileData(string $block): array |
250
|
|
|
{ |
251
|
|
|
$block = ltrim($block, "\r\n"); |
252
|
|
|
|
253
|
|
|
list($rawHeaders, $content) = explode("\r\n\r\n", $block); |
254
|
|
|
|
255
|
|
|
list($name, $filename, $contentType) = self::parseHeaders($rawHeaders); |
256
|
|
|
|
257
|
|
|
$content = substr($content, 0, strlen($content) - 2); |
258
|
|
|
|
259
|
|
|
return [ |
260
|
|
|
$name, |
261
|
|
|
$filename, |
262
|
|
|
$contentType, |
263
|
|
|
$content |
264
|
|
|
]; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Parses and returns the parmeter |
269
|
|
|
* @param string $block |
270
|
|
|
* @return array |
271
|
|
|
*/ |
272
|
|
|
private static function getParsedParameter(string $block): array |
273
|
|
|
{ |
274
|
|
|
$data = []; |
275
|
|
|
|
276
|
|
|
if (preg_match('/name=\"([^\"]*)\"[\n|\r]+([^\n\r].*)?\r$/s', $block, $match)) { |
277
|
|
|
if (preg_match('/^(.*)\[\]$/i', $match[1], $tmp)) { |
278
|
|
|
$data[$tmp[1]][] = $match[2] ?: ''; |
279
|
|
|
} else { |
280
|
|
|
$data[$match[1]] = $match[2] ?: ''; |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
return $data; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Parses the header information of the file string |
289
|
|
|
* @param string $rawHeaders |
290
|
|
|
* @return array |
291
|
|
|
*/ |
292
|
|
|
private static function parseHeaders(string $rawHeaders): array |
293
|
|
|
{ |
294
|
|
|
$rawHeaders = explode("\r\n", $rawHeaders); |
295
|
|
|
|
296
|
|
|
$name = '-unknown-'; |
297
|
|
|
$filename = '-unknown-'; |
298
|
|
|
$contentType = 'application/octet-stream'; |
299
|
|
|
|
300
|
|
|
foreach ($rawHeaders as $header) { |
301
|
|
|
|
302
|
|
|
list($key, $value) = explode(':', $header); |
303
|
|
|
|
304
|
|
|
if ($key == 'Content-Type') { |
305
|
|
|
$contentType = ltrim($value, ' '); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
if (preg_match('/name=\"([^\"]*)\"/', $header, $match)) { |
309
|
|
|
$name = $match[1]; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
if (preg_match('/filename=\"([^\"]*)\"/', $header, $match)) { |
313
|
|
|
$filename = $match[1]; |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
return [$name, $filename, $contentType]; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Finds if the param is array type or not |
322
|
|
|
* @param string $parameter |
323
|
|
|
* @return array|string |
324
|
|
|
*/ |
325
|
|
|
private static function arrayParam(string $parameter) |
326
|
|
|
{ |
327
|
|
|
if (strpos($parameter, '[') !== false) { |
328
|
|
|
if (preg_match('/^([^[]*)\[([^]]*)\](.*)$/', $parameter, $match)) { |
329
|
|
|
return [$match[1], $match[2]]; |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
return $parameter; |
334
|
|
|
} |
335
|
|
|
} |