ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ServerRequest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace GuzzleHttp\Psr7;
4 
10 
26 {
30  private $attributes = [];
31 
35  private $cookieParams = [];
36 
40  private $parsedBody;
41 
45  private $queryParams = [];
46 
50  private $serverParams;
51 
55  private $uploadedFiles = [];
56 
65  public function __construct(
66  $method,
67  $uri,
68  array $headers = [],
69  $body = null,
70  $version = '1.1',
71  array $serverParams = []
72  ) {
73  $this->serverParams = $serverParams;
74 
75  parent::__construct($method, $uri, $headers, $body, $version);
76  }
77 
85  public static function normalizeFiles(array $files)
86  {
87  $normalized = [];
88 
89  foreach ($files as $key => $value) {
90  if ($value instanceof UploadedFileInterface) {
91  $normalized[$key] = $value;
92  } elseif (is_array($value) && isset($value['tmp_name'])) {
93  $normalized[$key] = self::createUploadedFileFromSpec($value);
94  } elseif (is_array($value)) {
95  $normalized[$key] = self::normalizeFiles($value);
96  continue;
97  } else {
98  throw new InvalidArgumentException('Invalid value in files specification');
99  }
100  }
101 
102  return $normalized;
103  }
104 
114  private static function createUploadedFileFromSpec(array $value)
115  {
116  if (is_array($value['tmp_name'])) {
117  return self::normalizeNestedFileSpec($value);
118  }
119 
120  return new UploadedFile(
121  $value['tmp_name'],
122  (int) $value['size'],
123  (int) $value['error'],
124  $value['name'],
125  $value['type']
126  );
127  }
128 
138  private static function normalizeNestedFileSpec(array $files = [])
139  {
140  $normalizedFiles = [];
141 
142  foreach (array_keys($files['tmp_name']) as $key) {
143  $spec = [
144  'tmp_name' => $files['tmp_name'][$key],
145  'size' => $files['size'][$key],
146  'error' => $files['error'][$key],
147  'name' => $files['name'][$key],
148  'type' => $files['type'][$key],
149  ];
150  $normalizedFiles[$key] = self::createUploadedFileFromSpec($spec);
151  }
152 
153  return $normalizedFiles;
154  }
155 
166  public static function fromGlobals()
167  {
168  $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
169  $headers = function_exists('getallheaders') ? getallheaders() : [];
170  $uri = self::getUriFromGlobals();
171  $body = new LazyOpenStream('php://input', 'r+');
172  $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']) : '1.1';
173 
174  $serverRequest = new ServerRequest($method, $uri, $headers, $body, $protocol, $_SERVER);
175 
176  return $serverRequest
177  ->withCookieParams($_COOKIE)
178  ->withQueryParams($_GET)
179  ->withParsedBody($_POST)
180  ->withUploadedFiles(self::normalizeFiles($_FILES));
181  }
182 
188  public static function getUriFromGlobals() {
189  $uri = new Uri('');
190 
191  $uri = $uri->withScheme(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http');
192 
193  $hasPort = false;
194  if (isset($_SERVER['HTTP_HOST'])) {
195  $hostHeaderParts = explode(':', $_SERVER['HTTP_HOST']);
196  $uri = $uri->withHost($hostHeaderParts[0]);
197  if (isset($hostHeaderParts[1])) {
198  $hasPort = true;
199  $uri = $uri->withPort($hostHeaderParts[1]);
200  }
201  } elseif (isset($_SERVER['SERVER_NAME'])) {
202  $uri = $uri->withHost($_SERVER['SERVER_NAME']);
203  } elseif (isset($_SERVER['SERVER_ADDR'])) {
204  $uri = $uri->withHost($_SERVER['SERVER_ADDR']);
205  }
206 
207  if (!$hasPort && isset($_SERVER['SERVER_PORT'])) {
208  $uri = $uri->withPort($_SERVER['SERVER_PORT']);
209  }
210 
211  $hasQuery = false;
212  if (isset($_SERVER['REQUEST_URI'])) {
213  $requestUriParts = explode('?', $_SERVER['REQUEST_URI']);
214  $uri = $uri->withPath($requestUriParts[0]);
215  if (isset($requestUriParts[1])) {
216  $hasQuery = true;
217  $uri = $uri->withQuery($requestUriParts[1]);
218  }
219  }
220 
221  if (!$hasQuery && isset($_SERVER['QUERY_STRING'])) {
222  $uri = $uri->withQuery($_SERVER['QUERY_STRING']);
223  }
224 
225  return $uri;
226  }
227 
228 
232  public function getServerParams()
233  {
234  return $this->serverParams;
235  }
236 
240  public function getUploadedFiles()
241  {
242  return $this->uploadedFiles;
243  }
244 
248  public function withUploadedFiles(array $uploadedFiles)
249  {
250  $new = clone $this;
251  $new->uploadedFiles = $uploadedFiles;
252 
253  return $new;
254  }
255 
259  public function getCookieParams()
260  {
261  return $this->cookieParams;
262  }
263 
267  public function withCookieParams(array $cookies)
268  {
269  $new = clone $this;
270  $new->cookieParams = $cookies;
271 
272  return $new;
273  }
274 
278  public function getQueryParams()
279  {
280  return $this->queryParams;
281  }
282 
286  public function withQueryParams(array $query)
287  {
288  $new = clone $this;
289  $new->queryParams = $query;
290 
291  return $new;
292  }
293 
297  public function getParsedBody()
298  {
299  return $this->parsedBody;
300  }
301 
305  public function withParsedBody($data)
306  {
307  $new = clone $this;
308  $new->parsedBody = $data;
309 
310  return $new;
311  }
312 
316  public function getAttributes()
317  {
318  return $this->attributes;
319  }
320 
324  public function getAttribute($attribute, $default = null)
325  {
326  if (false === array_key_exists($attribute, $this->attributes)) {
327  return $default;
328  }
329 
330  return $this->attributes[$attribute];
331  }
332 
336  public function withAttribute($attribute, $value)
337  {
338  $new = clone $this;
339  $new->attributes[$attribute] = $value;
340 
341  return $new;
342  }
343 
347  public function withoutAttribute($attribute)
348  {
349  if (false === array_key_exists($attribute, $this->attributes)) {
350  return $this;
351  }
352 
353  $new = clone $this;
354  unset($new->attributes[$attribute]);
355 
356  return $new;
357  }
358 }
__construct( $method, $uri, array $headers=[], $body=null, $version='1.1', array $serverParams=[])
static createUploadedFileFromSpec(array $value)
Create and return an UploadedFile instance from a $_FILES specification.
$_COOKIE['client_id']
Definition: server.php:9
getQueryParams()
{Retrieve query string arguments.Retrieves the deserialized query string arguments, if any.Note: the query params might not be in sync with the URI or server params. If you need to ensure you are only getting the original values, you may need to parse the query string from getUri()->getQuery() or from the QUERY_STRING server param.array}
Representation of an incoming, server-side HTTP request.
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
$files
Definition: metarefresh.php:49
getServerParams()
{Retrieve server parameters.Retrieves data related to the incoming request environment, typically derived from PHP&#39;s $_SERVER superglobal. The data IS NOT REQUIRED to originate from $_SERVER.array}
$_GET["client_id"]
static fromGlobals()
Return a ServerRequest populated with superglobals: $_GET $_POST $_COOKIE $_FILES $_SERVER...
Server-side HTTP request.
PSR-7 URI implementation.
Definition: Uri.php:13
withAttribute($attribute, $value)
{Return an instance with the specified derived request attribute.This method allows setting a single ...
getCookieParams()
{Retrieve cookies.Retrieves cookies sent by the client to the server.The data MUST be compatible with...
PSR-7 request implementation.
Definition: Request.php:12
$version
Definition: build.php:27
static normalizeNestedFileSpec(array $files=[])
Normalize an array of file specifications.
static normalizeFiles(array $files)
Return an UploadedFile instance array.
withCookieParams(array $cookies)
{Return an instance with the specified cookies.The data IS NOT REQUIRED to come from the $_COOKIE sup...
getAttributes()
{Retrieve attributes derived from the request.The request "attributes" may be used to allow injection...
$query
withoutAttribute($attribute)
{Return an instance that removes the specified derived request attribute.This method allows removing ...
$default
Definition: build.php:20
withParsedBody($data)
{Return an instance with the specified body parameters.These MAY be injected during instantiation...
Lazily reads or writes to a file that is opened only after an IO operation take place on the stream...
getParsedBody()
{Retrieve any parameters provided in the request body.If the request Content-Type is either applicati...
withQueryParams(array $query)
{Return an instance with the specified query string arguments.These values SHOULD remain immutable ov...
getAttribute($attribute, $default=null)
{Retrieve a single derived request attribute.Retrieves a single derived request attribute as describe...
getUploadedFiles()
{Retrieve normalized file upload data.This method returns upload metadata in a normalized tree...
static getUriFromGlobals()
Get a Uri populated with values from $_SERVER.
$key
Definition: croninfo.php:18
$_POST["username"]
Value object representing a file uploaded through an HTTP request.
$data
Definition: bench.php:6
withUploadedFiles(array $uploadedFiles)
{Create a new instance with the specified uploaded files.This method MUST be implemented in such a wa...