Source for file HttpResponse.php
Documentation is available at HttpResponse.php
6: * @copyright Copyright (c) 2004, 2010 David Grudl
7: * @license http://nettephp.com/license Nette license
8: * @link http://nettephp.com
16: * HttpResponse class.
18: * @copyright Copyright (c) 2004, 2010 David Grudl
21: * @property int $code
22: * @property-read array $headers
23: * @property-read mixed $sent
27: /** @var bool Send invisible garbage for IE 6? */
28: private static $fixIE =
TRUE;
30: /** @var string The domain in which the cookie will be available */
33: /** @var string The path in which the cookie will be available */
36: /** @var string The path in which the cookie will be available */
39: /** @var int HTTP response code */
40: private $code =
self::S200_OK;
45: * Sets HTTP response code.
47: * @return HttpResponse provides a fluent interface
48: * @throws InvalidArgumentException if code is invalid
49: * @throws InvalidStateException if HTTP headers have been sent
55: static $allowed =
array(
56: 200=>
1, 201=>
1, 202=>
1, 203=>
1, 204=>
1, 205=>
1, 206=>
1,
57: 300=>
1, 301=>
1, 302=>
1, 303=>
1, 304=>
1, 307=>
1,
58: 400=>
1, 401=>
1, 403=>
1, 404=>
1, 406=>
1, 408=>
1, 410=>
1, 412=>
1, 415=>
1, 416=>
1,
59: 500=>
1, 501=>
1, 503=>
1, 505=>
1
62: if (!isset($allowed[$code])) {
63: throw new InvalidArgumentException("Bad HTTP response '
$code'.
");
65: } elseif (headers_sent($file, $line)) {
66: throw new InvalidStateException("Cannot set HTTP code after HTTP headers have been sent" .
($file ?
" (output started at
$file:
$line).
" :
"."));
70: $protocol =
isset($_SERVER['SERVER_PROTOCOL']) ?
$_SERVER['SERVER_PROTOCOL'] :
'HTTP/1.1';
71: header($protocol .
' ' .
$code, TRUE, $code);
79: * Returns HTTP response code.
90: * Sends a HTTP header and replaces a previous one.
91: * @param string header name
92: * @param string header value
93: * @return HttpResponse provides a fluent interface
94: * @throws InvalidStateException if HTTP headers have been sent
99: throw new InvalidStateException("Cannot send header after HTTP headers have been sent" .
($file ?
" (output started at $file:$line)." :
"."));
103: header_remove($name);
105: header($name .
': ' .
$value, TRUE, $this->code);
114: * @param string header name
115: * @param string header value
117: * @throws InvalidStateException if HTTP headers have been sent
122: throw new InvalidStateException("Cannot send header after HTTP headers have been sent" .
($file ?
" (output started at $file:$line)." :
"."));
125: header($name .
': ' .
$value, FALSE, $this->code);
131: * Sends a Content-type HTTP header.
132: * @param string mime-type
133: * @param string charset
134: * @return HttpResponse provides a fluent interface
135: * @throws InvalidStateException if HTTP headers have been sent
139: $this->setHeader('Content-Type', $type .
($charset ?
'; charset=' .
$charset :
''));
146: * Redirects to a new URL. Note: call exit() after it.
148: * @param int HTTP code
150: * @throws InvalidStateException if HTTP headers have been sent
152: public function redirect($url, $code =
self::S302_FOUND)
154: if (isset($_SERVER['SERVER_SOFTWARE']) &&
preg_match('#^Microsoft-IIS/[1-5]#', $_SERVER['SERVER_SOFTWARE']) &&
$this->getHeader('Set-Cookie') !==
NULL) {
161: echo "<h1>Redirect</h1>\n\n<p><a href=\"" .
htmlSpecialChars($url) .
"\">Please click here to continue</a>.</p>";
167: * Sets the number of seconds before a page cached on a browser expires.
168: * @param string|int|DateTime time, value 0 means "until the browser is closed"
169: * @return HttpResponse provides a fluent interface
170: * @throws InvalidStateException if HTTP headers have been sent
174: if (!$time) { // no cache
175: $this->setHeader('Cache-Control', 's-maxage=0, max-age=0, must-revalidate');
176: $this->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
191: trigger_error(__METHOD__ .
'() is deprecated; use setExpiration() instead.', E_USER_WARNING);
198: * Checks if headers have been sent.
209: * Return the value of the HTTP header.
229: * Returns a list of headers to sent.
245: * Returns HTTP valid date format.
246: * @param string|int|DateTime
249: public static function date($time =
NULL)
252: $time->setTimezone(new DateTimeZone('GMT'));
253: return $time->format('D, d M Y H:i:s \G\M\T');
259: * Enables compression. (warning: may not work)
269: return FALSE; // called twice
273: if ($ok ===
FALSE) {
274: return FALSE; // not allowed
293: // Sends invisible garbage for IE.
294: if (!isset($_SERVER['HTTP_USER_AGENT']) ||
strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') ===
FALSE) return;
295: if (!in_array($this->code, array(400, 403, 404, 405, 406, 408, 409, 410, 500, 501, 505), TRUE)) return;
296: if ($this->getHeader('Content-Type', 'text/html') !==
'text/html') return;
298: for ($i =
2e3; $i; $i--
) echo $s{rand(0, 3)};
299: self::$fixIE =
FALSE;
307: * @param string name of the cookie
308: * @param string value
309: * @param string|int|DateTime expiration time, value 0 means "until the browser is closed"
313: * @return HttpResponse provides a fluent interface
314: * @throws InvalidStateException if HTTP headers have been sent
316: public function setCookie($name, $value, $time, $path =
NULL, $domain =
NULL, $secure =
NULL)
319: throw new InvalidStateException("Cannot set cookie after HTTP headers have been sent" .
($file ?
" (output started at $file:$line)." :
"."));
327: $domain ===
NULL ?
$this->cookieDomain : (string)
$domain, // . '; httponly'
329: TRUE // added in PHP 5.2.0.
338: * @param string name of the cookie.
343: * @throws InvalidStateException if HTTP headers have been sent
345: public function deleteCookie($name, $path =
NULL, $domain =
NULL, $secure =
NULL)
348: throw new InvalidStateException("Cannot delete cookie after HTTP headers have been sent" .
($file ?
" (output started at $file:$line)." :
"."));
356: $domain ===
NULL ?
$this->cookieDomain : (string)
$domain, // . '; httponly'
358: TRUE // added in PHP 5.2.0.