PluginProbe
Media Cloud Sync / 1.2.3
Media Cloud Sync v1.2.3
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Psr7 / ServerRequest.php

ServerRequest.php in Media Cloud Sync 1.2.3, at includes/sdk/s3/GuzzleHttp/Psr7/ServerRequest.php

307 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7;
4
5 use InvalidArgumentException;
6 use Dudlewebs\WPMCS\s3\Psr\Http\Message\ServerRequestInterface;
7 use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface;
8 use Dudlewebs\WPMCS\s3\Psr\Http\Message\UploadedFileInterface;
9 use Dudlewebs\WPMCS\s3\Psr\Http\Message\UriInterface;
10 /**
11 * Server-side HTTP request
12 *
13 * Extends the Request definition to add methods for accessing incoming data,
14 * specifically server parameters, cookies, matched path parameters, query
15 * string arguments, body parameters, and upload file information.
16 *
17 * "Attributes" are discovered via decomposing the request (and usually
18 * specifically the URI path), and typically will be injected by the application.
19 *
20 * Requests are considered immutable; all methods that might change state are
21 * implemented such that they retain the internal state of the current
22 * message and return a new instance that contains the changed state.
23 */
24 class ServerRequest extends Request implements ServerRequestInterface
25 {
26 /**
27 * @var array
28 */
29 private $attributes = [];
30 /**
31 * @var array
32 */
33 private $cookieParams = [];
34 /**
35 * @var array|object|null
36 */
37 private $parsedBody;
38 /**
39 * @var array
40 */
41 private $queryParams = [];
42 /**
43 * @var array
44 */
45 private $serverParams;
46 /**
47 * @var array
48 */
49 private $uploadedFiles = [];
50 /**
51 * @param string $method HTTP method
52 * @param string|UriInterface $uri URI
53 * @param array $headers Request headers
54 * @param string|resource|StreamInterface|null $body Request body
55 * @param string $version Protocol version
56 * @param array $serverParams Typically the $_SERVER superglobal
57 */
58 public function __construct($method, $uri, array $headers = [], $body = null, $version = '1.1', array $serverParams = [])
59 {
60 $this->serverParams = $serverParams;
61 parent::__construct($method, $uri, $headers, $body, $version);
62 }
63 /**
64 * Return an UploadedFile instance array.
65 *
66 * @param array $files A array which respect $_FILES structure
67 *
68 * @return array
69 *
70 * @throws InvalidArgumentException for unrecognized values
71 */
72 public static function normalizeFiles(array $files)
73 {
74 $normalized = [];
75 foreach ($files as $key => $value) {
76 if ($value instanceof UploadedFileInterface) {
77 $normalized[$key] = $value;
78 } elseif (\is_array($value) && isset($value['tmp_name'])) {
79 $normalized[$key] = self::createUploadedFileFromSpec($value);
80 } elseif (\is_array($value)) {
81 $normalized[$key] = self::normalizeFiles($value);
82 continue;
83 } else {
84 throw new InvalidArgumentException('Invalid value in files specification');
85 }
86 }
87 return $normalized;
88 }
89 /**
90 * Create and return an UploadedFile instance from a $_FILES specification.
91 *
92 * If the specification represents an array of values, this method will
93 * delegate to normalizeNestedFileSpec() and return that return value.
94 *
95 * @param array $value $_FILES struct
96 *
97 * @return array|UploadedFileInterface
98 */
99 private static function createUploadedFileFromSpec(array $value)
100 {
101 if (\is_array($value['tmp_name'])) {
102 return self::normalizeNestedFileSpec($value);
103 }
104 return new UploadedFile($value['tmp_name'], (int) $value['size'], (int) $value['error'], $value['name'], $value['type']);
105 }
106 /**
107 * Normalize an array of file specifications.
108 *
109 * Loops through all nested files and returns a normalized array of
110 * UploadedFileInterface instances.
111 *
112 * @param array $files
113 *
114 * @return UploadedFileInterface[]
115 */
116 private static function normalizeNestedFileSpec(array $files = [])
117 {
118 $normalizedFiles = [];
119 foreach (\array_keys($files['tmp_name']) as $key) {
120 $spec = ['tmp_name' => $files['tmp_name'][$key], 'size' => $files['size'][$key], 'error' => $files['error'][$key], 'name' => $files['name'][$key], 'type' => $files['type'][$key]];
121 $normalizedFiles[$key] = self::createUploadedFileFromSpec($spec);
122 }
123 return $normalizedFiles;
124 }
125 /**
126 * Return a ServerRequest populated with superglobals:
127 * $_GET
128 * $_POST
129 * $_COOKIE
130 * $_FILES
131 * $_SERVER
132 *
133 * @return ServerRequestInterface
134 */
135 public static function fromGlobals()
136 {
137 $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
138 $headers = \getallheaders();
139 $uri = self::getUriFromGlobals();
140 $body = new CachingStream(new LazyOpenStream('php://input', 'r+'));
141 $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? \str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']) : '1.1';
142 $serverRequest = new ServerRequest($method, $uri, $headers, $body, $protocol, $_SERVER);
143 return $serverRequest->withCookieParams($_COOKIE)->withQueryParams($_GET)->withParsedBody($_POST)->withUploadedFiles(self::normalizeFiles($_FILES));
144 }
145 private static function extractHostAndPortFromAuthority($authority)
146 {
147 $uri = 'http://' . $authority;
148 $parts = \parse_url($uri);
149 if (\false === $parts) {
150 return [null, null];
151 }
152 $host = isset($parts['host']) ? $parts['host'] : null;
153 $port = isset($parts['port']) ? $parts['port'] : null;
154 return [$host, $port];
155 }
156 /**
157 * Get a Uri populated with values from $_SERVER.
158 *
159 * @return UriInterface
160 */
161 public static function getUriFromGlobals()
162 {
163 $uri = new Uri('');
164 $uri = $uri->withScheme(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http');
165 $hasPort = \false;
166 if (isset($_SERVER['HTTP_HOST'])) {
167 list($host, $port) = self::extractHostAndPortFromAuthority($_SERVER['HTTP_HOST']);
168 if ($host !== null) {
169 $uri = $uri->withHost($host);
170 }
171 if ($port !== null) {
172 $hasPort = \true;
173 $uri = $uri->withPort($port);
174 }
175 } elseif (isset($_SERVER['SERVER_NAME'])) {
176 $uri = $uri->withHost($_SERVER['SERVER_NAME']);
177 } elseif (isset($_SERVER['SERVER_ADDR'])) {
178 $uri = $uri->withHost($_SERVER['SERVER_ADDR']);
179 }
180 if (!$hasPort && isset($_SERVER['SERVER_PORT'])) {
181 $uri = $uri->withPort($_SERVER['SERVER_PORT']);
182 }
183 $hasQuery = \false;
184 if (isset($_SERVER['REQUEST_URI'])) {
185 $requestUriParts = \explode('?', $_SERVER['REQUEST_URI'], 2);
186 $uri = $uri->withPath($requestUriParts[0]);
187 if (isset($requestUriParts[1])) {
188 $hasQuery = \true;
189 $uri = $uri->withQuery($requestUriParts[1]);
190 }
191 }
192 if (!$hasQuery && isset($_SERVER['QUERY_STRING'])) {
193 $uri = $uri->withQuery($_SERVER['QUERY_STRING']);
194 }
195 return $uri;
196 }
197 /**
198 * {@inheritdoc}
199 */
200 public function getServerParams()
201 {
202 return $this->serverParams;
203 }
204 /**
205 * {@inheritdoc}
206 */
207 public function getUploadedFiles()
208 {
209 return $this->uploadedFiles;
210 }
211 /**
212 * {@inheritdoc}
213 */
214 public function withUploadedFiles(array $uploadedFiles)
215 {
216 $new = clone $this;
217 $new->uploadedFiles = $uploadedFiles;
218 return $new;
219 }
220 /**
221 * {@inheritdoc}
222 */
223 public function getCookieParams()
224 {
225 return $this->cookieParams;
226 }
227 /**
228 * {@inheritdoc}
229 */
230 public function withCookieParams(array $cookies)
231 {
232 $new = clone $this;
233 $new->cookieParams = $cookies;
234 return $new;
235 }
236 /**
237 * {@inheritdoc}
238 */
239 public function getQueryParams()
240 {
241 return $this->queryParams;
242 }
243 /**
244 * {@inheritdoc}
245 */
246 public function withQueryParams(array $query)
247 {
248 $new = clone $this;
249 $new->queryParams = $query;
250 return $new;
251 }
252 /**
253 * {@inheritdoc}
254 */
255 public function getParsedBody()
256 {
257 return $this->parsedBody;
258 }
259 /**
260 * {@inheritdoc}
261 */
262 public function withParsedBody($data)
263 {
264 $new = clone $this;
265 $new->parsedBody = $data;
266 return $new;
267 }
268 /**
269 * {@inheritdoc}
270 */
271 public function getAttributes()
272 {
273 return $this->attributes;
274 }
275 /**
276 * {@inheritdoc}
277 */
278 public function getAttribute($attribute, $default = null)
279 {
280 if (\false === \array_key_exists($attribute, $this->attributes)) {
281 return $default;
282 }
283 return $this->attributes[$attribute];
284 }
285 /**
286 * {@inheritdoc}
287 */
288 public function withAttribute($attribute, $value)
289 {
290 $new = clone $this;
291 $new->attributes[$attribute] = $value;
292 return $new;
293 }
294 /**
295 * {@inheritdoc}
296 */
297 public function withoutAttribute($attribute)
298 {
299 if (\false === \array_key_exists($attribute, $this->attributes)) {
300 return $this;
301 }
302 $new = clone $this;
303 unset($new->attributes[$attribute]);
304 return $new;
305 }
306 }
307