Overview

Namespaces

  • Budovy
  • Kdyby
    • BootstrapFormRenderer
      • DI
      • Latte
  • 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
  • Nextras
    • Datagrid
  • None
  • PHP
  • Tester
    • CodeCoverage
    • Runner
      • Output
  • Vodacek
    • Forms
      • Controls
  • WebLoader
    • Filter
    • Nette

Classes

  • Context
  • FileUpload
  • Helpers
  • Request
  • RequestFactory
  • Response
  • Session
  • SessionSection
  • Url
  • UrlScript
  • UserStorage

Interfaces

  • IRequest
  • IResponse
  • Overview
  • Namespace
  • Class
  • Tree
  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: 
 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 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(http_response_code())) {
 47:                 $this->code = http_response_code();
 48:             }
 49:             header_register_callback($this->removeDuplicateCookies);
 50:         }
 51:     }
 52: 
 53: 
 54:     /**
 55:      * Sets HTTP response code.
 56:      * @param  int
 57:      * @return self
 58:      * @throws Nette\InvalidArgumentException  if code is invalid
 59:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
 60:      */
 61:     public function setCode($code)
 62:     {
 63:         $code = (int) $code;
 64:         if ($code < 100 || $code > 599) {
 65:             throw new Nette\InvalidArgumentException("Bad HTTP response '$code'.");
 66:         }
 67:         self::checkHeaders();
 68:         $this->code = $code;
 69:         $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
 70:         header($protocol . ' ' . $code, TRUE, $code);
 71:         return $this;
 72:     }
 73: 
 74: 
 75:     /**
 76:      * Returns HTTP response code.
 77:      * @return int
 78:      */
 79:     public function getCode()
 80:     {
 81:         return $this->code;
 82:     }
 83: 
 84: 
 85:     /**
 86:      * Sends a HTTP header and replaces a previous one.
 87:      * @param  string  header name
 88:      * @param  string  header value
 89:      * @return self
 90:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
 91:      */
 92:     public function setHeader($name, $value)
 93:     {
 94:         self::checkHeaders();
 95:         if ($value === NULL && function_exists('header_remove')) {
 96:             header_remove($name);
 97:         } elseif (strcasecmp($name, 'Content-Length') === 0 && ini_get('zlib.output_compression')) {
 98:             // ignore, PHP bug #44164
 99:         } else {
100:             header($name . ': ' . $value, TRUE, $this->code);
101:         }
102:         return $this;
103:     }
104: 
105: 
106:     /**
107:      * Adds HTTP header.
108:      * @param  string  header name
109:      * @param  string  header value
110:      * @return self
111:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
112:      */
113:     public function addHeader($name, $value)
114:     {
115:         self::checkHeaders();
116:         header($name . ': ' . $value, FALSE, $this->code);
117:         return $this;
118:     }
119: 
120: 
121:     /**
122:      * Sends a Content-type HTTP header.
123:      * @param  string  mime-type
124:      * @param  string  charset
125:      * @return self
126:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
127:      */
128:     public function setContentType($type, $charset = NULL)
129:     {
130:         $this->setHeader('Content-Type', $type . ($charset ? '; charset=' . $charset : ''));
131:         return $this;
132:     }
133: 
134: 
135:     /**
136:      * Redirects to a new URL. Note: call exit() after it.
137:      * @param  string  URL
138:      * @param  int     HTTP code
139:      * @return void
140:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
141:      */
142:     public function redirect($url, $code = self::S302_FOUND)
143:     {
144:         $this->setCode($code);
145:         $this->setHeader('Location', $url);
146:         echo "<h1>Redirect</h1>\n\n<p><a href=\"" . htmlSpecialChars($url, ENT_IGNORE | ENT_QUOTES) . "\">Please click here to continue</a>.</p>";
147:     }
148: 
149: 
150:     /**
151:      * Sets the number of seconds before a page cached on a browser expires.
152:      * @param  string|int|DateTime  time, value 0 means "until the browser is closed"
153:      * @return self
154:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
155:      */
156:     public function setExpiration($time)
157:     {
158:         if (!$time) { // no cache
159:             $this->setHeader('Cache-Control', 's-maxage=0, max-age=0, must-revalidate');
160:             $this->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
161:             return $this;
162:         }
163: 
164:         $time = Nette\DateTime::from($time);
165:         $this->setHeader('Cache-Control', 'max-age=' . ($time->format('U') - time()));
166:         $this->setHeader('Expires', self::date($time));
167:         return $this;
168:     }
169: 
170: 
171:     /**
172:      * Checks if headers have been sent.
173:      * @return bool
174:      */
175:     public function isSent()
176:     {
177:         return headers_sent();
178:     }
179: 
180: 
181:     /**
182:      * Return the value of the HTTP header.
183:      * @param  string
184:      * @param  mixed
185:      * @return mixed
186:      */
187:     public function getHeader($header, $default = NULL)
188:     {
189:         $header .= ':';
190:         $len = strlen($header);
191:         foreach (headers_list() as $item) {
192:             if (strncasecmp($item, $header, $len) === 0) {
193:                 return ltrim(substr($item, $len));
194:             }
195:         }
196:         return $default;
197:     }
198: 
199: 
200:     /**
201:      * Returns a list of headers to sent.
202:      * @return array
203:      */
204:     public function getHeaders()
205:     {
206:         $headers = array();
207:         foreach (headers_list() as $header) {
208:             $a = strpos($header, ':');
209:             $headers[substr($header, 0, $a)] = (string) substr($header, $a + 2);
210:         }
211:         return $headers;
212:     }
213: 
214: 
215:     /**
216:      * Returns HTTP valid date format.
217:      * @param  string|int|DateTime
218:      * @return string
219:      */
220:     public static function date($time = NULL)
221:     {
222:         $time = Nette\DateTime::from($time);
223:         $time->setTimezone(new \DateTimeZone('GMT'));
224:         return $time->format('D, d M Y H:i:s \G\M\T');
225:     }
226: 
227: 
228:     /**
229:      * @return void
230:      */
231:     public function __destruct()
232:     {
233:         if (self::$fixIE && isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') !== FALSE
234:             && in_array($this->code, array(400, 403, 404, 405, 406, 408, 409, 410, 500, 501, 505), TRUE)
235:             && preg_match('#^text/html(?:;|$)#', $this->getHeader('Content-Type', 'text/html'))
236:         ) {
237:             echo Nette\Utils\Strings::random(2e3, " \t\r\n"); // sends invisible garbage for IE
238:             self::$fixIE = FALSE;
239:         }
240:     }
241: 
242: 
243:     /**
244:      * Sends a cookie.
245:      * @param  string name of the cookie
246:      * @param  string value
247:      * @param  string|int|DateTime  expiration time, value 0 means "until the browser is closed"
248:      * @param  string
249:      * @param  string
250:      * @param  bool
251:      * @param  bool
252:      * @return self
253:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
254:      */
255:     public function setCookie($name, $value, $time, $path = NULL, $domain = NULL, $secure = NULL, $httpOnly = NULL)
256:     {
257:         self::checkHeaders();
258:         setcookie(
259:             $name,
260:             $value,
261:             $time ? Nette\DateTime::from($time)->format('U') : 0,
262:             $path === NULL ? $this->cookiePath : (string) $path,
263:             $domain === NULL ? $this->cookieDomain : (string) $domain,
264:             $secure === NULL ? $this->cookieSecure : (bool) $secure,
265:             $httpOnly === NULL ? $this->cookieHttpOnly : (bool) $httpOnly
266:         );
267:         $this->removeDuplicateCookies();
268:         return $this;
269:     }
270: 
271: 
272:     /**
273:      * Deletes a cookie.
274:      * @param  string name of the cookie.
275:      * @param  string
276:      * @param  string
277:      * @param  bool
278:      * @return void
279:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
280:      */
281:     public function deleteCookie($name, $path = NULL, $domain = NULL, $secure = NULL)
282:     {
283:         $this->setCookie($name, FALSE, 0, $path, $domain, $secure);
284:     }
285: 
286: 
287:     /**
288:      * Removes duplicate cookies from response.
289:      * @return void
290:      */
291:     public function removeDuplicateCookies()
292:     {
293:         if (headers_sent($file, $line) || ini_get('suhosin.cookie.encrypt')) {
294:             return;
295:         }
296: 
297:         $flatten = array();
298:         foreach (headers_list() as $header) {
299:             if (preg_match('#^Set-Cookie: .+?=#', $header, $m)) {
300:                 $flatten[$m[0]] = $header;
301:                 header_remove('Set-Cookie');
302:             }
303:         }
304:         foreach (array_values($flatten) as $key => $header) {
305:             header($header, $key === 0);
306:         }
307:     }
308: 
309: 
310:     private function checkHeaders()
311:     {
312:         if (headers_sent($file, $line)) {
313:             throw new Nette\InvalidStateException('Cannot send header after HTTP headers have been sent' . ($file ? " (output started at $file:$line)." : '.'));
314:         } elseif (ob_get_length() && !array_filter(ob_get_status(TRUE), function($i) { return !$i['chunk_size']; })) {
315:             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);
316:         }
317:     }
318: 
319: }
320: 
API documentation generated by ApiGen 2.8.0