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