Source code for file /joomla/environment/uri.php
Documentation is available at uri.php
* @version $Id: uri.php 6674 2007-02-19 05:52:03Z Jinx $
* @package Joomla.Framework
* @subpackage Environment
* @copyright Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
// Check to ensure this file is within the rest of the framework
* This class serves two purposes. First to parse a URI and provide a common interface
* for the Joomla Framework to access and manipulate a URI. Second to attain the URI of
* the current executing script from the server regardless of server.
* @package Joomla.Framework
* @subpackage Environment
* You can pass a URI string to the constructor to initialize a specific URI.
* @param string $uri The optional URI string
* Returns a reference to a global JURI object, only creating it
* if it doesn't already exist.
* This method must be invoked as:
* <pre> $uri =& JURI::getInstance([$uri]);</pre>
* @param string $uri The URI to parse. [optional: if null uses script URI]
* @return JURI The URI object.
static $instances =
array();
if (!isset
($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'];
// Create the new JURI instance
$instances[$uri] =
new JURI($theURI);
* Returns the base URI for the request.
* @return string The base URI string
// Get the base request URL if not set
$base .=
rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\').
'/';
* Parse a given URI and populate the class fields
* @param string $uri The URI string to parse
* @return boolean True on success
// Set the original URI to fall back on
// Decode the passed in uri
* Parse the URI and populate the object fields. If URI is parsed properly,
* set method return value to true.
$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'])) parse_str($_parts['query'], $this->_vars);
* Returns full uri string
* @param array $parts An array specifying the parts to render
* @return string The rendered URI string
function toString($parts =
array('scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment'))
$uri .=
in_array('pass', $parts) ?
(!empty ($this->_pass) ?
':' :
'') .
$this->_pass.
(!empty ($this->_user) ?
'@' :
'') :
'';
* 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
function setVar($name, $value)
$tmp =
@$this->_vars[$name];
$this->_vars[$name] =
$value;
* Returns a query variable by name
* @param string $name Name of the query variable to get
* @return array Query variables
function getVar($name =
null, $default=
null)
if(isset
($this->_vars[$name])) {
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 (array|string) $query The query string
* Returns flat query string
* @return string Query string
* 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 int The URI port number
return (isset
($this->_port)) ?
$this->_port :
null;
* @param int $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
$this->_path =
$this->_cleanPath($path);
* 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;
* 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 $uri The URI path to clean
* @return string Cleaned and resolved URI path
function _cleanPath($path)
for ($i =
0; $i <
count($path); $i ++
) {
elseif ($path[$i] ==
'..' AND ($i >
1 OR ($i ==
1 AND $path[0] !=
''))) {
elseif ($path[$i] ==
'..' AND $i ==
1 AND $path[0] ==
'') {
* Build a query from a array (reverse of the PHP parse_str())
* @return string The resulting query string
function _buildQuery ($params, $akey =
null)
//reset in case we are looping
if( !isset
($akey) &&
!count($out) ) {
foreach ( $params as $key =>
$val )
$out[] =
JURI::_buildQuery($val,$key);
$thekey =
( !$akey ) ?
$key :
$akey.
'[]';