PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/google-listings-and-ads/vendor/guzzlehttp/psr7/src/ServerRequest.php

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