TeamSpeak 3 PHP Framework
1.1.12
|
00001 <?php 00002 00003 /** 00004 * @file 00005 * TeamSpeak 3 PHP Framework 00006 * 00007 * $Id: Uri.php 2/18/2012 12:42:46 scp@orilla $ 00008 * 00009 * This program is free software: you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation, either version 3 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 * @package TeamSpeak3 00023 * @version 1.1.12 00024 * @author Sven 'ScP' Paulsen 00025 * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved. 00026 */ 00027 00028 /** 00029 * @class TeamSpeak3_Helper_Uri 00030 * @brief Helper class for URI handling. 00031 */ 00032 class TeamSpeak3_Helper_Uri 00033 { 00034 /** 00035 * Stores the URI scheme. 00036 * 00037 * @var string 00038 */ 00039 protected $scheme = null; 00040 00041 /** 00042 * Stores the URI username 00043 * 00044 * @var string 00045 */ 00046 protected $user = null; 00047 00048 /** 00049 * Stores the URI password. 00050 * 00051 * @var string 00052 */ 00053 protected $pass = null; 00054 00055 /** 00056 * Stores the URI host. 00057 * 00058 * @var string 00059 */ 00060 protected $host = null; 00061 00062 /** 00063 * Stores the URI port. 00064 * 00065 * @var string 00066 */ 00067 protected $port = null; 00068 00069 /** 00070 * Stores the URI path. 00071 * 00072 * @var string 00073 */ 00074 protected $path = null; 00075 00076 /** 00077 * Stores the URI query string. 00078 * 00079 * @var string 00080 */ 00081 protected $query = null; 00082 00083 /** 00084 * Stores the URI fragment string. 00085 * 00086 * @var string 00087 */ 00088 protected $fragment = null; 00089 00090 /** 00091 * Stores grammar rules for validation via regex. 00092 * 00093 * @var array 00094 */ 00095 protected $regex = array(); 00096 00097 /** 00098 * The TeamSpeak3_Helper_Uri constructor. 00099 * 00100 * @param string $uri 00101 * @throws TeamSpeak3_Helper_Exception 00102 * @return TeamSpeak3_Helper_Uri 00103 */ 00104 public function __construct($uri) 00105 { 00106 $uri = explode(":", strval($uri), 2); 00107 00108 $this->scheme = strtolower($uri[0]); 00109 $uriString = isset($uri[1]) ? $uri[1] : ""; 00110 00111 if(!ctype_alnum($this->scheme)) 00112 { 00113 throw new TeamSpeak3_Helper_Exception("invalid URI scheme '" . $this->scheme . "' supplied"); 00114 } 00115 00116 /* grammar rules for validation */ 00117 $this->regex["alphanum"] = "[^\W_]"; 00118 $this->regex["escaped"] = "(?:%[\da-fA-F]{2})"; 00119 $this->regex["mark"] = "[-_.!~*'()\[\]]"; 00120 $this->regex["reserved"] = "[;\/?:@&=+$,]"; 00121 $this->regex["unreserved"] = "(?:" . $this->regex["alphanum"] . "|" . $this->regex["mark"] . ")"; 00122 $this->regex["segment"] = "(?:(?:" . $this->regex["unreserved"] . "|" . $this->regex["escaped"] . "|[:@&=+$,;])*)"; 00123 $this->regex["path"] = "(?:\/" . $this->regex["segment"] . "?)+"; 00124 $this->regex["uric"] = "(?:" . $this->regex["reserved"] . "|" . $this->regex["unreserved"] . "|" . $this->regex["escaped"] . ")"; 00125 00126 if(strlen($uriString) > 0) 00127 { 00128 $this->parseUri($uriString); 00129 } 00130 00131 if(!$this->isValid()) 00132 { 00133 throw new TeamSpeak3_Helper_Exception("invalid URI supplied"); 00134 } 00135 } 00136 00137 /** 00138 * Parses the scheme-specific portion of the URI and place its parts into instance variables. 00139 * 00140 * @throws TeamSpeak3_Helper_Exception 00141 * @return void 00142 */ 00143 protected function parseUri($uriString = '') 00144 { 00145 $status = @preg_match("~^((//)([^/?#]*))([^?#]*)(\?([^#]*))?(#(.*))?$~", $uriString, $matches); 00146 00147 if($status === FALSE) 00148 { 00149 throw new TeamSpeak3_Helper_Exception("URI scheme-specific decomposition failed"); 00150 } 00151 00152 if(!$status) return; 00153 00154 $this->path = (isset($matches[4])) ? $matches[4] : ''; 00155 $this->query = (isset($matches[6])) ? $matches[6] : ''; 00156 $this->fragment = (isset($matches[8])) ? $matches[8] : ''; 00157 00158 $status = @preg_match("~^(([^:@]*)(:([^@]*))?@)?([^:]+)(:(.*))?$~", (isset($matches[3])) ? $matches[3] : "", $matches); 00159 00160 if($status === FALSE) 00161 { 00162 throw new TeamSpeak3_Helper_Exception("URI scheme-specific authority decomposition failed"); 00163 } 00164 00165 if(!$status) return; 00166 00167 $this->user = isset($matches[2]) ? $matches[2] : ""; 00168 $this->pass = isset($matches[4]) ? $matches[4] : ""; 00169 $this->host = isset($matches[5]) ? $matches[5] : ""; 00170 $this->port = isset($matches[7]) ? $matches[7] : ""; 00171 } 00172 00173 /** 00174 * Validate the current URI from the instance variables. 00175 * 00176 * @return boolean 00177 */ 00178 public function isValid() 00179 { 00180 return ($this->checkUser() && $this->checkPass() && $this->checkHost() && $this->checkPort() && $this->checkPath() && $this->checkQuery() && $this->checkFragment()); 00181 } 00182 00183 /** 00184 * Returns TRUE if a given URI is valid. 00185 * 00186 * @param string $uri 00187 * @return boolean 00188 */ 00189 public static function check($uri) 00190 { 00191 try 00192 { 00193 $uri = new self(strval($uri)); 00194 } 00195 catch(Exception $e) 00196 { 00197 return FALSE; 00198 } 00199 00200 return $uri->valid(); 00201 } 00202 00203 /** 00204 * Returns TRUE if the URI has a scheme. 00205 * 00206 * @return boolean 00207 */ 00208 public function hasScheme() 00209 { 00210 return strlen($this->scheme) ? TRUE : FALSE; 00211 } 00212 00213 /** 00214 * Returns the scheme. 00215 * 00216 * @param mixed default 00217 * @return TeamSpeak3_Helper_String 00218 */ 00219 public function getScheme($default = null) 00220 { 00221 return ($this->hasScheme()) ? new TeamSpeak3_Helper_String($this->scheme) : $default; 00222 } 00223 00224 /** 00225 * Returns TRUE if the username is valid. 00226 * 00227 * @param string $username 00228 * @throws TeamSpeak3_Helper_Exception 00229 * @return boolean 00230 */ 00231 public function checkUser($username = null) 00232 { 00233 if($username === null) 00234 { 00235 $username = $this->user; 00236 } 00237 00238 if(strlen($username) == 0) 00239 { 00240 return TRUE; 00241 } 00242 00243 $pattern = "/^(" . $this->regex["alphanum"] . "|" . $this->regex["mark"] . "|" . $this->regex["escaped"] . "|[;:&=+$,])+$/"; 00244 $status = @preg_match($pattern, $username); 00245 00246 if($status === FALSE) 00247 { 00248 throw new TeamSpeak3_Helper_Exception("URI username validation failed"); 00249 } 00250 00251 return ($status == 1); 00252 } 00253 00254 /** 00255 * Returns TRUE if the URI has a username. 00256 * 00257 * @return boolean 00258 */ 00259 public function hasUser() 00260 { 00261 return strlen($this->user) ? TRUE : FALSE; 00262 } 00263 00264 /** 00265 * Returns the username. 00266 * 00267 * @param mixed default 00268 * @return TeamSpeak3_Helper_String 00269 */ 00270 public function getUser($default = null) 00271 { 00272 return ($this->hasUser()) ? new TeamSpeak3_Helper_String($this->user) : $default; 00273 } 00274 00275 /** 00276 * Returns TRUE if the password is valid. 00277 * 00278 * @param string $password 00279 * @throws TeamSpeak3_Helper_Exception 00280 * @return boolean 00281 */ 00282 public function checkPass($password = null) 00283 { 00284 if($password === null) { 00285 $password = $this->pass; 00286 } 00287 00288 if(strlen($password) == 0) 00289 { 00290 return TRUE; 00291 } 00292 00293 $pattern = "/^(" . $this->regex["alphanum"] . "|" . $this->regex["mark"] . "|" . $this->regex["escaped"] . "|[;:&=+$,])+$/"; 00294 $status = @preg_match($pattern, $password); 00295 00296 if($status === FALSE) 00297 { 00298 throw new TeamSpeak3_Helper_Exception("URI password validation failed"); 00299 } 00300 00301 return ($status == 1); 00302 } 00303 00304 /** 00305 * Returns TRUE if the URI has a password. 00306 * 00307 * @return boolean 00308 */ 00309 public function hasPass() 00310 { 00311 return strlen($this->pass) ? TRUE : FALSE; 00312 } 00313 00314 /** 00315 * Returns the password. 00316 * 00317 * @param mixed default 00318 * @return TeamSpeak3_Helper_String 00319 */ 00320 public function getPass($default = null) 00321 { 00322 return ($this->hasPass()) ? new TeamSpeak3_Helper_String($this->pass) : $default; 00323 } 00324 00325 /** 00326 * Returns TRUE if the host is valid. 00327 * 00328 * @param string $host 00329 * @return boolean 00330 */ 00331 public function checkHost($host = null) 00332 { 00333 if($host === null) 00334 { 00335 $host = $this->host; 00336 } 00337 00338 return TRUE; 00339 } 00340 00341 /** 00342 * Returns TRUE if the URI has a host. 00343 * 00344 * @return boolean 00345 */ 00346 public function hasHost() 00347 { 00348 return strlen($this->host) ? TRUE : FALSE; 00349 } 00350 00351 /** 00352 * Returns the host. 00353 * 00354 * @param mixed default 00355 * @return TeamSpeak3_Helper_String 00356 */ 00357 public function getHost($default = null) 00358 { 00359 return ($this->hasHost()) ? new TeamSpeak3_Helper_String($this->host) : $default; 00360 } 00361 00362 /** 00363 * Returns TRUE if the port is valid. 00364 * 00365 * @param integer $port 00366 * @return boolean 00367 */ 00368 public function checkPort($port = null) 00369 { 00370 if($port === null) 00371 { 00372 $port = $this->port; 00373 } 00374 00375 return TRUE; 00376 } 00377 00378 /** 00379 * Returns TRUE if the URI has a port. 00380 * 00381 * @return boolean 00382 */ 00383 public function hasPort() 00384 { 00385 return strlen($this->port) ? TRUE : FALSE; 00386 } 00387 00388 /** 00389 * Returns the port. 00390 * 00391 * @param mixed default 00392 * @return integer 00393 */ 00394 public function getPort($default = null) 00395 { 00396 return ($this->hasPort()) ? intval($this->port) : $default; 00397 } 00398 00399 /** 00400 * Returns TRUE if the path is valid. 00401 * 00402 * @param string $path 00403 * @throws TeamSpeak3_Helper_Exception 00404 * @return boolean 00405 */ 00406 public function checkPath($path = null) 00407 { 00408 if($path === null) 00409 { 00410 $path = $this->path; 00411 } 00412 00413 if(strlen($path) == 0) 00414 { 00415 return TRUE; 00416 } 00417 00418 $pattern = "/^" . $this->regex["path"] . "$/"; 00419 $status = @preg_match($pattern, $path); 00420 00421 if($status === FALSE) 00422 { 00423 throw new TeamSpeak3_Helper_Exception("URI path validation failed"); 00424 } 00425 00426 return ($status == 1); 00427 } 00428 00429 /** 00430 * Returns TRUE if the URI has a path. 00431 * 00432 * @return boolean 00433 */ 00434 public function hasPath() 00435 { 00436 return strlen($this->path) ? TRUE : FALSE; 00437 } 00438 00439 /** 00440 * Returns the path. 00441 * 00442 * @param mixed default 00443 * @return TeamSpeak3_Helper_String 00444 */ 00445 public function getPath($default = null) 00446 { 00447 return ($this->hasPath()) ? new TeamSpeak3_Helper_String($this->path) : $default; 00448 } 00449 00450 /** 00451 * Returns TRUE if the query string is valid. 00452 * 00453 * @param string $query 00454 * @throws TeamSpeak3_Helper_Exception 00455 * @return boolean 00456 */ 00457 public function checkQuery($query = null) 00458 { 00459 if($query === null) 00460 { 00461 $query = $this->query; 00462 } 00463 00464 if(strlen($query) == 0) 00465 { 00466 return TRUE; 00467 } 00468 00469 $pattern = "/^" . $this->regex["uric"] . "*$/"; 00470 $status = @preg_match($pattern, $query); 00471 00472 if($status === FALSE) 00473 { 00474 throw new TeamSpeak3_Helper_Exception("URI query string validation failed"); 00475 } 00476 00477 return ($status == 1); 00478 } 00479 00480 /** 00481 * Returns TRUE if the URI has a query string. 00482 * 00483 * @return boolean 00484 */ 00485 public function hasQuery() 00486 { 00487 return strlen($this->query) ? TRUE : FALSE; 00488 } 00489 00490 /** 00491 * Returns an array containing the query string elements. 00492 * 00493 * @param mixed $default 00494 * @return array 00495 */ 00496 public function getQuery($default = array()) 00497 { 00498 if(!$this->hasQuery()) 00499 { 00500 return $default; 00501 } 00502 00503 parse_str($this->query, $queryArray); 00504 00505 return $queryArray; 00506 } 00507 00508 /** 00509 * Returns TRUE if the URI has a query variable. 00510 * 00511 * @return boolean 00512 */ 00513 public function hasQueryVar($key) 00514 { 00515 if(!$this->hasQuery()) return FALSE; 00516 00517 parse_str($this->query, $queryArray); 00518 00519 return array_key_exists($key, $queryArray) ? TRUE : FALSE; 00520 } 00521 00522 /** 00523 * Returns a single variable from the query string. 00524 * 00525 * @param string $key 00526 * @param mixed $default 00527 * @return mixed 00528 */ 00529 public function getQueryVar($key, $default = null) 00530 { 00531 if(!$this->hasQuery()) return $default; 00532 00533 parse_str($this->query, $queryArray); 00534 00535 if(array_key_exists($key, $queryArray)) 00536 { 00537 $val = $queryArray[$key]; 00538 00539 if(ctype_digit($val)) 00540 { 00541 return intval($val); 00542 } 00543 elseif(is_string($val)) 00544 { 00545 return new TeamSpeak3_Helper_String($val); 00546 } 00547 else 00548 { 00549 return $val; 00550 } 00551 } 00552 00553 return $default; 00554 } 00555 00556 /** 00557 * Returns TRUE if the fragment string is valid. 00558 * 00559 * @param string $fragment 00560 * @throws TeamSpeak3_Helper_Exception 00561 * @return boolean 00562 */ 00563 public function checkFragment($fragment = null) 00564 { 00565 if($fragment === null) 00566 { 00567 $fragment = $this->fragment; 00568 } 00569 00570 if(strlen($fragment) == 0) 00571 { 00572 return TRUE; 00573 } 00574 00575 $pattern = "/^" . $this->regex["uric"] . "*$/"; 00576 $status = @preg_match($pattern, $fragment); 00577 00578 if($status === FALSE) 00579 { 00580 throw new TeamSpeak3_Helper_Exception("URI fragment validation failed"); 00581 } 00582 00583 return ($status == 1); 00584 } 00585 00586 /** 00587 * Returns TRUE if the URI has a fragment string. 00588 * 00589 * @return boolean 00590 */ 00591 public function hasFragment() 00592 { 00593 return strlen($this->fragment) ? TRUE : FALSE; 00594 } 00595 00596 /** 00597 * Returns the fragment. 00598 * 00599 * @param mixed default 00600 * @return TeamSpeak3_Helper_String 00601 */ 00602 public function getFragment($default = null) 00603 { 00604 return ($this->hasFragment()) ? new TeamSpeak3_Helper_String($this->fragment) : $default; 00605 } 00606 00607 /** 00608 * Returns a specified instance parameter from the $_REQUEST array. 00609 * 00610 * @param string $key 00611 * @param mixed $default 00612 * @return mixed 00613 */ 00614 public static function getUserParam($key, $default = null) 00615 { 00616 return (array_key_exists($key, $_REQUEST) && !empty($_REQUEST[$key])) ? self::stripslashesRecursive($_REQUEST[$key]) : $default; 00617 } 00618 00619 /** 00620 * Returns a specified environment parameter from the $_SERVER array. 00621 * 00622 * @param string $key 00623 * @param mixed $default 00624 * @return mixed 00625 */ 00626 public static function getHostParam($key, $default = null) 00627 { 00628 return (array_key_exists($key, $_SERVER) && !empty($_SERVER[$key])) ? $_SERVER[$key] : $default; 00629 } 00630 00631 /** 00632 * Returns a specified session parameter from the $_SESSION array. 00633 * 00634 * @param string $key 00635 * @param mixed $default 00636 * @return mixed 00637 */ 00638 public static function getSessParam($key, $default = null) 00639 { 00640 return (array_key_exists($key, $_SESSION) && !empty($_SESSION[$key])) ? $_SESSION[$key] : $default; 00641 } 00642 00643 /** 00644 * Returns an array containing the three main parts of a FQDN (Fully Qualified Domain Name), including the 00645 * top-level domain, the second-level domains or hostname and the third-level domain. 00646 * 00647 * @param string $hostname 00648 * @return array 00649 */ 00650 public static function getFQDNParts($hostname) 00651 { 00652 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)) 00653 { 00654 return array(); 00655 } 00656 00657 $parts["tld"] = $matches[3]; 00658 $parts["2nd"] = $matches[2]; 00659 $parts["3rd"] = $matches[1]; 00660 00661 return $parts; 00662 } 00663 00664 /** 00665 * Returns the applications host address. 00666 * 00667 * @return TeamSpeak3_Helper_String 00668 */ 00669 public static function getHostUri() 00670 { 00671 $sheme = (self::getHostParam("HTTPS") == "on") ? "https" : "http"; 00672 00673 $serverName = new TeamSpeak3_Helper_String(self::getHostParam("HTTP_HOST")); 00674 $serverPort = self::getHostParam("SERVER_PORT"); 00675 $serverPort = ($serverPort != 80 && $serverPort != 443) ? ":" . $serverPort : ""; 00676 00677 if($serverName->endsWith($serverPort)) 00678 { 00679 $serverName = $serverName->replace($serverPort, ""); 00680 } 00681 00682 return new TeamSpeak3_Helper_String($sheme . "://" . $serverName . $serverPort); 00683 } 00684 00685 /** 00686 * Returns the applications base address. 00687 * 00688 * @return string 00689 */ 00690 public static function getBaseUri() 00691 { 00692 $scriptPath = new TeamSpeak3_Helper_String(dirname(self::getHostParam("SCRIPT_NAME"))); 00693 00694 return self::getHostUri()->append(($scriptPath == DIRECTORY_SEPARATOR ? "" : $scriptPath) . "/"); 00695 } 00696 00697 /** 00698 * Strips slashes from each element of an array using stripslashes(). 00699 * 00700 * @param mixed $var 00701 * @return mixed 00702 */ 00703 protected static function stripslashesRecursive($var) 00704 { 00705 if(!is_array($var)) 00706 { 00707 return stripslashes(strval($var)); 00708 } 00709 00710 foreach($var as $key => $val) 00711 { 00712 $var[$key] = (is_array($val)) ? stripslashesRecursive($val) : stripslashes(strval($val)); 00713 } 00714 00715 return $var; 00716 } 00717 }