| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikWP - Libraries |
| 4 |
* @subpackage adapter.uri |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* URI adapter class. |
| 16 |
* |
| 17 |
* @since 10.0 |
| 18 |
*/ |
| 19 |
class JUri |
| 20 |
{ |
| 21 |
/** |
| 22 |
* A list of URI instances. |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
protected static $instances = array(); |
| 27 |
|
| 28 |
/** |
| 29 |
* The base URI. |
| 30 |
* |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
protected static $base = array(); |
| 34 |
|
| 35 |
/** |
| 36 |
* The root URI. |
| 37 |
* |
| 38 |
* @var array |
| 39 |
*/ |
| 40 |
protected static $root = array(); |
| 41 |
|
| 42 |
/** |
| 43 |
* The current URI. |
| 44 |
* |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
protected static $current = null; |
| 48 |
|
| 49 |
/** |
| 50 |
* The full URI string. |
| 51 |
* |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
protected $uri = null; |
| 55 |
|
| 56 |
/** |
| 57 |
* The URI scheme. |
| 58 |
* |
| 59 |
* @var string |
| 60 |
*/ |
| 61 |
protected $scheme = null; |
| 62 |
|
| 63 |
/** |
| 64 |
* The URI host. |
| 65 |
* |
| 66 |
* @var string |
| 67 |
*/ |
| 68 |
protected $host = null; |
| 69 |
|
| 70 |
/** |
| 71 |
* The URI port. |
| 72 |
* |
| 73 |
* @var integer |
| 74 |
*/ |
| 75 |
protected $port = null; |
| 76 |
|
| 77 |
/** |
| 78 |
* The URI username. |
| 79 |
* |
| 80 |
* @var string |
| 81 |
*/ |
| 82 |
protected $user = null; |
| 83 |
|
| 84 |
/** |
| 85 |
* The URI password. |
| 86 |
* |
| 87 |
* @var string |
| 88 |
*/ |
| 89 |
protected $pass = null; |
| 90 |
|
| 91 |
/** |
| 92 |
* The URI path. |
| 93 |
* |
| 94 |
* @var string |
| 95 |
*/ |
| 96 |
protected $path = null; |
| 97 |
|
| 98 |
/** |
| 99 |
* The URI anchor. |
| 100 |
* |
| 101 |
* @var string |
| 102 |
*/ |
| 103 |
protected $fragment = null; |
| 104 |
|
| 105 |
/** |
| 106 |
* The URI query. |
| 107 |
* |
| 108 |
* @var array |
| 109 |
*/ |
| 110 |
protected $query = array(); |
| 111 |
|
| 112 |
/** |
| 113 |
* Returns the base URI for the request. |
| 114 |
* |
| 115 |
* @param boolean $pathonly If false, prepend the scheme, host and port information. |
| 116 |
* |
| 117 |
* @return string The base URI string |
| 118 |
*/ |
| 119 |
public static function base($pathonly = false) |
| 120 |
{ |
| 121 |
$sign = serialize($pathonly); |
| 122 |
|
| 123 |
if (!isset(static::$base[$sign])) |
| 124 |
{ |
| 125 |
static::$base[$sign] = new static(rtrim(home_url('', $pathonly ? 'relative' : null), '/') . '/'); |
| 126 |
} |
| 127 |
|
| 128 |
// MUST return a string to avoid the manipulation of the base constant |
| 129 |
return (string) static::$base[$sign]; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Returns the root URI for the request. |
| 134 |
* |
| 135 |
* @param boolean $pathonly If false, prepend the scheme, host and port information. |
| 136 |
* @param string $path The URI path. |
| 137 |
* |
| 138 |
* @return string The root URI string. |
| 139 |
*/ |
| 140 |
public static function root($pathonly = false, $path = null) |
| 141 |
{ |
| 142 |
$sign = serialize(array($pathonly, $path)); |
| 143 |
|
| 144 |
if (!isset(static::$root[$sign])) |
| 145 |
{ |
| 146 |
// get base URI |
| 147 |
$uri = static::base($pathonly); |
| 148 |
|
| 149 |
// create a new URI object |
| 150 |
static::$root[$sign] = new static($uri); |
| 151 |
|
| 152 |
if ($path) |
| 153 |
{ |
| 154 |
// concat the path to the uri object |
| 155 |
$path = '/' . ltrim($path, '/'); |
| 156 |
static::$root[$sign]->path = $path; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
// MUST return a string to avoid the manipulation of the base constant |
| 161 |
return (string) static::$root[$sign]; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Returns the URL for the request, minus the query. |
| 166 |
* |
| 167 |
* @return string The current URI string. |
| 168 |
*/ |
| 169 |
public static function current() |
| 170 |
{ |
| 171 |
if (static::$current === null) |
| 172 |
{ |
| 173 |
/** |
| 174 |
* Do not use any filters to prevent issues with sanitizing. |
| 175 |
* The string-type filter was stripping url-encoded characters. |
| 176 |
* |
| 177 |
* @since 10.1.38 |
| 178 |
*/ |
| 179 |
$path = JFactory::getApplication()->input->server->getRaw('REQUEST_URI', ''); |
| 180 |
|
| 181 |
static::$current = (string) static::root(false, $path); |
| 182 |
} |
| 183 |
|
| 184 |
return static::$current; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Returns the global JUri object, only creating it if it doesn't already exist. |
| 189 |
* |
| 190 |
* @param string $uri The URI to parse (by default current URI). |
| 191 |
* |
| 192 |
* @return JUri The URI object. |
| 193 |
*/ |
| 194 |
public static function getInstance($uri = null) |
| 195 |
{ |
| 196 |
if (is_null($uri)) |
| 197 |
{ |
| 198 |
$uri = static::current(); |
| 199 |
} |
| 200 |
|
| 201 |
// cast to string because we may access a JUri object |
| 202 |
$uri = (string) $uri; |
| 203 |
|
| 204 |
if (!isset(static::$instances[$uri])) |
| 205 |
{ |
| 206 |
static::$instances[$uri] = new static($uri); |
| 207 |
} |
| 208 |
|
| 209 |
return static::$instances[$uri]; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Class constructor. |
| 214 |
* |
| 215 |
* @param string $uri The URI. |
| 216 |
* |
| 217 |
* @uses parse() |
| 218 |
*/ |
| 219 |
public function __construct($uri) |
| 220 |
{ |
| 221 |
$this->parse($uri); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Parse a given URI and populate the class fields. |
| 226 |
* |
| 227 |
* @param string $uri The URI string to parse. |
| 228 |
* |
| 229 |
* @return boolean True on success. |
| 230 |
*/ |
| 231 |
protected function parse($uri) |
| 232 |
{ |
| 233 |
// set the original URI to fall back on |
| 234 |
$this->uri = $uri; |
| 235 |
|
| 236 |
// parse the URI and populate the object fields |
| 237 |
$parts = parse_url($uri); |
| 238 |
|
| 239 |
// we need to replace & with & for parse_str to work right |
| 240 |
if (isset($parts['query']) && strpos($parts['query'], '&')) |
| 241 |
{ |
| 242 |
$parts['query'] = str_replace('&', '&', $parts['query']); |
| 243 |
} |
| 244 |
|
| 245 |
$this->scheme = isset($parts['scheme']) ? $parts['scheme'] : null; |
| 246 |
$this->user = isset($parts['user']) ? $parts['user'] : null; |
| 247 |
$this->pass = isset($parts['pass']) ? $parts['pass'] : null; |
| 248 |
$this->host = isset($parts['host']) ? $parts['host'] : null; |
| 249 |
$this->port = isset($parts['port']) ? $parts['port'] : null; |
| 250 |
$this->path = isset($parts['path']) ? $parts['path'] : null; |
| 251 |
$this->fragment = isset($parts['fragment']) ? $parts['fragment'] : null; |
| 252 |
|
| 253 |
// parse the query |
| 254 |
if (isset($parts['query'])) |
| 255 |
{ |
| 256 |
parse_str($parts['query'], $this->query); |
| 257 |
} |
| 258 |
|
| 259 |
return (bool) $parts; |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Magic method to get the string representation of the URI object. |
| 264 |
* |
| 265 |
* @return string The URI. |
| 266 |
* |
| 267 |
* @uses toString() |
| 268 |
*/ |
| 269 |
public function __toString() |
| 270 |
{ |
| 271 |
return $this->toString(); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Returns full URI string. |
| 276 |
* |
| 277 |
* @param array $parts An array specifying the parts to get. |
| 278 |
* |
| 279 |
* @return string The rendered URI string. |
| 280 |
* |
| 281 |
* @uses getQuery() |
| 282 |
*/ |
| 283 |
public function toString(array $parts = array('scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment')) |
| 284 |
{ |
| 285 |
// make sure the query is created |
| 286 |
$query = $this->getQuery(); |
| 287 |
|
| 288 |
$uri = ''; |
| 289 |
$uri .= in_array('scheme', $parts) ? (!empty($this->scheme) ? $this->scheme . '://' : '') : ''; |
| 290 |
$uri .= in_array('user', $parts) ? $this->user : ''; |
| 291 |
$uri .= in_array('pass', $parts) ? (!empty($this->pass) ? ':' : '') . $this->pass . (!empty($this->user) ? '@' : '') : ''; |
| 292 |
$uri .= in_array('host', $parts) ? $this->host : ''; |
| 293 |
$uri .= in_array('port', $parts) ? (!empty($this->port) ? ':' : '') . $this->port : ''; |
| 294 |
$uri .= in_array('path', $parts) ? $this->path : ''; |
| 295 |
$uri .= in_array('query', $parts) ? (!empty($query) ? '?' . $query : '') : ''; |
| 296 |
$uri .= in_array('fragment', $parts) ? (!empty($this->fragment) ? '#' . $this->fragment : '') : ''; |
| 297 |
|
| 298 |
return $uri; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Sets the URI host. |
| 303 |
* |
| 304 |
* @param string $host The URI host. |
| 305 |
* |
| 306 |
* @return void |
| 307 |
* |
| 308 |
* @since 10.1.35 |
| 309 |
*/ |
| 310 |
public function setHost($host) |
| 311 |
{ |
| 312 |
$this->host = $host; |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Checks if a query variable exists. |
| 317 |
* |
| 318 |
* @param string $name Name of the query variable to check. |
| 319 |
* |
| 320 |
* @return boolean True if the variable exists. |
| 321 |
*/ |
| 322 |
public function hasVar($name) |
| 323 |
{ |
| 324 |
return array_key_exists($name, $this->query); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Returns a query variable by name. |
| 329 |
* |
| 330 |
* @param string $name Name of the query variable to get. |
| 331 |
* @param string $default Default value to return if the variable is not set. |
| 332 |
* |
| 333 |
* @return array Query variables. |
| 334 |
* |
| 335 |
* @uses hasVar() |
| 336 |
*/ |
| 337 |
public function getVar($name, $default = null) |
| 338 |
{ |
| 339 |
if ($this->hasVar($name)) |
| 340 |
{ |
| 341 |
return $this->query[$name]; |
| 342 |
} |
| 343 |
|
| 344 |
return $default; |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Adds a query variable and value, replacing the value if it |
| 349 |
* already exists and returning the old value. |
| 350 |
* |
| 351 |
* @param string $name Name of the query variable to set. |
| 352 |
* @param string $value Value of the query variable. |
| 353 |
* |
| 354 |
* @return string Previous value for the query variable. |
| 355 |
* |
| 356 |
* @uses getVar() |
| 357 |
* @uses delVar() |
| 358 |
*/ |
| 359 |
public function setVar($name, $value) |
| 360 |
{ |
| 361 |
$tmp = $this->getVar($name, null); |
| 362 |
|
| 363 |
if (is_null($value)) |
| 364 |
{ |
| 365 |
$this->delVar($name); |
| 366 |
} |
| 367 |
else |
| 368 |
{ |
| 369 |
$this->query[$name] = $value; |
| 370 |
} |
| 371 |
|
| 372 |
return $tmp; |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* Removes an item from the query string variables if it exists. |
| 377 |
* |
| 378 |
* @param string $name Name of variable to remove. |
| 379 |
* |
| 380 |
* @return void |
| 381 |
* |
| 382 |
* @uses hasVar() |
| 383 |
*/ |
| 384 |
public function delVar($name) |
| 385 |
{ |
| 386 |
if ($this->hasVar($name)) |
| 387 |
{ |
| 388 |
unset($this->query[$name]); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Returns the URI query as string. |
| 394 |
* |
| 395 |
* @param boolean $to_array True to return the query as associative array. |
| 396 |
* False to return an HTTP query. |
| 397 |
* |
| 398 |
* @return mixed The query string or array. |
| 399 |
*/ |
| 400 |
public function getQuery($to_array = false) |
| 401 |
{ |
| 402 |
if ($to_array) |
| 403 |
{ |
| 404 |
return $this->query; |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* Decode special characters after building the |
| 409 |
* query string. |
| 410 |
* |
| 411 |
* @since 10.1.29 |
| 412 |
*/ |
| 413 |
return urldecode(http_build_query($this->query, '', '&')); |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* Sets the query to a supplied string in format: |
| 418 |
* foo=bar&x=y |
| 419 |
* |
| 420 |
* @param mixed $query The query string or array. |
| 421 |
* |
| 422 |
* @return void |
| 423 |
* |
| 424 |
* @since 10.1.29 |
| 425 |
*/ |
| 426 |
public function setQuery($query) |
| 427 |
{ |
| 428 |
if (is_array($query)) |
| 429 |
{ |
| 430 |
$this->query = $query; |
| 431 |
} |
| 432 |
else |
| 433 |
{ |
| 434 |
if (strpos($query, '&') !== false) |
| 435 |
{ |
| 436 |
$query = str_replace('&', '&', $query); |
| 437 |
} |
| 438 |
|
| 439 |
parse_str($query, $this->query); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* Get URI scheme (protocol). |
| 445 |
* ie. http, https, ftp, etc... |
| 446 |
* |
| 447 |
* @return string The URI scheme. |
| 448 |
* |
| 449 |
* @since 10.1.7 |
| 450 |
*/ |
| 451 |
public function getScheme() |
| 452 |
{ |
| 453 |
return $this->scheme; |
| 454 |
} |
| 455 |
|
| 456 |
/** |
| 457 |
* Set URI scheme (protocol). |
| 458 |
* ie. http, https, ftp, etc... |
| 459 |
* |
| 460 |
* @param string $scheme The URI scheme. |
| 461 |
* |
| 462 |
* @return void |
| 463 |
* |
| 464 |
* @since 10.1.50 |
| 465 |
*/ |
| 466 |
public function setScheme($scheme) |
| 467 |
{ |
| 468 |
$this->scheme = $scheme; |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* Gets URI username. |
| 473 |
* |
| 474 |
* @return string The URI username (null if not specified). |
| 475 |
* |
| 476 |
* @since 10.1.7 |
| 477 |
*/ |
| 478 |
public function getUser() |
| 479 |
{ |
| 480 |
return $this->user; |
| 481 |
} |
| 482 |
|
| 483 |
/** |
| 484 |
* Gets URI password. |
| 485 |
* |
| 486 |
* @return string The URI password (null if not specified). |
| 487 |
* |
| 488 |
* @since 10.1.7 |
| 489 |
*/ |
| 490 |
public function getPass() |
| 491 |
{ |
| 492 |
return $this->pass; |
| 493 |
} |
| 494 |
|
| 495 |
/** |
| 496 |
* Gets URI host. |
| 497 |
* |
| 498 |
* @return string The URI hostname or ip (null if not specified). |
| 499 |
* |
| 500 |
* @since 10.1.7 |
| 501 |
*/ |
| 502 |
public function getHost() |
| 503 |
{ |
| 504 |
return $this->host; |
| 505 |
} |
| 506 |
|
| 507 |
/** |
| 508 |
* Gets URI port. |
| 509 |
* |
| 510 |
* @return integer The URI port number (null if not specified). |
| 511 |
* |
| 512 |
* @since 10.1.7 |
| 513 |
*/ |
| 514 |
public function getPort() |
| 515 |
{ |
| 516 |
return (isset($this->port)) ? $this->port : null; |
| 517 |
} |
| 518 |
|
| 519 |
/** |
| 520 |
* Gets the URI path string. |
| 521 |
* |
| 522 |
* @return string The URI path string. |
| 523 |
* |
| 524 |
* @since 10.1.7 |
| 525 |
*/ |
| 526 |
public function getPath() |
| 527 |
{ |
| 528 |
return $this->path; |
| 529 |
} |
| 530 |
|
| 531 |
/** |
| 532 |
* Sets the URI path string. |
| 533 |
* |
| 534 |
* @param string $path The URI path string. |
| 535 |
* |
| 536 |
* @return void |
| 537 |
* |
| 538 |
* @since 10.1.72 |
| 539 |
*/ |
| 540 |
public function setPath($path) |
| 541 |
{ |
| 542 |
// remove duplicate slashes |
| 543 |
$this->path = preg_replace("/\/{2,}/", '/', $path); |
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
* Gets the URI anchor string (everything after the "#"). |
| 548 |
* |
| 549 |
* @return string The URI anchor string. |
| 550 |
* |
| 551 |
* @since 10.1.7 |
| 552 |
*/ |
| 553 |
public function getFragment() |
| 554 |
{ |
| 555 |
return $this->fragment; |
| 556 |
} |
| 557 |
|
| 558 |
/** |
| 559 |
* Checks whether the current URI is using HTTPS. |
| 560 |
* |
| 561 |
* @return boolean True if using SSL via HTTPS, otherwise false. |
| 562 |
* |
| 563 |
* @uses getScheme() |
| 564 |
* |
| 565 |
* @since 10.1.7 |
| 566 |
*/ |
| 567 |
public function isSsl() |
| 568 |
{ |
| 569 |
return $this->getScheme() == 'https' ? true : false; |
| 570 |
} |
| 571 |
} |
| 572 |
|