TeamSpeak 3 PHP Framework
Modern use-at-will framework that provides individual components to manage TeamSpeak 3 Server instances
TeamSpeak3_Helper_Uri Class Reference

Helper class for URI handling. More...

Public Member Functions

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

Static Public Member Functions

static check ($uri)
 Returns TRUE if a given URI is valid. More...
 
static getBaseUri ()
 Returns the applications base address. More...
 
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. More...
 
static getHostParam ($key, $default=null)
 Returns a specified environment parameter from the $_SERVER array. More...
 
static getHostUri ()
 Returns the applications host address. More...
 
static getSessParam ($key, $default=null)
 Returns a specified session parameter from the $_SESSION array. More...
 
static getUserParam ($key, $default=null)
 Returns a specified instance parameter from the $_REQUEST array. More...
 

Protected Member Functions

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

Static Protected Member Functions

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

Detailed Description

Helper class for URI handling.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $uri)

The TeamSpeak3_Helper_Uri constructor.

Parameters
string$uri
Exceptions
TeamSpeak3_Helper_Exception
Returns
TeamSpeak3_Helper_Uri
102  {
103  $uri = explode(":", strval($uri), 2);
104 
105  $this->scheme = strtolower($uri[0]);
106  $uriString = isset($uri[1]) ? $uri[1] : "";
107 
108  if(!ctype_alnum($this->scheme))
109  {
110  throw new TeamSpeak3_Helper_Exception("invalid URI scheme '" . $this->scheme . "' supplied");
111  }
112 
113  /* grammar rules for validation */
114  $this->regex["alphanum"] = "[^\W_]";
115  $this->regex["escaped"] = "(?:%[\da-fA-F]{2})";
116  $this->regex["mark"] = "[-_.!~*'()\[\]]";
117  $this->regex["reserved"] = "[;\/?:@&=+$,]";
118  $this->regex["unreserved"] = "(?:" . $this->regex["alphanum"] . "|" . $this->regex["mark"] . ")";
119  $this->regex["segment"] = "(?:(?:" . $this->regex["unreserved"] . "|" . $this->regex["escaped"] . "|[:@&=+$,;])*)";
120  $this->regex["path"] = "(?:\/" . $this->regex["segment"] . "?)+";
121  $this->regex["uric"] = "(?:" . $this->regex["reserved"] . "|" . $this->regex["unreserved"] . "|" . $this->regex["escaped"] . ")";
122 
123  if(strlen($uriString) > 0)
124  {
125  $this->parseUri($uriString);
126  }
127 
128  if(!$this->isValid())
129  {
130  throw new TeamSpeak3_Helper_Exception("invalid URI supplied");
131  }
132  }
parseUri($uriString='')
Parses the scheme-specific portion of the URI and place its parts into instance variables.
Definition: Uri.php:140
Enhanced exception class for TeamSpeak3_Helper_* objects.
Definition: Exception.php:29
isValid()
Validate the current URI from the instance variables.
Definition: Uri.php:175

Member Function Documentation

◆ check()

static check (   $uri)
static

Returns TRUE if a given URI is valid.

Parameters
string$uri
Returns
boolean
187  {
188  try
189  {
190  $uri = new self(strval($uri));
191  }
192  catch(Exception $e)
193  {
194  return FALSE;
195  }
196 
197  return $uri->valid();
198  }

◆ checkFragment()

checkFragment (   $fragment = null)

Returns TRUE if the fragment string is valid.

Parameters
string$fragment
Exceptions
TeamSpeak3_Helper_Exception
Returns
boolean
561  {
562  if($fragment === null)
563  {
564  $fragment = $this->fragment;
565  }
566 
567  if(strlen($fragment) == 0)
568  {
569  return TRUE;
570  }
571 
572  $pattern = "/^" . $this->regex["uric"] . "*$/";
573  $status = @preg_match($pattern, $fragment);
574 
575  if($status === FALSE)
576  {
577  throw new TeamSpeak3_Helper_Exception("URI fragment validation failed");
578  }
579 
580  return ($status == 1);
581  }
Enhanced exception class for TeamSpeak3_Helper_* objects.
Definition: Exception.php:29

