TeamSpeak 3 PHP Framework  1.1.16
Copyright © Planet TeamSpeak. All rights reserved.
 All Classes Namespaces Files Functions Variables Pages
TeamSpeak3_Helper_Uri Class Reference

Helper class for URI handling. More...

Public Member Functions

 __construct ($uri)
 The TeamSpeak3_Helper_Uri constructor.
 
 isValid ()
 Validate the current URI from the instance variables.
 
 hasScheme ()
 Returns TRUE if the URI has a scheme.
 
 getScheme ($default=null)
 Returns the scheme.
 
 checkUser ($username=null)
 Returns TRUE if the username is valid.
 
 hasUser ()
 Returns TRUE if the URI has a username.
 
 getUser ($default=null)
 Returns the username.
 
 checkPass ($password=null)
 Returns TRUE if the password is valid.
 
 hasPass ()
 Returns TRUE if the URI has a password.
 
 getPass ($default=null)
 Returns the password.
 
 checkHost ($host=null)
 Returns TRUE if the host is valid.
 
 hasHost ()
 Returns TRUE if the URI has a host.
 
 getHost ($default=null)
 Returns the host.
 
 checkPort ($port=null)
 Returns TRUE if the port is valid.
 
 hasPort ()
 Returns TRUE if the URI has a port.
 
 getPort ($default=null)
 Returns the port.
 
 checkPath ($path=null)
 Returns TRUE if the path is valid.
 
 hasPath ()
 Returns TRUE if the URI has a path.
 
 getPath ($default=null)
 Returns the path.
 
 checkQuery ($query=null)
 Returns TRUE if the query string is valid.
 
 hasQuery ()
 Returns TRUE if the URI has a query string.
 
 getQuery ($default=array())
 Returns an array containing the query string elements.
 
 hasQueryVar ($key)
 Returns TRUE if the URI has a query variable.
 
 getQueryVar ($key, $default=null)
 Returns a single variable from the query string.
 
 checkFragment ($fragment=null)
 Returns TRUE if the fragment string is valid.
 
 hasFragment ()
 Returns TRUE if the URI has a fragment string.
 
 getFragment ($default=null)
 Returns the fragment.
 

Static Public Member Functions

static check ($uri)
 Returns TRUE if a given URI is valid.
 
static getUserParam ($key, $default=null)
 Returns a specified instance parameter from the $_REQUEST array.
 
static getHostParam ($key, $default=null)
 Returns a specified environment parameter from the $_SERVER array.
 
static getSessParam ($key, $default=null)
 Returns a specified session parameter from the $_SESSION array.
 
static getFQDNParts ($hostname)
 Returns an array containing the three main parts of a FQDN (Fully Qualified Domain Name), including the top-level domain, the second-level domains or hostname and the third-level domain.
 
static getHostUri ()
 Returns the applications host address.
 
static getBaseUri ()
 Returns the applications base address.
 

Protected Member Functions

 parseUri ($uriString= '')
 Parses the scheme-specific portion of the URI and place its parts into instance variables.
 

Static Protected Member Functions

static stripslashesRecursive ($var)
 Strips slashes from each element of an array using stripslashes().
 

Protected Attributes

 $scheme = null
 
 $user = null
 
 $pass = null
 
 $host = null
 
 $port = null
 
 $path = null
 
 $query = null
 
 $fragment = null
 
 $regex = array()
 

Detailed Description

Helper class for URI handling.

Definition at line 32 of file Uri.php.

Constructor & Destructor Documentation

TeamSpeak3_Helper_Uri::__construct (   $uri)

The TeamSpeak3_Helper_Uri constructor.

Parameters
string$uri
Exceptions
TeamSpeak3_Helper_Exception
Returns
TeamSpeak3_Helper_Uri

Definition at line 104 of file Uri.php.

References isValid(), and parseUri().

