TeamSpeak 3 PHP Framework
1.1.12
|
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() |
TeamSpeak3_Helper_Uri::__construct | ( | $ | uri | ) |
The TeamSpeak3_Helper_Uri constructor.
string | $uri |
TeamSpeak3_Helper_Exception |
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"); } }
TeamSpeak3_Helper_Uri::parseUri | ( | $ | uriString = '' | ) | [protected] |
Parses the scheme-specific portion of the URI and place its parts into instance variables.
TeamSpeak3_Helper_Exception |
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] : ""; }
Validate the current URI from the instance variables.
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 the URI has a scheme.
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.
mixed | default |
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.
string | $username |
TeamSpeak3_Helper_Exception |
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::getUser | ( | $ | default = null | ) |
Returns the username.
mixed | default |
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.
string | $password |
TeamSpeak3_Helper_Exception |
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::getPass | ( | $ | default = null | ) |
Returns the password.
mixed | default |
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 | ) |
TeamSpeak3_Helper_Uri::getHost | ( | $ | default = null | ) |
Returns the host.
mixed | default |
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 | ) |
TeamSpeak3_Helper_Uri::getPort | ( | $ | default = null | ) |
TeamSpeak3_Helper_Uri::checkPath | ( | $ | path = null | ) |
Returns TRUE if the path is valid.
string | $path |
TeamSpeak3_Helper_Exception |
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::getPath | ( | $ | default = null | ) |
Returns the path.
mixed | default |
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.
string | $query |
TeamSpeak3_Helper_Exception |
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); }
Returns TRUE if the URI has a query string.
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.
mixed | $default |
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.
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.
string | $key | |
mixed | $default |
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.
string | $fragment |
TeamSpeak3_Helper_Exception |
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); }
Returns TRUE if the URI has a fragment string.
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.
mixed | default |
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.
string | $key | |
mixed | $default |
Definition at line 614 of file Uri.php.
References stripslashesRecursive().
{ 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.
string | $key | |
mixed | $default |
Definition at line 626 of file Uri.php.
Referenced by getHostUri().
{
return (array_key_exists($key, $_SERVER) && !empty($_SERVER[$key])) ? $_SERVER[$key] : $default;
}
static TeamSpeak3_Helper_Uri::getSessParam | ( | $ | key, |
$ | default = null |
||
) | [static] |
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.
string | $hostname |
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.
Definition at line 669 of file Uri.php.
References getHostParam().
Referenced by getBaseUri().
{ $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.
Definition at line 690 of file Uri.php.
References getHostUri().
{ $scriptPath = new TeamSpeak3_Helper_String(dirname(self::getHostParam("SCRIPT_NAME"))); return self::getHostUri()->append(($scriptPath == DIRECTORY_SEPARATOR ? "" : $scriptPath) . "/"); }
static TeamSpeak3_Helper_Uri::stripslashesRecursive | ( | $ | var | ) | [static, protected] |
Strips slashes from each element of an array using stripslashes().
mixed | $var |
Definition at line 703 of file Uri.php.
Referenced by getUserParam().
{ 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; }