PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.9
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / psr7 / src / ServerRequest.php

ServerRequest.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.9, at vendor_prefixed/guzzlehttp/psr7/src/ServerRequest.php

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