{
$uri = explode(":", strval($uri), 2);
$this->scheme = strtolower($uri[0]);
$uriString = isset($uri[1]) ? $uri[1] : "";
if(!ctype_alnum($this->scheme))
{
throw new TeamSpeak3_Helper_Exception("invalid URI scheme '" . $this->scheme . "' supplied");
}
/* grammar rules for validation */
$this->regex["alphanum"] = "[^\W_]";
$this->regex["escaped"] = "(?:%[\da-fA-F]{2})";
$this->regex["mark"] = "[-_.!~*'()\[\]]";
$this->regex["reserved"] = "[;\/?:@&=+$,]";
$this->regex["unreserved"] = "(?:" . $this->regex["alphanum"] . "|" . $this->regex["mark"] . ")";
$this->regex["segment"] = "(?:(?:" . $this->regex["unreserved"] . "|" . $this->regex["escaped"] . "|[:@&=+$,;])*)";
$this->regex["path"] = "(?:\/" . $this->regex["segment"] . "?)+";
$this->regex["uric"] = "(?:" . $this->regex["reserved"] . "|" . $this->regex["unreserved"] . "|" . $this->regex["escaped"] . ")";
if(strlen($uriString) > 0)
{
$this->parseUri($uriString);
}
if(!$this->isValid())
{
throw new TeamSpeak3_Helper_Exception("invalid URI supplied");
}
}

Member Function Documentation

TeamSpeak3_Helper_Uri::parseUri (   $uriString = '')
protected

Parses the scheme-specific portion of the URI and place its parts into instance variables.

Exceptions
TeamSpeak3_Helper_Exception
Returns
void

Definition at line 143 of file Uri.php.

Referenced by __construct().

{
$status = @preg_match("~^((//)([^/?#]*))([^?#]*)(\?([^#]*))?(#(.*))?$~", $uriString, $matches);
if($status === FALSE)
{
throw new TeamSpeak3_Helper_Exception("URI scheme-specific decomposition failed");
}
if(!$status) return;
$this->path = (isset($matches[4])) ? $matches[4] : '';
$this->query = (isset($matches[6])) ? $matches[6] : '';
$this->fragment = (isset($matches[8])) ? $matches[8] : '';
$status = @preg_match("~^(([^:@]*)(:([^@]*))?@)?([^:]+)(:(.*))?$~", (isset($matches[3])) ? $matches[3] : "", $matches);
if($status === FALSE)
{
throw new TeamSpeak3_Helper_Exception("URI scheme-specific authority decomposition failed");
}
if(!$status) return;
$this->user = isset($matches[2]) ? $matches[2] : "";
$this->pass = isset($matches[4]) ? $matches[4] : "";
$this->host = isset($matches[5]) ? $matches[5] : "";
$this->port = isset($matches[7]) ? $matches[7] : "";
}
TeamSpeak3_Helper_Uri::isValid ( )

Validate the current URI from the instance variables.

Returns
boolean

Definition at line 178 of file Uri.php.

References checkFragment(), checkHost(), checkPass(), checkPath(), checkPort(), checkQuery(), and checkUser().

Referenced by __construct().

{
return ($this->checkUser() && $this->checkPass() && $this->checkHost() && $this->checkPort() && $this->checkPath() && $this->checkQuery() && $this->checkFragment());
}
static TeamSpeak3_Helper_Uri::check (   $uri)
static

Returns TRUE if a given URI is valid.

Parameters
string$uri
Returns
boolean

Definition at line 189 of file Uri.php.

{
try
{
$uri = new self(strval($uri));
}
catch(Exception $e)
{
return FALSE;
}
return $uri->valid();
}
TeamSpeak3_Helper_Uri::hasScheme ( )

Returns TRUE if the URI has a scheme.

Returns
boolean

Definition at line 208 of file Uri.php.

Referenced by getScheme().

{
return strlen($this->scheme) ? TRUE : FALSE;
}
TeamSpeak3_Helper_Uri::getScheme (   $default = null)

Returns the scheme.

Parameters
mixeddefault
Returns
TeamSpeak3_Helper_String

Definition at line 219 of file Uri.php.

References hasScheme().

{
return ($this->hasScheme()) ? new TeamSpeak3_Helper_String($this->scheme) : $default;
}
TeamSpeak3_Helper_Uri::checkUser (   $username = null)

Returns TRUE if the username is valid.

Parameters
string$username
Exceptions
TeamSpeak3_Helper_Exception
Returns
boolean

Definition at line 231 of file Uri.php.

Referenced by isValid().

