Namespaces

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

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: 
 12: 
 13: /**
 14:  * HttpResponse class.
 15:  *
 16:  * @author     David Grudl
 17:  *
 18:  * @property   int $code
 19:  * @property-read bool $sent
 20:  * @property-read array $headers
 21:  */
 22: class Response extends Nette\Object implements IResponse
 23: {
 24:     /** @var bool  Send invisible garbage for IE 6? */
 25:     private static $fixIE = TRUE;
 26: 
 27:     /** @var string The domain in which the cookie will be available */
 28:     public $cookieDomain = '';
 29: 
 30:     /** @var string The path in which the cookie will be available */
 31:     public $cookiePath = '/';
 32: 
 33:     /** @var string Whether the cookie is available only through HTTPS */
 34:     public $cookieSecure = FALSE;
 35: 
 36:     /** @var string Whether the cookie is hidden from client-side */
 37:     public $cookieHttpOnly = TRUE;
 38: 
 39:     /** @var bool Whether warn on possible problem with data in output buffer */
 40:     public $warnOnBuffer = TRUE;
 41: 
 42:     /** @var int HTTP response code */
 43:     private $code = self::S200_OK;
 44: 
 45: 
 46:     public function __construct()
 47:     {
 48:         if (PHP_VERSION_ID >= 50400) {
 49:             if (is_int($code = http_response_code())) {
 50:                 $this->code = $code;
 51:             }
 52:         }
 53: 
 54:         if (PHP_VERSION_ID >= 50401) { // PHP bug #61106
 55:             header_register_callback($this->removeDuplicateCookies); // requires closure due PHP bug #66375
 56:         }
 57:     }
 58: 
 59: 
 60:     /**
 61:      * Sets HTTP response code.
 62:      * @param  int
 63:      * @return self
 64:      * @throws Nette\InvalidArgumentException  if code is invalid
 65:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
 66:      */
 67:     public function setCode($code)
 68:     {
 69:         $code = (int) $code;
 70:         if ($code < 100 || $code > 599) {
 71:             throw new Nette\InvalidArgumentException("Bad HTTP response '$code'.");
 72:         }
 73:         self::checkHeaders();
 74:         $this->code = $code;
 75:         $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
 76:         header($protocol . ' ' . $code, TRUE, $code);
 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 self
 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 self
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 self
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 "until the browser is closed"
162:      * @return self
163:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
164:      */
165:     public function setExpiration($time)
166:     {
167:         if (!$time) { // no cache
168:             $this->setHeader('Cache-Control', 's-maxage=0, max-age=0, must-revalidate');
169:             $this->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
170:             return $this;
171:         }
172: 
173:         $time = Nette\DateTime::from($time);
174:         $this->setHeader('Cache-Control', 'max-age=' . ($time->format('U') - time()));
175:         $this->setHeader('Expires', self::date($time));
176:         return $this;
177:     }
178: 
179: 
180:     /**
181:      * Checks if headers have been sent.
182:      * @return bool
183:      */
184:     public function isSent()
185:     {
186:         return headers_sent();
187:     }
188: 
189: 
190:     /**
191:      * Returns value of an HTTP header.
192:      * @param  string
193:      * @param  mixed
194:      * @return mixed
195:      */
196:     public function getHeader($header, $default = NULL)
197:     {
198:         $header .= ':';
199:         $len = strlen($header);
200:         foreach (headers_list() as $item) {
201:             if (strncasecmp($item, $header, $len) === 0) {
202:                 return ltrim(substr($item, $len));
203:             }
204:         }
205:         return $default;
206:     }
207: 
208: 
209:     /**
210:      * Returns a list of headers to sent.
211:      * @return array (name => value)
212:      */
213:     public function getHeaders()
214:     {
215:         $headers = array();
216:         foreach (headers_list() as $header) {
217:             $a = strpos($header, ':');
218:             $headers[substr($header, 0, $a)] = (string) substr($header, $a + 2);
219:         }
220:         return $headers;
221:     }
222: 
223: 
224:     /**
225:      * Returns HTTP valid date format.
226:      * @param  string|int|DateTime
227:      * @return string
228:      */
229:     public static function date($time = NULL)
230:     {
231:         $time = Nette\DateTime::from($time);
232:         $time->setTimezone(new \DateTimeZone('GMT'));
233:         return $time->format('D, d M Y H:i:s \G\M\T');
234:     }
235: 
236: 
237:     /**
238:      * @return void
239:      */
240:     public function __destruct()
241:     {
242:         if (self::$fixIE && isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') !== FALSE
243:             && in_array($this->code, array(400, 403, 404, 405, 406, 408, 409, 410, 500, 501, 505), TRUE)
244:             && preg_match('#^text/html(?:;|$)#', $this->getHeader('Content-Type', 'text/html'))
245:         ) {
246:             echo Nette\Utils\Strings::random(2e3, " \t\r\n"); // sends invisible garbage for IE
247:             self::$fixIE = FALSE;
248:         }
249:     }
250: 
251: 
252:     /**
253:      * Sends a cookie.
254:      * @param  string name of the cookie
255:      * @param  string value
256:      * @param  string|int|\DateTime  expiration time, value 0 means "until the browser is closed"
257:      * @param  string
258:      * @param  string
259:      * @param  bool
260:      * @param  bool
261:      * @return self
262:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
263:      */
264:     public function setCookie($name, $value, $time, $path = NULL, $domain = NULL, $secure = NULL, $httpOnly = NULL)
265:     {
266:         self::checkHeaders();
267:         setcookie(
268:             $name,
269:             $value,
270:             $time ? Nette\DateTime::from($time)->format('U') : 0,
271:             $path === NULL ? $this->cookiePath : (string) $path,
272:             $domain === NULL ? $this->cookieDomain : (string) $domain,
273:             $secure === NULL ? $this->cookieSecure : (bool) $secure,
274:             $httpOnly === NULL ? $this->cookieHttpOnly : (bool) $httpOnly
275:         );
276:         $this->removeDuplicateCookies();
277:         return $this;
278:     }
279: 
280: 
281:     /**
282:      * Deletes a cookie.
283:      * @param  string name of the cookie.
284:      * @param  string
285:      * @param  string
286:      * @param  bool
287:      * @return void
288:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
289:      */
290:     public function deleteCookie($name, $path = NULL, $domain = NULL, $secure = NULL)
291:     {
292:         $this->setCookie($name, FALSE, 0, $path, $domain, $secure);
293:     }
294: 
295: 
296:     /**
297:      * Removes duplicate cookies from response.
298:      * @return void
299:      * @internal
300:      */
301:     public function removeDuplicateCookies()
302:     {
303:         if (headers_sent($file, $line) || ini_get('suhosin.cookie.encrypt')) {
304:             return;
305:         }
306: 
307:         $flatten = array();
308:         foreach (headers_list() as $header) {
309:             if (preg_match('#^Set-Cookie: .+?=#', $header, $m)) {
310:                 $flatten[$m[0]] = $header;
311:                 header_remove('Set-Cookie');
312:             }
313:         }
314:         foreach (array_values($flatten) as $key => $header) {
315:             header($header, $key === 0);
316:         }
317:     }
318: 
319: 
320:     private function checkHeaders()
321:     {
322:         if (headers_sent($file, $line)) {
323:             throw new Nette\InvalidStateException('Cannot send header after HTTP headers have been sent' . ($file ? " (output started at $file:$line)." : '.'));
324: 
325:         } elseif ($this->warnOnBuffer && ob_get_length() && !array_filter(ob_get_status(TRUE), function ($i) { return !$i['chunk_size']; })) {
326:             trigger_error('Possible problem: you are sending a HTTP header while already having some data in output buffer. Try OutputDebugger or start session earlier.', E_USER_NOTICE);
327:         }
328:     }
329: 
330: }
331: 
Nette 2.1 API documentation generated by ApiGen 2.8.0