|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Leaf\Http; |
| 4 | + |
| 5 | +/** |
| 6 | + * HTTP Headers |
| 7 | + * --------------------- |
| 8 | + * Response header management made simple with Leaf |
| 9 | + * |
| 10 | + * @author Michael Darko |
| 11 | + * @since 2.0.0 |
| 12 | + */ |
| 13 | +class Headers |
| 14 | +{ |
| 15 | + protected static $http_code; |
| 16 | + |
| 17 | + /** |
| 18 | + * Get or Set an HTTP code for response |
| 19 | + * |
| 20 | + * @param int|null $http_code The current response code. |
| 21 | + */ |
| 22 | + public static function status($http_code = null) |
| 23 | + { |
| 24 | + if (!$http_code) return self::$http_code; |
| 25 | + self::$http_code = $http_code; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Force an HTTP code for response using PHP's `http_response_code` |
| 30 | + */ |
| 31 | + public static function resetStatus($http_code = null) |
| 32 | + { |
| 33 | + return http_response_code($http_code); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Get all headers passed into application |
| 38 | + * |
| 39 | + * @param bool $safeOutput Try to sanitize header data |
| 40 | + */ |
| 41 | + public static function all($safeOutput = false): array |
| 42 | + { |
| 43 | + if ($safeOutput === false) return self::findHeaders(); |
| 44 | + return \Leaf\Anchor::sanitize(self::findHeaders()); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Return a particular header passed into app |
| 49 | + * |
| 50 | + * @param string|array $param The header(s) to return |
| 51 | + * @param bool $safeOutput Try to sanitize header data |
| 52 | + * |
| 53 | + * @return string|array |
| 54 | + */ |
| 55 | + public static function get($params, $safeOutput = false) |
| 56 | + { |
| 57 | + if (is_string($params)) return self::all($safeOutput)[$params] ?? null; |
| 58 | + |
| 59 | + $data = []; |
| 60 | + foreach ($params as $param) { |
| 61 | + $data[$param] = self::get($param, $safeOutput); |
| 62 | + } |
| 63 | + return $data; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Set a new header |
| 68 | + */ |
| 69 | + public static function set($key, $value = "", $replace = true, $http_code = null): void |
| 70 | + { |
| 71 | + if (!is_array($key)) { |
| 72 | + header("$key: $value", $replace, $http_code ?? self::$http_code); |
| 73 | + } else { |
| 74 | + foreach ($key as $header => $header_value) { |
| 75 | + self::set($header, $header_value, $replace, $http_code); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public static function remove($keys) |
| 81 | + { |
| 82 | + if (!is_array($keys)) { |
| 83 | + header_remove($keys); |
| 84 | + } else { |
| 85 | + foreach ($keys as $key) { |
| 86 | + self::remove($key); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public static function contentPlain($code = null): void |
| 92 | + { |
| 93 | + self::set("Content-Type", "text/plain", true, $code ?? self::$http_code); |
| 94 | + } |
| 95 | + |
| 96 | + public static function contentHtml($code = null): void |
| 97 | + { |
| 98 | + self::set("Content-Type", "text/html", true, $code ?? self::$http_code); |
| 99 | + } |
| 100 | + |
| 101 | + public static function contentJSON($code = null): void |
| 102 | + { |
| 103 | + self::set("Content-Type", "application/json", true, $code ?? self::$http_code); |
| 104 | + } |
| 105 | + |
| 106 | + public static function accessControl($key, $value = "", $code = null) |
| 107 | + { |
| 108 | + if (is_string($key)) { |
| 109 | + self::set("Access-Control-$key", $value, true, $code ?? self::$http_code); |
| 110 | + } else { |
| 111 | + foreach ($key as $header => $header_value) { |
| 112 | + self::accessControl($header, $header_value, $code); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + protected static function findHeaders() |
| 118 | + { |
| 119 | + if (function_exists("getallheaders") && \getallheaders()) return \getallheaders(); |
| 120 | + |
| 121 | + $headers = []; |
| 122 | + foreach ($_SERVER as $name => $value) { |
| 123 | + if ((substr($name, 0, 5) == 'HTTP_') || ($name == 'CONTENT_TYPE') || ($name == 'CONTENT_LENGTH')) { |
| 124 | + $headers[str_replace([' ', 'Http'], ['-', 'HTTP'], ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; |
| 125 | + } |
| 126 | + } |
| 127 | + return $headers; |
| 128 | + } |
| 129 | +} |
0 commit comments