32,12 → 32,14 |
// | Author: Richard Heyes <richard at php net> | |
// +-----------------------------------------------------------------------+ |
// |
// $Id: URL.php,v 1.2 2006-03-13 21:00:48 ddelon Exp $ |
// $Id: URL.php,v 1.3 2007-11-19 14:06:54 alexandre_tb Exp $ |
// |
// Net_URL Class |
|
|
class Net_URL |
{ |
var $options = array('encode_query_keys' => false); |
/** |
* Full url |
* @var string |
121,10 → 123,16 |
*/ |
function __construct($url = null, $useBrackets = true) |
{ |
$this->url = $url; |
$this->useBrackets = $useBrackets; |
|
$this->initialize(); |
} |
|
function initialize() |
{ |
$HTTP_SERVER_VARS = !empty($_SERVER) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS']; |
|
$this->useBrackets = $useBrackets; |
$this->url = $url; |
$this->user = ''; |
$this->pass = ''; |
$this->host = ''; |
134,15 → 142,15 |
$this->anchor = ''; |
|
// Only use defaults if not an absolute URL given |
if (!preg_match('/^[a-z0-9]+:\/\//i', $url)) { |
if (!preg_match('/^[a-z0-9]+:\/\//i', $this->url)) { |
$this->protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http'); |
|
$this->protocol = 'http'; |
|
|
/** |
* Figure out host/port |
*/ |
if (!empty($HTTP_SERVER_VARS['HTTP_HOST']) AND preg_match('/^(.*)(:([0-9]+))?$/U', $HTTP_SERVER_VARS['HTTP_HOST'], $matches)) { |
if (!empty($HTTP_SERVER_VARS['HTTP_HOST']) && |
preg_match('/^(.*)(:([0-9]+))?$/U', $HTTP_SERVER_VARS['HTTP_HOST'], $matches)) |
{ |
$host = $matches[1]; |
if (!empty($matches[3])) { |
$port = $matches[3]; |
161,8 → 169,8 |
} |
|
// Parse the url and store the various parts |
if (!empty($url)) { |
$urlinfo = parse_url($url); |
if (!empty($this->url)) { |
$urlinfo = parse_url($this->url); |
|
// Default querystring |
$this->querystring = array(); |
201,7 → 209,6 |
} |
} |
} |
|
/** |
* Returns full url |
* |
224,7 → 231,11 |
} |
|
/** |
* Adds a querystring item |
* Adds or updates a querystring item (URL parameter). |
* Automatically encodes parameters with rawurlencode() if $preencoded |
* is false. |
* You can pass an array to $value, it gets mapped via [] in the URL if |
* $this->useBrackets is activated. |
* |
* @param string $name Name of item |
* @param string $value Value of item |
233,6 → 244,10 |
*/ |
function addQueryString($name, $value, $preencoded = false) |
{ |
if ($this->getOption('encode_query_keys')) { |
$name = rawurlencode($name); |
} |
|
if ($preencoded) { |
$this->querystring[$name] = $value; |
} else { |
248,6 → 263,10 |
*/ |
function removeQueryString($name) |
{ |
if ($this->getOption('encode_query_keys')) { |
$name = rawurlencode($name); |
} |
|
if (isset($this->querystring[$name])) { |
unset($this->querystring[$name]); |
} |
274,6 → 293,9 |
{ |
if (!empty($this->querystring)) { |
foreach ($this->querystring as $name => $value) { |
// Encode var name |
$name = rawurlencode($name); |
|
if (is_array($value)) { |
foreach ($value as $k => $v) { |
$querystring[] = $this->useBrackets ? sprintf('%s[%s]=%s', $name, $k, $v) : ($name . '=' . $v); |
312,13 → 334,25 |
$value = null; |
$key = $part; |
} |
if (substr($key, -2) == '[]') { |
$key = substr($key, 0, -2); |
if (@!is_array($return[$key])) { |
|
if (!$this->getOption('encode_query_keys')) { |
$key = rawurldecode($key); |
} |
|
if (preg_match('#^(.*)\[([0-9a-z_-]*)\]#i', $key, $matches)) { |
$key = $matches[1]; |
$idx = $matches[2]; |
|
// Ensure is an array |
if (empty($return[$key]) || !is_array($return[$key])) { |
$return[$key] = array(); |
} |
|
// Add data |
if ($idx === '') { |
$return[$key][] = $value; |
} else { |
$return[$key][] = $value; |
$return[$key][$idx] = $value; |
} |
} elseif (!$this->useBrackets AND !empty($return[$key])) { |
$return[$key] = (array)$return[$key]; |
341,7 → 375,7 |
* |
* This method can also be called statically. |
* |
* @param string $url URL path to resolve |
* @param string $path URL path to resolve |
* @return string The result |
*/ |
function resolvePath($path) |
404,8 → 438,48 |
function setProtocol($protocol, $port = null) |
{ |
$this->protocol = $protocol; |
$this->port = is_null($port) ? $this->getStandardPort() : $port; |
$this->port = is_null($port) ? $this->getStandardPort($protocol) : $port; |
} |
|
/** |
* Set an option |
* |
* This function set an option |
* to be used thorough the script. |
* |
* @access public |
* @param string $optionName The optionname to set |
* @param string $value The value of this option. |
*/ |
function setOption($optionName, $value) |
{ |
if (!array_key_exists($optionName, $this->options)) { |
return false; |
} |
|
$this->options[$optionName] = $value; |
$this->initialize(); |
} |
|
/** |
* Get an option |
* |
* This function gets an option |
* from the $this->options array |
* and return it's value. |
* |
* @access public |
* @param string $opionName The name of the option to retrieve |
* @see $this->options |
*/ |
function getOption($optionName) |
{ |
if (!isset($this->options[$optionName])) { |
return false; |
} |
|
return $this->options[$optionName]; |
} |
|
} |
?> |