Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • none

Classes

Interfaces

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