{
if($username === null)
{
$username = $this->user;
}
if(strlen($username) == 0)
{
return TRUE;
}
$pattern = "/^(" . $this->regex["alphanum"] . "|" . $this->regex["mark"] . "|" . $this->regex["escaped"] . "|[;:&=+$,])+$/";
$status = @preg_match($pattern, $username);
if($status === FALSE)
{
throw new TeamSpeak3_Helper_Exception("URI username validation failed");
}
return ($status == 1);
}
TeamSpeak3_Helper_Uri::hasUser ( )

Returns TRUE if the URI has a username.

Returns
boolean

Definition at line 259 of file Uri.php.

Referenced by getUser().

{
return strlen($this->user) ? TRUE : FALSE;
}
TeamSpeak3_Helper_Uri::getUser (   $default = null)

Returns the username.

Parameters
mixeddefault
Returns
TeamSpeak3_Helper_String

Definition at line 270 of file Uri.php.

References hasUser().

{
return ($this->hasUser()) ? new TeamSpeak3_Helper_String($this->user) : $default;
}
TeamSpeak3_Helper_Uri::checkPass (   $password = null)

Returns TRUE if the password is valid.

Parameters
string$password
Exceptions
TeamSpeak3_Helper_Exception
Returns
boolean

Definition at line 282 of file Uri.php.

Referenced by isValid().

{
if($password === null) {
$password = $this->pass;
}
if(strlen($password) == 0)
{
return TRUE;
}
$pattern = "/^(" . $this->regex["alphanum"] . "|" . $this->regex["mark"] . "|" . $this->regex["escaped"] . "|[;:&=+$,])+$/";
$status = @preg_match($pattern, $password);
if($status === FALSE)
{
throw new TeamSpeak3_Helper_Exception("URI password validation failed");
}
return ($status == 1);
}
TeamSpeak3_Helper_Uri::hasPass ( )

Returns TRUE if the URI has a password.

Returns
boolean

Definition at line 309 of file Uri.php.

Referenced by getPass().

{
return strlen($this->pass) ? TRUE : FALSE;
}
TeamSpeak3_Helper_Uri::getPass (   $default = null)

Returns the password.

Parameters
mixeddefault
Returns
TeamSpeak3_Helper_String

Definition at line 320 of file Uri.php.

References hasPass().

{
return ($this->hasPass()) ? new TeamSpeak3_Helper_String($this->pass) : $default;
}
TeamSpeak3_Helper_Uri::checkHost (   $host = null)

Returns TRUE if the host is valid.

Parameters
string$host
Returns
boolean

Definition at line 331 of file Uri.php.

Referenced by isValid().

{
if($host === null)
{
$host = $this->host;
}
return TRUE;
}
TeamSpeak3_Helper_Uri::hasHost ( )

Returns TRUE if the URI has a host.

Returns
boolean

Definition at line 346 of file Uri.php.

Referenced by getHost().

{
return strlen($this->host) ? TRUE : FALSE;
}
TeamSpeak3_Helper_Uri::getHost (   $default = null)

Returns the host.

Parameters
mixeddefault
Returns
TeamSpeak3_Helper_String

Definition at line 357 of file Uri.php.

References hasHost().

{
return ($this->hasHost()) ? new TeamSpeak3_Helper_String($this->host) : $default;
}
TeamSpeak3_Helper_Uri::checkPort (   $port = null)

Returns TRUE if the port is valid.

Parameters
integer$port
Returns
boolean

Definition at line 368 of file Uri.php.

Referenced by isValid().

{
if($port === null)
{
$port = $this->port;
}
return TRUE;
}
TeamSpeak3_Helper_Uri::hasPort ( )

Returns TRUE if the URI has a port.

Returns
boolean

Definition at line 383 of file Uri.php.

Referenced by getPort().

{
return strlen($this->port) ? TRUE : FALSE;
}
TeamSpeak3_Helper_Uri::getPort (   $default = null)

Returns the port.

Parameters
mixeddefault
Returns
integer

Definition at line 394 of file Uri.php.

References hasPort().

{
return ($this->hasPort()) ? intval($this->port) : $default;
}
TeamSpeak3_Helper_Uri::checkPath (   $path = null)

Returns TRUE if the path is valid.

