PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/kirby/toolkit/lib/header.php

https://github.com/aurer/aurer-kirby
PHP | 230 lines | 97 code | 29 blank | 104 comment | 6 complexity | 6cdcb4ab48a03cd198c8c52607ba27d0 MD5 | raw file
  1. <?php
  2. /**
  3. * Header
  4. *
  5. * Makes sending HTTP headers a breeze
  6. *
  7. * @package Kirby Toolkit
  8. * @author Bastian Allgeier <bastian@getkirby.com>
  9. * @link http://getkirby.com
  10. * @copyright Bastian Allgeier
  11. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  12. */
  13. class Header {
  14. // configuration
  15. static public $codes = array(
  16. '_200' => 'Ok',
  17. '_201' => 'Created',
  18. '_202' => 'Accepted',
  19. //...
  20. '_301' => 'Moved Permanently',
  21. '_302' => 'Found',
  22. '_303' => 'See Other',
  23. '_304' => 'Not Modified',
  24. //...
  25. '_400' => 'Bad Request',
  26. '_401' => 'Unauthorized',
  27. '_402' => 'Payment required',
  28. '_403' => 'Forbidden',
  29. '_404' => 'Not found',
  30. '_405' => 'Method not allowed',
  31. //...
  32. '_500' => 'Internal Server Error',
  33. '_501' => 'Not implemented',
  34. '_502' => 'Bad Gateway',
  35. '_503' => 'Service Unavailable'
  36. );
  37. /**
  38. * Sends a content type header
  39. *
  40. * @param string $mime
  41. * @param string $charset
  42. * @param boolean $send
  43. * @return mixed
  44. */
  45. static public function contentType($mime, $charset = 'UTF-8', $send = true) {
  46. if(f::extensionToMime($mime)) $mime = f::extensionToMime($mime);
  47. $header = 'Content-type: ' . $mime;
  48. if($charset) $header .= '; charset=' . $charset;
  49. if(!$send) return $header;
  50. header($header);
  51. }
  52. /**
  53. * Shortcut for static::contentType()
  54. *
  55. * @param string $mime
  56. * @param string $charset
  57. * @param boolean $send
  58. * @return mixed
  59. */
  60. static public function type($mime, $charset = 'UTF-8', $send = true) {
  61. return static::contentType($mime, $charset, $send);
  62. }
  63. /**
  64. * Sends a status header
  65. *
  66. * @param int $code The HTTP status code
  67. * @param boolean $send If set to false the header will be returned instead
  68. * @return mixed
  69. */
  70. static public function status($code, $send = true) {
  71. $codes = static::$codes;
  72. $code = !array_key_exists('_' . $code, $codes) ? 400 : $code;
  73. $message = isset($codes['_' . $code]) ? $codes['_' . $code] : 'Something went wrong';
  74. $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
  75. $header = $protocol . ' ' . $code . ' ' . $message;
  76. if(!$send) return $header;
  77. // try to send the header
  78. header($header);
  79. }
  80. /**
  81. * Sends a 200 header
  82. *
  83. * @param boolean $send
  84. * @return mixed
  85. */
  86. static public function success($send = true) {
  87. return static::status(200, $send);
  88. }
  89. /**
  90. * Sends a 201 header
  91. *
  92. * @param boolean $send
  93. * @return mixed
  94. */
  95. static public function created($send = true) {
  96. return static::status(201, $send);
  97. }
  98. /**
  99. * Sends a 202 header
  100. *
  101. * @param boolean $send
  102. * @return mixed
  103. */
  104. static public function accepted($send = true) {
  105. return static::status(202, $send);
  106. }
  107. /**
  108. * Sends a 400 header
  109. *
  110. * @param boolean $send
  111. * @return mixed
  112. */
  113. static public function error($send = true) {
  114. return static::status(400, $send);
  115. }
  116. /**
  117. * Sends a 403 header
  118. *
  119. * @param boolean $send
  120. * @return mixed
  121. */
  122. static public function forbidden($send = true) {
  123. return static::status(403, $send);
  124. }
  125. /**
  126. * Sends a 404 header
  127. *
  128. * @param boolean $send
  129. * @return mixed
  130. */
  131. static public function notfound($send = true) {
  132. return static::status(404, $send);
  133. }
  134. /**
  135. * Sends a 404 header
  136. *
  137. * @param boolean $send
  138. * @return mixed
  139. */
  140. static public function missing($send = true) {
  141. return static::status(404, $send);
  142. }
  143. /**
  144. * Sends a 500 header
  145. *
  146. * @param boolean $send
  147. * @return mixed
  148. */
  149. static public function panic($send = true) {
  150. return static::status(500, $send);
  151. }
  152. /**
  153. * Sends a 503 header
  154. *
  155. * @param boolean $send
  156. * @return mixed
  157. */
  158. static public function unavailable($send = true) {
  159. return static::status(503, $send);
  160. }
  161. /**
  162. * Sends a redirect header
  163. *
  164. * @param boolean $send
  165. * @return mixed
  166. */
  167. static public function redirect($url, $code = 301, $send = true) {
  168. $status = static::status($code, false);
  169. $location = 'Location:' . $url;
  170. if(!$send) {
  171. return $status . PHP_EOL . $location;
  172. }
  173. header($status);
  174. header($location);
  175. exit();
  176. }
  177. /**
  178. * Sends download headers for anything that is downloadable
  179. *
  180. * @param array $params Check out the defaults array for available parameters
  181. */
  182. static public function download($params = array()) {
  183. $defaults = array(
  184. 'name' => 'download',
  185. 'size' => false,
  186. 'mime' => 'application/force-download',
  187. 'modified' => time()
  188. );
  189. $options = array_merge($defaults, $params);
  190. header('Pragma: public');
  191. header('Expires: 0');
  192. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  193. header('Last-Modified: '. gmdate('D, d M Y H:i:s', $options['modified']) . ' GMT');
  194. header('Cache-Control: private', false);
  195. static::contentType($options['mime']);
  196. header('Content-Disposition: attachment; filename="' . $options['name'] . '"');
  197. header('Content-Transfer-Encoding: binary');
  198. if($options['size']) header('Content-Length: ' . $options['size']);
  199. header('Connection: close');
  200. }
  201. }