‪TYPO3CMS  10.4
Uri.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
16 namespace ‪TYPO3\CMS\Core\Http;
17 
18 use Psr\Http\Message\UriInterface;
20 
28 class ‪Uri implements UriInterface
29 {
35  const ‪SUBDELIMITER_CHARLIST = '!\$&\'\‍(\‍)\*\+,;=';
36 
42  const ‪UNRESERVED_CHARLIST = 'a-zA-Z0-9_\-\.~';
43 
48  protected ‪$scheme = '';
49 
53  protected ‪$supportedSchemes = [
54  'http' => 80,
55  'https' => 443
56  ];
57 
62  protected ‪$authority = '';
63 
68  protected ‪$userInfo = '';
69 
74  protected ‪$host = '';
75 
80  protected ‪$port;
81 
86  protected ‪$path = '';
87 
92  protected ‪$query = '';
93 
98  protected ‪$fragment;
99 
104  public function ‪__construct($uri = '')
105  {
106  if (!is_string($uri)) {
107  $argumentType = is_object($uri) ? get_class($uri) : gettype($uri);
108  throw new \InvalidArgumentException('URI passed must be a string, but is of type "' . $argumentType . '"', 1436717320);
109  }
110  if (!empty($uri)) {
111  $this->‪parseUri($uri);
112  }
113  }
114 
120  protected function ‪parseUri($uri)
121  {
122  $uriParts = parse_url($uri);
123 
124  if ($uriParts === false) {
125  throw new \InvalidArgumentException('The parsedUri "' . $uri . '" appears to be malformed', 1436717322);
126  }
127 
128  if (isset($uriParts['scheme'])) {
129  $this->scheme = $this->‪sanitizeScheme($uriParts['scheme']);
130  }
131 
132  if (isset($uriParts['user'])) {
133  $this->userInfo = $uriParts['user'];
134  if (isset($uriParts['pass'])) {
135  $this->userInfo .= ':' . $uriParts['pass'];
136  }
137  }
138 
139  if (isset($uriParts['host'])) {
140  $this->host = $uriParts['host'];
141  }
142 
143  if (isset($uriParts['port'])) {
144  $this->port = (int)$uriParts['port'];
145  }
146 
147  if (isset($uriParts['path'])) {
148  $this->path = $this->‪sanitizePath($uriParts['path']);
149  }
150 
151  if (isset($uriParts['query'])) {
152  $this->query = $this->‪sanitizeQuery($uriParts['query']);
153  }
154 
155  if (isset($uriParts['fragment'])) {
156  $this->fragment = $this->‪sanitizeFragment($uriParts['fragment']);
157  }
158  }
159 
174  public function ‪getScheme()
175  {
176  return ‪$this->scheme;
177  }
178 
197  public function ‪getAuthority()
198  {
199  if (empty($this->host)) {
200  return '';
201  }
202 
204  if (!empty($this->userInfo)) {
205  ‪$authority = $this->userInfo . '@' . ‪$authority;
206  }
207 
208  if ($this->‪isNonStandardPort($this->scheme, $this->host, $this->port)) {
209  ‪$authority .= ':' . ‪$this->port;
210  }
211 
212  return ‪$authority;
213  }
214 
230  public function ‪getUserInfo()
231  {
232  return ‪$this->userInfo;
233  }
234 
246  public function ‪getHost()
247  {
248  return ‪$this->host;
249  }
250 
266  public function ‪getPort()
267  {
268  return $this->‪isNonStandardPort($this->scheme, $this->host, $this->port) ? $this->port : null;
269  }
270 
296  public function ‪getPath()
297  {
298  return ‪$this->path;
299  }
300 
321  public function ‪getQuery()
322  {
323  return ‪$this->query;
324  }
325 
342  public function ‪getFragment()
343  {
344  return ‪$this->fragment;
345  }
346 
363  public function ‪withScheme(‪$scheme)
364  {
366 
367  $clonedObject = clone $this;
368  $clonedObject->scheme = ‪$scheme;
369  return $clonedObject;
370  }
371 
387  public function ‪withUserInfo($user, $password = null)
388  {
389  ‪$userInfo = $user;
390  if (!empty($password)) {
391  ‪$userInfo .= ':' . $password;
392  }
393 
394  $clonedObject = clone $this;
395  $clonedObject->userInfo = ‪$userInfo;
396  return $clonedObject;
397  }
398 
412  public function ‪withHost(‪$host)
413  {
414  $clonedObject = clone $this;
415  $clonedObject->host = ‪$host;
416  return $clonedObject;
417  }
418 
437  public function ‪withPort(‪$port)
438  {
439  if (‪$port !== null) {
441  $argumentType = is_object(‪$port) ? get_class(‪$port) : gettype(‪$port);
442  throw new \InvalidArgumentException('Invalid port "' . $argumentType . '" specified, must be an integer.', 1436717324);
443  }
444 
445  ‪$port = (int)‪$port;
446  if ($port < 1 || $port > 65535) {
447  throw new \InvalidArgumentException('Invalid port "' . ‪$port . '" specified, must be a valid TCP/UDP port.', 1436717326);
448  }
449  }
450 
451  $clonedObject = clone $this;
452  $clonedObject->port = ‪$port;
453  return $clonedObject;
454  }
455 
479  public function ‪withPath(‪$path)
480  {
481  if (!is_string(‪$path)) {
482  throw new \InvalidArgumentException('Invalid path provided. Must be of type string.', 1436717328);
483  }
484 
485  if (strpos(‪$path, '?') !== false) {
486  throw new \InvalidArgumentException('Invalid path provided. Must not contain a query string.', 1436717330);
487  }
488 
489  if (strpos(‪$path, '#') !== false) {
490  throw new \InvalidArgumentException('Invalid path provided; must not contain a URI fragment', 1436717332);
491  }
492 
494  $clonedObject = clone $this;
495  $clonedObject->path = ‪$path;
496  return $clonedObject;
497  }
498 
515  public function ‪withQuery(‪$query)
516  {
517  if (!is_string(‪$query)) {
518  throw new \InvalidArgumentException('Query string must be a string.', 1436717334);
519  }
520 
521  if (strpos(‪$query, '#') !== false) {
522  throw new \InvalidArgumentException('Query string must not include a URI fragment.', 1436717336);
523  }
524 
526  $clonedObject = clone $this;
527  $clonedObject->query = ‪$query;
528  return $clonedObject;
529  }
530 
546  public function ‪withFragment(‪$fragment)
547  {
549  $clonedObject = clone $this;
550  $clonedObject->fragment = ‪$fragment;
551  return $clonedObject;
552  }
553 
577  public function ‪__toString()
578  {
579  $uri = '';
580 
581  if (!empty($this->scheme)) {
582  $uri .= $this->scheme . ':';
583  }
584 
585  ‪$authority = $this->‪getAuthority();
586  if (!empty(‪$authority)) {
587  $uri .= '//' . ‪$authority;
588  }
589 
590  ‪$path = $this->‪getPath();
591  if (!empty(‪$path)) {
592  $uri .= '/' . ltrim(‪$path, '/');
593  }
594 
595  if ($this->query) {
596  $uri .= '?' . ‪$this->query;
597  }
598  if ($this->fragment) {
599  $uri .= '#' . ‪$this->fragment;
600  }
601  return $uri;
602  }
603 
612  protected function ‪isNonStandardPort(‪$scheme, ‪$host, ‪$port)
613  {
614  if (empty(‪$scheme)) {
615  return empty(‪$host) || !empty(‪$port);
616  }
617 
618  if (empty(‪$host) || empty(‪$port)) {
619  return false;
620  }
621 
622  return !isset($this->supportedSchemes[‪$scheme]) || ‪$port !== $this->supportedSchemes[‪$scheme];
623  }
624 
633  protected function ‪sanitizeScheme(‪$scheme)
634  {
635  ‪$scheme = strtolower(‪$scheme);
636  ‪$scheme = preg_replace('#:(//)?$#', '', ‪$scheme);
637 
638  if (empty(‪$scheme)) {
639  return '';
640  }
641 
642  if (!array_key_exists(‪$scheme, $this->supportedSchemes)) {
643  throw new \InvalidArgumentException('Unsupported scheme "' . ‪$scheme . '"; must be any empty string or in the set (' . implode(', ', array_keys($this->supportedSchemes)) . ')', 1436717338);
644  }
645 
646  return ‪$scheme;
647  }
648 
655  protected function ‪sanitizePath(‪$path)
656  {
657  return preg_replace_callback(
658  '/(?:[^' . self::UNRESERVED_CHARLIST . ':@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/',
659  function ($matches) {
660  return rawurlencode($matches[0]);
661  },
662  ‪$path
663  );
664  }
665 
674  protected function ‪sanitizeQuery(‪$query)
675  {
676  if (!empty(‪$query) && strpos(‪$query, '?') === 0) {
677  ‪$query = substr(‪$query, 1);
678  }
679 
680  $parts = explode('&', ‪$query);
681  foreach ($parts as $index => $part) {
682  [$key, $value] = $this->‪splitQueryValue($part);
683  if ($value === null) {
684  $parts[$index] = $this->‪sanitizeQueryOrFragment($key);
685  continue;
686  }
687  $parts[$index] = $this->‪sanitizeQueryOrFragment($key) . '=' . $this->‪sanitizeQueryOrFragment($value);
688  }
689 
690  return implode('&', $parts);
691  }
692 
699  protected function ‪splitQueryValue($value)
700  {
701  $data = explode('=', $value, 2);
702  if (count($data) === 1) {
703  $data[] = null;
704  }
705  return $data;
706  }
707 
714  protected function ‪sanitizeFragment(‪$fragment)
715  {
716  if (‪$fragment === null) {
717  ‪$fragment = '';
718  }
719 
720  if (!empty(‪$fragment) && strpos(‪$fragment, '#') === 0) {
721  ‪$fragment = substr(‪$fragment, 1);
722  }
723 
725  }
726 
733  protected function ‪sanitizeQueryOrFragment($value)
734  {
735  return preg_replace_callback(
736  '/(?:[^' . self::UNRESERVED_CHARLIST . self::SUBDELIMITER_CHARLIST . '%:@\/\?]+|%(?![A-Fa-f0-9]{2}))/',
737  function ($matches) {
738  return rawurlencode($matches[0]);
739  },
740  $value
741  );
742  }
743 }
‪TYPO3\CMS\Core\Http\Uri\withPath
‪static withPath($path)
Definition: Uri.php:470
‪TYPO3\CMS\Core\Http\Uri\parseUri
‪parseUri($uri)
Definition: Uri.php:111
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Core\Http\Uri\$fragment
‪string $fragment
Definition: Uri.php:89
‪TYPO3\CMS\Core\Http\Uri\splitQueryValue
‪array splitQueryValue($value)
Definition: Uri.php:690
‪TYPO3\CMS\Core\Http\Uri\$host
‪string $host
Definition: Uri.php:69
‪TYPO3\CMS\Core\Http\Uri\getPath
‪string getPath()
Definition: Uri.php:287
‪TYPO3\CMS\Core\Http\Uri\sanitizeFragment
‪string sanitizeFragment($fragment)
Definition: Uri.php:705
‪TYPO3\CMS\Core\Http\Uri\__construct
‪__construct($uri='')
Definition: Uri.php:95
‪TYPO3\CMS\Core\Http\Uri\getPort
‪int null getPort()
Definition: Uri.php:257
‪TYPO3\CMS\Core\Http\Uri\sanitizeQueryOrFragment
‪string sanitizeQueryOrFragment($value)
Definition: Uri.php:724
‪TYPO3\CMS\Core\Http\Uri\getUserInfo
‪string getUserInfo()
Definition: Uri.php:221
‪TYPO3\CMS\Core\Http\Uri\$path
‪string $path
Definition: Uri.php:79
‪TYPO3\CMS\Core\Http\Uri\getAuthority
‪string getAuthority()
Definition: Uri.php:188
‪TYPO3\CMS\Core\Http\Uri\$port
‪int null $port
Definition: Uri.php:74
‪TYPO3\CMS\Core\Http\Uri\$query
‪string $query
Definition: Uri.php:84
‪TYPO3\CMS\Core\Http\Uri\sanitizePath
‪string sanitizePath($path)
Definition: Uri.php:646
‪TYPO3\CMS\Core\Http\Uri\getQuery
‪string getQuery()
Definition: Uri.php:312
‪TYPO3\CMS\Core\Http\Uri\withPort
‪static withPort($port)
Definition: Uri.php:428
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:29
‪TYPO3\CMS\Core\Http\Uri\SUBDELIMITER_CHARLIST
‪const SUBDELIMITER_CHARLIST
Definition: Uri.php:35
‪TYPO3\CMS\Core\Http\Uri\$supportedSchemes
‪int[] $supportedSchemes
Definition: Uri.php:51
‪TYPO3\CMS\Core\Http\Uri\withFragment
‪static withFragment($fragment)
Definition: Uri.php:537
‪TYPO3\CMS\Core\Http\Uri\withUserInfo
‪static withUserInfo($user, $password=null)
Definition: Uri.php:378
‪TYPO3\CMS\Core\Http\Uri\$authority
‪string $authority
Definition: Uri.php:59
‪TYPO3\CMS\Core\Http\Uri\withHost
‪static withHost($host)
Definition: Uri.php:403
‪TYPO3\CMS\Core\Http\Uri\$userInfo
‪string $userInfo
Definition: Uri.php:64
‪TYPO3\CMS\Core\Http\Uri\isNonStandardPort
‪bool isNonStandardPort($scheme, $host, $port)
Definition: Uri.php:603
‪TYPO3\CMS\Core\Http\Uri\withQuery
‪static withQuery($query)
Definition: Uri.php:506
‪TYPO3\CMS\Core\Http\Uri\getFragment
‪string getFragment()
Definition: Uri.php:333
‪TYPO3\CMS\Core\Http\Uri\UNRESERVED_CHARLIST
‪const UNRESERVED_CHARLIST
Definition: Uri.php:42
‪TYPO3\CMS\Core\Http\Uri\getHost
‪string getHost()
Definition: Uri.php:237
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Http\Uri\getScheme
‪string getScheme()
Definition: Uri.php:165
‪TYPO3\CMS\Core\Http\Uri\__toString
‪string __toString()
Definition: Uri.php:568
‪TYPO3\CMS\Core\Http\Uri\withScheme
‪static withScheme($scheme)
Definition: Uri.php:354
‪TYPO3\CMS\Core\Http\Uri\sanitizeQuery
‪string sanitizeQuery($query)
Definition: Uri.php:665
‪TYPO3\CMS\Core\Http\Uri\$scheme
‪string $scheme
Definition: Uri.php:47
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18
‪TYPO3\CMS\Core\Http\Uri\sanitizeScheme
‪string sanitizeScheme($scheme)
Definition: Uri.php:624