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