-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerRequest.php
More file actions
238 lines (228 loc) · 7.99 KB
/
Copy pathServerRequest.php
File metadata and controls
238 lines (228 loc) · 7.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
* This file is part of HttpMessage
*
* @package bdk/http-message
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2024 Brad Kent
* @version v1.0
*/
namespace bdk\HttpMessage\Utility;
use bdk\HttpMessage\ServerRequest as ServerRequestImplementation;
use bdk\HttpMessage\Stream;
use bdk\HttpMessage\UploadedFile;
use bdk\HttpMessage\Uri as UriImplementation; // https://bugs.php.net/bug.php?id=66773
use bdk\HttpMessage\Utility\ContentType;
use bdk\HttpMessage\Utility\ParseStr;
use InvalidArgumentException;
/**
* Build ServerRequest from globals ($_SERVER, $_COOKIE, $_POST, $_FILES)
*/
class ServerRequest
{
/** @var non-empty-string used for unit tests */
public static $inputStream = 'php://input';
/**
* Instantiate self from superglobals
*
* @param array $parseStrOpts Parse options (default: {convDot:false, convSpace:false})
*
* @return ServerRequestImplementation
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public static function fromGlobals($parseStrOpts = array()): ServerRequestImplementation
{
$method = isset($_SERVER['REQUEST_METHOD'])
? $_SERVER['REQUEST_METHOD']
: 'GET';
$uri = UriImplementation::fromGlobals();
$files = self::filesFromGlobals($_FILES);
$serverRequest = new ServerRequestImplementation($method, $uri, $_SERVER);
$contentType = $serverRequest->getHeaderLine('Content-Type');
// note: php://input not available with content-type = "multipart/form-data".
$parsedBody = $method !== 'GET'
? self::postFromInput($contentType, self::$inputStream, $parseStrOpts) ?: $_POST
: null;
$query = $uri->getQuery();
$queryParams = ParseStr::parse($query, $parseStrOpts);
return $serverRequest
->withBody(new Stream(
PHP_VERSION_ID < 70000
? \stream_get_contents(\fopen('php://input', 'r+')) // prev 5.6 is not seekable / read once.. still not reliable in 5.6
: \fopen('php://input', 'r+')
))
->withCookieParams($_COOKIE)
->withParsedBody($parsedBody)
->withQueryParams($queryParams)
->withUploadedFiles($files);
}
/**
* Create UploadedFiles tree from $_FILES
*
* @param array $phpFiles $_FILES type array
* @param string[] $path {@internal} Path to current value
*
* @return array
*
* @throws InvalidArgumentException
*/
private static function filesFromGlobals(array $phpFiles, array $path = array())
{
$files = array();
/** @var mixed $value */
foreach ($phpFiles as $key => $value) {
$pathCurKey = $path;
$pathCurKey[] = (string) $key;
if (\is_array($value) === false) {
throw new InvalidArgumentException(\sprintf(
'Invalid value in files specification at %s. Array expected. %s provided.',
\implode('.', $pathCurKey),
\gettype($value)
));
}
if (self::isUploadFileInfoArray($value)) {
$files[$key] = self::fileFromGlobalCreate($value);
continue;
}
$files[$key] = self::filesFromGlobals($value, $pathCurKey);
}
return $files;
}
/**
* Create UploadedFile(s) from $_FILES entry
*
* @param array{
* name: array|string,
* type: array|string,
* tmp_name: array|string,
* size: array|int,
* error: array|int,
* full_path: array|string} $fileInfo $_FILES entry
*
* @return UploadedFile|array
*
* @psalm-suppress PossiblyInvalidArrayAccess
* @psalm-suppress PossiblyInvalidArrayOffset
* @psalm-suppress MixedArgumentTypeCoercion doesn't trust array being passed to fileFromGlobalCreate
*/
private static function fileFromGlobalCreate(array $fileInfo)
{
if (\is_array($fileInfo['tmp_name']) === false) {
return new UploadedFile($fileInfo);
}
/*
<input type="file" name="foo[bar][a]">
<input type="file" name="bar[baz][a]">
will create something like
'foo' => [
'name' => [
'bar' => [
'a' => 'test2.jpg',
'b' => 'test3.jpg',
],
],
'type' => [
'bar' => []
'a' => 'image/jpeg',
'b' => 'image/jpeg',
],
],
...
]
*/
$files = array();
$keys = \array_keys($fileInfo['tmp_name']);
foreach ($keys as $key) {
$files[$key] = self::fileFromGlobalCreate(array(
'error' => $fileInfo['error'][$key],
'full_path' => isset($fileInfo['full_path'][$key])
? $fileInfo['full_path'][$key]
: null,
'name' => $fileInfo['name'][$key],
'size' => $fileInfo['size'][$key],
'tmp_name' => $fileInfo['tmp_name'][$key],
'type' => $fileInfo['type'][$key],
));
}
return $files;
}
/**
* Is the given Content-Type parsable
*
* @param string $contentType Content-Type / Mime-Type
*
* @return bool
*/
private static function isContentTypeParsable($contentType)
{
$parsableTypes = array(
ContentType::FORM,
ContentType::FORM_MULTIPART, // would be parsable... but php doesn't make available
ContentType::JSON,
);
return \in_array($contentType, $parsableTypes, true);
}
/**
* Are we uploaded file info array? ('tmp_name', 'size', 'error', name', 'type'...
*
* Don't base this off a single key like 'tmp_name'.
* <input type="file" name="tmp_name" "some dingus named this tmp_name" />
*
* @param array $array branch of $_FILES structure
*
* @return bool
*
* @psalm-assert-if-true array{
* name: array|string,
* type: array|string,
* tmp_name: array|string,
* size: array|int,
* error: array|int,
* full_path: array|string} $array
*/
private static function isUploadFileInfoArray(array $array)
{
$keysMustHave = array('name', 'type', 'tmp_name', 'size', 'error');
$keysMayHave = array('full_path');
$keys = \array_keys($array);
if (\array_intersect($keysMustHave, $keys) !== $keysMustHave) {
// missing must have
return false;
}
// return true if no unknown keys
return \array_diff($keys, \array_merge($keysMustHave, $keysMayHave)) === array();
}
/**
* Get parsed body (POST data)
*
* Note: this will return null if content-type = "multipart/form-data" and input = "php://input"
*
* @param string $contentType Content-Type header value
* @param string $input ('php://input') specify input
* @param array $parseStrOpts Parse options (default: {convDot:false, convSpace:false})
*
* @return array|null
*/
private static function postFromInput($contentType, $input = 'php://input', array $parseStrOpts = array())
{
$contentType = \preg_replace('/\s*[;,].*$/', '', $contentType);
$contentType = \strtolower($contentType);
if (self::isContentTypeParsable($contentType) === false) {
return null;
}
$rawBody = \file_get_contents($input);
if ($rawBody === '') {
return null;
}
if ($contentType !== ContentType::JSON) {
return ParseStr::parse($rawBody, $parseStrOpts);
}
/** @var array */
$jsonParsedBody = \json_decode($rawBody, true);
return \json_last_error() === JSON_ERROR_NONE
? $jsonParsedBody
: null;
}
}