Source for file url.php
Documentation is available at url.php
* base include file for SimpleTest
* @version $Id: url.php 1997 2010-07-27 09:53:01Z pp11 $
* 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.
if ($this->scheme ===
'file') {
// Unescaped backslashes not used in directory separator context
// will get caught by this, but they should have been urlencoded
// anyway so we don't care. If this ends up being a problem, the
// host regexp must be modified to match for backslashes when
list
($this->username, $this->password) =
$this->chompLogin($url);
if (preg_match('/(.*?):(.*)/', $this->host, $host_parts)) {
if ($this->scheme ===
'file' &&
strlen($this->host) ===
2) {
// DOS drive was placed in authority; promote it to path.
$url =
'/' .
$this->host .
$url;
$this->host =
$host_parts[1];
$this->port = (integer)
$host_parts[2];
$this->fragment =
(strncmp($url, "#", 1) ==
0 ?
substr($url, 1) :
false);
* 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.
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.
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.
if (preg_match('#^([^/]*)@(.*)#', $url, $matches)) {
$url =
$prefix .
$matches[2];
$parts =
explode(":", $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.
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 '/'.
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.
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.
foreach (explode("&", $raw) as $pair) {
if (preg_match('/(.*?)=(.*)/', $pair, $matches)) {
* Accessor for protocol part.
* @param string $default Value to use if not present.
* @return string Scheme name, e.g "http".
return $this->scheme ?
$this->scheme :
$default;
* 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) {
return $this->host ?
$this->host :
$default;
* 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.
if (! $this->path &&
$this->host) {
* 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.
$this->request->add($key, $value);
* Adds additional parameters to the request.
* @param hash/SimpleFormEncoding $parameters Additional
$this->request->merge($parameters);
* 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 =
$port =
$encoded =
$fragment =
'';
if ($this->username &&
$this->password) {
$identity =
$this->username .
':' .
$this->password .
'@';
// Safest way; otherwise, file URLs on Windows have an extra
// leading slash. It might be possible to convert file://
// URIs to local file paths, but that requires more research.
$coords =
$this->getX() ===
false ?
'' :
'?' .
$this->getX() .
',' .
$this->getY();
return "$scheme$identity$host$port$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() .
'@' :
'';
$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.
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 "/".
return (substr($path, 0, 1) !=
'/');
* Extracts the username and password for use in rendering
* @return string/boolean Form of username:password or false.
if ($this->username &&
$this->password) {
return $this->username .
':' .
$this->password;
* 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, 31 Oct 2010 16:32:55 -0500 by phpDocumentor 1.4.3