◆ checkHost()

checkHost (   $host = null)

Returns TRUE if the host is valid.

Parameters
string$host
Returns
boolean
329  {
330  if($host === null)
331  {
332  $host = $this->host;
333  }
334 
335  return TRUE;
336  }

◆ checkPass()

checkPass (   $password = null)

Returns TRUE if the password is valid.

Parameters
string$password
Exceptions
TeamSpeak3_Helper_Exception
Returns
boolean
280  {
281  if($password === null) {
282  $password = $this->pass;
283  }
284 
285  if(strlen($password) == 0)
286  {
287  return TRUE;
288  }
289 
290  $pattern = "/^(" . $this->regex["alphanum"] . "|" . $this->regex["mark"] . "|" . $this->regex["escaped"] . "|[;:&=+$,])+$/";
291  $status = @preg_match($pattern, $password);
292 
293  if($status === FALSE)
294  {
295  throw new TeamSpeak3_Helper_Exception("URI password validation failed");
296  }
297 
298  return ($status == 1);
299  }
Enhanced exception class for TeamSpeak3_Helper_* objects.
Definition: Exception.php:29

◆ checkPath()

checkPath (   $path = null)

Returns TRUE if the path is valid.

Parameters
string$path
Exceptions
TeamSpeak3_Helper_Exception
Returns
boolean
404  {
405  if($path === null)
406  {
407  $path = $this->path;
408  }
409 
410  if(strlen($path) == 0)
411  {
412  return TRUE;
413  }
414 
415  $pattern = "/^" . $this->regex["path"] . "$/";
416  $status = @preg_match($pattern, $path);
417 
418  if($status === FALSE)
419  {
420  throw new TeamSpeak3_Helper_Exception("URI path validation failed");
421  }
422 
423  return ($status == 1);
424  }
Enhanced exception class for TeamSpeak3_Helper_* objects.
Definition: Exception.php:29

◆ checkPort()

checkPort (   $port = null)

Returns TRUE if the port is valid.

Parameters
integer$port
Returns
boolean
366  {
367  if($port === null)
368  {
369  $port = $this->port;
370  }
371 
372  return TRUE;
373  }

◆ checkQuery()

checkQuery (   $query = null)

Returns TRUE if the query string is valid.

Parameters
string$query
Exceptions
TeamSpeak3_Helper_Exception
Returns
boolean
455  {
456  if($query === null)
457  {
458  $query = $this->query;
459  }
460 
461  if(strlen($query) == 0)
462  {
463  return TRUE;
464  }
465 
466  $pattern = "/^" . $this->regex["uric"] . "*$/";
467  $status = @preg_match($pattern, $query);
468 
469  if($status === FALSE)
470  {
471  throw new TeamSpeak3_Helper_Exception("URI query string validation failed");
472  }
473 
474  return ($status == 1);
475  }
Enhanced exception class for TeamSpeak3_Helper_* objects.
Definition: Exception.php:29

◆ checkUser()

checkUser (   $username = null)

Returns TRUE if the username is valid.

Parameters
string$username
Exceptions
TeamSpeak3_Helper_Exception
Returns
boolean
229  {
230  if($username === null)
231  {
232  $username = $this->user;
233  }
234 
235  if(strlen($username) == 0)
236  {
237  return TRUE;
238  }
239 
240  $pattern = "/^(" . $this->regex["alphanum"] . "|" . $this->regex["mark"] . "|" . $this->regex["escaped"] . "|[;:&=+$,])+$/";
241  $status = @preg_match($pattern, $username);
242 
243  if($status === FALSE)
244  {
245  throw new TeamSpeak3_Helper_Exception("URI username validation failed");
246  }
247 
248  return ($status == 1);
249  }
Enhanced exception class for TeamSpeak3_Helper_* objects.
Definition: Exception.php:29

