[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Part of the Joomla Framework Uri Package 4 * 5 * @copyright Copyright (C) 2005 - 2022 Open Source Matters, Inc. All rights reserved. 6 * @license GNU General Public License version 2 or later; see LICENSE 7 */ 8 9 namespace Joomla\Uri; 10 11 /** 12 * Base Joomla Uri Class 13 * 14 * @since 1.0 15 */ 16 abstract class AbstractUri implements UriInterface 17 { 18 /** 19 * Original URI 20 * 21 * @var string 22 * @since 1.0 23 */ 24 protected $uri; 25 26 /** 27 * Protocol 28 * 29 * @var string 30 * @since 1.0 31 */ 32 protected $scheme; 33 34 /** 35 * Host 36 * 37 * @var string 38 * @since 1.0 39 */ 40 protected $host; 41 42 /** 43 * Port 44 * 45 * @var integer 46 * @since 1.0 47 */ 48 protected $port; 49 50 /** 51 * Username 52 * 53 * @var string 54 * @since 1.0 55 */ 56 protected $user; 57 58 /** 59 * Password 60 * 61 * @var string 62 * @since 1.0 63 */ 64 protected $pass; 65 66 /** 67 * Path 68 * 69 * @var string 70 * @since 1.0 71 */ 72 protected $path; 73 74 /** 75 * Query 76 * 77 * @var string 78 * @since 1.0 79 */ 80 protected $query; 81 82 /** 83 * Anchor 84 * 85 * @var string 86 * @since 1.0 87 */ 88 protected $fragment; 89 90 /** 91 * Query variable hash 92 * 93 * @var array 94 * @since 1.0 95 */ 96 protected $vars = []; 97 98 /** 99 * Constructor. 100 * 101 * You can pass a URI string to the constructor to initialise a specific URI. 102 * 103 * @param string $uri The optional URI string 104 * 105 * @since 1.0 106 */ 107 public function __construct($uri = null) 108 { 109 if ($uri !== null) 110 { 111 $this->parse($uri); 112 } 113 } 114 115 /** 116 * Magic method to get the string representation of the UriInterface object. 117 * 118 * @return string 119 * 120 * @since 1.0 121 */ 122 public function __toString() 123 { 124 return $this->toString(); 125 } 126 127 /** 128 * Returns full URI string. 129 * 130 * @param array $parts An array of strings specifying the parts to render. 131 * 132 * @return string The rendered URI string. 133 * 134 * @since 1.0 135 */ 136 public function toString($parts = ['scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment']) 137 { 138 $bitmask = 0; 139 140 foreach ($parts as $part) 141 { 142 $const = 'static::' . strtoupper($part); 143 144 if (\defined($const)) 145 { 146 $bitmask |= \constant($const); 147 } 148 } 149 150 return $this->render($bitmask); 151 } 152 153 /** 154 * Returns full uri string. 155 * 156 * @param integer $parts A bitmask specifying the parts to render. 157 * 158 * @return string The rendered URI string. 159 * 160 * @since 1.2.0 161 */ 162 public function render($parts = self::ALL) 163 { 164 // Make sure the query is created 165 $query = $this->getQuery(); 166 167 $uri = ''; 168 $uri .= $parts & static::SCHEME ? (!empty($this->scheme) ? $this->scheme . '://' : '') : ''; 169 $uri .= $parts & static::USER ? $this->user : ''; 170 $uri .= $parts & static::PASS ? (!empty($this->pass) ? ':' : '') . $this->pass . (!empty($this->user) ? '@' : '') : ''; 171 $uri .= $parts & static::HOST ? $this->host : ''; 172 $uri .= $parts & static::PORT ? (!empty($this->port) ? ':' : '') . $this->port : ''; 173 $uri .= $parts & static::PATH ? $this->path : ''; 174 $uri .= $parts & static::QUERY ? (!empty($query) ? '?' . $query : '') : ''; 175 $uri .= $parts & static::FRAGMENT ? (!empty($this->fragment) ? '#' . $this->fragment : '') : ''; 176 177 return $uri; 178 } 179 180 /** 181 * Checks if variable exists. 182 * 183 * @param string $name Name of the query variable to check. 184 * 185 * @return boolean True if the variable exists. 186 * 187 * @since 1.0 188 */ 189 public function hasVar($name) 190 { 191 return array_key_exists($name, $this->vars); 192 } 193 194 /** 195 * Returns a query variable by name. 196 * 197 * @param string $name Name of the query variable to get. 198 * @param string $default Default value to return if the variable is not set. 199 * 200 * @return mixed Requested query variable if present otherwise the default value. 201 * 202 * @since 1.0 203 */ 204 public function getVar($name, $default = null) 205 { 206 if (array_key_exists($name, $this->vars)) 207 { 208 return $this->vars[$name]; 209 } 210 211 return $default; 212 } 213 214 /** 215 * Returns flat query string. 216 * 217 * @param boolean $toArray True to return the query as a key => value pair array. 218 * 219 * @return string|array Query string or Array of parts in query string depending on the function param 220 * 221 * @since 1.0 222 */ 223 public function getQuery($toArray = false) 224 { 225 if ($toArray) 226 { 227 return $this->vars; 228 } 229 230 // If the query is empty build it first 231 if ($this->query === null) 232 { 233 $this->query = static::buildQuery($this->vars); 234 } 235 236 return $this->query; 237 } 238 239 /** 240 * Get the URI scheme (protocol) 241 * 242 * @return string The URI scheme. 243 * 244 * @since 1.0 245 */ 246 public function getScheme() 247 { 248 return $this->scheme; 249 } 250 251 /** 252 * Get the URI username 253 * 254 * @return string The username, or null if no username was specified. 255 * 256 * @since 1.0 257 */ 258 public function getUser() 259 { 260 return $this->user; 261 } 262 263 /** 264 * Get the URI password 265 * 266 * @return string The password, or null if no password was specified. 267 * 268 * @since 1.0 269 */ 270 public function getPass() 271 { 272 return $this->pass; 273 } 274 275 /** 276 * Get the URI host 277 * 278 * @return string The hostname/IP or null if no hostname/IP was specified. 279 * 280 * @since 1.0 281 */ 282 public function getHost() 283 { 284 return $this->host; 285 } 286 287 /** 288 * Get the URI port 289 * 290 * @return integer The port number, or null if no port was specified. 291 * 292 * @since 1.0 293 */ 294 public function getPort() 295 { 296 return $this->port; 297 } 298 299 /** 300 * Gets the URI path string 301 * 302 * @return string The URI path string. 303 * 304 * @since 1.0 305 */ 306 public function getPath() 307 { 308 return $this->path; 309 } 310 311 /** 312 * Get the URI anchor string 313 * 314 * @return string The URI anchor string. 315 * 316 * @since 1.0 317 */ 318 public function getFragment() 319 { 320 return $this->fragment; 321 } 322 323 /** 324 * Checks whether the current URI is using HTTPS. 325 * 326 * @return boolean True if using SSL via HTTPS. 327 * 328 * @since 1.0 329 */ 330 public function isSsl() 331 { 332 return strtolower($this->getScheme()) === 'https'; 333 } 334 335 /** 336 * Build a query from an array (reverse of the PHP parse_str()). 337 * 338 * @param array $params The array of key => value pairs to return as a query string. 339 * 340 * @return string The resulting query string. 341 * 342 * @see parse_str() 343 * @since 1.0 344 */ 345 protected static function buildQuery(array $params) 346 { 347 return urldecode(http_build_query($params, '', '&')); 348 } 349 350 /** 351 * Parse a given URI and populate the class fields. 352 * 353 * @param string $uri The URI string to parse. 354 * 355 * @return boolean True on success. 356 * 357 * @since 1.0 358 */ 359 protected function parse($uri) 360 { 361 // Set the original URI to fall back on 362 $this->uri = $uri; 363 364 /* 365 * Parse the URI and populate the object fields. If URI is parsed properly, 366 * set method return value to true. 367 */ 368 369 $parts = UriHelper::parse_url($uri); 370 371 if ($parts === false) 372 { 373 throw new \RuntimeException(sprintf('Could not parse the requested URI %s', $uri)); 374 } 375 376 $retval = ($parts) ? true : false; 377 378 // We need to replace & with & for parse_str to work right... 379 if (isset($parts['query']) && strpos($parts['query'], '&') !== false) 380 { 381 $parts['query'] = str_replace('&', '&', $parts['query']); 382 } 383 384 foreach ($parts as $key => $value) 385 { 386 $this->$key = $value; 387 } 388 389 // Parse the query 390 if (isset($parts['query'])) 391 { 392 parse_str($parts['query'], $this->vars); 393 } 394 395 return $retval; 396 } 397 398 /** 399 * Resolves //, ../ and ./ from a path and returns the result. 400 * 401 * For example: 402 * /foo/bar/../boo.php => /foo/boo.php 403 * /foo/bar/../../boo.php => /boo.php 404 * /foo/bar/.././/boo.php => /foo/boo.php 405 * 406 * @param string $path The URI path to clean. 407 * 408 * @return string Cleaned and resolved URI path. 409 * 410 * @since 1.0 411 */ 412 protected function cleanPath($path) 413 { 414 $path = explode('/', preg_replace('#(/+)#', '/', $path)); 415 416 for ($i = 0, $n = \count($path); $i < $n; $i++) 417 { 418 if ($path[$i] == '.' || $path[$i] == '..') 419 { 420 if (($path[$i] == '.') || ($path[$i] == '..' && $i == 1 && $path[0] == '')) 421 { 422 unset($path[$i]); 423 $path = array_values($path); 424 $i--; 425 $n--; 426 } 427 elseif ($path[$i] == '..' && ($i > 1 || ($i == 1 && $path[0] != ''))) 428 { 429 unset($path[$i], $path[$i - 1]); 430 431 $path = array_values($path); 432 $i -= 2; 433 $n -= 2; 434 } 435 } 436 } 437 438 return implode('/', $path); 439 } 440 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |