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
  • PHP

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:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\Http;
 13: 
 14: use Nette;
 15: 
 16: 
 17: /**
 18:  * HttpResponse class.
 19:  *
 20:  * @author     David Grudl
 21:  *
 22:  * @property   int $code
 23:  * @property-read bool $sent
 24:  * @property-read array $headers
 25:  */
 26: final class Response extends Nette\Object implements IResponse
 27: {
 28:     /** @var bool  Send invisible garbage for IE 6? */
 29:     private static $fixIE = TRUE;
 30: 
 31:     /** @var string The domain in which the cookie will be available */
 32:     public $cookieDomain = '';
 33: 
 34:     /** @var string The path in which the cookie will be available */
 35:     public $cookiePath = '/';
 36: 
 37:     /** @var string Whether the cookie is available only through HTTPS */
 38:     public $cookieSecure = FALSE;
 39: 
 40:     /** @var string Whether the cookie is hidden from client-side */
 41:     public $cookieHttpOnly = TRUE;
 42: 
 43:     /** @var int HTTP response code */
 44:     private $code = self::S200_OK;
 45: 
 46: 
 47:     /**
 48:      * Sets HTTP response code.
 49:      * @param  int
 50:      * @return self
 51:      * @throws Nette\InvalidArgumentException  if code is invalid
 52:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
 53:      */
 54:     public function setCode($code)
 55:     {
 56:         $code = (int) $code;
 57: 
 58:         if ($code < 100 || $code > 599) {
 59:             throw new Nette\InvalidArgumentException("Bad HTTP response '$code'.");
 60: 
 61:         } elseif (headers_sent($file, $line)) {
 62:             throw new Nette\InvalidStateException("Cannot set HTTP code after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
 63: 
 64:         } else {
 65:             $this->code = $code;
 66:             $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
 67:             header($protocol . ' ' . $code, TRUE, $code);
 68:         }
 69:         return $this;
 70:     }
 71: 
 72: 
 73:     /**
 74:      * Returns HTTP response code.
 75:      * @return int
 76:      */
 77:     public function getCode()
 78:     {
 79:         return $this->code;
 80:     }
 81: 
 82: 
 83:     /**
 84:      * Sends a HTTP header and replaces a previous one.
 85:      * @param  string  header name
 86:      * @param  string  header value
 87:      * @return self
 88:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
 89:      */
 90:     public function setHeader($name, $value)
 91:     {
 92:         if (headers_sent($file, $line)) {
 93:             throw new Nette\InvalidStateException("Cannot send header after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
 94:         }
 95: 
 96:         if ($value === NULL && function_exists('header_remove')) {
 97:             header_remove($name);
 98:         } elseif (strcasecmp($name, 'Content-Length') === 0 && ini_get('zlib.output_compression')) {
 99:             // ignore, PHP bug #44164
100:         } else {
101:             header($name . ': ' . $value, TRUE, $this->code);
102:         }
103:         return $this;
104:     }
105: 
106: 
107:     /**
108:      * Adds HTTP header.
109:      * @param  string  header name
110:      * @param  string  header value
111:      * @return self
112:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
113:      */
114:     public function addHeader($name, $value)
115:     {
116:         if (headers_sent($file, $line)) {
117:             throw new Nette\InvalidStateException("Cannot send header after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
118:         }
119: 
120:         header($name . ': ' . $value, FALSE, $this->code);
121:         return $this;
122:     }
123: 
124: 
125:     /**
126:      * Sends a Content-type HTTP header.
127:      * @param  string  mime-type
128:      * @param  string  charset
129:      * @return self
130:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
131:      */
132:     public function setContentType($type, $charset = NULL)
133:     {
134:         $this->setHeader('Content-Type', $type . ($charset ? '; charset=' . $charset : ''));
135:         return $this;
136:     }
137: 
138: 
139:     /**
140:      * Redirects to a new URL. Note: call exit() after it.
141:      * @param  string  URL
142:      * @param  int     HTTP code
143:      * @return void
144:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
145:      */
146:     public function redirect($url, $code = self::S302_FOUND)
147:     {
148:         if (isset($_SERVER['SERVER_SOFTWARE']) && preg_match('#^Microsoft-IIS/[1-5]#', $_SERVER['SERVER_SOFTWARE'])
149:             && $this->getHeader('Set-Cookie') !== NULL
150:         ) {
151:             $this->setHeader('Refresh', "0;url=$url");
152:             return;
153:         }
154: 
155:         $this->setCode($code);
156:         $this->setHeader('Location', $url);
157:         echo "<h1>Redirect</h1>\n\n<p><a href=\"" . htmlSpecialChars($url) . "\">Please click here to continue</a>.</p>";
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 = Nette\DateTime::from($time);
176:         $this->setHeader('Cache-Control', 'max-age=' . ($time->format('U') - time()));
177:         $this->setHeader('Expires', self::date($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:      * Return the value of the 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
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:      * Returns HTTP valid date format.
228:      * @param  string|int|DateTime
229:      * @return string
230:      */
231:     public static function date($time = NULL)
232:     {
233:         $time = Nette\DateTime::from($time);
234:         $time->setTimezone(new \DateTimeZone('GMT'));
235:         return $time->format('D, d M Y H:i:s \G\M\T');
236:     }
237: 
238: 
239:     /**
240:      * @return void
241:      */
242:     public function __destruct()
243:     {
244:         if (self::$fixIE && isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') !== FALSE
245:             && in_array($this->code, array(400, 403, 404, 405, 406, 408, 409, 410, 500, 501, 505), TRUE)
246:             && $this->getHeader('Content-Type', 'text/html') === 'text/html'
247:         ) {
248:             echo Nette\Utils\Strings::random(2e3, " \t\r\n"); // sends invisible garbage for IE
249:             self::$fixIE = FALSE;
250:         }
251:     }
252: 
253: 
254:     /**
255:      * Sends a cookie.
256:      * @param  string name of the cookie
257:      * @param  string value
258:      * @param  string|int|DateTime  expiration time, value 0 means "until the browser is closed"
259:      * @param  string
260:      * @param  string
261:      * @param  bool
262:      * @param  bool
263:      * @return self
264:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
265:      */
266:     public function setCookie($name, $value, $time, $path = NULL, $domain = NULL, $secure = NULL, $httpOnly = NULL)
267:     {
268:         if (headers_sent($file, $line)) {
269:             throw new Nette\InvalidStateException("Cannot set cookie after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
270:         }
271: 
272:         setcookie(
273:             $name,
274:             $value,
275:             $time ? Nette\DateTime::from($time)->format('U') : 0,
276:             $path === NULL ? $this->cookiePath : (string) $path,
277:             $domain === NULL ? $this->cookieDomain : (string) $domain,
278:             $secure === NULL ? $this->cookieSecure : (bool) $secure,
279:             $httpOnly === NULL ? $this->cookieHttpOnly : (bool) $httpOnly
280:         );
281: 
282:         $this->removeDuplicateCookies();
283:         return $this;
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:                 if (PHP_VERSION_ID < 50300) { // multiple deleting due PHP bug #61605
302:                     header('Set-Cookie:');
303:                 } else {
304:                     header_remove('Set-Cookie');
305:                 }
306:             }
307:         }
308:         foreach (array_values($flatten) as $key => $header) {
309:             header($header, $key === 0);
310:         }
311:     }
312: 
313: 
314:     /**
315:      * Deletes a cookie.
316:      * @param  string name of the cookie.
317:      * @param  string
318:      * @param  string
319:      * @param  bool
320:      * @return void
321:      * @throws Nette\InvalidStateException  if HTTP headers have been sent
322:      */
323:     public function deleteCookie($name, $path = NULL, $domain = NULL, $secure = NULL)
324:     {
325:         $this->setCookie($name, FALSE, 0, $path, $domain, $secure);
326:     }
327: 
328: }
329: 
Nette Framework 2.0.12 API API documentation generated by ApiGen 2.8.0