Namespaces

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

Classes

  • Context
  • FileUpload
  • 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: 
 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:     /**
 44:      * Sets HTTP response code.
 45:      * @param  int
 46:      * @return self
 47:      * @throws Nette\InvalidArgumentException  if code is invalid
 48:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
 49:      */
 50:     public function setCode($code)
 51:     {
 52:         $code = (int) $code;
 53: 
 54:         if ($code < 100 || $code > 599) {
 55:             throw new Nette\InvalidArgumentException("Bad HTTP response '$code'.");
 56: 
 57:         } elseif (headers_sent($file, $line)) {
 58:             throw new Nette\InvalidStateException("Cannot set HTTP code after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
 59: 
 60:         } else {
 61:             $this->code = $code;
 62:             $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
 63:             header($protocol . ' ' . $code, TRUE, $code);
 64:         }
 65:         return $this;
 66:     }
 67: 
 68: 
 69:     /**
 70:      * Returns HTTP response code.
 71:      * @return int
 72:      */
 73:     public function getCode()
 74:     {
 75:         return $this->code;
 76:     }
 77: 
 78: 
 79:     /**
 80:      * Sends a HTTP header and replaces a previous one.
 81:      * @param  string  header name
 82:      * @param  string  header value
 83:      * @return self
 84:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
 85:      */
 86:     public function setHeader($name, $value)
 87:     {
 88:         if (headers_sent($file, $line)) {
 89:             throw new Nette\InvalidStateException("Cannot send header after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
 90:         }
 91: 
 92:         if ($value === NULL && function_exists('header_remove')) {
 93:             header_remove($name);
 94:         } elseif (strcasecmp($name, 'Content-Length') === 0 && ini_get('zlib.output_compression')) {
 95:             // ignore, PHP bug #44164
 96:         } else {
 97:             header($name . ': ' . $value, TRUE, $this->code);
 98:         }
 99:         return $this;
100:     }
101: 
102: 
103:     /**
104:      * Adds HTTP header.
105:      * @param  string  header name
106:      * @param  string  header value
107:      * @return self
108:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
109:      */
110:     public function addHeader($name, $value)
111:     {
112:         if (headers_sent($file, $line)) {
113:             throw new Nette\InvalidStateException("Cannot send header after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
114:         }
115: 
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:         if (isset($_SERVER['SERVER_SOFTWARE']) && preg_match('#^Microsoft-IIS/[1-5]#', $_SERVER['SERVER_SOFTWARE'])
145:             && $this->getHeader('Set-Cookie') !== NULL
146:         ) {
147:             $this->setHeader('Refresh', "0;url=$url");
148:             return;
149:         }
150: 
151:         $this->setCode($code);
152:         $this->setHeader('Location', $url);
153:         echo "<h1>Redirect</h1>\n\n<p><a href=\"" . htmlSpecialChars($url) . "\">Please click here to continue</a>.</p>";
154:     }
155: 
156: 
157:     /**
158:      * Sets the number of seconds before a page cached on a browser expires.
159:      * @param  string|int|DateTime  time, value 0 means "until the browser is closed"
160:      * @return self
161:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
162:      */
163:     public function setExpiration($time)
164:     {
165:         if (!$time) { // no cache
166:             $this->setHeader('Cache-Control', 's-maxage=0, max-age=0, must-revalidate');
167:             $this->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
168:             return $this;
169:         }
170: 
171:         $time = Nette\DateTime::from($time);
172:         $this->setHeader('Cache-Control', 'max-age=' . ($time->format('U') - time()));
173:         $this->setHeader('Expires', self::date($time));
174:         return $this;
175:     }
176: 
177: 
178:     /**
179:      * Checks if headers have been sent.
180:      * @return bool
181:      */
182:     public function isSent()
183:     {
184:         return headers_sent();
185:     }
186: 
187: 
188:     /**
189:      * Return the value of the HTTP header.
190:      * @param  string
191:      * @param  mixed
192:      * @return mixed
193:      */
194:     public function getHeader($header, $default = NULL)
195:     {
196:         $header .= ':';
197:         $len = strlen($header);
198:         foreach (headers_list() as $item) {
199:             if (strncasecmp($item, $header, $len) === 0) {
200:                 return ltrim(substr($item, $len));
201:             }
202:         }
203:         return $default;
204:     }
205: 
206: 
207:     /**
208:      * Returns a list of headers to sent.
209:      * @return array
210:      */
211:     public function getHeaders()
212:     {
213:         $headers = array();
214:         foreach (headers_list() as $header) {
215:             $a = strpos($header, ':');
216:             $headers[substr($header, 0, $a)] = (string) substr($header, $a + 2);
217:         }
218:         return $headers;
219:     }
220: 
221: 
222:     /**
223:      * Returns HTTP valid date format.
224:      * @param  string|int|DateTime
225:      * @return string
226:      */
227:     public static function date($time = NULL)
228:     {
229:         $time = Nette\DateTime::from($time);
230:         $time->setTimezone(new \DateTimeZone('GMT'));
231:         return $time->format('D, d M Y H:i:s \G\M\T');
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:             && $this->getHeader('Content-Type', 'text/html') === 'text/html'
243:         ) {
244:             echo Nette\Utils\Strings::random(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:         if (headers_sent($file, $line)) {
265:             throw new Nette\InvalidStateException("Cannot set cookie after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
266:         }
267: 
268:         setcookie(
269:             $name,
270:             $value,
271:             $time ? Nette\DateTime::from($time)->format('U') : 0,
272:             $path === NULL ? $this->cookiePath : (string) $path,
273:             $domain === NULL ? $this->cookieDomain : (string) $domain,
274:             $secure === NULL ? $this->cookieSecure : (bool) $secure,
275:             $httpOnly === NULL ? $this->cookieHttpOnly : (bool) $httpOnly
276:         );
277: 
278:         $this->removeDuplicateCookies();
279:         return $this;
280:     }
281: 
282: 
283:     /**
284:      * Removes duplicate cookies from response.
285:      * @return void
286:      * @internal
287:      */
288:     public function removeDuplicateCookies()
289:     {
290:         if (headers_sent($file, $line) || ini_get('suhosin.cookie.encrypt')) {
291:             return;
292:         }
293: 
294:         $flatten = array();
295:         foreach (headers_list() as $header) {
296:             if (preg_match('#^Set-Cookie: .+?=#', $header, $m)) {
297:                 $flatten[$m[0]] = $header;
298:                 if (PHP_VERSION_ID < 50300) { // multiple deleting due PHP bug #61605
299:                     header('Set-Cookie:');
300:                 } else {
301:                     header_remove('Set-Cookie');
302:                 }
303:             }
304:         }
305:         foreach (array_values($flatten) as $key => $header) {
306:             header($header, $key === 0);
307:         }
308:     }
309: 
310: 
311:     /**
312:      * Deletes a cookie.
313:      * @param  string name of the cookie.
314:      * @param  string
315:      * @param  string
316:      * @param  bool
317:      * @return void
318:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
319:      */
320:     public function deleteCookie($name, $path = NULL, $domain = NULL, $secure = NULL)
321:     {
322:         $this->setCookie($name, FALSE, 0, $path, $domain, $secure);
323:     }
324: 
325: }
326: 
Nette Framework 2.0.17 API API documentation generated by ApiGen 2.8.0