Source for file Uri.php

Documentation is available at Uri.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Web
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../FreezableObject.php';
  23. 23:  
  24. 24:  
  25. 25:  
  26. 26: /**
  27. 27:  * URI Syntax (RFC 3986).
  28. 28:  *
  29. 29:  * <pre>
  30. 30:  * http://user:password@nettephp.com:8042/en/manual.html?name=param#fragment
  31. 31:  * \__/^^^\_____________________________/\_____________/^\________/^\______/
  32. 32:  *   |                    |                     |            |         |
  33. 33:  * scheme             authority               path         query    fragment
  34. 34:  * </pre>
  35. 35:  *
  36. 36:  * - authority:   [user[:password]@]host[:port]
  37. 37:  * - hostUri:     http://user:password@nettephp.com:8042
  38. 38:  *
  39. 39:  * @author     David Grudl
  40. 40:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  41. 41:  * @package    Nette\Web
  42. 42:  *
  43. 43:  * @property   string $scheme 
  44. 44:  * @property   string $user 
  45. 45:  * @property   string $password 
  46. 46:  * @property   string $host 
  47. 47:  * @property   string $port 
  48. 48:  * @property   string $path 
  49. 49:  * @property   string $query 
  50. 50:  * @property   string $fragment 
  51. 51:  * @property-read string $absoluteUri 
  52. 52:  * @property-read string $authority 
  53. 53:  * @property-read string $hostUri 
  54. 54:  */
  55. 55: class Uri extends FreezableObject
  56. 56: {
  57. 57:     /** @var array */
  58. 58:     public static $defaultPorts array(
  59. 59:         'http' => 80,
  60. 60:         'https' => 443,
  61. 61:         'ftp' => 21,
  62. 62:         'news' => 119,
  63. 63:         'nntp' => 119,
  64. 64:     );
  65. 65:  
  66. 66:     /** @var string */
  67. 67:     private $scheme '';
  68. 68:  
  69. 69:     /** @var string */
  70. 70:     private $user '';
  71. 71:  
  72. 72:     /** @var string */
  73. 73:     private $pass '';
  74. 74:  
  75. 75:     /** @var string */
  76. 76:     private $host '';
  77. 77:  
  78. 78:     /** @var int */
  79. 79:     private $port NULL;
  80. 80:  
  81. 81:     /** @var string */
  82. 82:     private $path '';
  83. 83:  
  84. 84:     /** @var string */
  85. 85:     private $query '';
  86. 86:  
  87. 87:     /** @var string */
  88. 88:     private $fragment '';
  89. 89:  
  90. 90:  
  91. 91:  
  92. 92:     /**
  93. 93:      * @param  string  URL
  94. 94:      * @throws InvalidArgumentException
  95. 95:      */
  96. 96:     public function __construct($uri NULL)
  97. 97:     {
  98. 98:         if (is_string($uri)) {
  99. 99:             $parts @parse_url($uri)// intentionally @
  100. 100:             if ($parts === FALSE{
  101. 101:                 throw new InvalidArgumentException("Malformed or unsupported URI '$uri'.");
  102. 102:             }
  103. 103:  
  104. 104:             foreach ($parts as $key => $val{
  105. 105:                 $this->$key $val;
  106. 106:             }
  107. 107:  
  108. 108:             if (!$this->port && isset(self::$defaultPorts[$this->scheme])) {
  109. 109:                 $this->port self::$defaultPorts[$this->scheme];
  110. 110:             }
  111. 111:  
  112. 112:         elseif ($uri instanceof self{
  113. 113:             foreach ($uri as $key => $val{
  114. 114:                 $this->$key $val;
  115. 115:             }
  116. 116:         }
  117. 117:     }
  118. 118:  
  119. 119:  
  120. 120:  
  121. 121:     /**
  122. 122:      * Sets the scheme part of URI.
  123. 123:      * @param  string 
  124. 124:      * @return Uri  provides a fluent interface
  125. 125:      */
  126. 126:     public function setScheme($value)
  127. 127:     {
  128. 128:         $this->updating();
  129. 129:         $this->scheme = (string) $value;
  130. 130:         return $this;
  131. 131:     }
  132. 132:  
  133. 133:  
  134. 134:  
  135. 135:     /**
  136. 136:      * Returns the scheme part of URI.
  137. 137:      * @return string 
  138. 138:      */
  139. 139:     public function getScheme()
  140. 140:     {
  141. 141:         return $this->scheme;
  142. 142:     }
  143. 143:  
  144. 144:  
  145. 145:  
  146. 146:     /**
  147. 147:      * Sets the user name part of URI.
  148. 148:      * @param  string 
  149. 149:      * @return Uri  provides a fluent interface
  150. 150:      */
  151. 151:     public function setUser($value)
  152. 152:     {
  153. 153:         $this->updating();
  154. 154:         $this->user = (string) $value;
  155. 155:         return $this;
  156. 156:     }
  157. 157:  
  158. 158:  
  159. 159:  
  160. 160:     /**
  161. 161:      * Returns the user name part of URI.
  162. 162:      * @return string 
  163. 163:      */
  164. 164:     public function getUser()
  165. 165:     {
  166. 166:         return $this->user;
  167. 167:     }
  168. 168:  
  169. 169:  
  170. 170:  
  171. 171:     /**
  172. 172:      * Sets the password part of URI.
  173. 173:      * @param  string 
  174. 174:      * @return Uri  provides a fluent interface
  175. 175:      */
  176. 176:     public function setPassword($value)
  177. 177:     {
  178. 178:         $this->updating();
  179. 179:         $this->pass = (string) $value;
  180. 180:         return $this;
  181. 181:     }
  182. 182:  
  183. 183:  
  184. 184:  
  185. 185:     /**
  186. 186:      * Returns the password part of URI.
  187. 187:      * @return string 
  188. 188:      */
  189. 189:     public function getPassword()
  190. 190:     {
  191. 191:         return $this->pass;
  192. 192:     }
  193. 193:  
  194. 194:  
  195. 195:  
  196. 196:     /**
  197. 197:      * @deprecated
  198. 198:      */
  199. 199:     public function setPass($value)
  200. 200:     {
  201. 201:         $this->setPassword($value);
  202. 202:     }
  203. 203:  
  204. 204:  
  205. 205:  
  206. 206:     /**
  207. 207:      * @deprecated
  208. 208:      */
  209. 209:     public function getPass()
  210. 210:     {
  211. 211:         return $this->pass;
  212. 212:     }
  213. 213:  
  214. 214:  
  215. 215:  
  216. 216:     /**
  217. 217:      * Sets the host part of URI.
  218. 218:      * @param  string 
  219. 219:      * @return Uri  provides a fluent interface
  220. 220:      */
  221. 221:     public function setHost($value)
  222. 222:     {
  223. 223:         $this->updating();
  224. 224:         $this->host = (string) $value;
  225. 225:         return $this;
  226. 226:     }
  227. 227:  
  228. 228:  
  229. 229:  
  230. 230:     /**
  231. 231:      * Returns the host part of URI.
  232. 232:      * @return string 
  233. 233:      */
  234. 234:     public function getHost()
  235. 235:     {
  236. 236:         return $this->host;
  237. 237:     }
  238. 238:  
  239. 239:  
  240. 240:  
  241. 241:     /**
  242. 242:      * Sets the port part of URI.
  243. 243:      * @param  string 
  244. 244:      * @return Uri  provides a fluent interface
  245. 245:      */
  246. 246:     public function setPort($value)
  247. 247:     {
  248. 248:         $this->updating();
  249. 249:         $this->port = (int) $value;
  250. 250:         return $this;
  251. 251:     }
  252. 252:  
  253. 253:  
  254. 254:  
  255. 255:     /**
  256. 256:      * Returns the port part of URI.
  257. 257:      * @return string 
  258. 258:      */
  259. 259:     public function getPort()
  260. 260:     {
  261. 261:         return $this->port;
  262. 262:     }
  263. 263:  
  264. 264:  
  265. 265:  
  266. 266:     /**
  267. 267:      * Sets the path part of URI.
  268. 268:      * @param  string 
  269. 269:      * @return Uri  provides a fluent interface
  270. 270:      */
  271. 271:     public function setPath($value)
  272. 272:     {
  273. 273:         $this->updating();
  274. 274:         $this->path = (string) $value;
  275. 275:         return $this;
  276. 276:     }
  277. 277:  
  278. 278:  
  279. 279:  
  280. 280:     /**
  281. 281:      * Returns the path part of URI.
  282. 282:      * @return string 
  283. 283:      */
  284. 284:     public function getPath()
  285. 285:     {
  286. 286:         return $this->path;
  287. 287:     }
  288. 288:  
  289. 289:  
  290. 290:  
  291. 291:     /**
  292. 292:      * Sets the query part of URI.
  293. 293:      * @param  string|array
  294. 294:      * @return Uri  provides a fluent interface
  295. 295:      */
  296. 296:     public function setQuery($value)
  297. 297:     {
  298. 298:         $this->updating();
  299. 299:         $this->query = (string) (is_array($valuehttp_build_query($value'''&'$value);
  300. 300:         return $this;
  301. 301:     }
  302. 302:  
  303. 303:  
  304. 304:  
  305. 305:     /**
  306. 306:      * Appends the query part of URI.
  307. 307:      * @param  string|array
  308. 308:      * @return void 
  309. 309:      */
  310. 310:     public function appendQuery($value)
  311. 311:     {
  312. 312:         $this->updating();
  313. 313:         $value = (string) (is_array($valuehttp_build_query($value'''&'$value);
  314. 314:         $this->query .= ($this->query === '' || $value === ''$value '&' $value;
  315. 315:     }
  316. 316:  
  317. 317:  
  318. 318:  
  319. 319:     /**
  320. 320:      * Returns the query part of URI.
  321. 321:      * @return string 
  322. 322:      */
  323. 323:     public function getQuery()
  324. 324:     {
  325. 325:         return $this->query;
  326. 326:     }
  327. 327:  
  328. 328:  
  329. 329:  
  330. 330:     /**
  331. 331:      * Sets the fragment part of URI.
  332. 332:      * @param  string 
  333. 333:      * @return Uri  provides a fluent interface
  334. 334:      */
  335. 335:     public function setFragment($value)
  336. 336:     {
  337. 337:         $this->updating();
  338. 338:         $this->fragment = (string) $value;
  339. 339:         return $this;
  340. 340:     }
  341. 341:  
  342. 342:  
  343. 343:  
  344. 344:     /**
  345. 345:      * Returns the fragment part of URI.
  346. 346:      * @return string 
  347. 347:      */
  348. 348:     public function getFragment()
  349. 349:     {
  350. 350:         return $this->fragment;
  351. 351:     }
  352. 352:  
  353. 353:  
  354. 354:  
  355. 355:     /**
  356. 356:      * Returns the entire URI including query string and fragment.
  357. 357:      * @return string 
  358. 358:      */
  359. 359:     public function getAbsoluteUri()
  360. 360:     {
  361. 361:         return $this->scheme '://' $this->getAuthority($this->path
  362. 362:             . ($this->query === '' '' '?' $this->query)
  363. 363:             . ($this->fragment === '' '' '#' $this->fragment);
  364. 364:     }
  365. 365:  
  366. 366:  
  367. 367:  
  368. 368:     /**
  369. 369:      * Returns the [user[:pass]@]host[:port] part of URI.
  370. 370:      * @return string 
  371. 371:      */
  372. 372:     public function getAuthority()
  373. 373:     {
  374. 374:         $authority $this->host;
  375. 375:         if ($this->port && isset(self::$defaultPorts[$this->scheme]&& $this->port !== self::$defaultPorts[$this->scheme]{
  376. 376:             $authority .= ':' $this->port;
  377. 377:         }
  378. 378:  
  379. 379:         if ($this->user !== '' && $this->scheme !== 'http' && $this->scheme !== 'https'{
  380. 380:             $authority $this->user ($this->pass === '' '' ':' $this->pass'@' $authority;
  381. 381:         }
  382. 382:  
  383. 383:         return $authority;
  384. 384:     }
  385. 385:  
  386. 386:  
  387. 387:  
  388. 388:     /**
  389. 389:      * Returns the scheme and authority part of URI.
  390. 390:      * @return string 
  391. 391:      */
  392. 392:     public function getHostUri()
  393. 393:     {
  394. 394:         return $this->scheme '://' $this->getAuthority();
  395. 395:     }
  396. 396:  
  397. 397:  
  398. 398:  
  399. 399:     /**
  400. 400:      * URI comparsion (this object must be in canonical form).
  401. 401:      * @param  string 
  402. 402:      * @return bool 
  403. 403:      */
  404. 404:     public function isEqual($uri)
  405. 405:     {
  406. 406:         // compare host + path
  407. 407:         $part self::unescape(strtok($uri'?#')'%/');
  408. 408:         if (strncmp($part'//'2=== 0// absolute URI without scheme
  409. 409:             if ($part !== '//' $this->getAuthority($this->pathreturn FALSE;
  410. 410:  
  411. 411:         elseif (strncmp($part'/'1=== 0// absolute path
  412. 412:             if ($part !== $this->pathreturn FALSE;
  413. 413:  
  414. 414:         else {
  415. 415:             if ($part !== $this->scheme '://' $this->getAuthority($this->pathreturn FALSE;
  416. 416:         }
  417. 417:  
  418. 418:         // compare query strings
  419. 419:         $part = (string) strtok('?#');
  420. 420:         if ($part !== ''{
  421. 421:             $tmp preg_split('#[&;]#'self::unescape(strtr($part'+'' ')'%&;=+'));
  422. 422:             sort($tmp);
  423. 423:             $part implode('&'$tmp);
  424. 424:         }
  425. 425:         return $part === $this->query;
  426. 426:     }
  427. 427:  
  428. 428:  
  429. 429:  
  430. 430:     /**
  431. 431:      * Transform to canonical form.
  432. 432:      * @return void 
  433. 433:      */
  434. 434:     public function canonicalize()
  435. 435:     {
  436. 436:         $this->updating();
  437. 437:         $this->path $this->path === '' '/' self::unescape($this->path'%/');
  438. 438:  
  439. 439:         $this->host strtolower(rawurldecode($this->host));
  440. 440:  
  441. 441:         if ($this->query !== ''{
  442. 442:             $tmp preg_split('#[&;]#'self::unescape(strtr($this->query'+'' ')'%&;=+'));
  443. 443:             sort($tmp);
  444. 444:             $this->query implode('&'$tmp);
  445. 445:         }
  446. 446:     }
  447. 447:  
  448. 448:  
  449. 449:  
  450. 450:     /**
  451. 451:      * @return string 
  452. 452:      */
  453. 453:     public function __toString()
  454. 454:     {
  455. 455:         return $this->getAbsoluteUri();
  456. 456:     }
  457. 457:  
  458. 458:  
  459. 459:  
  460. 460:     /**
  461. 461:      * Similar to rawurldecode, but preserve reserved chars encoded.
  462. 462:      * @param  string to decode
  463. 463:      * @param  string reserved characters
  464. 464:      * @return string 
  465. 465:      */
  466. 466:     public static function unescape($s$reserved '%;/?:@&=+$,')
  467. 467:     {
  468. 468:         // reserved (@see RFC 2396) = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
  469. 469:         // within a path segment, the characters "/", ";", "=", "?" are reserved
  470. 470:         // within a query component, the characters ";", "/", "?", ":", "@", "&", "=", "+", ",", "$" are reserved.
  471. 471:         preg_match_all('#(?<=%)[a-f0-9][a-f0-9]#i'$s$matchesPREG_OFFSET_CAPTURE PREG_SET_ORDER);
  472. 472:         foreach (array_reverse($matchesas $match{
  473. 473:             $ch chr(hexdec($match[0][0]));
  474. 474:             if (strpos($reserved$ch=== FALSE{
  475. 475:                 $s substr_replace($s$ch$match[0][113);
  476. 476:             }
  477. 477:         }
  478. 478:         return $s;
  479. 479:     }
  480. 480: