response.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. class Response
  14. {
  15. /**
  16. * @var array An array of status codes and messages
  17. */
  18. public static $statuses = array(
  19. 100 => 'Continue',
  20. 101 => 'Switching Protocols',
  21. 200 => 'OK',
  22. 201 => 'Created',
  23. 202 => 'Accepted',
  24. 203 => 'Non-Authoritative Information',
  25. 204 => 'No Content',
  26. 205 => 'Reset Content',
  27. 206 => 'Partial Content',
  28. 207 => 'Multi-Status',
  29. 300 => 'Multiple Choices',
  30. 301 => 'Moved Permanently',
  31. 302 => 'Found',
  32. 303 => 'See Other',
  33. 304 => 'Not Modified',
  34. 305 => 'Use Proxy',
  35. 307 => 'Temporary Redirect',
  36. 400 => 'Bad Request',
  37. 401 => 'Unauthorized',
  38. 402 => 'Payment Required',
  39. 403 => 'Forbidden',
  40. 404 => 'Not Found',
  41. 405 => 'Method Not Allowed',
  42. 406 => 'Not Acceptable',
  43. 407 => 'Proxy Authentication Required',
  44. 408 => 'Request Timeout',
  45. 409 => 'Conflict',
  46. 410 => 'Gone',
  47. 411 => 'Length Required',
  48. 412 => 'Precondition Failed',
  49. 413 => 'Request Entity Too Large',
  50. 414 => 'Request-URI Too Long',
  51. 415 => 'Unsupported Media Type',
  52. 416 => 'Requested Range Not Satisfiable',
  53. 417 => 'Expectation Failed',
  54. 422 => 'Unprocessable Entity',
  55. 423 => 'Locked',
  56. 424 => 'Failed Dependency',
  57. 500 => 'Internal Server Error',
  58. 501 => 'Not Implemented',
  59. 502 => 'Bad Gateway',
  60. 503 => 'Service Unavailable',
  61. 504 => 'Gateway Timeout',
  62. 505 => 'HTTP Version Not Supported',
  63. 507 => 'Insufficient Storage',
  64. 509 => 'Bandwidth Limit Exceeded'
  65. );
  66. /**
  67. * Creates an instance of the Response class
  68. *
  69. * @param string $body The response body
  70. * @param int $status The HTTP response status for this response
  71. * @param array $headers Array of HTTP headers for this reponse
  72. *
  73. * @return Response
  74. */
  75. public static function forge($body = null, $status = 200, array $headers = array())
  76. {
  77. $response = new static($body, $status, $headers);
  78. // fire any response created events
  79. \Event::instance()->has_events('response_created') and \Event::instance()->trigger('response_created', '', 'none');
  80. return $response;
  81. }
  82. /**
  83. * Redirects to another uri/url. Sets the redirect header,
  84. * sends the headers and exits. Can redirect via a Location header
  85. * or using a refresh header.
  86. *
  87. * The refresh header works better on certain servers like IIS.
  88. *
  89. * @param string $url The url
  90. * @param string $method The redirect method
  91. * @param int $code The redirect status code
  92. *
  93. * @return void
  94. */
  95. public static function redirect($url = '', $method = 'location', $code = 302)
  96. {
  97. $response = new static;
  98. $response->set_status($code);
  99. if (strpos($url, '://') === false)
  100. {
  101. $url = $url !== '' ? \Uri::create($url) : \Uri::base();
  102. }
  103. strpos($url, '*') !== false and $url = \Uri::segment_replace($url);
  104. if ($method == 'location')
  105. {
  106. $response->set_header('Location', $url);
  107. }
  108. elseif ($method == 'refresh')
  109. {
  110. $response->set_header('Refresh', '0;url='.$url);
  111. }
  112. else
  113. {
  114. return;
  115. }
  116. $response->send(true);
  117. exit;
  118. }
  119. /**
  120. * @var int The HTTP status code
  121. */
  122. public $status = 200;
  123. /**
  124. * @var array An array of HTTP headers
  125. */
  126. public $headers = array();
  127. /**
  128. * @var string The content of the response
  129. */
  130. public $body = null;
  131. /**
  132. * Sets up the response with a body and a status code.
  133. *
  134. * @param string $body The response body
  135. * @param string $status The response status
  136. */
  137. public function __construct($body = null, $status = 200, array $headers = array())
  138. {
  139. foreach ($headers as $k => $v)
  140. {
  141. $this->set_header($k, $v);
  142. }
  143. $this->body = $body;
  144. $this->status = $status;
  145. }
  146. /**
  147. * Sets the response status code
  148. *
  149. * @param string $status The status code
  150. *
  151. * @return Response
  152. */
  153. public function set_status($status = 200)
  154. {
  155. $this->status = $status;
  156. return $this;
  157. }
  158. /**
  159. * Adds a header to the queue
  160. *
  161. * @param string The header name
  162. * @param string The header value
  163. * @param string Whether to replace existing value for the header, will never overwrite/be overwritten when false
  164. *
  165. * @return Response
  166. */
  167. public function set_header($name, $value, $replace = true)
  168. {
  169. if ($replace)
  170. {
  171. $this->headers[$name] = $value;
  172. }
  173. else
  174. {
  175. $this->headers[] = array($name, $value);
  176. }
  177. return $this;
  178. }
  179. /**
  180. * Gets header information from the queue
  181. *
  182. * @param string The header name, or null for all headers
  183. *
  184. * @return mixed
  185. */
  186. public function get_header($name = null)
  187. {
  188. if (func_num_args())
  189. {
  190. return isset($this->headers[$name]) ? $this->headers[$name] : null;
  191. }
  192. else
  193. {
  194. return $this->headers;
  195. }
  196. }
  197. /**
  198. * Sets (or returns) the body for the response
  199. *
  200. * @param string The response content
  201. *
  202. * @return Response|string
  203. */
  204. public function body($value = false)
  205. {
  206. if (func_num_args())
  207. {
  208. $this->body = $value;
  209. return $this;
  210. }
  211. return $this->body;
  212. }
  213. /**
  214. * Sends the headers if they haven't already been sent. Returns whether
  215. * they were sent or not.
  216. *
  217. * @return bool
  218. */
  219. public function send_headers()
  220. {
  221. if ( ! headers_sent())
  222. {
  223. // Send the protocol/status line first, FCGI servers need different status header
  224. if ( ! empty($_SERVER['FCGI_SERVER_VERSION']))
  225. {
  226. header('Status: '.$this->status.' '.static::$statuses[$this->status]);
  227. }
  228. else
  229. {
  230. $protocol = \Input::server('SERVER_PROTOCOL') ? \Input::server('SERVER_PROTOCOL') : 'HTTP/1.1';
  231. header($protocol.' '.$this->status.' '.static::$statuses[$this->status]);
  232. }
  233. foreach ($this->headers as $name => $value)
  234. {
  235. // Parse non-replace headers
  236. if (is_int($name) and is_array($value))
  237. {
  238. isset($value[0]) and $name = $value[0];
  239. isset($value[1]) and $value = $value[1];
  240. }
  241. // Create the header
  242. is_string($name) and $value = "{$name}: {$value}";
  243. // Send it
  244. header($value, true);
  245. }
  246. return true;
  247. }
  248. return false;
  249. }
  250. /**
  251. * Sends the response to the output buffer. Optionally will send the
  252. * headers.
  253. *
  254. * @param bool $send_headers Whether or not to send the defined HTTP headers
  255. *
  256. * @return void
  257. */
  258. public function send($send_headers = false)
  259. {
  260. $body = $this->__toString();
  261. if ($send_headers)
  262. {
  263. $this->send_headers();
  264. }
  265. if ($this->body != null)
  266. {
  267. echo $body;
  268. }
  269. }
  270. /**
  271. * Returns the body as a string.
  272. *
  273. * @return string
  274. */
  275. public function __toString()
  276. {
  277. // special treatment for array's
  278. if (is_array($this->body))
  279. {
  280. // this var_dump() is here intentionally !
  281. ob_start();
  282. var_dump($this->body);
  283. $this->body = html_entity_decode(ob_get_clean());
  284. }
  285. return (string) $this->body;
  286. }
  287. }