Source code for file /joomla/environment/uri.php
Documentation is available at uri.php
* @version $Id: uri.php 9764 2007-12-30 07:48:11Z ircmaxell $
* @package Joomla.Framework
* @subpackage Environment
* @copyright Copyright (C) 2005 - 2008 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.
* @author Louis Landry <louis.landry@joomla.org>
* @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'];
// Now we need to clean what we got since we can't trust the server var
$theURI =
preg_replace('/[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']/', '""', $theURI);
// Create the new JURI instance
$instances[$uri] =
new JURI($theURI);
* Returns the base URI for the request.
* @param boolean $pathonly If true, prepend the scheme, host and port information. Default is false.
* @return string The base URI string
function base($pathonly =
false)
// Get the base request path
$base['prefix'] =
$uri->toString( array('scheme', 'host', 'port'));
$base['path'] =
rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$base['path'] =
rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\');
return $pathonly ===
false ?
$base['prefix'].
$base['path'].
'/' :
$base['path'];
* Returns the root URI for the request.
* @param boolean $pathonly If true, prepend the scheme, host and port information. Default is false.
* @return string The root URI string
function root($pathonly =
false, $path =
null)
$root['prefix'] =
$uri->toString( array('scheme', 'host', 'port'));
return $pathonly ===
false ?
$root['prefix'].
$root['path'].
'/' :
$root['path'];
* Returns the URL for the request, minus the query
$current =
$uri->toString( array('scheme', 'host', 'port', 'path'));
* 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
* Parse the URI and populate the object fields. If URI is parsed properly,
* set method return value to true.
if ($_parts =
$this->_parseURL($uri)) {
$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'))
$query =
$this->getQuery(); //make sure the query is created
$uri .=
in_array('pass', $parts) ?
(!empty ($this->_pass) ?
':' :
'') .
$this->_pass.
(!empty ($this->_user) ?
'@' :
'') :
'';
$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
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
//If the query is empty build it first
* Build a query from a array (reverse of the PHP parse_str())
* @return string The resulting query string
//reset in case we are looping
if( !isset
($akey) &&
!count($out) ) {
foreach ( $params as $key =>
$val )