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