Uri.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. <?php
  2. /**
  3. * @file
  4. * TeamSpeak 3 PHP Framework
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * @package TeamSpeak3
  20. * @author Sven 'ScP' Paulsen
  21. * @copyright Copyright (c) Planet TeamSpeak. All rights reserved.
  22. */
  23. /**
  24. * @class TeamSpeak3_Helper_Uri
  25. * @brief Helper class for URI handling.
  26. */
  27. class TeamSpeak3_Helper_Uri
  28. {
  29. /**
  30. * Stores the URI scheme.
  31. *
  32. * @var string
  33. */
  34. protected $scheme = null;
  35. /**
  36. * Stores the URI username
  37. *
  38. * @var string
  39. */
  40. protected $user = null;
  41. /**
  42. * Stores the URI password.
  43. *
  44. * @var string
  45. */
  46. protected $pass = null;
  47. /**
  48. * Stores the URI host.
  49. *
  50. * @var string
  51. */
  52. protected $host = null;
  53. /**
  54. * Stores the URI port.
  55. *
  56. * @var string
  57. */
  58. protected $port = null;
  59. /**
  60. * Stores the URI path.
  61. *
  62. * @var string
  63. */
  64. protected $path = null;
  65. /**
  66. * Stores the URI query string.
  67. *
  68. * @var string
  69. */
  70. protected $query = null;
  71. /**
  72. * Stores the URI fragment string.
  73. *
  74. * @var string
  75. */
  76. protected $fragment = null;
  77. /**
  78. * Stores grammar rules for validation via regex.
  79. *
  80. * @var array
  81. */
  82. protected $regex = array();
  83. /**
  84. * The TeamSpeak3_Helper_Uri constructor.
  85. *
  86. * @param string $uri
  87. * @throws TeamSpeak3_Helper_Exception
  88. * @return TeamSpeak3_Helper_Uri
  89. */
  90. public function __construct($uri)
  91. {
  92. $uri = explode(":", strval($uri), 2);
  93. $this->scheme = strtolower($uri[0]);
  94. $uriString = isset($uri[1]) ? $uri[1] : "";
  95. if(!ctype_alnum($this->scheme))
  96. {
  97. throw new TeamSpeak3_Helper_Exception("invalid URI scheme '" . $this->scheme . "' supplied");
  98. }
  99. /* grammar rules for validation */
  100. $this->regex["alphanum"] = "[^\W_]";
  101. $this->regex["escaped"] = "(?:%[\da-fA-F]{2})";
  102. $this->regex["mark"] = "[-_.!~*'()\[\]]";
  103. $this->regex["reserved"] = "[;\/?:@&=+$,]";
  104. $this->regex["unreserved"] = "(?:" . $this->regex["alphanum"] . "|" . $this->regex["mark"] . ")";
  105. $this->regex["segment"] = "(?:(?:" . $this->regex["unreserved"] . "|" . $this->regex["escaped"] . "|[:@&=+$,;])*)";
  106. $this->regex["path"] = "(?:\/" . $this->regex["segment"] . "?)+";
  107. $this->regex["uric"] = "(?:" . $this->regex["reserved"] . "|" . $this->regex["unreserved"] . "|" . $this->regex["escaped"] . ")";
  108. if(strlen($uriString) > 0)
  109. {
  110. $this->parseUri($uriString);
  111. }
  112. if(!$this->isValid())
  113. {
  114. throw new TeamSpeak3_Helper_Exception("invalid URI supplied");
  115. }
  116. }
  117. /**
  118. * Parses the scheme-specific portion of the URI and place its parts into instance variables.
  119. *
  120. * @throws TeamSpeak3_Helper_Exception
  121. * @return void
  122. */
  123. protected function parseUri($uriString = '')
  124. {
  125. $status = @preg_match("~^((//)([^/?#]*))([^?#]*)(\?([^#]*))?(#(.*))?$~", $uriString, $matches);
  126. if($status === FALSE)
  127. {
  128. throw new TeamSpeak3_Helper_Exception("URI scheme-specific decomposition failed");
  129. }
  130. if(!$status) return;
  131. $this->path = (isset($matches[4])) ? $matches[4] : '';
  132. $this->query = (isset($matches[6])) ? $matches[6] : '';
  133. $this->fragment = (isset($matches[8])) ? $matches[8] : '';
  134. $status = @preg_match("~^(([^:@]*)(:([^@]*))?@)?((?(?=[[])[[][^]]+[]]|[^:]+))(:(.*))?$~", (isset($matches[3])) ? $matches[3] : "", $matches);
  135. if($status === FALSE)
  136. {
  137. throw new TeamSpeak3_Helper_Exception("URI scheme-specific authority decomposition failed");
  138. }
  139. if(!$status) return;
  140. $this->user = isset($matches[2]) ? $matches[2] : "";
  141. $this->pass = isset($matches[4]) ? $matches[4] : "";
  142. $this->host = isset($matches[5]) === TRUE ? preg_replace('~^\[([^]]+)\]$~', '\1', $matches[5]) : "";
  143. $this->port = isset($matches[7]) ? $matches[7] : "";
  144. }
  145. /**
  146. * Validate the current URI from the instance variables.
  147. *
  148. * @return boolean
  149. */
  150. public function isValid()
  151. {
  152. return ($this->checkUser() && $this->checkPass() && $this->checkHost() && $this->checkPort() && $this->checkPath() && $this->checkQuery() && $this->checkFragment());
  153. }
  154. /**
  155. * Returns TRUE if a given URI is valid.
  156. *
  157. * @param string $uri
  158. * @return boolean
  159. */
  160. public static function check($uri)
  161. {
  162. try
  163. {
  164. $uri = new self(strval($uri));
  165. }
  166. catch(Exception $e)
  167. {
  168. return FALSE;
  169. }
  170. return $uri->valid();
  171. }
  172. /**
  173. * Returns TRUE if the URI has a scheme.
  174. *
  175. * @return boolean
  176. */
  177. public function hasScheme()
  178. {
  179. return strlen($this->scheme) ? TRUE : FALSE;
  180. }
  181. /**
  182. * Returns the scheme.
  183. *
  184. * @param mixed default
  185. * @return TeamSpeak3_Helper_String
  186. */
  187. public function getScheme($default = null)
  188. {
  189. return ($this->hasScheme()) ? new TeamSpeak3_Helper_String($this->scheme) : $default;
  190. }
  191. /**
  192. * Returns TRUE if the username is valid.
  193. *
  194. * @param string $username
  195. * @throws TeamSpeak3_Helper_Exception
  196. * @return boolean
  197. */
  198. public function checkUser($username = null)
  199. {
  200. if($username === null)
  201. {
  202. $username = $this->user;
  203. }
  204. if(strlen($username) == 0)
  205. {
  206. return TRUE;
  207. }
  208. $pattern = "/^(" . $this->regex["alphanum"] . "|" . $this->regex["mark"] . "|" . $this->regex["escaped"] . "|[;:&=+$,])+$/";
  209. $status = @preg_match($pattern, $username);
  210. if($status === FALSE)
  211. {
  212. throw new TeamSpeak3_Helper_Exception("URI username validation failed");
  213. }
  214. return ($status == 1);
  215. }
  216. /**
  217. * Returns TRUE if the URI has a username.
  218. *
  219. * @return boolean
  220. */
  221. public function hasUser()
  222. {
  223. return strlen($this->user) ? TRUE : FALSE;
  224. }
  225. /**
  226. * Returns the username.
  227. *
  228. * @param mixed default
  229. * @return TeamSpeak3_Helper_String
  230. */
  231. public function getUser($default = null)
  232. {
  233. return ($this->hasUser()) ? new TeamSpeak3_Helper_String(urldecode($this->user)) : $default;
  234. }
  235. /**
  236. * Returns TRUE if the password is valid.
  237. *
  238. * @param string $password
  239. * @throws TeamSpeak3_Helper_Exception
  240. * @return boolean
  241. */
  242. public function checkPass($password = null)
  243. {
  244. if($password === null) {
  245. $password = $this->pass;
  246. }
  247. if(strlen($password) == 0)
  248. {
  249. return TRUE;
  250. }
  251. $pattern = "/^(" . $this->regex["alphanum"] . "|" . $this->regex["mark"] . "|" . $this->regex["escaped"] . "|[;:&=+$,])+$/";
  252. $status = @preg_match($pattern, $password);
  253. if($status === FALSE)
  254. {
  255. throw new TeamSpeak3_Helper_Exception("URI password validation failed");
  256. }
  257. return ($status == 1);
  258. }
  259. /**
  260. * Returns TRUE if the URI has a password.
  261. *
  262. * @return boolean
  263. */
  264. public function hasPass()
  265. {
  266. return strlen($this->pass) ? TRUE : FALSE;
  267. }
  268. /**
  269. * Returns the password.
  270. *
  271. * @param mixed default
  272. * @return TeamSpeak3_Helper_String
  273. */
  274. public function getPass($default = null)
  275. {
  276. return ($this->hasPass()) ? new TeamSpeak3_Helper_String(urldecode($this->pass)) : $default;
  277. }
  278. /**
  279. * Returns TRUE if the host is valid.
  280. *
  281. * @todo: Implement check for host URI segment
  282. *
  283. * @param string $host
  284. *
  285. * @return boolean
  286. */
  287. public function checkHost($host = null)
  288. {
  289. if($host === null)
  290. {
  291. $host = $this->host;
  292. }
  293. return TRUE;
  294. }
  295. /**
  296. * Returns TRUE if the URI has a host.
  297. *
  298. * @return boolean
  299. */
  300. public function hasHost()
  301. {
  302. return strlen($this->host) ? TRUE : FALSE;
  303. }
  304. /**
  305. * Returns the host.
  306. *
  307. * @param mixed default
  308. * @return TeamSpeak3_Helper_String
  309. */
  310. public function getHost($default = null)
  311. {
  312. return ($this->hasHost()) ? new TeamSpeak3_Helper_String(rawurldecode($this->host)) : $default;
  313. }
  314. /**
  315. * Returns TRUE if the port is valid.
  316. *
  317. * @todo: Implement check for port URI segment
  318. *
  319. * @param integer $port
  320. *
  321. * @return boolean
  322. */
  323. public function checkPort($port = null)
  324. {
  325. if($port === null)
  326. {
  327. $port = $this->port;
  328. }
  329. return TRUE;
  330. }
  331. /**
  332. * Returns TRUE if the URI has a port.
  333. *
  334. * @return boolean
  335. */
  336. public function hasPort()
  337. {
  338. return strlen($this->port) ? TRUE : FALSE;
  339. }
  340. /**
  341. * Returns the port.
  342. *
  343. * @param mixed default
  344. * @return integer
  345. */
  346. public function getPort($default = null)
  347. {
  348. return ($this->hasPort()) ? intval($this->port) : $default;
  349. }
  350. /**
  351. * Returns TRUE if the path is valid.
  352. *
  353. * @param string $path
  354. * @throws TeamSpeak3_Helper_Exception
  355. * @return boolean
  356. */
  357. public function checkPath($path = null)
  358. {
  359. if($path === null)
  360. {
  361. $path = $this->path;
  362. }
  363. if(strlen($path) == 0)
  364. {
  365. return TRUE;
  366. }
  367. $pattern = "/^" . $this->regex["path"] . "$/";
  368. $status = @preg_match($pattern, $path);
  369. if($status === FALSE)
  370. {
  371. throw new TeamSpeak3_Helper_Exception("URI path validation failed");
  372. }
  373. return ($status == 1);
  374. }
  375. /**
  376. * Returns TRUE if the URI has a path.
  377. *
  378. * @return boolean
  379. */
  380. public function hasPath()
  381. {
  382. return strlen($this->path) ? TRUE : FALSE;
  383. }
  384. /**
  385. * Returns the path.
  386. *
  387. * @param mixed default
  388. * @return TeamSpeak3_Helper_String
  389. */
  390. public function getPath($default = null)
  391. {
  392. return ($this->hasPath()) ? new TeamSpeak3_Helper_String(rawurldecode($this->path)) : $default;
  393. }
  394. /**
  395. * Returns TRUE if the query string is valid.
  396. *
  397. * @param string $query
  398. * @throws TeamSpeak3_Helper_Exception
  399. * @return boolean
  400. */
  401. public function checkQuery($query = null)
  402. {
  403. if($query === null)
  404. {
  405. $query = $this->query;
  406. }
  407. if(strlen($query) == 0)
  408. {
  409. return TRUE;
  410. }
  411. $pattern = "/^" . $this->regex["uric"] . "*$/";
  412. $status = @preg_match($pattern, $query);
  413. if($status === FALSE)
  414. {
  415. throw new TeamSpeak3_Helper_Exception("URI query string validation failed");
  416. }
  417. return ($status == 1);
  418. }
  419. /**
  420. * Returns TRUE if the URI has a query string.
  421. *
  422. * @return boolean
  423. */
  424. public function hasQuery()
  425. {
  426. return strlen($this->query) ? TRUE : FALSE;
  427. }
  428. /**
  429. * Returns an array containing the query string elements.
  430. *
  431. * @param mixed $default
  432. * @return array
  433. */
  434. public function getQuery($default = array())
  435. {
  436. if(!$this->hasQuery())
  437. {
  438. return $default;
  439. }
  440. parse_str(rawurldecode($this->query), $queryArray);
  441. return $queryArray;
  442. }
  443. /**
  444. * Returns TRUE if the URI has a query variable.
  445. *
  446. * @return boolean
  447. */
  448. public function hasQueryVar($key)
  449. {
  450. if(!$this->hasQuery()) return FALSE;
  451. parse_str($this->query, $queryArray);
  452. return array_key_exists($key, $queryArray) ? TRUE : FALSE;
  453. }
  454. /**
  455. * Returns a single variable from the query string.
  456. *
  457. * @param string $key
  458. * @param mixed $default
  459. * @return mixed
  460. */
  461. public function getQueryVar($key, $default = null)
  462. {
  463. if(!$this->hasQuery()) return $default;
  464. parse_str(rawurldecode($this->query), $queryArray);
  465. if(array_key_exists($key, $queryArray))
  466. {
  467. $val = $queryArray[$key];
  468. if(ctype_digit($val))
  469. {
  470. return intval($val);
  471. }
  472. elseif(is_string($val))
  473. {
  474. return new TeamSpeak3_Helper_String($val);
  475. }
  476. else
  477. {
  478. return $val;
  479. }
  480. }
  481. return $default;
  482. }
  483. /**
  484. * Returns TRUE if the fragment string is valid.
  485. *
  486. * @param string $fragment
  487. * @throws TeamSpeak3_Helper_Exception
  488. * @return boolean
  489. */
  490. public function checkFragment($fragment = null)
  491. {
  492. if($fragment === null)
  493. {
  494. $fragment = $this->fragment;
  495. }
  496. if(strlen($fragment) == 0)
  497. {
  498. return TRUE;
  499. }
  500. $pattern = "/^" . $this->regex["uric"] . "*$/";
  501. $status = @preg_match($pattern, $fragment);
  502. if($status === FALSE)
  503. {
  504. throw new TeamSpeak3_Helper_Exception("URI fragment validation failed");
  505. }
  506. return ($status == 1);
  507. }
  508. /**
  509. * Returns TRUE if the URI has a fragment string.
  510. *
  511. * @return boolean
  512. */
  513. public function hasFragment()
  514. {
  515. return strlen($this->fragment) ? TRUE : FALSE;
  516. }
  517. /**
  518. * Returns the fragment.
  519. *
  520. * @param mixed default
  521. * @return TeamSpeak3_Helper_String
  522. */
  523. public function getFragment($default = null)
  524. {
  525. return ($this->hasFragment()) ? new TeamSpeak3_Helper_String(rawurldecode($this->fragment)) : $default;
  526. }
  527. /**
  528. * Returns a specified instance parameter from the $_REQUEST array.
  529. *
  530. * @param string $key
  531. * @param mixed $default
  532. * @return mixed
  533. */
  534. public static function getUserParam($key, $default = null)
  535. {
  536. return (array_key_exists($key, $_REQUEST) && !empty($_REQUEST[$key])) ? self::stripslashesRecursive($_REQUEST[$key]) : $default;
  537. }
  538. /**
  539. * Returns a specified environment parameter from the $_SERVER array.
  540. *
  541. * @param string $key
  542. * @param mixed $default
  543. * @return mixed
  544. */
  545. public static function getHostParam($key, $default = null)
  546. {
  547. return (array_key_exists($key, $_SERVER) && !empty($_SERVER[$key])) ? $_SERVER[$key] : $default;
  548. }
  549. /**
  550. * Returns a specified session parameter from the $_SESSION array.
  551. *
  552. * @param string $key
  553. * @param mixed $default
  554. * @return mixed
  555. */
  556. public static function getSessParam($key, $default = null)
  557. {
  558. return (array_key_exists($key, $_SESSION) && !empty($_SESSION[$key])) ? $_SESSION[$key] : $default;
  559. }
  560. /**
  561. * Returns an array containing the three main parts of a FQDN (Fully Qualified Domain Name), including the
  562. * top-level domain, the second-level domains or hostname and the third-level domain.
  563. *
  564. * @param string $hostname
  565. * @return array
  566. */
  567. public static function getFQDNParts($hostname)
  568. {
  569. 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))
  570. {
  571. return array();
  572. }
  573. $parts["tld"] = $matches[3];
  574. $parts["2nd"] = $matches[2];
  575. $parts["3rd"] = $matches[1];
  576. return $parts;
  577. }
  578. /**
  579. * Returns the applications host address.
  580. *
  581. * @return TeamSpeak3_Helper_String
  582. */
  583. public static function getHostUri()
  584. {
  585. $sheme = (self::getHostParam("HTTPS") == "on") ? "https" : "http";
  586. $serverName = new TeamSpeak3_Helper_String(self::getHostParam("HTTP_HOST"));
  587. $serverPort = self::getHostParam("SERVER_PORT");
  588. $serverPort = ($serverPort != 80 && $serverPort != 443) ? ":" . $serverPort : "";
  589. if($serverName->endsWith($serverPort))
  590. {
  591. $serverName = $serverName->replace($serverPort, "");
  592. }
  593. return new TeamSpeak3_Helper_String($sheme . "://" . $serverName . $serverPort);
  594. }
  595. /**
  596. * Returns the applications base address.
  597. *
  598. * @return string
  599. */
  600. public static function getBaseUri()
  601. {
  602. $scriptPath = new TeamSpeak3_Helper_String(dirname(self::getHostParam("SCRIPT_NAME")));
  603. return self::getHostUri()->append(($scriptPath == DIRECTORY_SEPARATOR ? "" : $scriptPath) . "/");
  604. }
  605. /**
  606. * Strips slashes from each element of an array using stripslashes().
  607. *
  608. * @param mixed $var
  609. * @return mixed
  610. */
  611. protected static function stripslashesRecursive($var)
  612. {
  613. if(!is_array($var))
  614. {
  615. return stripslashes(strval($var));
  616. }
  617. foreach($var as $key => $val)
  618. {
  619. $var[$key] = (is_array($val)) ? stripslashesRecursive($val) : stripslashes(strval($val));
  620. }
  621. return $var;
  622. }
  623. }