◆ getBaseUri()

static getBaseUri ( )
static

Returns the applications base address.

Returns
string
688  {
689  $scriptPath = new TeamSpeak3_Helper_String(dirname(self::getHostParam("SCRIPT_NAME")));
690 
691  return self::getHostUri()->append(($scriptPath == DIRECTORY_SEPARATOR ? "" : $scriptPath) . "/");
692  }
Helper class for string handling.
Definition: String.php:29

◆ getFQDNParts()

static 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
648  {
649  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))
650  {
651  return array();
652  }
653 
654  $parts["tld"] = $matches[3];
655  $parts["2nd"] = $matches[2];
656  $parts["3rd"] = $matches[1];
657 
658  return $parts;
659  }

◆ getFragment()

getFragment (   $default = null)

Returns the fragment.

Parameters
mixeddefault
Returns
TeamSpeak3_Helper_String
600  {
601  return ($this->hasFragment()) ? new TeamSpeak3_Helper_String(rawurldecode($this->fragment)) : $default;
602  }
Helper class for string handling.
Definition: String.php:29
hasFragment()
Returns TRUE if the URI has a fragment string.
Definition: Uri.php:588

◆ getHost()

getHost (   $default = null)

Returns the host.

Parameters
mixeddefault
Returns
TeamSpeak3_Helper_String
355  {
356  return ($this->hasHost()) ? new TeamSpeak3_Helper_String(rawurldecode($this->host)) : $default;
357  }
Helper class for string handling.
Definition: String.php:29
hasHost()
Returns TRUE if the URI has a host.
Definition: Uri.php:343

◆ getHostParam()

static getHostParam (   $key,
  $default = null 
)
static

Returns a specified environment parameter from the $_SERVER array.

Parameters
string$key
mixed$default
Returns
mixed
624  {
625  return (array_key_exists($key, $_SERVER) && !empty($_SERVER[$key])) ? $_SERVER[$key] : $default;
626  }

◆ getHostUri()

static getHostUri ( )
static

Returns the applications host address.

Returns
TeamSpeak3_Helper_String
667  {
668  $sheme = (self::getHostParam("HTTPS") == "on") ? "https" : "http";
669 
670  $serverName = new TeamSpeak3_Helper_String(self::getHostParam("HTTP_HOST"));
671  $serverPort = self::getHostParam("SERVER_PORT");
672  $serverPort = ($serverPort != 80 && $serverPort != 443) ? ":" . $serverPort : "";
673 
674  if($serverName->endsWith($serverPort))
675  {
676  $serverName = $serverName->replace($serverPort, "");
677  }
678 
679  return new TeamSpeak3_Helper_String($sheme . "://" . $serverName . $serverPort);
680  }
Helper class for string handling.
Definition: String.php:29

◆ getPass()

getPass (   $default = null)

Returns the password.

Parameters
mixeddefault
Returns
TeamSpeak3_Helper_String
318  {
319  return ($this->hasPass()) ? new TeamSpeak3_Helper_String(urldecode($this->pass)) : $default;
320  }
Helper class for string handling.
Definition: String.php:29
hasPass()
Returns TRUE if the URI has a password.
Definition: Uri.php:306

◆ getPath()

getPath (   $default = null)

Returns the path.

Parameters
mixeddefault
Returns
TeamSpeak3_Helper_String
443  {
444  return ($this->hasPath()) ? new TeamSpeak3_Helper_String(rawurldecode($this->path)) : $default;
445  }
Helper class for string handling.
Definition: String.php:29
hasPath()
Returns TRUE if the URI has a path.
Definition: Uri.php:431

◆ getPort()

getPort (   $default = null)

Returns the port.

Parameters
mixeddefault
Returns
integer
392  {
393  return ($this->hasPort()) ? intval($this->port) : $default;
394  }
hasPort()
Returns TRUE if the URI has a port.
Definition: Uri.php:380

◆ getQuery()

getQuery (   $default = array())

