Response.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace Typecho;
  3. use Typecho\Widget\Terminal;
  4. /**
  5. * Typecho公用方法
  6. *
  7. * @category typecho
  8. * @package Response
  9. * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
  10. * @license GNU General Public License 2.0
  11. */
  12. class Response
  13. {
  14. /**
  15. * http code
  16. *
  17. * @access private
  18. * @var array
  19. */
  20. private const HTTP_CODE = [
  21. 100 => 'Continue',
  22. 101 => 'Switching Protocols',
  23. 200 => 'OK',
  24. 201 => 'Created',
  25. 202 => 'Accepted',
  26. 203 => 'Non-Authoritative Information',
  27. 204 => 'No Content',
  28. 205 => 'Reset Content',
  29. 206 => 'Partial Content',
  30. 300 => 'Multiple Choices',
  31. 301 => 'Moved Permanently',
  32. 302 => 'Found',
  33. 303 => 'See Other',
  34. 304 => 'Not Modified',
  35. 305 => 'Use Proxy',
  36. 307 => 'Temporary Redirect',
  37. 400 => 'Bad Request',
  38. 401 => 'Unauthorized',
  39. 402 => 'Payment Required',
  40. 403 => 'Forbidden',
  41. 404 => 'Not Found',
  42. 405 => 'Method Not Allowed',
  43. 406 => 'Not Acceptable',
  44. 407 => 'Proxy Authentication Required',
  45. 408 => 'Request Timeout',
  46. 409 => 'Conflict',
  47. 410 => 'Gone',
  48. 411 => 'Length Required',
  49. 412 => 'Precondition Failed',
  50. 413 => 'Request Entity Too Large',
  51. 414 => 'Request-URI Too Long',
  52. 415 => 'Unsupported Media Type',
  53. 416 => 'Requested Range Not Satisfiable',
  54. 417 => 'Expectation Failed',
  55. 500 => 'Internal Server Error',
  56. 501 => 'Not Implemented',
  57. 502 => 'Bad Gateway',
  58. 503 => 'Service Unavailable',
  59. 504 => 'Gateway Timeout',
  60. 505 => 'HTTP Version Not Supported'
  61. ];
  62. //默认的字符编码
  63. /**
  64. * 单例句柄
  65. *
  66. * @access private
  67. * @var Response
  68. */
  69. private static $instance;
  70. /**
  71. * 字符编码
  72. *
  73. * @var string
  74. */
  75. private $charset = 'UTF-8';
  76. /**
  77. * @var string
  78. */
  79. private $contentType = 'text/html';
  80. /**
  81. * @var callable[]
  82. */
  83. private $responders = [];
  84. /**
  85. * @var array
  86. */
  87. private $cookies = [];
  88. /**
  89. * @var array
  90. */
  91. private $headers = [];
  92. /**
  93. * @var int
  94. */
  95. private $status = 200;
  96. /**
  97. * @var bool
  98. */
  99. private $enableAutoSendHeaders = true;
  100. /**
  101. * @var bool
  102. */
  103. private $sandbox = false;
  104. /**
  105. * init responder
  106. */
  107. public function __construct()
  108. {
  109. $this->clean();
  110. }
  111. /**
  112. * 获取单例句柄
  113. *
  114. * @return Response
  115. */
  116. public static function getInstance(): Response
  117. {
  118. if (!isset(self::$instance)) {
  119. self::$instance = new self();
  120. }
  121. return self::$instance;
  122. }
  123. /**
  124. * @return $this
  125. */
  126. public function beginSandbox(): Response
  127. {
  128. $this->sandbox = true;
  129. return $this;
  130. }
  131. /**
  132. * @return $this
  133. */
  134. public function endSandbox(): Response
  135. {
  136. $this->sandbox = false;
  137. return $this;
  138. }
  139. /**
  140. * @param bool $enable
  141. */
  142. public function enableAutoSendHeaders(bool $enable = true)
  143. {
  144. $this->enableAutoSendHeaders = $enable;
  145. }
  146. /**
  147. * clean all
  148. */
  149. public function clean()
  150. {
  151. $this->headers = [];
  152. $this->cookies = [];
  153. $this->status = 200;
  154. $this->responders = [];
  155. $this->setContentType('text/html');
  156. }
  157. /**
  158. * send all headers
  159. */
  160. public function sendHeaders()
  161. {
  162. if ($this->sandbox) {
  163. return;
  164. }
  165. $sentHeaders = [];
  166. foreach (headers_list() as $header) {
  167. [$key] = explode(':', $header, 2);
  168. $sentHeaders[] = strtolower(trim($key));
  169. }
  170. header('HTTP/1.1 ' . $this->status . ' ' . self::HTTP_CODE[$this->status], true, $this->status);
  171. // set header
  172. foreach ($this->headers as $name => $value) {
  173. if (!in_array(strtolower($name), $sentHeaders)) {
  174. header($name . ': ' . $value, true);
  175. }
  176. }
  177. // set cookie
  178. foreach ($this->cookies as $cookie) {
  179. [$key, $value, $timeout, $path, $domain, $secure, $httponly] = $cookie;
  180. if ($timeout > 0) {
  181. $now = time();
  182. $timeout += $timeout > $now - 86400 ? 0 : $now;
  183. } elseif ($timeout < 0) {
  184. $timeout = 1;
  185. }
  186. setrawcookie($key, rawurlencode($value), $timeout, $path, $domain, $secure, $httponly);
  187. }
  188. }
  189. /**
  190. * respond data
  191. * @throws Terminal
  192. */
  193. public function respond()
  194. {
  195. if ($this->sandbox) {
  196. throw new Terminal();
  197. }
  198. if ($this->enableAutoSendHeaders) {
  199. $this->sendHeaders();
  200. }
  201. foreach ($this->responders as $responder) {
  202. call_user_func($responder, $this);
  203. }
  204. exit;
  205. }
  206. /**
  207. * 设置HTTP状态
  208. *
  209. * @access public
  210. * @param integer $code http代码
  211. * @return $this
  212. */
  213. public function setStatus(int $code): Response
  214. {
  215. if (!$this->sandbox) {
  216. $this->status = $code;
  217. }
  218. return $this;
  219. }
  220. /**
  221. * 设置http头
  222. *
  223. * @param string $name 名称
  224. * @param string $value 对应值
  225. * @return $this
  226. */
  227. public function setHeader(string $name, string $value): Response
  228. {
  229. if (!$this->sandbox) {
  230. $name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name)));
  231. $this->headers[$name] = $value;
  232. }
  233. return $this;
  234. }
  235. /**
  236. * 设置指定的COOKIE值
  237. *
  238. * @param string $key 指定的参数
  239. * @param mixed $value 设置的值
  240. * @param integer $timeout 过期时间,默认为0,表示随会话时间结束
  241. * @param string $path 路径信息
  242. * @param string|null $domain 域名信息
  243. * @param bool $secure 是否仅可通过安全的 HTTPS 连接传给客户端
  244. * @param bool $httponly 是否仅可通过 HTTP 协议访问
  245. * @return $this
  246. */
  247. public function setCookie(
  248. string $key,
  249. $value,
  250. int $timeout = 0,
  251. string $path = '/',
  252. string $domain = '',
  253. bool $secure = false,
  254. bool $httponly = false
  255. ): Response {
  256. if (!$this->sandbox) {
  257. $this->cookies[] = [$key, $value, $timeout, $path, $domain, $secure, $httponly];
  258. }
  259. return $this;
  260. }
  261. /**
  262. * 在http头部请求中声明类型和字符集
  263. *
  264. * @param string $contentType 文档类型
  265. * @return $this
  266. */
  267. public function setContentType(string $contentType = 'text/html'): Response
  268. {
  269. if (!$this->sandbox) {
  270. $this->contentType = $contentType;
  271. $this->setHeader('Content-Type', $this->contentType . '; charset=' . $this->charset);
  272. }
  273. return $this;
  274. }
  275. /**
  276. * 获取字符集
  277. *
  278. * @return string
  279. */
  280. public function getCharset(): string
  281. {
  282. return $this->charset;
  283. }
  284. /**
  285. * 设置默认回执编码
  286. *
  287. * @param string $charset 字符集
  288. * @return $this
  289. */
  290. public function setCharset(string $charset): Response
  291. {
  292. if (!$this->sandbox) {
  293. $this->charset = $charset;
  294. $this->setHeader('Content-Type', $this->contentType . '; charset=' . $this->charset);
  295. }
  296. return $this;
  297. }
  298. /**
  299. * add responder
  300. *
  301. * @param callable $responder
  302. * @return $this
  303. */
  304. public function addResponder(callable $responder): Response
  305. {
  306. if (!$this->sandbox) {
  307. $this->responders[] = $responder;
  308. }
  309. return $this;
  310. }
  311. }