Source for file URL.php
Documentation is available at URL.php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2002-2004, Richard Heyes |
// | All rights reserved. |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// +-----------------------------------------------------------------------+
// | Author: Richard Heyes <richard at php net> |
// +-----------------------------------------------------------------------+
// $Id: URL.php,v 1.46 2007/05/08 05:01:49 davidc Exp $
var $options = array ('encode_query_keys' => false );
function Net_URL ($url = null , $useBrackets = true )
$this->__construct ($url, $useBrackets);
* Parses the given url and stores the various parts
* Defaults are used in certain cases
* @param string $url Optional URL
* @param bool $useBrackets Whether to use square brackets when
* multiple querystrings with the same name
function __construct ($url = null , $useBrackets = true )
$this->useBrackets = $useBrackets;
function initialize ($url = null , $useBrackets = true )
$HTTP_SERVER_VARS = !empty ($_SERVER) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
$this->useBrackets = $useBrackets;
$this->querystring = array ();
// Only use defaults if not an absolute URL given
$this->protocol = (!empty ($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http');
if (!empty ($HTTP_SERVER_VARS['HTTP_HOST']) AND preg_match('/^(.*)(:([0-9]+))?$/U', $HTTP_SERVER_VARS['HTTP_HOST'], $matches)) {
if (!empty ($matches[3 ])) {
$port = $this->getStandardPort ($this->protocol);
$this->host = !empty ($host) ? $host : (isset ($HTTP_SERVER_VARS['SERVER_NAME']) ? $HTTP_SERVER_VARS['SERVER_NAME'] : 'localhost');
$this->port = !empty ($port) ? $port : (isset ($HTTP_SERVER_VARS['SERVER_PORT']) ? $HTTP_SERVER_VARS['SERVER_PORT'] : $this->getStandardPort ($this->protocol));
$this->path = !empty ($HTTP_SERVER_VARS['PHP_SELF']) ? $HTTP_SERVER_VARS['PHP_SELF'] : '/';
$this->querystring = isset ($HTTP_SERVER_VARS['QUERY_STRING']) ? $this->_parseRawQuerystring ($HTTP_SERVER_VARS['QUERY_STRING']) : null;
// Parse the url and store the various parts
$this->querystring = array ();
foreach ($urlinfo as $key => $value) {
$this->protocol = $value;
$this->port = $this->getStandardPort ($value);
$path = dirname($this->path) == DIRECTORY_SEPARATOR ? '' : dirname($this->path);
$this->path = sprintf('%s/%s', $path, $value);
$this->querystring = $this->_parseRawQueryString ($value);
* @return string Full url
$querystring = $this->getQueryString ();
$this->url = $this->protocol . '://'
. $this->user . (!empty ($this->pass) ? ':' : '')
. $this->pass . (!empty ($this->user) ? '@' : '')
. $this->host . ($this->port == $this->getStandardPort ($this->protocol) ? '' : ':' . $this->port)
. (!empty ($querystring) ? '?' . $querystring : '')
. (!empty ($this->anchor) ? '#' . $this->anchor : '');
* Adds a querystring item
* @param string $name Name of item
* @param string $value Value of item
* @param bool $preencoded Whether value is urlencoded or not, default = not
function addQueryString ($name, $value, $preencoded = false )
if ($this->getOption ('encode_query_keys')) {
$this->querystring[$name] = $value;
* Removes a querystring item
* @param string $name Name of item
function removeQueryString ($name)
if ($this->getOption ('encode_query_keys')) {
if (isset ($this->querystring[$name])) {
unset ($this->querystring[$name]);
* Sets the querystring to literally what you supply
* @param string $querystring The querystring data. Should be of the format foo=bar&x=y etc
function addRawQueryString ($querystring)
$this->querystring = $this->_parseRawQueryString ($querystring);
* Returns flat querystring
* @return string Querystring
function getQueryString ()
if (!empty ($this->querystring)) {
foreach ($this->querystring as $name => $value) {
foreach ($value as $k => $v) {
$querystring[] = $this->useBrackets ? sprintf('%s[%s]=%s', $name, $k, $v) : ($name . '=' . $v);
$querystring[] = $name . '=' . $value;
$querystring = implode(ini_get('arg_separator.output'), $querystring);
* Parses raw querystring and returns an array of it
* @param string $querystring The querystring to parse
* @return array An array of the querystring data
function _parseRawQuerystring ($querystring)
foreach ($parts as $part) {
if (strpos($part, '=') !== false ) {
if (!$this->getOption ('encode_query_keys')) {
if (preg_match('#^(.*)\[([0-9a-z_-]*)\]#i', $key, $matches)) {
if (empty ($return[$key]) || !is_array($return[$key])) {
$return[$key][] = $value;
$return[$key][$idx] = $value;
} elseif (!$this->useBrackets AND !empty ($return[$key])) {
$return[$key] = (array) $return[$key];
$return[$key][] = $value;
* 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
* This method can also be called statically.
* @param string $url URL path to resolve
* @return string The result
function resolvePath ($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 ] == '') {
* Returns the standard port number for a protocol
* @param string $scheme The protocol to lookup
* @return integer Port number or NULL if no scheme matches
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
function getStandardPort ($scheme)
case 'https': return 443;
case 'imaps': return 993;
case 'pop3s': return 995;
* Forces the URL to a particular protocol
* @param string $protocol Protocol to force the URL to
* @param integer $port Optional port (standard port is used by default)
function setProtocol ($protocol, $port = null )
$this->protocol = $protocol;
$this->port = is_null($port) ? $this->getStandardPort ($protocol) : $port;
* This function set an option
* to be used thorough the script.
* @param string $optionName The optionname to set
* @param string $value The value of this option.
function setOption ($optionName, $value)
$this->options[$optionName] = $value;
* This function gets an option
* from the $this->options array
* @param string $opionName The name of the option to retrieve
function getOption ($optionName)
if (!isset ($this->options[$optionName])) {
return $this->options[$optionName];
Documentation generated on Mon, 11 Mar 2019 15:01:53 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|