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