TeamSpeak 3 PHP Framework  1.1.23
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. More...
 
 isValid ()
 Validate the current URI from the instance variables. More...
 
 hasScheme ()
 Returns TRUE if the URI has a scheme. More...
 
 getScheme ($default=null)
 Returns the scheme. More...
 
 checkUser ($username=null)
 Returns TRUE if the username is valid. More...
 
 hasUser ()
 Returns TRUE if the URI has a username. More...
 
 getUser ($default=null)
 Returns the username. More...
 
 checkPass ($password=null)
 Returns TRUE if the password is valid. More...
 
 hasPass ()
 Returns TRUE if the URI has a password. More...
 
 getPass ($default=null)
 Returns the password. More...
 
 checkHost ($host=null)
 Returns TRUE if the host is valid. More...
 
 hasHost ()
 Returns TRUE if the URI has a host. More...
 
 getHost ($default=null)
 Returns the host. More...
 
 checkPort ($port=null)
 Returns TRUE if the port is valid. More...
 
 hasPort ()
 Returns TRUE if the URI has a port. More...
 
 getPort ($default=null)
 Returns the port. More...
 
 checkPath ($path=null)
 Returns TRUE if the path is valid. More...
 
 hasPath ()
 Returns TRUE if the URI has a path. More...
 
 getPath ($default=null)
 Returns the path. More...
 
 checkQuery ($query=null)
 Returns TRUE if the query string is valid. More...
 
 hasQuery ()
 Returns TRUE if the URI has a query string. More...
 
 getQuery ($default=array())
 Returns an array containing the query string elements. More...
 
 hasQueryVar ($key)
 Returns TRUE if the URI has a query variable. More...
 
 getQueryVar ($key, $default=null)
 Returns a single variable from the query string. More...
 
 checkFragment ($fragment=null)
 Returns TRUE if the fragment string is valid. More...
 
 hasFragment ()
 Returns TRUE if the URI has a fragment string. More...
 
 getFragment ($default=null)
 Returns the fragment. More...
 

Static Public Member Functions

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

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().

105  {
106  $uri = explode(":", strval($uri), 2);
107 
108  $this->scheme = strtolower($uri[0]);
109  $uriString = isset($uri[1]) ? $uri[1] : "";
110 
111  if(!ctype_alnum($this->scheme))
112  {
113  throw new TeamSpeak3_Helper_Exception("invalid URI scheme '" . $this->scheme . "' supplied");
114  }
115 
116  /* grammar rules for validation */
117  $this->regex["alphanum"] = "[^\W_]";
118  $this->regex["escaped"] = "(?:%[\da-fA-F]{2})";
119  $this->regex["mark"] = "[-_.!~*'()\[\]]";
120  $this->regex["reserved"] = "[;\/?:@&=+$,]";
121  $this->regex["unreserved"] = "(?:" . $this->regex["alphanum"] . "|" . $this->regex["mark"] . ")";
122  $this->regex["segment"] = "(?:(?:" . $this->regex["unreserved"] . "|" . $this->regex["escaped"] . "|[:@&=+$,;])*)";
123  $this->regex["path"] = "(?:\/" . $this->regex["segment"] . "?)+";
124  $this->regex["uric"] = "(?:" . $this->regex["reserved"] . "|" . $this->regex["unreserved"] . "|" . $this->regex["escaped"] . ")";
125 
126  if(strlen($uriString) > 0)
127  {
128  $this->parseUri($uriString);
129  }
130 
131  if(!$this->isValid())
132  {
133  throw new TeamSpeak3_Helper_Exception("invalid URI supplied");
134  }
135  }

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().

144  {
145  $status = @preg_match("~^((//)([^/?#]*))([^?#]*)(\?([^#]*))?(#(.*))?$~", $uriString, $matches);
146 
147  if($status === FALSE)
148  {
149  throw new TeamSpeak3_Helper_Exception("URI scheme-specific decomposition failed");
150  }
151 
152  if(!$status) return;
153 
154  $this->path = (isset($matches[4])) ? $matches[4] : '';
155  $this->query = (isset($matches[6])) ? $matches[6] : '';
156  $this->fragment = (isset($matches[8])) ? $matches[8] : '';
157 
158  $status = @preg_match("~^(([^:@]*)(:([^@]*))?@)?([^:]+)(:(.*))?$~", (isset($matches[3])) ? $matches[3] : "", $matches);
159 
160  if($status === FALSE)
161  {
162  throw new TeamSpeak3_Helper_Exception("URI scheme-specific authority decomposition failed");
163  }
164 
165  if(!$status) return;
166 
167  $this->user = isset($matches[2]) ? $matches[2] : "";
168  $this->pass = isset($matches[4]) ? $matches[4] : "";
169  $this->host = isset($matches[5]) ? $matches[5] : "";
170  $this->port = isset($matches[7]) ? $matches[7] : "";
171  }
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().

