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 void 
  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:     }
  88. 88:  
  89. 89:  
  90. 90:  
  91. 91:     /**
  92. 92:      * Returns HTTP response code.
  93. 93:      * @return int 
  94. 94:      */
  95. 95:     public function getCode()
  96. 96:     {
  97. 97:         return $this->code;
  98. 98:     }
  99. 99:  
  100. 100:  
  101. 101:  
  102. 102:     /**
  103. 103:      * Sends a HTTP header and replaces a previous one.
  104. 104:      * @param  string  header name
  105. 105:      * @param  string  header value
  106. 106:      * @return void 
  107. 107:      * @throws InvalidStateException  if HTTP headers have been sent
  108. 108:      */
  109. 109:     public function setHeader($name$value)
  110. 110:     {
  111. 111:         if (headers_sent($file$line)) {
  112. 112:             throw new InvalidStateException("Cannot send header after HTTP headers have been sent" ($file " (output started at $file:$line)."."));
  113. 113:         }
  114. 114:  
  115. 115:         header($name ': ' $valueTRUE$this->code);
  116. 116:     }
  117. 117:  
  118. 118:  
  119. 119:  
  120. 120:     /**
  121. 121:      * Adds HTTP header.
  122. 122:      * @param  string  header name
  123. 123:      * @param  string  header value
  124. 124:      * @return void 
  125. 125:      * @throws InvalidStateException  if HTTP headers have been sent
  126. 126:      */
  127. 127:     public function addHeader($name$value)
  128. 128:     {
  129. 129:         if (headers_sent($file$line)) {
  130. 130:             throw new InvalidStateException("Cannot send header after HTTP headers have been sent" ($file " (output started at $file:$line)."."));
  131. 131:         }
  132. 132:  
  133. 133:         header($name ': ' $valueFALSE$this->code);
  134. 134:     }
  135. 135:  
  136. 136:  
  137. 137:  
  138. 138:     /**
  139. 139:      * Sends a Content-type HTTP header.
  140. 140:      * @param  string  mime-type
  141. 141:      * @param  string  charset
  142. 142:      * @return void 
  143. 143:      * @throws InvalidStateException  if HTTP headers have been sent
  144. 144:      */
  145. 145:     public function setContentType($type$charset NULL)
  146. 146:     {
  147. 147:         $this->setHeader('Content-Type'$type ($charset '; charset=' $charset ''));
  148. 148:     }
  149. 149:  
  150. 150:  
  151. 151:  
  152. 152:     /**
  153. 153:      * Redirects to a new URL. Note: call exit() after it.
  154. 154:      * @param  string  URL
  155. 155:      * @param  int     HTTP code
  156. 156:      * @return void 
  157. 157:      * @throws InvalidStateException  if HTTP headers have been sent
  158. 158:      */
  159. 159:     public function redirect($url$code self::S302_FOUND)
  160. 160:     {
  161. 161:         if (isset($_SERVER['SERVER_SOFTWARE']&& preg_match('#^Microsoft-IIS/[1-5]#'$_SERVER['SERVER_SOFTWARE']&& $this->getHeader('Set-Cookie'!== NULL{
  162. 162:             $this->setHeader('Refresh'"0;url=$url");
  163. 163:             return;
  164. 164:         }
  165. 165:  
  166. 166:         $this->setCode($code);
  167. 167:         $this->setHeader('Location'$url);
  168. 168:         echo "<h1>Redirect</h1>\n\n<p><a href=\"" htmlSpecialChars($url"\">Please click here to continue</a>.</p>";
  169. 169:     }
  170. 170:  
  171. 171:  
  172. 172:  
  173. 173:     /**
  174. 174:      * Sets the number of seconds before a page cached on a browser expires.
  175. 175:      * @param  mixed  timestamp or number of seconds
  176. 176:      * @return void 
  177. 177:      * @throws InvalidStateException  if HTTP headers have been sent
  178. 178:      */
  179. 179:     public function expire($seconds)
  180. 180:     {
  181. 181:         if (is_string($seconds&& !is_numeric($seconds)) {
  182. 182:             $seconds strtotime($seconds);
  183. 183:         }
  184. 184:  
  185. 185:         if ($seconds 0{
  186. 186:             if ($seconds <= Tools::YEAR{
  187. 187:                 $seconds += time();
  188. 188:             }
  189. 189:             $this->setHeader('Cache-Control''max-age=' ($seconds time())',must-revalidate');
  190. 190:             $this->setHeader('Expires'self::date($seconds));
  191. 191:  
  192. 192:         else // no cache
  193. 193:             $this->setHeader('Expires''Mon, 23 Jan 1978 10:00:00 GMT');
  194. 194:             $this->setHeader('Cache-Control''s-maxage=0, max-age=0, must-revalidate');
  195. 195:         }
  196. 196:     }
  197. 197:  
  198. 198:  
  199. 199:  
  200. 200:     /**
  201. 201:      * Checks if headers have been sent.
  202. 202:      * @return bool 
  203. 203:      */
  204. 204:     public function isSent()
  205. 205:     {
  206. 206:         return headers_sent();
  207. 207:     }
  208. 208:  
  209. 209:  
  210. 210:  
  211. 211:     /**
  212. 212:      * Return the value of the HTTP header.
  213. 213:      * @param  string 
  214. 214:      * @param  mixed 
  215. 215:      * @return mixed 
  216. 216:      */
  217. 217:     public function getHeader($header$default NULL)
  218. 218:     {
  219. 219:         $header .= ':';
  220. 220:         $len strlen($header);
  221. 221:         foreach (headers_list(as $item{
  222. 222:             if (strncasecmp($item$header$len=== 0{
  223. 223:                 return ltrim(substr($item$len));
  224. 224:             }
  225. 225:         }
  226. 226:         return $default;
  227. 227:     }
  228. 228:  
  229. 229:  
  230. 230:  
  231. 231:     /**
  232. 232:      * Returns a list of headers to sent.
  233. 233:      * @return array 
  234. 234:      */
  235. 235:     public function getHeaders()
  236. 236:     {
  237. 237:         $headers array();
  238. 238:         foreach (headers_list(as $header{
  239. 239:             $a strpos($header':');
  240. 240:             $headers[substr($header0$a)= (string) substr($header$a 2);
  241. 241:         }
  242. 242:         return $headers;
  243. 243:     }
  244. 244:  
  245. 245:  
  246. 246:  
  247. 247:     /**
  248. 248:      * Returns HTTP valid date format.
  249. 249:      * @param  int  timestamp
  250. 250:      * @return string 
  251. 251:      */
  252. 252:     public static function date($time NULL)
  253. 253:     {
  254. 254:         return gmdate('D, d M Y H:i:s \G\M\T'$time === NULL time($time);
  255. 255:     }
  256. 256:  
  257. 257:  
  258. 258:  
  259. 259:     /**
  260. 260:      * Enables compression. (warning: may not work)
  261. 261:      * @return bool 
  262. 262:      */
  263. 263:     public function enableCompression()
  264. 264:     {
  265. 265:         if (headers_sent()) return FALSE;
  266. 266:  
  267. 267:         if ($this->getHeader('Content-Encoding'!== NULL{
  268. 268:             return FALSE// called twice
  269. 269:         }
  270. 270:  
  271. 271:         $ok ob_gzhandler(''PHP_OUTPUT_HANDLER_START);
  272. 272:         if ($ok === FALSE{
  273. 273:             return FALSE// not allowed
  274. 274:         }
  275. 275:  
  276. 276:         if (function_exists('ini_set')) {
  277. 277:             ini_set('zlib.output_compression''Off');
  278. 278:             ini_set('zlib.output_compression_level''6');
  279. 279:         }
  280. 280:         ob_start('ob_gzhandler');
  281. 281:         return TRUE;
  282. 282:     }
  283. 283:  
  284. 284:  
  285. 285:  
  286. 286:     /**
  287. 287:      * @return void 
  288. 288:      */
  289. 289:     public function __destruct()
  290. 290:     {
  291. 291:         if (self::$fixIE{
  292. 292:             // Sends invisible garbage for IE.
  293. 293:             if (!isset($_SERVER['HTTP_USER_AGENT']|| strpos($_SERVER['HTTP_USER_AGENT']'MSIE '=== FALSEreturn;
  294. 294:             if (!in_array($this->codearray(400403404405406408409410500501505)TRUE)) return;
  295. 295:             if ($this->getHeader('Content-Type''text/html'!== 'text/html'return;
  296. 296:             $s " \t\r\n";
  297. 297:             for ($i 2e3$i$i--echo $s{rand(03)};
  298. 298:             self::$fixIE FALSE;
  299. 299:         }
  300. 300:     }
  301. 301:  
  302. 302:  
  303. 303:  
  304. 304:     /**
  305. 305:      * Sends a cookie.
  306. 306:      * @param  string name of the cookie
  307. 307:      * @param  string value
  308. 308:      * @param  mixed  expiration as unix timestamp or number of seconds; Value 0 means "until the browser is closed"
  309. 309:      * @param  string 
  310. 310:      * @param  string 
  311. 311:      * @param  bool 
  312. 312:      * @return void 
  313. 313:      * @throws InvalidStateException  if HTTP headers have been sent
  314. 314:      */
  315. 315:     public function setCookie($name$value$expire$path NULL$domain NULL$secure NULL)
  316. 316:     {
  317. 317:         if (headers_sent($file$line)) {
  318. 318:             throw new InvalidStateException("Cannot set cookie after HTTP headers have been sent" ($file " (output started at $file:$line)."."));
  319. 319:         }
  320. 320:  
  321. 321:         if (is_string($expire&& !is_numeric($expire)) {
  322. 322:             $expire strtotime($expire);
  323. 323:  
  324. 324:         elseif ($expire && $expire <= Tools::YEAR{
  325. 325:             $expire += time();
  326. 326:         }
  327. 327:  
  328. 328:         setcookie(
  329. 329:             $name,
  330. 330:             $value,
  331. 331:             $expire,
  332. 332:             $path === NULL $this->cookiePath : (string) $path,
  333. 333:             $domain === NULL $this->cookieDomain : (string) $domain//  . '; httponly'
  334. 334:             $secure === NULL $this->cookieSecure : (bool) $secure,
  335. 335:             TRUE // added in PHP 5.2.0.
  336. 336:         );
  337. 337:     }
  338. 338:  
  339. 339:  
  340. 340:  
  341. 341:     /**
  342. 342:      * Deletes a cookie.
  343. 343:      * @param  string name of the cookie.
  344. 344:      * @param  string 
  345. 345:      * @param  string 
  346. 346:      * @param  bool 
  347. 347:      * @return void 
  348. 348:      * @throws InvalidStateException  if HTTP headers have been sent
  349. 349:      */
  350. 350:     public function deleteCookie($name$path NULL$domain NULL$secure NULL)
  351. 351:     {
  352. 352:         if (headers_sent($file$line)) {
  353. 353:             throw new InvalidStateException("Cannot delete cookie after HTTP headers have been sent" ($file " (output started at $file:$line)."."));
  354. 354:         }
  355. 355:  
  356. 356:         setcookie(
  357. 357:             $name,
  358. 358:             FALSE,
  359. 359:             254400000,
  360. 360:             $path === NULL $this->cookiePath : (string) $path,
  361. 361:             $domain === NULL $this->cookieDomain : (string) $domain//  . '; httponly'
  362. 362:             $secure === NULL $this->cookieSecure : (bool) $secure,
  363. 363:             TRUE // added in PHP 5.2.0.
  364. 364:         );
  365. 365:     }
  366. 366: