Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • Context
  • FileUpload
  • Helpers
  • Request
  • RequestFactory
  • Response
  • Session
  • SessionSection
  • Url
  • UrlScript
  • UserStorage

Interfaces

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