179  {
180  return ($this->checkUser() && $this->checkPass() && $this->checkHost() && $this->checkPort() && $this->checkPath() && $this->checkQuery() && $this->checkFragment());
181  }
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.

190  {
191  try
192  {
193  $uri = new self(strval($uri));
194  }
195  catch(Exception $e)
196  {
197  return FALSE;
198  }
199 
200  return $uri->valid();
201  }
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().

209  {
210  return strlen($this->scheme) ? TRUE : FALSE;
211  }
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().

220  {
221  return ($this->hasScheme()) ? new TeamSpeak3_Helper_String($this->scheme) : $default;
222  }
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().

232  {
233  if($username === null)
234  {
235  $username = $this->user;
236  }
237 
238  if(strlen($username) == 0)
239  {
240  return TRUE;
241  }
242 
243  $pattern = "/^(" . $this->regex["alphanum"] . "|" . $this->regex["mark"] . "|" . $this->regex["escaped"] . "|[;:&=+$,])+$/";
244  $status = @preg_match($pattern, $username);
245 
246  if($status === FALSE)
247  {
248  throw new TeamSpeak3_Helper_Exception("URI username validation failed");
249  }
250 
251  return ($status == 1);
252  }
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().

260  {
261  return strlen($this->user) ? TRUE : FALSE;
262  }
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().

271  {
272  return ($this->hasUser()) ? new TeamSpeak3_Helper_String($this->user) : $default;
273  }
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().

283  {
284  if($password === null) {
285  $password = $this->pass;
286  }
287 
288  if(strlen($password) == 0)
289  {
290  return TRUE;
291  }
292 
293  $pattern = "/^(" . $this->regex["alphanum"] . "|" . $this->regex["mark"] . "|" . $this->regex["escaped"] . "|[;:&=+$,])+$/";
294  $status = @preg_match($pattern, $password);
295 
296  if($status === FALSE)
297  {
298  throw new TeamSpeak3_Helper_Exception("URI password validation failed");
299  }
300 
301  return ($status == 1);
302  }
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().

310  {
311  return strlen($this->pass) ? TRUE : FALSE;
312  }
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().

321  {
322  return ($this->hasPass()) ? new TeamSpeak3_Helper_String($this->pass) : $default;
323  }
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().

332  {
333  if($host === null)
334  {
335  $host = $this->host;
336  }
337 
338  return TRUE;
339  }
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().

347  {
348  return strlen($this->host) ? TRUE : FALSE;
349  }
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().

358  {
359  return ($this->hasHost()) ? new TeamSpeak3_Helper_String($this->host) : $default;
360  }
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().

369  {
370  if($port === null)
371  {
372  $port = $this->port;
373  }
374 
375  return TRUE;
376  }
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().

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

Returns the port.

Parameters
mixeddefault
Returns
integer

Definition at line 394 of file Uri.php.

References hasPort().

395  {
396  return ($this->hasPort()) ? intval($this->port) : $default;
397  }
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().

407  {
408  if($path === null)
409  {
410  $path = $this->path;
411  }
412 
413  if(strlen($path) == 0)
414  {
415  return TRUE;
416  }
417 
418  $pattern = "/^" . $this->regex["path"] . "$/";
419  $status = @preg_match($pattern, $path);
420 
421  if($status === FALSE)
422  {
423  throw new TeamSpeak3_Helper_Exception("URI path validation failed");
424  }
425 
426  return ($status == 1);
427  }
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().

435  {
436  return strlen($this->path) ? TRUE : FALSE;
437  }
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().

446  {
447  return ($this->hasPath()) ? new TeamSpeak3_Helper_String($this->path) : $default;
448  }
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().

