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