close Warning: AdminModule failed with TracError: Unable to instantiate component <class 'trac.admin.web_ui.BasicsAdminPanel'> (super(type, obj): obj (type BasicsAdminPanel) is not an instance or subtype of type (Markup).)

source: externals/php/vendor/guzzlehttp/psr7/src/Uri.php@ b757e0e

Last change on this file since b757e0e was 01a620f, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Removed submodules for php externals and use composer to provide them and versioned them afterwards

  • Property mode set to 100644
File size: 15.1 KB
Line 
1<?php
2namespace GuzzleHttp\Psr7;
3
4use Psr\Http\Message\UriInterface;
5
6/**
7 * Basic PSR-7 URI implementation.
8 *
9 * @link https://github.com/phly/http This class is based upon
10 * Matthew Weier O'Phinney's URI implementation in phly/http.
11 */
12class Uri implements UriInterface
13{
14 private static $schemes = [
15 'http' => 80,
16 'https' => 443,
17 ];
18
19 private static $charUnreserved = 'a-zA-Z0-9_\-\.~';
20 private static $charSubDelims = '!\$&\'\(\)\*\+,;=';
21 private static $replaceQuery = ['=' => '%3D', '&' => '%26'];
22
23 /** @var string Uri scheme. */
24 private $scheme = '';
25
26 /** @var string Uri user info. */
27 private $userInfo = '';
28
29 /** @var string Uri host. */
30 private $host = '';
31
32 /** @var int|null Uri port. */
33 private $port;
34
35 /** @var string Uri path. */
36 private $path = '';
37
38 /** @var string Uri query string. */
39 private $query = '';
40
41 /** @var string Uri fragment. */
42 private $fragment = '';
43
44 /**
45 * @param string $uri URI to parse and wrap.
46 */
47 public function __construct($uri = '')
48 {
49 if ($uri != null) {
50 $parts = parse_url($uri);
51 if ($parts === false) {
52 throw new \InvalidArgumentException("Unable to parse URI: $uri");
53 }
54 $this->applyParts($parts);
55 }
56 }
57
58 public function __toString()
59 {
60 return self::createUriString(
61 $this->scheme,
62 $this->getAuthority(),
63 $this->getPath(),
64 $this->query,
65 $this->fragment
66 );
67 }
68
69 /**
70 * Removes dot segments from a path and returns the new path.
71 *
72 * @param string $path
73 *
74 * @return string
75 * @link http://tools.ietf.org/html/rfc3986#section-5.2.4
76 */
77 public static function removeDotSegments($path)
78 {
79 static $noopPaths = ['' => true, '/' => true, '*' => true];
80 static $ignoreSegments = ['.' => true, '..' => true];
81
82 if (isset($noopPaths[$path])) {
83 return $path;
84 }
85
86 $results = [];
87 $segments = explode('/', $path);
88 foreach ($segments as $segment) {
89 if ($segment == '..') {
90 array_pop($results);
91 } elseif (!isset($ignoreSegments[$segment])) {
92 $results[] = $segment;
93 }
94 }
95
96 $newPath = implode('/', $results);
97 // Add the leading slash if necessary
98 if (substr($path, 0, 1) === '/' &&
99 substr($newPath, 0, 1) !== '/'
100 ) {
101 $newPath = '/' . $newPath;
102 }
103
104 // Add the trailing slash if necessary
105 if ($newPath != '/' && isset($ignoreSegments[end($segments)])) {
106 $newPath .= '/';
107 }
108
109 return $newPath;
110 }
111
112 /**
113 * Resolve a base URI with a relative URI and return a new URI.
114 *
115 * @param UriInterface $base Base URI
116 * @param string $rel Relative URI
117 *
118 * @return UriInterface
119 */
120 public static function resolve(UriInterface $base, $rel)
121 {
122 if ($rel === null || $rel === '') {
123 return $base;
124 }
125
126 if (!($rel instanceof UriInterface)) {
127 $rel = new self($rel);
128 }
129
130 // Return the relative uri as-is if it has a scheme.
131 if ($rel->getScheme()) {
132 return $rel->withPath(static::removeDotSegments($rel->getPath()));
133 }
134
135 $relParts = [
136 'scheme' => $rel->getScheme(),
137 'authority' => $rel->getAuthority(),
138 'path' => $rel->getPath(),
139 'query' => $rel->getQuery(),
140 'fragment' => $rel->getFragment()
141 ];
142
143 $parts = [
144 'scheme' => $base->getScheme(),
145 'authority' => $base->getAuthority(),
146 'path' => $base->getPath(),
147 'query' => $base->getQuery(),
148 'fragment' => $base->getFragment()
149 ];
150
151 if (!empty($relParts['authority'])) {
152 $parts['authority'] = $relParts['authority'];
153 $parts['path'] = self::removeDotSegments($relParts['path']);
154 $parts['query'] = $relParts['query'];
155 $parts['fragment'] = $relParts['fragment'];
156 } elseif (!empty($relParts['path'])) {
157 if (substr($relParts['path'], 0, 1) == '/') {
158 $parts['path'] = self::removeDotSegments($relParts['path']);
159 $parts['query'] = $relParts['query'];
160 $parts['fragment'] = $relParts['fragment'];
161 } else {
162 if (!empty($parts['authority']) && empty($parts['path'])) {
163 $mergedPath = '/';
164 } else {
165 $mergedPath = substr($parts['path'], 0, strrpos($parts['path'], '/') + 1);
166 }
167 $parts['path'] = self::removeDotSegments($mergedPath . $relParts['path']);
168 $parts['query'] = $relParts['query'];
169 $parts['fragment'] = $relParts['fragment'];
170 }
171 } elseif (!empty($relParts['query'])) {
172 $parts['query'] = $relParts['query'];
173 } elseif ($relParts['fragment'] != null) {
174 $parts['fragment'] = $relParts['fragment'];
175 }
176
177 return new self(static::createUriString(
178 $parts['scheme'],
179 $parts['authority'],
180 $parts['path'],
181 $parts['query'],
182 $parts['fragment']
183 ));
184 }
185
186 /**
187 * Create a new URI with a specific query string value removed.
188 *
189 * Any existing query string values that exactly match the provided key are
190 * removed.
191 *
192 * Note: this function will convert "=" to "%3D" and "&" to "%26".
193 *
194 * @param UriInterface $uri URI to use as a base.
195 * @param string $key Query string key value pair to remove.
196 *
197 * @return UriInterface
198 */
199 public static function withoutQueryValue(UriInterface $uri, $key)
200 {
201 $current = $uri->getQuery();
202 if (!$current) {
203 return $uri;
204 }
205
206 $result = [];
207 foreach (explode('&', $current) as $part) {
208 if (explode('=', $part)[0] !== $key) {
209 $result[] = $part;
210 };
211 }
212
213 return $uri->withQuery(implode('&', $result));
214 }
215
216 /**
217 * Create a new URI with a specific query string value.
218 *
219 * Any existing query string values that exactly match the provided key are
220 * removed and replaced with the given key value pair.
221 *
222 * Note: this function will convert "=" to "%3D" and "&" to "%26".
223 *
224 * @param UriInterface $uri URI to use as a base.
225 * @param string $key Key to set.
226 * @param string $value Value to set.
227 *
228 * @return UriInterface
229 */
230 public static function withQueryValue(UriInterface $uri, $key, $value)
231 {
232 $current = $uri->getQuery();
233 $key = strtr($key, self::$replaceQuery);
234
235 if (!$current) {
236 $result = [];
237 } else {
238 $result = [];
239 foreach (explode('&', $current) as $part) {
240 if (explode('=', $part)[0] !== $key) {
241 $result[] = $part;
242 };
243 }
244 }
245
246 if ($value !== null) {
247 $result[] = $key . '=' . strtr($value, self::$replaceQuery);
248 } else {
249 $result[] = $key;
250 }
251
252 return $uri->withQuery(implode('&', $result));
253 }
254
255 /**
256 * Create a URI from a hash of parse_url parts.
257 *
258 * @param array $parts
259 *
260 * @return self
261 */
262 public static function fromParts(array $parts)
263 {
264 $uri = new self();
265 $uri->applyParts($parts);
266 return $uri;
267 }
268
269 public function getScheme()
270 {
271 return $this->scheme;
272 }
273
274 public function getAuthority()
275 {
276 if (empty($this->host)) {
277 return '';
278 }
279
280 $authority = $this->host;
281 if (!empty($this->userInfo)) {
282 $authority = $this->userInfo . '@' . $authority;
283 }
284
285 if ($this->isNonStandardPort($this->scheme, $this->host, $this->port)) {
286 $authority .= ':' . $this->port;
287 }
288
289 return $authority;
290 }
291
292 public function getUserInfo()
293 {
294 return $this->userInfo;
295 }
296
297 public function getHost()
298 {
299 return $this->host;
300 }
301
302 public function getPort()
303 {
304 return $this->port;
305 }
306
307 public function getPath()
308 {
309 return $this->path == null ? '' : $this->path;
310 }
311
312 public function getQuery()
313 {
314 return $this->query;
315 }
316
317 public function getFragment()
318 {
319 return $this->fragment;
320 }
321
322 public function withScheme($scheme)
323 {
324 $scheme = $this->filterScheme($scheme);
325
326 if ($this->scheme === $scheme) {
327 return $this;
328 }
329
330 $new = clone $this;
331 $new->scheme = $scheme;
332 $new->port = $new->filterPort($new->scheme, $new->host, $new->port);
333 return $new;
334 }
335
336 public function withUserInfo($user, $password = null)
337 {
338 $info = $user;
339 if ($password) {
340 $info .= ':' . $password;
341 }
342
343 if ($this->userInfo === $info) {
344 return $this;
345 }
346
347 $new = clone $this;
348 $new->userInfo = $info;
349 return $new;
350 }
351
352 public function withHost($host)
353 {
354 if ($this->host === $host) {
355 return $this;
356 }
357
358 $new = clone $this;
359 $new->host = $host;
360 return $new;
361 }
362
363 public function withPort($port)
364 {
365 $port = $this->filterPort($this->scheme, $this->host, $port);
366
367 if ($this->port === $port) {
368 return $this;
369 }
370
371 $new = clone $this;
372 $new->port = $port;
373 return $new;
374 }
375
376 public function withPath($path)
377 {
378 if (!is_string($path)) {
379 throw new \InvalidArgumentException(
380 'Invalid path provided; must be a string'
381 );
382 }
383
384 $path = $this->filterPath($path);
385
386 if ($this->path === $path) {
387 return $this;
388 }
389
390 $new = clone $this;
391 $new->path = $path;
392 return $new;
393 }
394
395 public function withQuery($query)
396 {
397 if (!is_string($query) && !method_exists($query, '__toString')) {
398 throw new \InvalidArgumentException(
399 'Query string must be a string'
400 );
401 }
402
403 $query = (string) $query;
404 if (substr($query, 0, 1) === '?') {
405 $query = substr($query, 1);
406 }
407
408 $query = $this->filterQueryAndFragment($query);
409
410 if ($this->query === $query) {
411 return $this;
412 }
413
414 $new = clone $this;
415 $new->query = $query;
416 return $new;
417 }
418
419 public function withFragment($fragment)
420 {
421 if (substr($fragment, 0, 1) === '#') {
422 $fragment = substr($fragment, 1);
423 }
424
425 $fragment = $this->filterQueryAndFragment($fragment);
426
427 if ($this->fragment === $fragment) {
428 return $this;
429 }
430
431 $new = clone $this;
432 $new->fragment = $fragment;
433 return $new;
434 }
435
436 /**
437 * Apply parse_url parts to a URI.
438 *
439 * @param $parts Array of parse_url parts to apply.
440 */
441 private function applyParts(array $parts)
442 {
443 $this->scheme = isset($parts['scheme'])
444 ? $this->filterScheme($parts['scheme'])
445 : '';
446 $this->userInfo = isset($parts['user']) ? $parts['user'] : '';
447 $this->host = isset($parts['host']) ? $parts['host'] : '';
448 $this->port = !empty($parts['port'])
449 ? $this->filterPort($this->scheme, $this->host, $parts['port'])
450 : null;
451 $this->path = isset($parts['path'])
452 ? $this->filterPath($parts['path'])
453 : '';
454 $this->query = isset($parts['query'])
455 ? $this->filterQueryAndFragment($parts['query'])
456 : '';
457 $this->fragment = isset($parts['fragment'])
458 ? $this->filterQueryAndFragment($parts['fragment'])
459 : '';
460 if (isset($parts['pass'])) {
461 $this->userInfo .= ':' . $parts['pass'];
462 }
463 }
464
465 /**
466 * Create a URI string from its various parts
467 *
468 * @param string $scheme
469 * @param string $authority
470 * @param string $path
471 * @param string $query
472 * @param string $fragment
473 * @return string
474 */
475 private static function createUriString($scheme, $authority, $path, $query, $fragment)
476 {
477 $uri = '';
478
479 if (!empty($scheme)) {
480 $uri .= $scheme . '://';
481 }
482
483 if (!empty($authority)) {
484 $uri .= $authority;
485 }
486
487 if ($path != null) {
488 // Add a leading slash if necessary.
489 if ($uri && substr($path, 0, 1) !== '/') {
490 $uri .= '/';
491 }
492 $uri .= $path;
493 }
494
495 if ($query != null) {
496 $uri .= '?' . $query;
497 }
498
499 if ($fragment != null) {
500 $uri .= '#' . $fragment;
501 }
502
503 return $uri;
504 }
505
506 /**
507 * Is a given port non-standard for the current scheme?
508 *
509 * @param string $scheme
510 * @param string $host
511 * @param int $port
512 * @return bool
513 */
514 private static function isNonStandardPort($scheme, $host, $port)
515 {
516 if (!$scheme && $port) {
517 return true;
518 }
519
520 if (!$host || !$port) {
521 return false;
522 }
523
524 return !isset(static::$schemes[$scheme]) || $port !== static::$schemes[$scheme];
525 }
526
527 /**
528 * @param string $scheme
529 *
530 * @return string
531 */
532 private function filterScheme($scheme)
533 {
534 $scheme = strtolower($scheme);
535 $scheme = rtrim($scheme, ':/');
536
537 return $scheme;
538 }
539
540 /**
541 * @param string $scheme
542 * @param string $host
543 * @param int $port
544 *
545 * @return int|null
546 *
547 * @throws \InvalidArgumentException If the port is invalid.
548 */
549 private function filterPort($scheme, $host, $port)
550 {
551 if (null !== $port) {
552 $port = (int) $port;
553 if (1 > $port || 0xffff < $port) {
554 throw new \InvalidArgumentException(
555 sprintf('Invalid port: %d. Must be between 1 and 65535', $port)
556 );
557 }
558 }
559
560 return $this->isNonStandardPort($scheme, $host, $port) ? $port : null;
561 }
562
563 /**
564 * Filters the path of a URI
565 *
566 * @param $path
567 *
568 * @return string
569 */
570 private function filterPath($path)
571 {
572 return preg_replace_callback(
573 '/(?:[^' . self::$charUnreserved . self::$charSubDelims . ':@\/%]+|%(?![A-Fa-f0-9]{2}))/',
574 [$this, 'rawurlencodeMatchZero'],
575 $path
576 );
577 }
578
579 /**
580 * Filters the query string or fragment of a URI.
581 *
582 * @param $str
583 *
584 * @return string
585 */
586 private function filterQueryAndFragment($str)
587 {
588 return preg_replace_callback(
589 '/(?:[^' . self::$charUnreserved . self::$charSubDelims . '%:@\/\?]+|%(?![A-Fa-f0-9]{2}))/',
590 [$this, 'rawurlencodeMatchZero'],
591 $str
592 );
593 }
594
595 private function rawurlencodeMatchZero(array $match)
596 {
597 return rawurlencode($match[0]);
598 }
599}
Note: See TracBrowser for help on using the repository browser.