Returns an array containing the query string elements.

Parameters
mixed$default
Returns
array
494  {
495  if(!$this->hasQuery())
496  {
497  return $default;
498  }
499 
500  parse_str(rawurldecode($this->query), $queryArray);
501 
502  return $queryArray;
503  }
hasQuery()
Returns TRUE if the URI has a query string.
Definition: Uri.php:482

◆ getQueryVar()

getQueryVar (   $key,
  $default = null 
)

Returns a single variable from the query string.

Parameters
string$key
mixed$default
Returns
mixed
527  {
528  if(!$this->hasQuery()) return $default;
529 
530  parse_str(rawurldecode($this->query), $queryArray);
531 
532  if(array_key_exists($key, $queryArray))
533  {
534  $val = $queryArray[$key];
535 
536  if(ctype_digit($val))
537  {
538  return intval($val);
539  }
540  elseif(is_string($val))
541  {
542  return new TeamSpeak3_Helper_String($val);
543  }
544  else
545  {
546  return $val;
547  }
548  }
549 
550  return $default;
551  }
hasQuery()
Returns TRUE if the URI has a query string.
Definition: Uri.php:482
Helper class for string handling.
Definition: String.php:29

◆ getScheme()

getScheme (   $default = null)

Returns the scheme.

Parameters
mixeddefault
Returns
TeamSpeak3_Helper_String
217  {
218  return ($this->hasScheme()) ? new TeamSpeak3_Helper_String($this->scheme) : $default;
219  }
hasScheme()
Returns TRUE if the URI has a scheme.
Definition: Uri.php:205
Helper class for string handling.
Definition: String.php:29

◆ getSessParam()

static getSessParam (   $key,
  $default = null 
)
static

Returns a specified session parameter from the $_SESSION array.

Parameters
string$key
mixed$default
Returns
mixed
636  {
637  return (array_key_exists($key, $_SESSION) && !empty($_SESSION[$key])) ? $_SESSION[$key] : $default;
638  }

◆ getUser()

getUser (   $default = null)

Returns the username.

Parameters
mixeddefault
Returns
TeamSpeak3_Helper_String
268  {
269  return ($this->hasUser()) ? new TeamSpeak3_Helper_String(urldecode($this->user)) : $default;
270  }
Helper class for string handling.
Definition: String.php:29
hasUser()
Returns TRUE if the URI has a username.
Definition: Uri.php:256

◆ getUserParam()

static getUserParam (   $key,
  $default = null 
)
static

Returns a specified instance parameter from the $_REQUEST array.

Parameters
string$key
mixed$default
Returns
mixed
612  {
613  return (array_key_exists($key, $_REQUEST) && !empty($_REQUEST[$key])) ? self::stripslashesRecursive($_REQUEST[$key]) : $default;
614  }

◆ hasFragment()

hasFragment ( )

Returns TRUE if the URI has a fragment string.

Returns
boolean
589  {
590  return strlen($this->fragment) ? TRUE : FALSE;
591  }

◆ hasHost()

hasHost ( )

Returns TRUE if the URI has a host.

Returns
boolean
344  {
345  return strlen($this->host) ? TRUE : FALSE;
346  }

◆ hasPass()

hasPass ( )

Returns TRUE if the URI has a password.

Returns
boolean
307  {
308  return strlen($this->pass) ? TRUE : FALSE;
309  }

◆ hasPath()

hasPath ( )

Returns TRUE if the URI has a path.

Returns
boolean
432  {
433  return strlen($this->path) ? TRUE : FALSE;
434  }

◆ hasPort()

hasPort ( )

Returns TRUE if the URI has a port.

Returns
boolean
381  {
382  return strlen($this->port) ? TRUE : FALSE;
383  }

◆ hasQuery()

hasQuery ( )

Returns TRUE if the URI has a query string.

Returns
boolean
483  {
484  return strlen($this->query) ? TRUE : FALSE;
485  }

◆ hasQueryVar()