Parameters
string$path
Exceptions
TeamSpeak3_Helper_Exception
Returns
boolean

Definition at line 406 of file Uri.php.

Referenced by isValid().

{
if($path === null)
{
$path = $this->path;
}
if(strlen($path) == 0)
{
return TRUE;
}
$pattern = "/^" . $this->regex["path"] . "$/";
$status = @preg_match($pattern, $path);
if($status === FALSE)
{
throw new TeamSpeak3_Helper_Exception("URI path validation failed");
}
return ($status == 1);
}
TeamSpeak3_Helper_Uri::hasPath ( )

Returns TRUE if the URI has a path.

Returns
boolean

Definition at line 434 of file Uri.php.

Referenced by getPath().

{
return strlen($this->path) ? TRUE : FALSE;
}
TeamSpeak3_Helper_Uri::getPath (   $default = null)

Returns the path.

Parameters
mixeddefault
Returns
TeamSpeak3_Helper_String

Definition at line 445 of file Uri.php.

References hasPath().

{
return ($this->hasPath()) ? new TeamSpeak3_Helper_String($this->path) : $default;
}
TeamSpeak3_Helper_Uri::checkQuery (   $query = null)

Returns TRUE if the query string is valid.

Parameters
string$query
Exceptions
TeamSpeak3_Helper_Exception
Returns
boolean

Definition at line 457 of file Uri.php.

Referenced by isValid().

{
if($query === null)
{
$query = $this->query;
}
if(strlen($query) == 0)
{
return TRUE;
}
$pattern = "/^" . $this->regex["uric"] . "*$/";
$status = @preg_match($pattern, $query);
if($status === FALSE)
{
throw new TeamSpeak3_Helper_Exception("URI query string validation failed");
}
return ($status == 1);
}
TeamSpeak3_Helper_Uri::hasQuery ( )

Returns TRUE if the URI has a query string.

Returns
boolean

Definition at line 485 of file Uri.php.

Referenced by getQuery(), getQueryVar(), and hasQueryVar().

{
return strlen($this->query) ? TRUE : FALSE;
}
TeamSpeak3_Helper_Uri::getQuery (   $default = array())

Returns an array containing the query string elements.

Parameters
mixed$default
Returns
array

Definition at line 496 of file Uri.php.

References hasQuery().

{
if(!$this->hasQuery())
{
return $default;
}
parse_str($this->query, $queryArray);
return $queryArray;
}
TeamSpeak3_Helper_Uri::hasQueryVar (   $key)

Returns TRUE if the URI has a query variable.

Returns
boolean

Definition at line 513 of file Uri.php.

References hasQuery().

{
if(!$this->hasQuery()) return FALSE;
parse_str($this->query, $queryArray);
return array_key_exists($key, $queryArray) ? TRUE : FALSE;
}
TeamSpeak3_Helper_Uri::getQueryVar (   $key,
  $default = null 
)

Returns a single variable from the query string.

Parameters
string$key
mixed$default
Returns
mixed

Definition at line 529 of file Uri.php.

References hasQuery().

{
if(!$this->hasQuery()) return $default;
parse_str($this->query, $queryArray);
if(array_key_exists($key, $queryArray))
{
$val = $queryArray[$key];
if(ctype_digit($val))
{
return intval($val);
}
elseif(is_string($val))
{
return new TeamSpeak3_Helper_String($val);
}
else
{
return $val;
}
}
return $default;
}
TeamSpeak3_Helper_Uri::checkFragment (   $fragment = null)

Returns TRUE if the fragment string is valid.

Parameters
string$fragment
Exceptions
TeamSpeak3_Helper_Exception
Returns
boolean

Definition at line 563 of file Uri.php.

Referenced by isValid().

{
if($fragment === null)
{
$fragment = $this->fragment;
}
if(strlen($fragment) == 0)
{
return TRUE;
}
$pattern = "/^" . $this->regex["uric"] . "*$/";
$status = @preg_match($pattern, $fragment);
if($status === FALSE)
{
throw new TeamSpeak3_Helper_Exception("URI fragment validation failed");
}
return ($status == 1);
}
TeamSpeak3_Helper_Uri::hasFragment ( )

Returns TRUE if the URI has a fragment string.

