Source for file HttpResponse.php

Documentation is available at HttpResponse.php

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