Source for file url.php
Documentation is available at url.php
* base include file for SimpleTest
* @version $Id: url.php 1723 2008-04-08 00:34:10Z lastcraft $
* include other SimpleTest class files
require_once(dirname(__FILE__
) .
'/encoding.php');
* URL parser to replace parse_url() PHP function which
* got broken in PHP 4.3.0. Adds some browser specific
* functionality such as expandomatics.
* Guesses a bit trying to separate the host from
* the path and tries to keep a raw, possibly unparsable,
* request string as long as possible.
* Constructor. Parses URL into sections.
* @param string $url Incoming URL.
list
($x, $y) =
$this->_chompCoordinates($url);
$this->_scheme =
$this->_chompScheme($url);
$this->_host =
$this->_chompHost($url);
$this->_host =
$host_parts[1];
$this->_port = (integer)
$host_parts[2];
$this->_path =
$this->_chompPath($url);
$this->_request =
$this->_parseRequest($this->_chompRequest($url));
* Extracts the X, Y coordinate pair from an image map.
* @param string $url URL so far. The coordinates will be
* @return array X, Y as a pair of integers.
function _chompCoordinates(&$url) {
if (preg_match('/(.*)\?(\d+),(\d+)$/', $url, $matches)) {
return array((integer)
$matches[2], (integer)
$matches[3]);
return array(false, false);
* Extracts the scheme part of an incoming URL.
* @param string $url URL so far. The scheme will be
* @return string Scheme part or false.
function _chompScheme(&$url) {
if (preg_match('/^([^\/:]*):(\/\/)(.*)/', $url, $matches)) {
$url =
$matches[2] .
$matches[3];
* Extracts the username and password from the
* incoming URL. The // prefix will be reattached
* to the URL after the doublet is extracted.
* @param string $url URL so far. The username and
* @return array Two item list of username and
* password. Will urldecode() them.
function _chompLogin(&$url) {
if (preg_match('/^(\/\/)(.*)/', $url, $matches)) {
if (preg_match('/^([^\/]*)@(.*)/', $url, $matches)) {
$url =
$prefix .
$matches[2];
$parts =
split(":", $matches[1]);
isset
($parts[1]) ?
urldecode($parts[1]) :
false);
return array(false, false);
* Extracts the host part of an incoming URL.
* Includes the port number part. Will extract
* the host if it starts with // or it has
* a top level domain or it has at least two
* @param string $url URL so far. The host will be
* @return string Host part guess or false.
function _chompHost(&$url) {
if (preg_match('/^(\/\/)(.*?)(\/.*|\?.*|#.*|$)/', $url, $matches)) {
if (preg_match('/(.*?)(\.\.\/|\.\/|\/|\?|#|$)(.*)/', $url, $matches)) {
if (preg_match('/[a-z0-9\-]+\.(' .
$tlds .
')/i', $matches[1])) {
$url =
$matches[2] .
$matches[3];
} elseif (preg_match('/[a-z0-9\-]+\.[a-z0-9\-]+\.[a-z0-9\-]+/i', $matches[1])) {
$url =
$matches[2] .
$matches[3];
* Extracts the path information from the incoming
* URL. Strips this path from the URL.
* @param string $url URL so far. The host will be
* @return string Path part or '/'.
function _chompPath(&$url) {
if (preg_match('/(.*?)(\?|#|$)(.*)/', $url, $matches)) {
$url =
$matches[2] .
$matches[3];
return ($matches[1] ?
$matches[1] :
'');
* Strips off the request data.
* @param string $url URL so far. The request will be
* @return string Raw request part.
function _chompRequest(&$url) {
if (preg_match('/\?(.*?)(#|$)(.*)/', $url, $matches)) {
$url =
$matches[2] .
$matches[3];
* Breaks the request down into an object.
* @param string $raw Raw request.
* @return SimpleFormEncoding Parsed data.
function _parseRequest($raw) {
foreach (split("&", $raw) as $pair) {
if (preg_match('/(.*?)=(.*)/', $pair, $matches)) {
$request->add($matches[1], urldecode($matches[2]));
$request->add($pair, '');
* Accessor for protocol part.
* @param string $default Value to use if not present.
* @return string Scheme name, e.g "http".
* Accessor for user name.
* @return string Username preceding host.
* @return string Password preceding host.
* Accessor for hostname and port.
* @param string $default Value to use if not present.
* @return string Hostname only.
function getHost($default =
false) {
* Accessor for top level domain.
* @return string Last part of host.
return (isset
($path_parts['extension']) ?
$path_parts['extension'] :
false);
* Accessor for port number.
* @return integer TCP/IP port number.
* @return string Full path including leading slash if implied.
* Accessor for page if any. This may be a
* directory name if ambiguious.
* Gets the path to the page.
* @return string Path less the page.
* Accessor for fragment at end of URL after the "#".
* @return string Part after "#".
* Sets image coordinates. Set to false to clear
* @param integer $x Horizontal position.
* @param integer $y Vertical position.
if (($x ===
false) ||
($y ===
false)) {
$this->_x =
$this->_y =
false;
* Accessor for horizontal image coordinate.
* @return integer X value.
* Accessor for vertical image coordinate.
* @return integer Y value.
* Accessor for current request parameters
* in URL string form. Will return teh original request
* if at all possible even if it doesn't make much
* @return string Form is string "?a=1&b=2", etc.
$encoded =
$this->_request->asUrlRequest();
* Adds an additional parameter to the request.
* @param string $key Name of parameter.
* @param string $value Value as string.
* Adds additional parameters to the request.
* @param hash/SimpleFormEncoding $parameters Additional
* Clears down all parameters.
* Gets the frame target if present. Although
* not strictly part of the URL specification it
* acts as similarily to the browser.
* @return boolean/string Frame name or false if none.
* Attaches a frame target.
* @param string $frame Name of frame.
* Renders the URL back into a string.
* @return string URL in canonical form.
$scheme =
$identity =
$host =
$encoded =
$fragment =
'';
$coords =
$this->getX() ===
false ?
'' :
'?' .
$this->getX() .
',' .
$this->getY();
return "$scheme$identity$host$path$encoded$fragment$coords";
* Replaces unknown sections to turn a relative
* URL into an absolute one. The base URL can
* be either a string or a SimpleUrl object.
* @param string/SimpleUrl $base Base URL.
$identity =
$base->getIdentity() ?
$base->getIdentity() .
'@' :
'';
$scheme =
$base->getScheme();
$host =
$base->getHost();
$port =
$base->getPort() ?
':' .
$base->getPort() :
'';
$identity =
$base->getIdentity() ?
$base->getIdentity() .
'@' :
'';
$path =
$this->normalisePath($this->_extractAbsolutePath($base));
$coords =
$this->getX() ===
false ?
'' :
'?' .
$this->getX() .
',' .
$this->getY();
return new SimpleUrl("$scheme://$identity$host$port$path$encoded$fragment$coords");
* Replaces unknown sections of the path with base parts
* to return a complete absolute one.
* @param string/SimpleUrl $base Base URL.
* @param string Absolute path.
function _extractAbsolutePath($base) {
if (! $this->_isRelativePath($this->_path)) {
return $base->getBasePath() .
$this->_path;
* Simple test to see if a path part is relative.
* @param string $path Path to test.
* @return boolean True if starts with a "/".
function _isRelativePath($path) {
return (substr($path, 0, 1) !=
'/');
* Extracts the username and password for use in rendering
* @return string/boolean Form of username:password or false.
* Replaces . and .. sections of the path.
* @param string $path Unoptimised path.
* @return string Path with dots removed if possible.
* A pipe seperated list of all TLDs that result in two part
* @return string Pipe separated list.
return 'com|edu|net|org|gov|mil|int|biz|info|name|pro|aero|coop|museum';
Documentation generated on Sun, 04 May 2008 09:22:23 -0500 by phpDocumentor 1.3.0