PageRenderTime 61ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/treeview-master/libs/Nette/Web/Uri.php

https://github.com/indesigner/tests
PHP | 458 lines | 188 code | 102 blank | 168 comment | 17 complexity | 0827f18fd3ed5e52c2c8c8fd1252d4aa MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * @copyright Copyright (c) 2004, 2010 David Grudl
  6. * @license http://nettephp.com/license Nette license
  7. * @link http://nettephp.com
  8. * @category Nette
  9. * @package Nette\Web
  10. */
  11. /**
  12. * URI Syntax (RFC 3986).
  13. *
  14. * <pre>
  15. * http://user:password@nettephp.com:8042/en/manual.html?name=param#fragment
  16. * \__/^^^\_____________________________/\_____________/^\________/^\______/
  17. * | | | | |
  18. * scheme authority path query fragment
  19. * </pre>
  20. *
  21. * - authority: [user[:password]@]host[:port]
  22. * - hostUri: http://user:password@nettephp.com:8042
  23. *
  24. * @copyright Copyright (c) 2004, 2010 David Grudl
  25. * @package Nette\Web
  26. *
  27. * @property string $scheme
  28. * @property string $user
  29. * @property string $password
  30. * @property string $host
  31. * @property string $port
  32. * @property string $path
  33. * @property string $query
  34. * @property string $fragment
  35. * @property-read string $absoluteUri
  36. * @property-read string $authority
  37. * @property-read string $hostUri
  38. */
  39. class Uri extends FreezableObject
  40. {
  41. /** @var array */
  42. public static $defaultPorts = array(
  43. 'http' => 80,
  44. 'https' => 443,
  45. 'ftp' => 21,
  46. 'news' => 119,
  47. 'nntp' => 119,
  48. );
  49. /** @var string */
  50. private $scheme = '';
  51. /** @var string */
  52. private $user = '';
  53. /** @var string */
  54. private $pass = '';
  55. /** @var string */
  56. private $host = '';
  57. /** @var int */
  58. private $port = NULL;
  59. /** @var string */
  60. private $path = '';
  61. /** @var string */
  62. private $query = '';
  63. /** @var string */
  64. private $fragment = '';
  65. /**
  66. * @param string URL
  67. * @throws InvalidArgumentException
  68. */
  69. public function __construct($uri = NULL)
  70. {
  71. if (is_string($uri)) {
  72. $parts = @parse_url($uri); // intentionally @
  73. if ($parts === FALSE) {
  74. throw new InvalidArgumentException("Malformed or unsupported URI '$uri'.");
  75. }
  76. foreach ($parts as $key => $val) {
  77. $this->$key = $val;
  78. }
  79. if (!$this->port && isset(self::$defaultPorts[$this->scheme])) {
  80. $this->port = self::$defaultPorts[$this->scheme];
  81. }
  82. } elseif ($uri instanceof self) {
  83. foreach ($uri as $key => $val) {
  84. $this->$key = $val;
  85. }
  86. }
  87. }
  88. /**
  89. * Sets the scheme part of URI.
  90. * @param string
  91. * @return Uri provides a fluent interface
  92. */
  93. public function setScheme($value)
  94. {
  95. $this->updating();
  96. $this->scheme = (string) $value;
  97. return $this;
  98. }
  99. /**
  100. * Returns the scheme part of URI.
  101. * @return string
  102. */
  103. public function getScheme()
  104. {
  105. return $this->scheme;
  106. }
  107. /**
  108. * Sets the user name part of URI.
  109. * @param string
  110. * @return Uri provides a fluent interface
  111. */
  112. public function setUser($value)
  113. {
  114. $this->updating();
  115. $this->user = (string) $value;
  116. return $this;
  117. }
  118. /**
  119. * Returns the user name part of URI.
  120. * @return string
  121. */
  122. public function getUser()
  123. {
  124. return $this->user;
  125. }
  126. /**
  127. * Sets the password part of URI.
  128. * @param string
  129. * @return Uri provides a fluent interface
  130. */
  131. public function setPassword($value)
  132. {
  133. $this->updating();
  134. $this->pass = (string) $value;
  135. return $this;
  136. }
  137. /**
  138. * Returns the password part of URI.
  139. * @return string
  140. */
  141. public function getPassword()
  142. {
  143. return $this->pass;
  144. }
  145. /**
  146. * @deprecated
  147. */
  148. public function setPass($value)
  149. {
  150. $this->setPassword($value);
  151. }
  152. /**
  153. * @deprecated
  154. */
  155. public function getPass()
  156. {
  157. return $this->pass;
  158. }
  159. /**
  160. * Sets the host part of URI.
  161. * @param string
  162. * @return Uri provides a fluent interface
  163. */
  164. public function setHost($value)
  165. {
  166. $this->updating();
  167. $this->host = (string) $value;
  168. return $this;
  169. }
  170. /**
  171. * Returns the host part of URI.
  172. * @return string
  173. */
  174. public function getHost()
  175. {
  176. return $this->host;
  177. }
  178. /**
  179. * Sets the port part of URI.
  180. * @param string
  181. * @return Uri provides a fluent interface
  182. */
  183. public function setPort($value)
  184. {
  185. $this->updating();
  186. $this->port = (int) $value;
  187. return $this;
  188. }
  189. /**
  190. * Returns the port part of URI.
  191. * @return string
  192. */
  193. public function getPort()
  194. {
  195. return $this->port;
  196. }
  197. /**
  198. * Sets the path part of URI.
  199. * @param string
  200. * @return Uri provides a fluent interface
  201. */
  202. public function setPath($value)
  203. {
  204. $this->updating();
  205. $this->path = (string) $value;
  206. return $this;
  207. }
  208. /**
  209. * Returns the path part of URI.
  210. * @return string
  211. */
  212. public function getPath()
  213. {
  214. return $this->path;
  215. }
  216. /**
  217. * Sets the query part of URI.
  218. * @param string|array
  219. * @return Uri provides a fluent interface
  220. */
  221. public function setQuery($value)
  222. {
  223. $this->updating();
  224. $this->query = (string) (is_array($value) ? http_build_query($value, '', '&') : $value);
  225. return $this;
  226. }
  227. /**
  228. * Appends the query part of URI.
  229. * @param string|array
  230. * @return void
  231. */
  232. public function appendQuery($value)
  233. {
  234. $this->updating();
  235. $value = (string) (is_array($value) ? http_build_query($value, '', '&') : $value);
  236. $this->query .= ($this->query === '' || $value === '') ? $value : '&' . $value;
  237. }
  238. /**
  239. * Returns the query part of URI.
  240. * @return string
  241. */
  242. public function getQuery()
  243. {
  244. return $this->query;
  245. }
  246. /**
  247. * Sets the fragment part of URI.
  248. * @param string
  249. * @return Uri provides a fluent interface
  250. */
  251. public function setFragment($value)
  252. {
  253. $this->updating();
  254. $this->fragment = (string) $value;
  255. return $this;
  256. }
  257. /**
  258. * Returns the fragment part of URI.
  259. * @return string
  260. */
  261. public function getFragment()
  262. {
  263. return $this->fragment;
  264. }
  265. /**
  266. * Returns the entire URI including query string and fragment.
  267. * @return string
  268. */
  269. public function getAbsoluteUri()
  270. {
  271. return $this->scheme . '://' . $this->getAuthority() . $this->path
  272. . ($this->query === '' ? '' : '?' . $this->query)
  273. . ($this->fragment === '' ? '' : '#' . $this->fragment);
  274. }
  275. /**
  276. * Returns the [user[:pass]@]host[:port] part of URI.
  277. * @return string
  278. */
  279. public function getAuthority()
  280. {
  281. $authority = $this->host;
  282. if ($this->port && isset(self::$defaultPorts[$this->scheme]) && $this->port !== self::$defaultPorts[$this->scheme]) {
  283. $authority .= ':' . $this->port;
  284. }
  285. if ($this->user !== '' && $this->scheme !== 'http' && $this->scheme !== 'https') {
  286. $authority = $this->user . ($this->pass === '' ? '' : ':' . $this->pass) . '@' . $authority;
  287. }
  288. return $authority;
  289. }
  290. /**
  291. * Returns the scheme and authority part of URI.
  292. * @return string
  293. */
  294. public function getHostUri()
  295. {
  296. return $this->scheme . '://' . $this->getAuthority();
  297. }
  298. /**
  299. * URI comparsion (this object must be in canonical form).
  300. * @param string
  301. * @return bool
  302. */
  303. public function isEqual($uri)
  304. {
  305. // compare host + path
  306. $part = self::unescape(strtok($uri, '?#'), '%/');
  307. if (strncmp($part, '//', 2) === 0) { // absolute URI without scheme
  308. if ($part !== '//' . $this->getAuthority() . $this->path) return FALSE;
  309. } elseif (strncmp($part, '/', 1) === 0) { // absolute path
  310. if ($part !== $this->path) return FALSE;
  311. } else {
  312. if ($part !== $this->scheme . '://' . $this->getAuthority() . $this->path) return FALSE;
  313. }
  314. // compare query strings
  315. $part = self::unescape(strtr((string) strtok('?#'), '+', ' '), '%&;=+');
  316. return $part === $this->query;
  317. }
  318. /**
  319. * Transform to canonical form.
  320. * @return void
  321. */
  322. public function canonicalize()
  323. {
  324. $this->updating();
  325. $this->path = $this->path === '' ? '/' : self::unescape($this->path, '%/');
  326. $this->host = strtolower(rawurldecode($this->host));
  327. $this->query = self::unescape(strtr($this->query, '+', ' '), '%&;=+');
  328. }
  329. /**
  330. * @return string
  331. */
  332. public function __toString()
  333. {
  334. return $this->getAbsoluteUri();
  335. }
  336. /**
  337. * Similar to rawurldecode, but preserve reserved chars encoded.
  338. * @param string to decode
  339. * @param string reserved characters
  340. * @return string
  341. */
  342. public static function unescape($s, $reserved = '%;/?:@&=+$,')
  343. {
  344. // reserved (@see RFC 2396) = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
  345. // within a path segment, the characters "/", ";", "=", "?" are reserved
  346. // within a query component, the characters ";", "/", "?", ":", "@", "&", "=", "+", ",", "$" are reserved.
  347. preg_match_all('#(?<=%)[a-f0-9][a-f0-9]#i', $s, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  348. foreach (array_reverse($matches) as $match) {
  349. $ch = chr(hexdec($match[0][0]));
  350. if (strpos($reserved, $ch) === FALSE) {
  351. $s = substr_replace($s, $ch, $match[0][1] - 1, 3);
  352. }
  353. }
  354. return $s;
  355. }
  356. }