Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • 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

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:  * @author     David Grudl
 18:  */
 19: class Response extends Nette\Object implements IResponse
 20: {
 21:     /** @var bool  Send invisible garbage for IE 6? */
 22:     private static $fixIE = TRUE;
 23: 
 24:     /** @var string The domain in which the cookie will be available */
 25:     public $cookieDomain = '';
 26: 
 27:     /** @var string The path in which the cookie will be available */
 28:     public $cookiePath = '/';
 29: 
 30:     /** @var string Whether the cookie is available only through HTTPS */
 31:     public $cookieSecure = FALSE;
 32: 
 33:     /** @var string Whether the cookie is hidden from client-side */
 34:     public $cookieHttpOnly = TRUE;
 35: 
 36:     /** @var bool Whether warn on possible problem with data in output buffer */
 37:     public $warnOnBuffer = TRUE;
 38: 
 39:     /** @var int HTTP response code */
 40:     private $code = self::S200_OK;
 41: 
 42: 
 43:     public function __construct()
 44:     {
 45:         if (PHP_VERSION_ID >= 50400) {
 46:             if (is_int($code = http_response_code())) {
 47:                 $this->code = $code;
 48:             }
 49:         }
 50: 
 51:         if (PHP_VERSION_ID >= 50401) { // PHP bug #61106
 52:             header_register_callback($this->removeDuplicateCookies); // requires closure due PHP bug #66375
 53:         }
 54:     }
 55: 
 56: 
 57:     /**
 58:      * Sets HTTP response code.
 59:      * @param  int
 60:      * @return self
 61:      * @throws Nette\InvalidArgumentException  if code is invalid
 62:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
 63:      */
 64:     public function setCode($code)
 65:     {
 66:         $code = (int) $code;
 67:         if ($code < 100 || $code > 599) {
 68:             throw new Nette\InvalidArgumentException("Bad HTTP response '$code'.");
 69:         }
 70:         self::checkHeaders();
 71:         $this->code = $code;
 72:         $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
 73:         header($protocol . ' ' . $code, TRUE, $code);
 74:         return $this;
 75:     }
 76: 
 77: 
 78:     /**
 79:      * Returns HTTP response code.
 80:      * @return int
 81:      */
 82:     public function getCode()
 83:     {
 84:         return $this->code;
 85:     }
 86: 
 87: 
 88:     /**
 89:      * Sends a HTTP header and replaces a previous one.
 90:      * @param  string  header name
 91:      * @param  string  header value
 92:      * @return self
 93:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
 94:      */
 95:     public function setHeader($name, $value)
 96:     {
 97:         self::checkHeaders();
 98:         if ($value === NULL) {
 99:             header_remove($name);
100:         } elseif (strcasecmp($name, 'Content-Length') === 0 && ini_get('zlib.output_compression')) {
101:             // ignore, PHP bug #44164
102:         } else {
103:             header($name . ': ' . $value, TRUE, $this->code);
104:         }
105:         return $this;
106:     }
107: 
108: 
109:     /**
110:      * Adds HTTP header.
111:      * @param  string  header name
112:      * @param  string  header value
113:      * @return self
114:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
115:      */
116:     public function addHeader($name, $value)
117:     {
118:         self::checkHeaders();
119:         header($name . ': ' . $value, FALSE, $this->code);
120:         return $this;
121:     }
122: 
123: 
124:     /**
125:      * Sends a Content-type HTTP header.
126:      * @param  string  mime-type
127:      * @param  string  charset
128:      * @return self
129:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
130:      */
131:     public function setContentType($type, $charset = NULL)
132:     {
133:         $this->setHeader('Content-Type', $type . ($charset ? '; charset=' . $charset : ''));
134:         return $this;
135:     }
136: 
137: 
138:     /**
139:      * Redirects to a new URL. Note: call exit() after it.
140:      * @param  string  URL
141:      * @param  int     HTTP code
142:      * @return void
143:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
144:      */
145:     public function redirect($url, $code = self::S302_FOUND)
146:     {
147:         $this->setCode($code);
148:         $this->setHeader('Location', $url);
149:         if (preg_match('#^https?:|^\s*+[a-z0-9+.-]*+[^:]#i', $url)) {
150:             $escapedUrl = htmlSpecialChars($url, ENT_IGNORE | ENT_QUOTES, 'UTF-8');
151:             echo "<h1>Redirect</h1>\n\n<p><a href=\"$escapedUrl\">Please click here to continue</a>.</p>";
152:         }
153:     }
154: 
155: 
156:     /**
157:      * Sets the number of seconds before a page cached on a browser expires.
158:      * @param  string|int|\DateTime  time, value 0 means "until the browser is closed"
159:      * @return self
160:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
161:      */
162:     public function setExpiration($time)
163:     {
164:         if (!$time) { // no cache
165:             $this->setHeader('Cache-Control', 's-maxage=0, max-age=0, must-revalidate');
166:             $this->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
167:             return $this;
168:         }
169: 
170:         $time = DateTime::from($time);
171:         $this->setHeader('Cache-Control', 'max-age=' . ($time->format('U') - time()));
172:         $this->setHeader('Expires', self::date($time));
173:         return $this;
174:     }
175: 
176: 
177:     /**
178:      * Checks if headers have been sent.
179:      * @return bool
180:      */
181:     public function isSent()
182:     {
183:         return headers_sent();
184:     }
185: 
186: 
187:     /**
188:      * Returns value of an HTTP header.
189:      * @param  string
190:      * @param  mixed
191:      * @return mixed
192:      */
193:     public function getHeader($header, $default = NULL)
194:     {
195:         $header .= ':';
196:         $len = strlen($header);
197:         foreach (headers_list() as $item) {
198:             if (strncasecmp($item, $header, $len) === 0) {
199:                 return ltrim(substr($item, $len));
200:             }
201:         }
202:         return $default;
203:     }
204: 
205: 
206:     /**
207:      * Returns a list of headers to sent.
208:      * @return array (name => value)
209:      */
210:     public function getHeaders()
211:     {
212:         $headers = array();
213:         foreach (headers_list() as $header) {
214:             $a = strpos($header, ':');
215:             $headers[substr($header, 0, $a)] = (string) substr($header, $a + 2);
216:         }
217:         return $headers;
218:     }
219: 
220: 
221:     /**
222:      * Returns HTTP valid date format.
223:      * @param  string|int|DateTime
224:      * @return string
225:      */
226:     public static function date($time = NULL)
227:     {
228:         $time = DateTime::from($time);
229:         $time->setTimezone(new \DateTimeZone('GMT'));
230:         return $time->format('D, d M Y H:i:s \G\M\T');
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 self
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:         $this->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:     /**
294:      * Removes duplicate cookies from response.
295:      * @return void
296:      * @internal
297:      */
298:     public function removeDuplicateCookies()
299:     {
300:         if (headers_sent($file, $line) || ini_get('suhosin.cookie.encrypt')) {
301:             return;
302:         }
303: 
304:         $flatten = array();
305:         foreach (headers_list() as $header) {
306:             if (preg_match('#^Set-Cookie: .+?=#', $header, $m)) {
307:                 $flatten[$m[0]] = $header;
308:                 header_remove('Set-Cookie');
309:             }
310:         }
311:         foreach (array_values($flatten) as $key => $header) {
312:             header($header, $key === 0);
313:         }
314:     }
315: 
316: 
317:     private function checkHeaders()
318:     {
319:         if (headers_sent($file, $line)) {
320:             throw new Nette\InvalidStateException('Cannot send header after HTTP headers have been sent' . ($file ? " (output started at $file:$line)." : '.'));
321: 
322:         } elseif ($this->warnOnBuffer && ob_get_length() && !array_filter(ob_get_status(TRUE), function ($i) { return !$i['chunk_size']; })) {
323:             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);
324:         }
325:     }
326: 
327: }
328: 
Nette 2.2 API documentation generated by ApiGen 2.8.0