Returns
boolean

Definition at line 591 of file Uri.php.

Referenced by getFragment().

{
return strlen($this->fragment) ? TRUE : FALSE;
}
TeamSpeak3_Helper_Uri::getFragment (   $default = null)

Returns the fragment.

Parameters
mixeddefault
Returns
TeamSpeak3_Helper_String

Definition at line 602 of file Uri.php.

References hasFragment().

{
return ($this->hasFragment()) ? new TeamSpeak3_Helper_String($this->fragment) : $default;
}
static TeamSpeak3_Helper_Uri::getUserParam (   $key,
  $default = null 
)
static

Returns a specified instance parameter from the $_REQUEST array.

Parameters
string$key
mixed$default
Returns
mixed

Definition at line 614 of file Uri.php.

{
return (array_key_exists($key, $_REQUEST) && !empty($_REQUEST[$key])) ? self::stripslashesRecursive($_REQUEST[$key]) : $default;
}
static TeamSpeak3_Helper_Uri::getHostParam (   $key,
  $default = null 
)
static

Returns a specified environment parameter from the $_SERVER array.

Parameters
string$key
mixed$default
Returns
mixed

Definition at line 626 of file Uri.php.

{
return (array_key_exists($key, $_SERVER) && !empty($_SERVER[$key])) ? $_SERVER[$key] : $default;
}
static TeamSpeak3_Helper_Uri::getSessParam (   $key,
  $default = null 
)
static

Returns a specified session parameter from the $_SESSION array.

Parameters
string$key
mixed$default
Returns
mixed

Definition at line 638 of file Uri.php.

{
return (array_key_exists($key, $_SESSION) && !empty($_SESSION[$key])) ? $_SESSION[$key] : $default;
}
static TeamSpeak3_Helper_Uri::getFQDNParts (   $hostname)
static

Returns an array containing the three main parts of a FQDN (Fully Qualified Domain Name), including the top-level domain, the second-level domains or hostname and the third-level domain.

Parameters
string$hostname
Returns
array

Definition at line 650 of file Uri.php.

Referenced by TeamSpeak3_Node_Host\serverGetByTSDNS().

{
if(!preg_match("/^([a-z0-9][a-z0-9-]{0,62}\.)*([a-z0-9][a-z0-9-]{0,62}\.)+([a-z]{2,6})$/i", $hostname, $matches))
{
return array();
}
$parts["tld"] = $matches[3];
$parts["2nd"] = $matches[2];
$parts["3rd"] = $matches[1];
return $parts;
}
static TeamSpeak3_Helper_Uri::getHostUri ( )
static

Returns the applications host address.

Returns
TeamSpeak3_Helper_String

Definition at line 669 of file Uri.php.

{
$sheme = (self::getHostParam("HTTPS") == "on") ? "https" : "http";
$serverName = new TeamSpeak3_Helper_String(self::getHostParam("HTTP_HOST"));
$serverPort = self::getHostParam("SERVER_PORT");
$serverPort = ($serverPort != 80 && $serverPort != 443) ? ":" . $serverPort : "";
if($serverName->endsWith($serverPort))
{
$serverName = $serverName->replace($serverPort, "");
}
return new TeamSpeak3_Helper_String($sheme . "://" . $serverName . $serverPort);
}
static TeamSpeak3_Helper_Uri::getBaseUri ( )
static

Returns the applications base address.

Returns
string

Definition at line 690 of file Uri.php.

{
$scriptPath = new TeamSpeak3_Helper_String(dirname(self::getHostParam("SCRIPT_NAME")));
return self::getHostUri()->append(($scriptPath == DIRECTORY_SEPARATOR ? "" : $scriptPath) . "/");
}
static TeamSpeak3_Helper_Uri::stripslashesRecursive (   $var)
staticprotected

Strips slashes from each element of an array using stripslashes().

Parameters
mixed$var
Returns
mixed

Definition at line 703 of file Uri.php.

{
if(!is_array($var))
{
return stripslashes(strval($var));
}
foreach($var as $key => $val)
{
$var[$key] = (is_array($val)) ? stripslashesRecursive($val) : stripslashes(strval($val));
}
return $var;
}

The documentation for this class was generated from the following file: