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