← All changes
|
vendor/wpfluent/framework/src/WPFluent/Http/URL.php
+343
-113
1.0.99
→
2.11.0
View file →
| @@ -2,23 +2,154 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace FluentCommunity\Framework\Http; |
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | +use InvalidArgumentException; | |
| 6 | 7 | use FluentCommunity\Framework\Support\Util; |
| 7 | 8 | use FluentCommunity\Framework\Foundation\App; |
| 8 | -use FluentCommunity\Framework\Support\DateTime; | |
| 9 | 9 | use FluentCommunity\Framework\Support\UriTemplate; |
| 10 | 10 | |
| 11 | 11 | class URL |
| 12 | 12 | { |
| 13 | - protected $encrypter = null; | |
| 13 | + /** | |
| 14 | + * The full URL string. | |
| 15 | + * | |
| 16 | + * @var string | |
| 17 | + */ | |
| 18 | + protected $url = ''; | |
| 14 | 19 | |
| 15 | - public function __construct($encrypter = null) | |
| 20 | + /** | |
| 21 | + * URL generator instance used for signing and route generation. | |
| 22 | + * | |
| 23 | + * @var UrlGenerator|null | |
| 24 | + */ | |
| 25 | + protected $generator = null; | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * URL scheme (e.g., 'http', 'https'). | |
| 29 | + * | |
| 30 | + * @var string | |
| 31 | + */ | |
| 32 | + protected $scheme = ''; | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * URL host (e.g., 'example.com'). | |
| 36 | + * | |
| 37 | + * @var string|null | |
| 38 | + */ | |
| 39 | + protected $host = null; | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * URL port (e.g., 80, 443). | |
| 43 | + * | |
| 44 | + * @var int|null | |
| 45 | + */ | |
| 46 | + protected $port = null; | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * URL path (e.g., '/users/view'). | |
| 50 | + * | |
| 51 | + * @var string | |
| 52 | + */ | |
| 53 | + protected $path = ''; | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * URL query parameters as an associative array. | |
| 57 | + * | |
| 58 | + * @var array | |
| 59 | + */ | |
| 60 | + protected $query = []; | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * URL fragment (e.g., 'top' in '#top'). | |
| 64 | + * | |
| 65 | + * @var string|null | |
| 66 | + */ | |
| 67 | + protected $fragment = null; | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * URL username for authentication (if any). | |
| 71 | + * | |
| 72 | + * @var string|null | |
| 73 | + */ | |
| 74 | + protected $user = null; | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * URL password for authentication (if any). | |
| 78 | + * | |
| 79 | + * @var string|null | |
| 80 | + */ | |
| 81 | + protected $pass = null; | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * Constructor. | |
| 85 | + * | |
| 86 | + * @param string|null $url Optional URL to initialize. Defaults to current URL. | |
| 87 | + * @param UrlGenerator|null $generator Optional generator instance for signing/routes. | |
| 88 | + * | |
| 89 | + * @throws InvalidArgumentException If the given URL is invalid. | |
| 90 | + */ | |
| 91 | + public function __construct($url = '', $generator = null) | |
| 16 | 92 | { |
| 17 | - $this->encrypter = $encrypter ?: App::make('encrypter'); | |
| 93 | + $this->prepareRfcProperties( | |
| 94 | + $this->url = $url ?: $this->current() | |
| 95 | + ); | |
| 96 | + | |
| 97 | + $this->generator = $generator ?: new UrlGenerator(); | |
| 18 | 98 | } |
| 19 | 99 | |
| 20 | 100 | /** |
| 101 | + * Parse a URL according to RFC 3986 and populate internal components. | |
| 102 | + * | |
| 103 | + * @param string $url The URL to parse. | |
| 104 | + * | |
| 105 | + * @return void | |
| 106 | + * | |
| 107 | + * @throws InvalidArgumentException If the URL cannot be parsed. | |
| 108 | + */ | |
| 109 | + protected function prepareRfcProperties($url) | |
| 110 | + { | |
| 111 | + // --- Parse URL according to RFC3986 --- | |
| 112 | + $parts = parse_url($url); | |
| 113 | + | |
| 114 | + if ($parts === false) { | |
| 115 | + throw new InvalidArgumentException("Invalid URL: {$url}"); | |
| 116 | + } | |
| 117 | + | |
| 118 | + $this->scheme = $parts['scheme'] ?? ''; | |
| 119 | + $this->host = $parts['host'] ?? null; | |
| 120 | + $this->port = $parts['port'] ?? null; | |
| 121 | + $this->path = $parts['path'] ?? null; | |
| 122 | + $this->user = $parts['user'] ?? null; | |
| 123 | + $this->pass = $parts['pass'] ?? null; | |
| 124 | + $this->fragment = $parts['fragment'] ?? null; | |
| 125 | + | |
| 126 | + if (isset($parts['query'])) { | |
| 127 | + parse_str($parts['query'], $query); | |
| 128 | + $this->query = $query; | |
| 129 | + } else { | |
| 130 | + $this->query = []; | |
| 131 | + } | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * Create a new URL instance. | |
| 136 | + * | |
| 137 | + * @param string $uri | |
| 138 | + * @return static | |
| 139 | + */ | |
| 140 | + public static function of($uri) | |
| 141 | + { | |
| 142 | + $parts = parse_url($uri); | |
| 143 | + | |
| 144 | + if ($parts === false) { | |
| 145 | + throw new InvalidArgumentException("Invalid URI: {$uri}"); | |
| 146 | + } | |
| 147 | + | |
| 148 | + return new static($uri); | |
| 149 | + } | |
| 150 | + | |
| 151 | + /** | |
| 21 | 152 | * Get the current URL |
| 22 | 153 | * |
| 23 | 154 | * @return string |
| 24 | 155 | */ |
| @@ -23,9 +154,9 @@ | ||
| 23 | 154 | * @return string |
| 24 | 155 | */ |
| 25 | 156 | public function current() |
| 26 | 157 | { |
| 27 | - return get_site_url() . rtrim($_SERVER['REQUEST_URI'], '/'); | |
| 158 | + return rtrim(get_site_url(), '/') . $_SERVER['REQUEST_URI']; | |
| 28 | 159 | } |
| 29 | 160 | |
| 30 | 161 | /** |
| 31 | 162 | * Parse a url from uncompiled route |
| @@ -53,120 +184,28 @@ | ||
| 53 | 184 | /** |
| 54 | 185 | * Sign a URL |
| 55 | 186 | * |
| 56 | 187 | * @param string $url |
| 188 | + * @param array $params | |
| 57 | 189 | * @return string |
| 58 | 190 | */ |
| 59 | 191 | public function sign($url, $params = []) |
| 60 | 192 | { |
| 61 | - if (!preg_match('#^(http|https)://#', $url)) { | |
| 62 | - $config = App::config(); | |
| 63 | - $slug = $config->get('app.slug'); | |
| 64 | - $version = $config->get('app.rest_version'); | |
| 65 | - $url = sprintf('%s%s/%s/%s',rest_url(), $slug, $version, $url); | |
| 66 | - } | |
| 67 | - | |
| 68 | - $parts = parse_url($url); | |
| 69 | - | |
| 70 | - $url = $parts['scheme'] . '://' . $parts['host'] . $parts['path']; | |
| 71 | - | |
| 72 | - parse_str($parts['query'] ?? '', $query); | |
| 73 | - | |
| 74 | - $params = $this->validateExpiryTime($params + $query); | |
| 75 | - | |
| 76 | - $params = $this->encrypter->encrypt(http_build_query($params)); | |
| 77 | - | |
| 78 | - $signature = hash_hmac('sha256', $params, $this->encrypter->getKey()); | |
| 79 | - | |
| 80 | - return $url . '?_data=' . $params . '&_signature=' . $signature; | |
| 193 | + return $this->generator->sign($url, $params); | |
| 81 | 194 | } |
| 82 | 195 | |
| 83 | 196 | /** |
| 84 | - * Normalize the expiry time. | |
| 85 | - * | |
| 86 | - * @param array $params | |
| 87 | - * @return array | |
| 88 | - */ | |
| 89 | - public function validateExpiryTime($params) | |
| 90 | - { | |
| 91 | - if (isset($params['expires_at'])) { | |
| 92 | - if (is_string($params['expires_at'])) { | |
| 93 | - $params['expires_at'] = strtotime($params['expires_at']); | |
| 94 | - } elseif ($params['expires_at'] instanceof DateTime) { | |
| 95 | - $params['expires_at'] = $params['expires_at']->getTimestamp(); | |
| 96 | - } elseif (is_numeric($params['expires_at'])) { | |
| 97 | - if ($params['expires_at'] <= time()) { | |
| 98 | - $params['expires_at'] = time() + (int) $params['expires_at']; | |
| 99 | - } | |
| 100 | - } else { | |
| 101 | - throw new Exception('The expiry time is invalid or has passed.'); | |
| 102 | - } | |
| 103 | - } | |
| 197 | + * Validate a URL | |
| 198 | + * | |
| 199 | + * @param string $url | |
| 200 | + * @return mixed (false or array) | |
| 201 | + */ | |
| 202 | + public function validate($url) | |
| 203 | + { | |
| 204 | + return $this->generator->validate($url); | |
| 205 | + } | |
| 104 | 206 | |
| 105 | - return $params; | |
| 106 | - } | |
| 107 | - | |
| 108 | 207 | /** |
| 109 | - * Validate a URL | |
| 110 | - * | |
| 111 | - * @param string $url | |
| 112 | - * @return mixed (false or array) | |
| 113 | - */ | |
| 114 | - public function validate($url) | |
| 115 | - { | |
| 116 | - if (!$query = $this->parseUrlAndGetQuery($url)) { | |
| 117 | - return false; | |
| 118 | - } | |
| 119 | - | |
| 120 | - if (!isset($query['_data']) || !isset($query['_signature'])) { | |
| 121 | - return false; | |
| 122 | - } | |
| 123 | - | |
| 124 | - return $this->verifySignature($query['_data'], $query['_signature']); | |
| 125 | - } | |
| 126 | - | |
| 127 | - /** | |
| 128 | - * Parse query string from the url. | |
| 129 | - * | |
| 130 | - * @param string $url | |
| 131 | - * @return mixed (bool or array) | |
| 132 | - */ | |
| 133 | - public function parseUrlAndGetQuery($url) | |
| 134 | - { | |
| 135 | - $parts = parse_url($url); | |
| 136 | - | |
| 137 | - if (!isset($parts['query'])) { | |
| 138 | - return false; | |
| 139 | - } | |
| 140 | - | |
| 141 | - parse_str($parts['query'], $query); | |
| 142 | - | |
| 143 | - return $query; | |
| 144 | - } | |
| 145 | - | |
| 146 | - /** | |
| 147 | - * Verify the signature. | |
| 148 | - * | |
| 149 | - * @param array $data | |
| 150 | - * @param string $signature | |
| 151 | - * @return mixed (bool or array) | |
| 152 | - */ | |
| 153 | - public function verifySignature($data, $signature) | |
| 154 | - { | |
| 155 | - $expected = hash_hmac('sha256', $data, $this->encrypter->getKey()); | |
| 156 | - | |
| 157 | - if (!hash_equals($expected, $signature)) return false; | |
| 158 | - | |
| 159 | - parse_str($this->encrypter->decrypt($data), $params); | |
| 160 | - | |
| 161 | - if (isset($params['expires_at']) && time() > $params['expires_at']) { | |
| 162 | - return false; | |
| 163 | - } | |
| 164 | - | |
| 165 | - return empty($params) ? true : $params; | |
| 166 | - } | |
| 167 | - | |
| 168 | - /** | |
| 169 | 208 | * Helper method for home_url. |
| 170 | 209 | * |
| 171 | 210 | * @return string |
| 172 | 211 | */ |
| @@ -259,23 +298,214 @@ | ||
| 259 | 298 | } |
| 260 | 299 | |
| 261 | 300 | /** |
| 262 | 301 | * Generate a URL from a file path. |
| 302 | + * | |
| 303 | + * @param string $path | |
| 304 | + * @param bool $checkFile Whether to check if the file exists | |
| 305 | + * @return string | |
| 306 | + * | |
| 307 | + * @phpstan-ignore-next-line | |
| 308 | + */ | |
| 309 | + public function fromFile(string $path, bool $checkFile = false): string | |
| 310 | + { | |
| 311 | + return Util::pathToUrl($path, $checkFile); | |
| 312 | + } | |
| 313 | + | |
| 314 | + /** | |
| 315 | + * Generate a URL from a named route. | |
| 263 | 316 | * |
| 264 | - * @param string $path | |
| 317 | + * @param string $name | |
| 318 | + * @param array $params | |
| 319 | + * @param array $query | |
| 320 | + * @return string|null | |
| 321 | + */ | |
| 322 | + public function route($name, $params = [], $query = []) | |
| 323 | + { | |
| 324 | + return $this->generator->route($name, $params, $query); | |
| 325 | + } | |
| 326 | + | |
| 327 | + /** | |
| 328 | + * Return a clone of the URL with a new path. | |
| 329 | + * | |
| 330 | + * @param string $path | |
| 331 | + * @return $this | |
| 332 | + */ | |
| 333 | + public function withPath($path) | |
| 334 | + { | |
| 335 | + $clone = clone $this; | |
| 336 | + $clone->path = $path; | |
| 337 | + return $clone; | |
| 338 | + } | |
| 339 | + | |
| 340 | + /** | |
| 341 | + * Return a clone of the URL with a new query. | |
| 342 | + * | |
| 343 | + * @param array|string $query An array of query params or a query string | |
| 344 | + * @return $this | |
| 345 | + */ | |
| 346 | + public function withQuery($query) | |
| 347 | + { | |
| 348 | + $clone = clone $this; | |
| 349 | + | |
| 350 | + if (is_string($query)) { | |
| 351 | + parse_str($query, $query); | |
| 352 | + } | |
| 353 | + | |
| 354 | + $clone->query = $query; | |
| 355 | + return $clone; | |
| 356 | + } | |
| 357 | + | |
| 358 | + /** | |
| 359 | + * Return a clone of the URL with a new fragment. | |
| 360 | + * | |
| 361 | + * @param string $fragment | |
| 362 | + * @return $this | |
| 363 | + */ | |
| 364 | + public function withFragment($fragment) | |
| 365 | + { | |
| 366 | + $clone = clone $this; | |
| 367 | + $clone->fragment = $fragment; | |
| 368 | + return $clone; | |
| 369 | + } | |
| 370 | + | |
| 371 | + /** | |
| 372 | + * Return a clone of the URL with a new scheme. | |
| 373 | + * | |
| 374 | + * @param string $scheme | |
| 375 | + * @return $this | |
| 376 | + */ | |
| 377 | + public function withScheme($scheme) | |
| 378 | + { | |
| 379 | + $clone = clone $this; | |
| 380 | + $clone->scheme = $scheme; | |
| 381 | + return $clone; | |
| 382 | + } | |
| 383 | + | |
| 384 | + /** | |
| 385 | + * Get the URL scheme (e.g., 'http' or 'https'). | |
| 386 | + * | |
| 265 | 387 | * @return string |
| 266 | 388 | */ |
| 267 | - public function fromFile($path) | |
| 389 | + public function scheme() | |
| 268 | 390 | { |
| 269 | - return Util::pathToUrl($path); | |
| 391 | + return $this->scheme; | |
| 270 | 392 | } |
| 271 | 393 | |
| 272 | 394 | /** |
| 273 | - * Returns the string representation of the URL object | |
| 395 | + * Get the URL host. | |
| 396 | + * | |
| 397 | + * @return string|null | |
| 398 | + */ | |
| 399 | + public function host() | |
| 400 | + { | |
| 401 | + return $this->host; | |
| 402 | + } | |
| 403 | + | |
| 404 | + /** | |
| 405 | + * Get the URL port. | |
| 406 | + * | |
| 407 | + * @return int|null | |
| 408 | + */ | |
| 409 | + public function port() | |
| 410 | + { | |
| 411 | + return $this->port; | |
| 412 | + } | |
| 413 | + | |
| 414 | + /** | |
| 415 | + * Get the URL path. | |
| 416 | + * | |
| 417 | + * @return string | |
| 418 | + */ | |
| 419 | + public function path() | |
| 420 | + { | |
| 421 | + return $this->path; | |
| 422 | + } | |
| 423 | + | |
| 424 | + /** | |
| 425 | + * Get the URL query as an array. | |
| 426 | + * | |
| 427 | + * @return array | |
| 428 | + */ | |
| 429 | + public function query() | |
| 430 | + { | |
| 431 | + return $this->query; | |
| 432 | + } | |
| 433 | + | |
| 434 | + /** | |
| 435 | + * Get the URL fragment. | |
| 436 | + * | |
| 437 | + * @return string|null | |
| 438 | + */ | |
| 439 | + public function fragment() | |
| 440 | + { | |
| 441 | + return $this->fragment; | |
| 442 | + } | |
| 443 | + | |
| 444 | + /** | |
| 445 | + * Get the URL pass. | |
| 274 | 446 | * |
| 447 | + * @return string|null | |
| 448 | + */ | |
| 449 | + public function pass() | |
| 450 | + { | |
| 451 | + return $this->pass; | |
| 452 | + } | |
| 453 | + | |
| 454 | + /** | |
| 455 | + * Get the URL user. | |
| 456 | + * | |
| 457 | + * @return string | |
| 458 | + */ | |
| 459 | + public function user() | |
| 460 | + { | |
| 461 | + return $this->user; | |
| 462 | + } | |
| 463 | + | |
| 464 | + /** | |
| 465 | + * Returns the string representation of the URL object. | |
| 466 | + * | |
| 275 | 467 | * @return string Current URL. |
| 276 | 468 | */ |
| 277 | 469 | public function __toString() |
| 278 | 470 | { |
| 279 | - return $this->current(); | |
| 471 | + $url = ''; | |
| 472 | + | |
| 473 | + if ($this->scheme) { | |
| 474 | + $url .= "{$this->scheme}://"; | |
| 475 | + } | |
| 476 | + | |
| 477 | + if ($this->user) { | |
| 478 | + $url .= $this->user; | |
| 479 | + if ($this->pass) { | |
| 480 | + $url .= ":{$this->pass}"; | |
| 481 | + } | |
| 482 | + $url .= '@'; | |
| 483 | + } | |
| 484 | + | |
| 485 | + if ($this->host) { | |
| 486 | + $url .= $this->host; | |
| 487 | + } | |
| 488 | + | |
| 489 | + if ($this->port) { | |
| 490 | + $url .= ":{$this->port}"; | |
| 491 | + } | |
| 492 | + | |
| 493 | + if ($this->path !== null) { | |
| 494 | + $url .= $this->path === '' || str_starts_with($this->path, '/') | |
| 495 | + ? $this->path | |
| 496 | + : '/' . $this->path; | |
| 497 | + } | |
| 498 | + | |
| 499 | + if (!empty($this->query)) { | |
| 500 | + $url .= '?' . http_build_query( | |
| 501 | + $this->query, '', '&', PHP_QUERY_RFC3986 | |
| 502 | + ); | |
| 503 | + } | |
| 504 | + | |
| 505 | + if ($this->fragment) { | |
| 506 | + $url .= "#{$this->fragment}"; | |
| 507 | + } | |
| 508 | + | |
| 509 | + return $url; | |
| 280 | 510 | } |
| 281 | 511 | } |