Source for file URL2.php
Documentation is available at URL2.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: URL2.php,v 1.2 2007/05/08 00:08:13 davidc Exp $
// Net_URL2 Class (PHP5 Only)
* This is the options available for the neturl
* @var array $options The options of Net_URL2
public static $options = array (
'encode_query_keys' => false ,
* Whether to use [] in querystrings
* 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
public function __construct ($url = null , $useBrackets = true )
$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 ($_SERVER['HTTP_HOST']) AND preg_match('/^(.*)(:([0-9]+))?$/U', $_SERVER['HTTP_HOST'], $matches)) {
if (!empty ($matches[3 ])) {
$port = $this->getStandardPort ($this->protocol);
$this->host = !empty ($host) ? $host : (isset ($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost');
$this->port = !empty ($port) ? $port : (isset ($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : $this->getStandardPort ($this->protocol));
$this->path = !empty ($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : '/';
$this->querystring = isset ($_SERVER['QUERY_STRING']) ? $this->_parseRawQuerystring ($_SERVER['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 rawurlencoded or not, default = not
public 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
public 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
public function addRawQueryString ($querystring)
$this->querystring = $this->_parseRawQueryString ($querystring);
* Returns flat querystring
* @return string Querystring
public 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);
* 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)
public function setProtocol ($protocol, $port = null )
$this->protocol = $protocol;
$this->port = is_null($port) ? $this->getStandardPort ($protocol) : $port;
* 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 $url URL path to resolve
* @return string The result
public static 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>
public static function getStandardPort ($scheme)
case 'https': return 443;
case 'imaps': return 993;
case 'pop3s': return 995;
* Parses raw querystring and returns an array of it
* @param string $querystring The querystring to parse
* @return array An array of the querystring data
private 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;
* This function sets a private option
* @param string $optionName The option name
* @param string $value The value of this option
public static function setOption ($optionName, $value)
self ::$options[$optionName] = $value;
* This function will get an option
* from the options private variable.
* @return mixed Bool false if the key doesn't exist and the value
* of the option if it exists.
public function getOption ($optionName)
if (!isset (self ::$options[$optionName])) {
return self ::$options[$optionName];
Documentation generated on Mon, 11 Mar 2019 15:01:42 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|