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

  • HttpContext
  • HttpRequest
  • HttpRequestFactory
  • HttpResponse
  • HttpUploadedFile
  • Session
  • SessionSection
  • Url
  • UrlScript
  • UserStorage

Interfaces

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