Source for file uri.php
Documentation is available at uri.php
* @package Joomla.Platform
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* This class serves two purposes. First it parses a URI and provides a common interface
* for the Joomla Platform to access and manipulate a URI. Second it obtains the URI of
* the current executing script from the server regardless of server.
* @package Joomla.Platform
* @var string Original URI
* @var array Query variable hash
protected $vars =
array();
* @var array An array of JUri instances.
protected static $instances =
array();
* @var array The current calculated base url segments.
protected static $base =
array();
* @var array The current calculated root url segments.
protected static $root =
array();
* @var string The current url.
protected static $current;
* You can pass a URI string to the constructor to initialise a specific URI.
* @param string $uri The optional URI string
* Magic method to get the string representation of the URI object.
* Returns the global JUri object, only creating it
* if it doesn't already exist.
* @param string $uri The URI to parse. [optional: if null uses script URI]
* @return JUri The URI object.
if (empty(self::$instances[$uri]))
// Are we obtaining the URI from the server?
// Determine if the request was over SSL (HTTPS).
if (isset
($_SERVER['HTTPS']) &&
!empty($_SERVER['HTTPS']) &&
(strtolower($_SERVER['HTTPS']) !=
'off'))
* Since we are assigning the URI from the server variables, we first need
* to determine if we are running on apache or IIS. If PHP_SELF and REQUEST_URI
* are present, we will assume we are running on apache.
if (!empty($_SERVER['PHP_SELF']) &&
!empty($_SERVER['REQUEST_URI']))
// To build the entire URI we need to prepend the protocol, and the http host
$theURI =
'http' .
$https .
$_SERVER['HTTP_HOST'] .
$_SERVER['REQUEST_URI'];
* Since we do not have REQUEST_URI to work with, we will assume we are
* running on IIS and will therefore need to work some magic with the SCRIPT_NAME and
* QUERY_STRING environment variables.
* IIS uses the SCRIPT_NAME variable instead of a REQUEST_URI variable... thanks, MS
$theURI =
'http' .
$https .
$_SERVER['HTTP_HOST'] .
$_SERVER['SCRIPT_NAME'];
// If the query string exists append it to the URI string
if (isset
($_SERVER['QUERY_STRING']) &&
!empty($_SERVER['QUERY_STRING']))
$theURI .=
'?' .
$_SERVER['QUERY_STRING'];
// Extra cleanup to remove invalid chars in the URL to prevent injections through the Host header
$theURI =
str_replace(array("'", '"', '<', '>'), array("%27", "%22", "%3C", "%3E"), $theURI);
self::$instances[$uri] =
new JUri($theURI);
return self::$instances[$uri];
* Returns the base URI for the request.
* @param boolean $pathonly If false, prepend the scheme, host and port information. Default is false.
* @return string The base URI string
public static function base($pathonly =
false)
// Get the base request path.
$config =
JFactory::getConfig();
$live_site =
$config->get('live_site');
if (trim($live_site) !=
'')
$uri =
self::getInstance($live_site);
self::$base['prefix'] =
$uri->toString(array('scheme', 'host', 'port'));
self::$base['path'] =
rtrim($uri->toString(array('path')), '/\\');
self::$base['path'] .=
'/administrator';
$uri =
self::getInstance();
self::$base['prefix'] =
$uri->toString(array('scheme', 'host', 'port'));
if (strpos(php_sapi_name(), 'cgi') !==
false &&
!ini_get('cgi.fix_pathinfo') &&
!empty($_SERVER['REQUEST_URI']))
// PHP-CGI on Apache with "cgi.fix_pathinfo = 0"
// We shouldn't have user-supplied PATH_INFO in PHP_SELF in this case
// because PHP will not work with PATH_INFO at all.
$script_name =
$_SERVER['PHP_SELF'];
$script_name =
$_SERVER['SCRIPT_NAME'];
self::$base['path'] =
rtrim(dirname($script_name), '/\\');
return $pathonly ===
false ?
self::$base['prefix'] .
self::$base['path'] .
'/' :
self::$base['path'];
* Returns the root URI for the request.
* @param boolean $pathonly If false, prepend the scheme, host and port information. Default is false.
* @param string $path The path
* @return string The root URI string.
public static function root($pathonly =
false, $path =
null)
$uri =
self::getInstance(self::base());
self::$root['prefix'] =
$uri->toString(array('scheme', 'host', 'port'));
self::$root['path'] =
rtrim($uri->toString(array('path')), '/\\');
self::$root['path'] =
$path;
return $pathonly ===
false ?
self::$root['prefix'] .
self::$root['path'] .
'/' :
self::$root['path'];
* Returns the URL for the request, minus the query.
if (empty(self::$current))
$uri =
self::getInstance();
self::$current =
$uri->toString(array('scheme', 'host', 'port', 'path'));
* Method to reset class static members for testing and other various issues.
public static function reset()
self::$instances =
array();
* Parse a given URI and populate the class fields.
* @param string $uri The URI string to parse.
* @return boolean True on success.
public function parse($uri)
// Set the original URI to fall back on
* Parse the URI and populate the object fields. If URI is parsed properly,
* set method return value to true.
$parts =
JString::parse_url($uri);
$retval =
($parts) ?
true :
false;
// We need to replace & with & for parse_str to work right...
if (isset
($parts['query']) &&
strpos($parts['query'], '&'))
$parts['query'] =
str_replace('&', '&', $parts['query']);
$this->scheme = isset
($parts['scheme']) ?
$parts['scheme'] :
null;
$this->user = isset
($parts['user']) ?
$parts['user'] :
null;
$this->pass = isset
($parts['pass']) ?
$parts['pass'] :
null;
$this->host = isset
($parts['host']) ?
$parts['host'] :
null;
$this->port = isset
($parts['port']) ?
$parts['port'] :
null;
$this->path = isset
($parts['path']) ?
$parts['path'] :
null;
$this->query = isset
($parts['query']) ?
$parts['query'] :
null;
$this->fragment = isset
($parts['fragment']) ?
$parts['fragment'] :
null;
if (isset
($parts['query']))
* Returns full uri string.
* @param array $parts An array specifying the parts to render.
* @return string The rendered URI string.
public function toString(array $parts =
array('scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment'))
// Make sure the query is created
$uri .=
in_array('pass', $parts) ?
(!empty($this->pass) ?
':' :
'') .
$this->pass .
(!empty($this->user) ?
'@' :
'') :
'';
$uri .=
in_array('port', $parts) ?
(!empty($this->port) ?
':' :
'') .
$this->port :
'';
$uri .=
in_array('query', $parts) ?
(!empty($query) ?
'?' .
$query :
'') :
'';
* Adds a query variable and value, replacing the value if it
* already exists and returning the old value.
* @param string $name Name of the query variable to set.
* @param string $value Value of the query variable.
* @return string Previous value for the query variable.
public function setVar($name, $value)
$tmp = isset
($this->vars[$name]) ?
$this->vars[$name] :
null;
$this->vars[$name] =
$value;
* Checks if variable exists.
* @param string $name Name of the query variable to check.
* @return boolean True if the variable exists.
* Returns a query variable by name.
* @param string $name Name of the query variable to get.
* @param string $default Default value to return if the variable is not set.
* @return array Query variables.
public function getVar($name, $default =
null)
return $this->vars[$name];
* Removes an item from the query string variables if it exists.
* @param string $name Name of variable to remove.
unset
($this->vars[$name]);
* Sets the query to a supplied string in format:
* @param mixed $query The query string or array.
if (strpos($query, '&') !==
false)
* Returns flat query string.
* @param boolean $toArray True to return the query as a key => value pair array.
* @return string Query string.
public function getQuery($toArray =
false)
// If the query is empty build it first
$this->query =
self::buildQuery($this->vars);
* Build a query from a array (reverse of the PHP parse_str()).
* @param array $params The array of key => value pairs to return as a query string.
* @return string The resulting query string.
return urldecode(http_build_query($params, '', '&'));
* Get URI scheme (protocol)
* ie. http, https, ftp, etc...
* @return string The URI scheme.
* Set URI scheme (protocol)
* ie. http, https, ftp, etc...
* @param string $scheme The URI scheme.
* Returns the username, or null if no username was specified.
* @return string The URI username.
* @param string $user The URI username.
* Returns the password, or null if no password was specified.
* @return string The URI password.
* @param string $pass The URI password.
* Returns the hostname/ip or null if no hostname/ip was specified.
* @return string The URI host.
* @param string $host The URI host.
* Returns the port number, or null if no port was specified.
* @return integer The URI port number.
return (isset
($this->port)) ?
$this->port :
null;
* @param integer $port The URI port number.
* Gets the URI path string.
* @return string The URI path string.
* Set the URI path string.
* @param string $path The URI path string.
* Get the URI archor string
* Everything after the "#".
* @return string The URI anchor string.
* Set the URI anchor string
* everything after the "#".
* @param string $anchor The URI anchor string.
* Checks whether the current URI is using HTTPS.
* @return boolean True if using SSL via HTTPS.
return $this->getScheme() ==
'https' ?
true :
false;
* Checks if the supplied URL is internal
* @param string $url The URL to check.
* @return boolean True if Internal.
$uri =
self::getInstance($url);
$base =
$uri->toString(array('scheme', 'host', 'port', 'path'));
$host =
$uri->toString(array('scheme', 'host', 'port'));
if (stripos($base, self::base()) !==
0 &&
!empty($host))
* Resolves //, ../ and ./ from a path and returns
* /foo/bar/../boo.php => /foo/boo.php
* /foo/bar/../../boo.php => /boo.php
* /foo/bar/.././/boo.php => /foo/boo.php
* @param string $path The URI path to clean.
* @return string Cleaned and resolved URI path.
for ($i =
0, $n =
count($path); $i <
$n; $i++
)
if ($path[$i] ==
'.' ||
$path[$i] ==
'..')
if (($path[$i] ==
'.') ||
($path[$i] ==
'..' &&
$i ==
1 &&
$path[0] ==
''))
elseif ($path[$i] ==
'..' &&
($i >
1 ||
($i ==
1 &&
$path[0] !=
'')))
Documentation generated on Tue, 19 Nov 2013 15:16:15 +0100 by phpDocumentor 1.4.3