Packages

  • 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

  • NHttpContext
  • NHttpRequest
  • NHttpRequestFactory
  • NHttpResponse
  • NHttpUploadedFile
  • NSession
  • NSessionSection
  • NUrl
  • NUrlScript
  • NUserStorage

Interfaces

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