458  {
459  if($query === null)
460  {
461  $query = $this->query;
462  }
463 
464  if(strlen($query) == 0)
465  {
466  return TRUE;
467  }
468 
469  $pattern = "/^" . $this->regex["uric"] . "*$/";
470  $status = @preg_match($pattern, $query);
471 
472  if($status === FALSE)
473  {
474  throw new TeamSpeak3_Helper_Exception("URI query string validation failed");
475  }
476 
477  return ($status == 1);
478  }
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().

486  {
487  return strlen($this->query) ? TRUE : FALSE;
488  }
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().

497  {
498  if(!$this->hasQuery())
499  {
500  return $default;
501  }
502 
503  parse_str($this->query, $queryArray);
504 
505  return $queryArray;
506  }
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().

514  {
515  if(!$this->hasQuery()) return FALSE;
516 
517  parse_str($this->query, $queryArray);
518 
519  return array_key_exists($key, $queryArray) ? TRUE : FALSE;
520  }
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().

530  {
531  if(!$this->hasQuery()) return $default;
532 
533  parse_str($this->query, $queryArray);
534 
535  if(array_key_exists($key, $queryArray))
536  {
537  $val = $queryArray[$key];
538 
539  if(ctype_digit($val))
540  {
541  return intval($val);
542  }
543  elseif(is_string($val))
544  {
545  return new TeamSpeak3_Helper_String($val);
546  }
547  else
548  {
549  return $val;
550  }
551  }
552 
553  return $default;
554  }
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().

564  {
565  if($fragment === null)
566  {
567  $fragment = $this->fragment;
568  }
569 
570  if(strlen($fragment) == 0)
571  {
572  return TRUE;
573  }
574 
575  $pattern = "/^" . $this->regex["uric"] . "*$/";
576  $status = @preg_match($pattern, $fragment);
577 
578  if($status === FALSE)
579  {
580  throw new TeamSpeak3_Helper_Exception("URI fragment validation failed");
581  }
582 
583  return ($status == 1);
584  }
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().

592  {
593  return strlen($this->fragment) ? TRUE : FALSE;
594  }
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().

603  {
604  return ($this->hasFragment()) ? new TeamSpeak3_Helper_String($this->fragment) : $default;
605  }
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.

615  {
616  return (array_key_exists($key, $_REQUEST) && !empty($_REQUEST[$key])) ? self::stripslashesRecursive($_REQUEST[$key]) : $default;
617  }
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.

627  {
628  return (array_key_exists($key, $_SERVER) && !empty($_SERVER[$key])) ? $_SERVER[$key] : $default;
629  }
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.

639  {
640  return (array_key_exists($key, $_SESSION) && !empty($_SESSION[$key])) ? $_SESSION[$key] : $default;
641  }
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().

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

Returns the applications host address.

Returns
TeamSpeak3_Helper_String

Definition at line 669 of file Uri.php.

670  {
671  $sheme = (self::getHostParam("HTTPS") == "on") ? "https" : "http";
672 
673  $serverName = new TeamSpeak3_Helper_String(self::getHostParam("HTTP_HOST"));
674  $serverPort = self::getHostParam("SERVER_PORT");
675  $serverPort = ($serverPort != 80 && $serverPort != 443) ? ":" . $serverPort : "";
676 
677  if($serverName->endsWith($serverPort))
678  {
679  $serverName = $serverName->replace($serverPort, "");
680  }
681 
682  return new TeamSpeak3_Helper_String($sheme . "://" . $serverName . $serverPort);
683  }
static TeamSpeak3_Helper_Uri::getBaseUri ( )
static

Returns the applications base address.

Returns
string

Definition at line 690 of file Uri.php.

691  {
692  $scriptPath = new TeamSpeak3_Helper_String(dirname(self::getHostParam("SCRIPT_NAME")));
693 
694  return self::getHostUri()->append(($scriptPath == DIRECTORY_SEPARATOR ? "" : $scriptPath) . "/");
695  }
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.

704  {
705  if(!is_array($var))
706  {
707  return stripslashes(strval($var));
708  }
709 
710  foreach($var as $key => $val)
711  {
712  $var[$key] = (is_array($val)) ? stripslashesRecursive($val) : stripslashes(strval($val));
713  }
714 
715  return $var;
716  }

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