hasQueryVar (   $key)

Returns TRUE if the URI has a query variable.

Returns
boolean
511  {
512  if(!$this->hasQuery()) return FALSE;
513 
514  parse_str($this->query, $queryArray);
515 
516  return array_key_exists($key, $queryArray) ? TRUE : FALSE;
517  }
hasQuery()
Returns TRUE if the URI has a query string.
Definition: Uri.php:482

◆ hasScheme()

hasScheme ( )

Returns TRUE if the URI has a scheme.

Returns
boolean
206  {
207  return strlen($this->scheme) ? TRUE : FALSE;
208  }

◆ hasUser()

hasUser ( )

Returns TRUE if the URI has a username.

Returns
boolean
257  {
258  return strlen($this->user) ? TRUE : FALSE;
259  }

◆ isValid()

isValid ( )

Validate the current URI from the instance variables.

Returns
boolean
176  {
177  return ($this->checkUser() && $this->checkPass() && $this->checkHost() && $this->checkPort() && $this->checkPath() && $this->checkQuery() && $this->checkFragment());
178  }
checkPass($password=null)
Returns TRUE if the password is valid.
Definition: Uri.php:279
checkPort($port=null)
Returns TRUE if the port is valid.
Definition: Uri.php:365
checkHost($host=null)
Returns TRUE if the host is valid.
Definition: Uri.php:328
checkUser($username=null)
Returns TRUE if the username is valid.
Definition: Uri.php:228
checkFragment($fragment=null)
Returns TRUE if the fragment string is valid.
Definition: Uri.php:560
checkQuery($query=null)
Returns TRUE if the query string is valid.
Definition: Uri.php:454
checkPath($path=null)
Returns TRUE if the path is valid.
Definition: Uri.php:403

◆ parseUri()

parseUri (   $uriString = '')
protected

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

Exceptions
TeamSpeak3_Helper_Exception
Returns
void
141  {
142  $status = @preg_match("~^((//)([^/?#]*))([^?#]*)(\?([^#]*))?(#(.*))?$~", $uriString, $matches);
143 
144  if($status === FALSE)
145  {
146  throw new TeamSpeak3_Helper_Exception("URI scheme-specific decomposition failed");
147  }
148 
149  if(!$status) return;
150 
151  $this->path = (isset($matches[4])) ? $matches[4] : '';
152  $this->query = (isset($matches[6])) ? $matches[6] : '';
153  $this->fragment = (isset($matches[8])) ? $matches[8] : '';
154 
155  $status = @preg_match("~^(([^:@]*)(:([^@]*))[email protected])?((?(?=[[])[[][^]]+[]]|[^:]+))(:(.*))?$~", (isset($matches[3])) ? $matches[3] : "", $matches);
156 
157  if($status === FALSE)
158  {
159  throw new TeamSpeak3_Helper_Exception("URI scheme-specific authority decomposition failed");
160  }
161 
162  if(!$status) return;
163 
164  $this->user = isset($matches[2]) ? $matches[2] : "";
165  $this->pass = isset($matches[4]) ? $matches[4] : "";
166  $this->host = isset($matches[5]) === TRUE ? preg_replace('~^\[([^]]+)\]$~', '\1', $matches[5]) : "";
167  $this->port = isset($matches[7]) ? $matches[7] : "";
168  }
Enhanced exception class for TeamSpeak3_Helper_* objects.
Definition: Exception.php:29

◆ stripslashesRecursive()

static stripslashesRecursive (   $var)
staticprotected

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

Parameters
mixed$var
Returns
mixed
701  {
702  if(!is_array($var))
703  {
704  return stripslashes(strval($var));
705  }
706 
707  foreach($var as $key => $val)
708  {
709  $var[$key] = (is_array($val)) ? stripslashesRecursive($val) : stripslashes(strval($val));
710  }
711 
712  return $var;
713  }
static stripslashesRecursive($var)
Strips slashes from each element of an array using stripslashes().
Definition: Uri.php:700

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