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 void 
  125. 125:      */
  126. 126:     public function setScheme($value)
  127. 127:     {
  128. 128:         $this->updating();
  129. 129:         $this->scheme = (string) $value;
  130. 130:     }
  131. 131:  
  132. 132:  
  133. 133:  
  134. 134:     /**
  135. 135:      * Returns the scheme part of URI.
  136. 136:      * @return string 
  137. 137:      */
  138. 138:     public function getScheme()
  139. 139:     {
  140. 140:         return $this->scheme;
  141. 141:     }
  142. 142:  
  143. 143:  
  144. 144:  
  145. 145:     /**
  146. 146:      * Sets the user name part of URI.
  147. 147:      * @param  string 
  148. 148:      * @return void 
  149. 149:      */
  150. 150:     public function setUser($value)
  151. 151:     {
  152. 152:         $this->updating();
  153. 153:         $this->user = (string) $value;
  154. 154:     }
  155. 155:  
  156. 156:  
  157. 157:  
  158. 158:     /**
  159. 159:      * Returns the user name part of URI.
  160. 160:      * @return string 
  161. 161:      */
  162. 162:     public function getUser()
  163. 163:     {
  164. 164:         return $this->user;
  165. 165:     }
  166. 166:  
  167. 167:  
  168. 168:  
  169. 169:     /**
  170. 170:      * Sets the password part of URI.
  171. 171:      * @param  string 
  172. 172:      * @return void 
  173. 173:      */
  174. 174:     public function setPassword($value)
  175. 175:     {
  176. 176:         $this->updating();
  177. 177:         $this->pass = (string) $value;
  178. 178:     }
  179. 179:  
  180. 180:  
  181. 181:  
  182. 182:     /**
  183. 183:      * Returns the password part of URI.
  184. 184:      * @return string 
  185. 185:      */
  186. 186:     public function getPassword()
  187. 187:     {
  188. 188:         return $this->pass;
  189. 189:     }
  190. 190:  
  191. 191:  
  192. 192:  
  193. 193:     /**
  194. 194:      * @deprecated
  195. 195:      */
  196. 196:     public function setPass($value)
  197. 197:     {
  198. 198:         $this->setPassword($value);
  199. 199:     }
  200. 200:  
  201. 201:  
  202. 202:  
  203. 203:     /**
  204. 204:      * @deprecated
  205. 205:      */
  206. 206:     public function getPass()
  207. 207:     {
  208. 208:         return $this->pass;
  209. 209:     }
  210. 210:  
  211. 211:  
  212. 212:  
  213. 213:     /**
  214. 214:      * Sets the host part of URI.
  215. 215:      * @param  string 
  216. 216:      * @return void 
  217. 217:      */
  218. 218:     public function setHost($value)
  219. 219:     {
  220. 220:         $this->updating();
  221. 221:         $this->host = (string) $value;
  222. 222:     }
  223. 223:  
  224. 224:  
  225. 225:  
  226. 226:     /**
  227. 227:      * Returns the host part of URI.
  228. 228:      * @return string 
  229. 229:      */
  230. 230:     public function getHost()
  231. 231:     {
  232. 232:         return $this->host;
  233. 233:     }
  234. 234:  
  235. 235:  
  236. 236:  
  237. 237:     /**
  238. 238:      * Sets the port part of URI.
  239. 239:      * @param  string 
  240. 240:      * @return void 
  241. 241:      */
  242. 242:     public function setPort($value)
  243. 243:     {
  244. 244:         $this->updating();
  245. 245:         $this->port = (int) $value;
  246. 246:     }
  247. 247:  
  248. 248:  
  249. 249:  
  250. 250:     /**
  251. 251:      * Returns the port part of URI.
  252. 252:      * @return string 
  253. 253:      */
  254. 254:     public function getPort()
  255. 255:     {
  256. 256:         return $this->port;
  257. 257:     }
  258. 258:  
  259. 259:  
  260. 260:  
  261. 261:     /**
  262. 262:      * Sets the path part of URI.
  263. 263:      * @param  string 
  264. 264:      * @return void 
  265. 265:      */
  266. 266:     public function setPath($value)
  267. 267:     {
  268. 268:         $this->updating();
  269. 269:         $this->path = (string) $value;
  270. 270:     }
  271. 271:  
  272. 272:  
  273. 273:  
  274. 274:     /**
  275. 275:      * Returns the path part of URI.
  276. 276:      * @return string 
  277. 277:      */
  278. 278:     public function getPath()
  279. 279:     {
  280. 280:         return $this->path;
  281. 281:     }
  282. 282:  
  283. 283:  
  284. 284:  
  285. 285:     /**
  286. 286:      * Sets the query part of URI.
  287. 287:      * @param  string|array
  288. 288:      * @return void 
  289. 289:      */
  290. 290:     public function setQuery($value)
  291. 291:     {
  292. 292:         $this->updating();
  293. 293:         $this->query = (string) (is_array($valuehttp_build_query($value'''&'$value);
  294. 294:  
  295. 295:     }
  296. 296:  
  297. 297:  
  298. 298:  
  299. 299:     /**
  300. 300:      * Appends the query part of URI.
  301. 301:      * @param  string|array
  302. 302:      * @return void 
  303. 303:      */
  304. 304:     public function appendQuery($value)
  305. 305:     {
  306. 306:         $this->updating();
  307. 307:         $value = (string) (is_array($valuehttp_build_query($value'''&'$value);
  308. 308:         $this->query .= ($this->query === '' || $value === ''$value '&' $value;
  309. 309:     }
  310. 310:  
  311. 311:  
  312. 312:  
  313. 313:     /**
  314. 314:      * Returns the query part of URI.
  315. 315:      * @return string 
  316. 316:      */
  317. 317:     public function getQuery()
  318. 318:     {
  319. 319:         return $this->query;
  320. 320:     }
  321. 321:  
  322. 322:  
  323. 323:  
  324. 324:     /**
  325. 325:      * Sets the fragment part of URI.
  326. 326:      * @param  string 
  327. 327:      * @return void 
  328. 328:      */
  329. 329:     public function setFragment($value)
  330. 330:     {
  331. 331:         $this->updating();
  332. 332:         $this->fragment = (string) $value;
  333. 333:     }
  334. 334:  
  335. 335:  
  336. 336:  
  337. 337:     /**
  338. 338:      * Returns the fragment part of URI.
  339. 339:      * @return string 
  340. 340:      */
  341. 341:     public function getFragment()
  342. 342:     {
  343. 343:         return $this->fragment;
  344. 344:     }
  345. 345:  
  346. 346:  
  347. 347:  
  348. 348:     /**
  349. 349:      * Returns the entire URI including query string and fragment.
  350. 350:      * @return string 
  351. 351:      */
  352. 352:     public function getAbsoluteUri()
  353. 353:     {
  354. 354:         return $this->scheme '://' $this->getAuthority($this->path
  355. 355:             . ($this->query === '' '' '?' $this->query)
  356. 356:             . ($this->fragment === '' '' '#' $this->fragment);
  357. 357:     }
  358. 358:  
  359. 359:  
  360. 360:  
  361. 361:     /**
  362. 362:      * Returns the [user[:pass]@]host[:port] part of URI.
  363. 363:      * @return string 
  364. 364:      */
  365. 365:     public function getAuthority()
  366. 366:     {
  367. 367:         $authority $this->host;
  368. 368:         if ($this->port && isset(self::$defaultPorts[$this->scheme]&& $this->port !== self::$defaultPorts[$this->scheme]{
  369. 369:             $authority .= ':' $this->port;
  370. 370:         }
  371. 371:  
  372. 372:         if ($this->user !== '' && $this->scheme !== 'http' && $this->scheme !== 'https'{
  373. 373:             $authority $this->user ($this->pass === '' '' ':' $this->pass'@' $authority;
  374. 374:         }
  375. 375:  
  376. 376:         return $authority;
  377. 377:     }
  378. 378:  
  379. 379:  
  380. 380:  
  381. 381:     /**
  382. 382:      * Returns the scheme and authority part of URI.
  383. 383:      * @return string 
  384. 384:      */
  385. 385:     public function getHostUri()
  386. 386:     {
  387. 387:         return $this->scheme '://' $this->getAuthority();
  388. 388:     }
  389. 389:  
  390. 390:  
  391. 391:  
  392. 392:     /**
  393. 393:      * URI comparsion (this object must be in canonical form).
  394. 394:      * @param  string 
  395. 395:      * @return bool 
  396. 396:      */
  397. 397:     public function isEqual($uri)
  398. 398:     {
  399. 399:         // compare host + path
  400. 400:         $part self::unescape(strtok($uri'?#')'%/');
  401. 401:         if (strncmp($part'//'2=== 0// absolute URI without scheme
  402. 402:             if ($part !== '//' $this->getAuthority($this->pathreturn FALSE;
  403. 403:  
  404. 404:         elseif (strncmp($part'/'1=== 0// absolute path
  405. 405:             if ($part !== $this->pathreturn FALSE;
  406. 406:  
  407. 407:         else {
  408. 408:             if ($part !== $this->scheme '://' $this->getAuthority($this->pathreturn FALSE;
  409. 409:         }
  410. 410:  
  411. 411:         // compare query strings
  412. 412:         $part = (string) strtok('?#');
  413. 413:         if ($part !== ''{
  414. 414:             $tmp preg_split('#[&;]#'self::unescape(strtr($part'+'' ')'%&;=+'));
  415. 415:             sort($tmp);
  416. 416:             $part implode('&'$tmp);
  417. 417:         }
  418. 418:         return $part === $this->query;
  419. 419:     }
  420. 420:  
  421. 421:  
  422. 422:  
  423. 423:     /**
  424. 424:      * Transform to canonical form.
  425. 425:      * @return void 
  426. 426:      */
  427. 427:     public function canonicalize()
  428. 428:     {
  429. 429:         $this->updating();
  430. 430:         $this->path $this->path === '' '/' self::unescape($this->path'%/');
  431. 431:  
  432. 432:         $this->host strtolower(rawurldecode($this->host));
  433. 433:  
  434. 434:         if ($this->query !== ''{
  435. 435:             $tmp preg_split('#[&;]#'self::unescape(strtr($this->query'+'' ')'%&;=+'));
  436. 436:             sort($tmp);
  437. 437:             $this->query implode('&'$tmp);
  438. 438:         }
  439. 439:     }
  440. 440:  
  441. 441:  
  442. 442:  
  443. 443:     /**
  444. 444:      * @return string 
  445. 445:      */
  446. 446:     public function __toString()
  447. 447:     {
  448. 448:         return $this->getAbsoluteUri();
  449. 449:     }
  450. 450:  
  451. 451:  
  452. 452:  
  453. 453:     /**
  454. 454:      * Similar to rawurldecode, but preserve reserved chars encoded.
  455. 455:      * @param  string to decode
  456. 456:      * @param  string reserved characters
  457. 457:      * @return string 
  458. 458:      */
  459. 459:     public static function unescape($s$reserved '%;/?:@&=+$,')
  460. 460:     {
  461. 461:         // reserved (@see RFC 2396) = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
  462. 462:         // within a path segment, the characters "/", ";", "=", "?" are reserved
  463. 463:         // within a query component, the characters ";", "/", "?", ":", "@", "&", "=", "+", ",", "$" are reserved.
  464. 464:         preg_match_all('#(?<=%)[a-f0-9][a-f0-9]#i'$s$matchesPREG_OFFSET_CAPTURE PREG_SET_ORDER);
  465. 465:         foreach (array_reverse($matchesas $match{
  466. 466:             $ch chr(hexdec($match[0][0]));
  467. 467:             if (strpos($reserved$ch=== FALSE{
  468. 468:                 $s substr_replace($s$ch$match[0][113);
  469. 469:             }
  470. 470:         }
  471. 471:         return $s;
  472. 472:     }
  473. 473: