Packages

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • NFtp
  • NHtml
  • NHttpContext
  • NHttpRequest
  • NHttpResponse
  • NHttpUploadedFile
  • NSession
  • NSessionNamespace
  • NUri
  • NUriScript
  • NUser

Interfaces

  • IHttpRequest
  • IHttpResponse
  • IUser

Exceptions

  • NFtpException
  • Overview
  • Package
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://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\Web
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * HttpResponse class.
 17:  *
 18:  * @author     David Grudl
 19:  *
 20:  * @property   int $code
 21:  * @property-read array $headers
 22:  * @property-read mixed $sent
 23:  * @package Nette\Web
 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 The path in which the cookie will be available */
 37:     public $cookieSecure = FALSE;
 38: 
 39:     /** @var int HTTP response code */
 40:     private $code = self::S200_OK;
 41: 
 42: 
 43: 
 44:     /**
 45:      * Sets HTTP response code.
 46:      * @param  int
 47:      * @return NHttpResponse  provides a fluent interface
 48:      * @throws InvalidArgumentException  if code is invalid
 49:      * @throws InvalidStateException  if HTTP headers have been sent
 50:      */
 51:     public function setCode($code)
 52:     {
 53:         $code = (int) $code;
 54: 
 55:         static $allowed = array(
 56:             200=>1, 201=>1, 202=>1, 203=>1, 204=>1, 205=>1, 206=>1,
 57:             300=>1, 301=>1, 302=>1, 303=>1, 304=>1, 307=>1,
 58:             400=>1, 401=>1, 403=>1, 404=>1, 406=>1, 408=>1, 410=>1, 412=>1, 415=>1, 416=>1,
 59:             500=>1, 501=>1, 503=>1, 505=>1
 60:         );
 61: 
 62:         if (!isset($allowed[$code])) {
 63:             throw new InvalidArgumentException("Bad HTTP response '$code'.");
 64: 
 65:         } elseif (headers_sent($file, $line)) {
 66:             throw new InvalidStateException("Cannot set HTTP code after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
 67: 
 68:         } else {
 69:             $this->code = $code;
 70:             $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
 71:             header($protocol . ' ' . $code, TRUE, $code);
 72:         }
 73:         return $this;
 74:     }
 75: 
 76: 
 77: 
 78:     /**
 79:      * Returns HTTP response code.
 80:      * @return int
 81:      */
 82:     public function getCode()
 83:     {
 84:         return $this->code;
 85:     }
 86: 
 87: 
 88: 
 89:     /**
 90:      * Sends a HTTP header and replaces a previous one.
 91:      * @param  string  header name
 92:      * @param  string  header value
 93:      * @return NHttpResponse  provides a fluent interface
 94:      * @throws InvalidStateException  if HTTP headers have been sent
 95:      */
 96:     public function setHeader($name, $value)
 97:     {
 98:         if (headers_sent($file, $line)) {
 99:             throw new InvalidStateException("Cannot send header after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
100:         }
101: 
102:         if ($value === NULL && function_exists('header_remove')) {
103:             header_remove($name);
104:         } else {
105:             header($name . ': ' . $value, TRUE, $this->code);
106:         }
107:         return $this;
108:     }
109: 
110: 
111: 
112:     /**
113:      * Adds HTTP header.
114:      * @param  string  header name
115:      * @param  string  header value
116:      * @return void
117:      * @throws InvalidStateException  if HTTP headers have been sent
118:      */
119:     public function addHeader($name, $value)
120:     {
121:         if (headers_sent($file, $line)) {
122:             throw new InvalidStateException("Cannot send header after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
123:         }
124: 
125:         header($name . ': ' . $value, FALSE, $this->code);
126:     }
127: 
128: 
129: 
130:     /**
131:      * Sends a Content-type HTTP header.
132:      * @param  string  mime-type
133:      * @param  string  charset
134:      * @return NHttpResponse  provides a fluent interface
135:      * @throws InvalidStateException  if HTTP headers have been sent
136:      */
137:     public function setContentType($type, $charset = NULL)
138:     {
139:         $this->setHeader('Content-Type', $type . ($charset ? '; charset=' . $charset : ''));
140:         return $this;
141:     }
142: 
143: 
144: 
145:     /**
146:      * Redirects to a new URL. Note: call exit() after it.
147:      * @param  string  URL
148:      * @param  int     HTTP code
149:      * @return void
150:      * @throws InvalidStateException  if HTTP headers have been sent
151:      */
152:     public function redirect($url, $code = self::S302_FOUND)
153:     {
154:         if (isset($_SERVER['SERVER_SOFTWARE']) && preg_match('#^Microsoft-IIS/[1-5]#', $_SERVER['SERVER_SOFTWARE']) && $this->getHeader('Set-Cookie') !== NULL) {
155:             $this->setHeader('Refresh', "0;url=$url");
156:             return;
157:         }
158: 
159:         $this->setCode($code);
160:         $this->setHeader('Location', $url);
161:         echo "<h1>Redirect</h1>\n\n<p><a href=\"" . htmlSpecialChars($url) . "\">Please click here to continue</a>.</p>";
162:     }
163: 
164: 
165: 
166:     /**
167:      * Sets the number of seconds before a page cached on a browser expires.
168:      * @param  string|int|DateTime  time, value 0 means "until the browser is closed"
169:      * @return NHttpResponse  provides a fluent interface
170:      * @throws InvalidStateException  if HTTP headers have been sent
171:      */
172:     public function setExpiration($time)
173:     {
174:         if (!$time) { // no cache
175:             $this->setHeader('Cache-Control', 's-maxage=0, max-age=0, must-revalidate');
176:             $this->setHeader('Expires', 'Mon, 23 Jan 1978 10:00:00 GMT');
177:             return $this;
178:         }
179: 
180:         $time = NDateTime53::from($time);
181:         $this->setHeader('Cache-Control', 'max-age=' . ($time->format('U') - time()));
182:         $this->setHeader('Expires', self::date($time));
183:         return $this;
184:     }
185: 
186: 
187: 
188:     /** @deprecated */
189:     public function expire($seconds)
190:     {
191:         trigger_error(__METHOD__ . '() is deprecated; use setExpiration() instead.', E_USER_WARNING);
192:         $this->setExpiration($seconds);
193:     }
194: 
195: 
196: 
197:     /**
198:      * Checks if headers have been sent.
199:      * @return bool
200:      */
201:     public function isSent()
202:     {
203:         return headers_sent();
204:     }
205: 
206: 
207: 
208:     /**
209:      * Return the value of the HTTP header.
210:      * @param  string
211:      * @param  mixed
212:      * @return mixed
213:      */
214:     public function getHeader($header, $default = NULL)
215:     {
216:         $header .= ':';
217:         $len = strlen($header);
218:         foreach (headers_list() as $item) {
219:             if (strncasecmp($item, $header, $len) === 0) {
220:                 return ltrim(substr($item, $len));
221:             }
222:         }
223:         return $default;
224:     }
225: 
226: 
227: 
228:     /**
229:      * Returns a list of headers to sent.
230:      * @return array
231:      */
232:     public function getHeaders()
233:     {
234:         $headers = array();
235:         foreach (headers_list() as $header) {
236:             $a = strpos($header, ':');
237:             $headers[substr($header, 0, $a)] = (string) substr($header, $a + 2);
238:         }
239:         return $headers;
240:     }
241: 
242: 
243: 
244:     /**
245:      * Returns HTTP valid date format.
246:      * @param  string|int|DateTime
247:      * @return string
248:      */
249:     public static function date($time = NULL)
250:     {
251:         $time = NDateTime53::from($time);
252:         $time->setTimezone(new DateTimeZone('GMT'));
253:         return $time->format('D, d M Y H:i:s \G\M\T');
254:     }
255: 
256: 
257: 
258:     /**
259:      * Enables compression. (warning: may not work)
260:      * @return bool
261:      */
262:     public function enableCompression()
263:     {
264:         if (headers_sent()) {
265:             return FALSE;
266:         }
267: 
268:         if ($this->getHeader('Content-Encoding') !== NULL) {
269:             return FALSE; // called twice
270:         }
271: 
272:         $ok = ob_gzhandler('', PHP_OUTPUT_HANDLER_START);
273:         if ($ok === FALSE) {
274:             return FALSE; // not allowed
275:         }
276: 
277:         if (function_exists('ini_set')) {
278:             ini_set('zlib.output_compression', 'Off');
279:             ini_set('zlib.output_compression_level', '6');
280:         }
281:         ob_start('ob_gzhandler', 1);
282:         return TRUE;
283:     }
284: 
285: 
286: 
287:     /**
288:      * @return void
289:      */
290:     public function __destruct()
291:     {
292:         if (self::$fixIE) {
293:             // Sends invisible garbage for IE.
294:             if (!isset($_SERVER['HTTP_USER_AGENT']) || strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') === FALSE) return;
295:             if (!in_array($this->code, array(400, 403, 404, 405, 406, 408, 409, 410, 500, 501, 505), TRUE)) return;
296:             if ($this->getHeader('Content-Type', 'text/html') !== 'text/html') return;
297:             $s = " \t\r\n";
298:             for ($i = 2e3; $i; $i--) echo $s{rand(0, 3)};
299:             self::$fixIE = FALSE;
300:         }
301:     }
302: 
303: 
304: 
305:     /**
306:      * Sends a cookie.
307:      * @param  string name of the cookie
308:      * @param  string value
309:      * @param  string|int|DateTime  expiration time, value 0 means "until the browser is closed"
310:      * @param  string
311:      * @param  string
312:      * @param  bool
313:      * @return NHttpResponse  provides a fluent interface
314:      * @throws InvalidStateException  if HTTP headers have been sent
315:      */
316:     public function setCookie($name, $value, $time, $path = NULL, $domain = NULL, $secure = NULL)
317:     {
318:         if (headers_sent($file, $line)) {
319:             throw new InvalidStateException("Cannot set cookie after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
320:         }
321: 
322:         setcookie(
323:             $name,
324:             $value,
325:             $time ? NDateTime53::from($time)->format('U') : 0,
326:             $path === NULL ? $this->cookiePath : (string) $path,
327:             $domain === NULL ? $this->cookieDomain : (string) $domain, //  . '; httponly'
328:             $secure === NULL ? $this->cookieSecure : (bool) $secure,
329:             TRUE // added in PHP 5.2.0.
330:         );
331:         return $this;
332:     }
333: 
334: 
335: 
336:     /**
337:      * Deletes a cookie.
338:      * @param  string name of the cookie.
339:      * @param  string
340:      * @param  string
341:      * @param  bool
342:      * @return void
343:      * @throws InvalidStateException  if HTTP headers have been sent
344:      */
345:     public function deleteCookie($name, $path = NULL, $domain = NULL, $secure = NULL)
346:     {
347:         if (headers_sent($file, $line)) {
348:             throw new InvalidStateException("Cannot delete cookie after HTTP headers have been sent" . ($file ? " (output started at $file:$line)." : "."));
349:         }
350: 
351:         setcookie(
352:             $name,
353:             FALSE,
354:             254400000,
355:             $path === NULL ? $this->cookiePath : (string) $path,
356:             $domain === NULL ? $this->cookieDomain : (string) $domain, //  . '; httponly'
357:             $secure === NULL ? $this->cookieSecure : (bool) $secure,
358:             TRUE // added in PHP 5.2.0.
359:         );
360:     }
361: 
362: }
363: 
Nette Framework 0.9.7 (for PHP 5.2) API documentation generated by ApiGen 2.3.0