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
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • 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 (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Http;
  9: 
 10: use Nette;
 11: use Nette\Utils\DateTime;
 12: 
 13: 
 14: /**
 15:  * HttpResponse class.
 16:  *
 17:  * @property-read array $headers
 18:  */
 19: class Response implements IResponse
 20: {
 21:     use Nette\SmartObject;
 22: 
 23:     /** @var bool  Send invisible garbage for IE 6? */
 24:     private static $fixIE = TRUE;
 25: 
 26:     /** @var string The domain in which the cookie will be available */
 27:     public $cookieDomain = '';
 28: 
 29:     /** @var string The path in which the cookie will be available */
 30:     public $cookiePath = '/';
 31: 
 32:     /** @var bool Whether the cookie is available only through HTTPS */
 33:     public $cookieSecure = FALSE;
 34: 
 35:     /** @var bool Whether the cookie is hidden from client-side */
 36:     public $cookieHttpOnly = TRUE;
 37: 
 38:     /** @var bool Whether warn on possible problem with data in output buffer */
 39:     public $warnOnBuffer = TRUE;
 40: 
 41:     /** @var int HTTP response code */
 42:     private $code = self::S200_OK;
 43: 
 44: 
 45:     public function __construct()
 46:     {
 47:         if (is_int($code = http_response_code())) {
 48:             $this->code = $code;
 49:         }
 50: 
 51:     }
 52: 
 53: 
 54:     /**
 55:      * Sets HTTP response code.
 56:      * @param  int
 57:      * @return self
 58:      * @throws Nette\InvalidArgumentException  if code is invalid
 59:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
 60:      */
 61:     public function setCode($code)
 62:     {
 63:         $code = (int) $code;
 64:         if ($code < 100 || $code > 599) {
 65:             throw new Nette\InvalidArgumentException("Bad HTTP response '$code'.");
 66:         }
 67:         self::checkHeaders();
 68:         $this->code = $code;
 69:         $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
 70:         header($protocol . ' ' . $code, TRUE, $code);
 71:         return $this;
 72:     }
 73: 
 74: 
 75:     /**
 76:      * Returns HTTP response code.
 77:      * @return int
 78:      */
 79:     public function getCode()
 80:     {
 81:         return $this->code;
 82:     }
 83: 
 84: 
 85:     /**
 86:      * Sends a HTTP header and replaces a previous one.
 87:      * @param  string  header name
 88:      * @param  string  header value
 89:      * @return self
 90:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
 91:      */
 92:     public function setHeader($name, $value)
 93:     {
 94:         self::checkHeaders();
 95:         if ($value === NULL) {
 96:             header_remove($name);
 97:         } elseif (strcasecmp($name, 'Content-Length') === 0 && ini_get('zlib.output_compression')) {
 98:             // ignore, PHP bug #44164
 99:         } else {
100:             header($name . ': ' . $value, TRUE, $this->code);
101:         }
102:         return $this;
103:     }
104: 
105: 
106:     /**
107:      * Adds HTTP header.
108:      * @param  string  header name
109:      * @param  string  header value
110:      * @return self
111:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
112:      */
113:     public function addHeader($name, $value)
114:     {
115:         self::checkHeaders();
116:         header($name . ': ' . $value, FALSE, $this->code);
117:         return $this;
118:     }
119: 
120: 
121:     /**
122:      * Sends a Content-type HTTP header.
123:      * @param  string  mime-type
124:      * @param  string  charset
125:      * @return self
126:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
127:      */
128:     public function setContentType($type, $charset = NULL)
129:     {
130:         $this->setHeader('Content-Type', $type . ($charset ? '; charset=' . $charset : ''));
131:         return $this;
132:     }
133: 
134: 
135:     /**
136:      * Redirects to a new URL. Note: call exit() after it.
137:      * @param  string  URL
138:      * @param  int     HTTP code
139:      * @return void
140:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
141:      */
142:     public function redirect($url, $code = self::S302_FOUND)
143:     {
144:         $this->setCode($code);
145:         $this->setHeader('Location', $url);
146:         if (preg_match('#^https?:|^\s*+[a-z0-9+.-]*+[^:]#i', $url)) {
147:             $escapedUrl = htmlSpecialChars($url, ENT_IGNORE | ENT_QUOTES, 'UTF-8');
148:             echo "<h1>Redirect</h1>\n\n<p><a href=\"$escapedUrl\">Please click here to continue</a>.</p>";
149:         }
150:     }
151: 
152: 
153:     /**
154:      * Sets the number of seconds before a page cached on a browser expires.
155:      * @param  string|int|\DateTimeInterface  time, value 0 means "until the browser is closed"
156:      * @return self
157:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
158:      */
159:     public function setExpiration($time)
160:     {
161:         $this->setHeader('Pragma', NULL);
162:         if (!$time) { // no cache
163:             $this->setHeader('Cache-Control', 's-maxage=0, max-age=0, must-revalidate');
164:             $this->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
165:             return $this;
166:         }
167: 
168:         $time = DateTime::from($time);
169:         $this->setHeader('Cache-Control', 'max-age=' . ($time->format('U') - time()));
170:         $this->setHeader('Expires', Helpers::formatDate($time));
171:         return $this;
172:     }
173: 
174: 
175:     /**
176:      * Checks if headers have been sent.
177:      * @return bool
178:      */
179:     public function isSent()
180:     {
181:         return headers_sent();
182:     }
183: 
184: 
185:     /**
186:      * Returns value of an HTTP header.
187:      * @param  string
188:      * @param  mixed
189:      * @return mixed
190:      */
191:     public function getHeader($header, $default = NULL)
192:     {
193:         $header .= ':';
194:         $len = strlen($header);
195:         foreach (headers_list() as $item) {
196:             if (strncasecmp($item, $header, $len) === 0) {
197:                 return ltrim(substr($item, $len));
198:             }
199:         }
200:         return $default;
201:     }
202: 
203: 
204:     /**
205:      * Returns a list of headers to sent.
206:      * @return array (name => value)
207:      */
208:     public function getHeaders()
209:     {
210:         $headers = [];
211:         foreach (headers_list() as $header) {
212:             $a = strpos($header, ':');
213:             $headers[substr($header, 0, $a)] = (string) substr($header, $a + 2);
214:         }
215:         return $headers;
216:     }
217: 
218: 
219:     /**
220:      * @deprecated
221:      */
222:     public static function date($time = NULL)
223:     {
224:         trigger_error('Method date() is deprecated, use Nette\Http\Helpers::formatDate() instead.', E_USER_DEPRECATED);
225:         return Helpers::formatDate($time);
226:     }
227: 
228: 
229:     /**
230:      * @return void
231:      */
232:     public function __destruct()
233:     {
234:         if (self::$fixIE && isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') !== FALSE
235:             && in_array($this->code, [400, 403, 404, 405, 406, 408, 409, 410, 500, 501, 505], TRUE)
236:             && preg_match('#^text/html(?:;|$)#', $this->getHeader('Content-Type', 'text/html'))
237:         ) {
238:             echo Nette\Utils\Random::generate(2e3, " \t\r\n"); // sends invisible garbage for IE
239:             self::$fixIE = FALSE;
240:         }
241:     }
242: 
243: 
244:     /**
245:      * Sends a cookie.
246:      * @param  string name of the cookie
247:      * @param  string value
248:      * @param  string|int|\DateTimeInterface  expiration time, value 0 means "until the browser is closed"
249:      * @param  string
250:      * @param  string
251:      * @param  bool
252:      * @param  bool
253:      * @return self
254:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
255:      */
256:     public function setCookie($name, $value, $time, $path = NULL, $domain = NULL, $secure = NULL, $httpOnly = NULL)
257:     {
258:         self::checkHeaders();
259:         setcookie(
260:             $name,
261:             $value,
262:             $time ? (int) DateTime::from($time)->format('U') : 0,
263:             $path === NULL ? $this->cookiePath : (string) $path,
264:             $domain === NULL ? $this->cookieDomain : (string) $domain,
265:             $secure === NULL ? $this->cookieSecure : (bool) $secure,
266:             $httpOnly === NULL ? $this->cookieHttpOnly : (bool) $httpOnly
267:         );
268:         Helpers::removeDuplicateCookies();
269:         return $this;
270:     }
271: 
272: 
273:     /**
274:      * Deletes a cookie.
275:      * @param  string name of the cookie.
276:      * @param  string
277:      * @param  string
278:      * @param  bool
279:      * @return void
280:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
281:      */
282:     public function deleteCookie($name, $path = NULL, $domain = NULL, $secure = NULL)
283:     {
284:         $this->setCookie($name, FALSE, 0, $path, $domain, $secure);
285:     }
286: 
287: 
288:     private function checkHeaders()
289:     {
290:         if (PHP_SAPI === 'cli') {
291: 
292:         } elseif (headers_sent($file, $line)) {
293:             throw new Nette\InvalidStateException('Cannot send header after HTTP headers have been sent' . ($file ? " (output started at $file:$line)." : '.'));
294: 
295:         } elseif ($this->warnOnBuffer && ob_get_length() && !array_filter(ob_get_status(TRUE), function ($i) { return !$i['chunk_size']; })) {
296:             trigger_error('Possible problem: you are sending a HTTP header while already having some data in output buffer. Try Tracy\OutputDebugger or start session earlier.');
297:         }
298:     }
299: 
300: }
301: 
Nette 2.4-20160930 API API documentation generated by ApiGen 2.8.0