Factory
2 months ago
Interfaces
2 months ago
Cookies.php
2 months ago
Environment.php
2 months ago
Header.php
2 months ago
Headers.php
2 months ago
Message.php
2 months ago
NonBufferedBody.php
2 months ago
Request.php
2 months ago
Response.php
2 months ago
Stream.php
2 months ago
UploadedFile.php
2 months ago
Uri.php
2 months ago
Cookies.php
219 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Slim Framework (https://slimframework.com) |
| 5 | * |
| 6 | * @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE.md (MIT License) |
| 7 | */ |
| 8 | |
| 9 | declare(strict_types=1); |
| 10 | |
| 11 | namespace Slim\Psr7; |
| 12 | |
| 13 | use InvalidArgumentException; |
| 14 | |
| 15 | use function array_key_exists; |
| 16 | use function array_replace; |
| 17 | use function count; |
| 18 | use function explode; |
| 19 | use function gmdate; |
| 20 | use function in_array; |
| 21 | use function is_array; |
| 22 | use function is_string; |
| 23 | use function preg_split; |
| 24 | use function rtrim; |
| 25 | use function strtolower; |
| 26 | use function strtotime; |
| 27 | use function urldecode; |
| 28 | use function urlencode; |
| 29 | |
| 30 | class Cookies |
| 31 | { |
| 32 | /** |
| 33 | * Cookies from HTTP request |
| 34 | */ |
| 35 | protected array $requestCookies = []; |
| 36 | |
| 37 | /** |
| 38 | * Cookies for HTTP response |
| 39 | */ |
| 40 | protected array $responseCookies = []; |
| 41 | |
| 42 | /** |
| 43 | * Default cookie properties |
| 44 | */ |
| 45 | protected array $defaults = [ |
| 46 | 'value' => '', |
| 47 | 'domain' => null, |
| 48 | 'hostonly' => null, |
| 49 | 'path' => null, |
| 50 | 'expires' => null, |
| 51 | 'secure' => false, |
| 52 | 'httponly' => false, |
| 53 | 'samesite' => null |
| 54 | ]; |
| 55 | |
| 56 | /** |
| 57 | * @param array $cookies |
| 58 | */ |
| 59 | public function __construct(array $cookies = []) |
| 60 | { |
| 61 | $this->requestCookies = $cookies; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Set default cookie properties |
| 66 | * |
| 67 | * @param array $settings |
| 68 | * |
| 69 | * @return static |
| 70 | */ |
| 71 | public function setDefaults(array $settings): self |
| 72 | { |
| 73 | $this->defaults = array_replace($this->defaults, $settings); |
| 74 | |
| 75 | return $this; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get cookie |
| 80 | * |
| 81 | * @param string $name |
| 82 | * @param string|array|null $default |
| 83 | * @return mixed|null |
| 84 | */ |
| 85 | public function get(string $name, $default = null) |
| 86 | { |
| 87 | return array_key_exists($name, $this->requestCookies) ? $this->requestCookies[$name] : $default; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Set cookie |
| 92 | * |
| 93 | * @param string $name |
| 94 | * @param string|array $value |
| 95 | * @return static |
| 96 | */ |
| 97 | public function set(string $name, $value): self |
| 98 | { |
| 99 | if (!is_array($value)) { |
| 100 | $value = ['value' => $value]; |
| 101 | } |
| 102 | |
| 103 | $this->responseCookies[$name] = array_replace($this->defaults, $value); |
| 104 | |
| 105 | return $this; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Convert all response cookies into an associate array of header values |
| 110 | * |
| 111 | * @return array |
| 112 | */ |
| 113 | public function toHeaders(): array |
| 114 | { |
| 115 | $headers = []; |
| 116 | |
| 117 | foreach ($this->responseCookies as $name => $properties) { |
| 118 | $headers[] = $this->toHeader($name, $properties); |
| 119 | } |
| 120 | |
| 121 | return $headers; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Convert to `Set-Cookie` header |
| 126 | * |
| 127 | * @param string $name Cookie name |
| 128 | * @param array $properties Cookie properties |
| 129 | * |
| 130 | * @return string |
| 131 | */ |
| 132 | protected function toHeader(string $name, array $properties): string |
| 133 | { |
| 134 | $result = urlencode($name) . '=' . urlencode($properties['value']); |
| 135 | |
| 136 | if (isset($properties['domain'])) { |
| 137 | $result .= '; domain=' . $properties['domain']; |
| 138 | } |
| 139 | |
| 140 | if (isset($properties['path'])) { |
| 141 | $result .= '; path=' . $properties['path']; |
| 142 | } |
| 143 | |
| 144 | if (isset($properties['expires'])) { |
| 145 | if (is_string($properties['expires'])) { |
| 146 | $timestamp = strtotime($properties['expires']); |
| 147 | } else { |
| 148 | $timestamp = (int) $properties['expires']; |
| 149 | } |
| 150 | if ($timestamp && $timestamp !== 0) { |
| 151 | $result .= '; expires=' . gmdate('D, d-M-Y H:i:s e', $timestamp); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (isset($properties['secure']) && $properties['secure']) { |
| 156 | $result .= '; secure'; |
| 157 | } |
| 158 | |
| 159 | if (isset($properties['hostonly']) && $properties['hostonly']) { |
| 160 | $result .= '; HostOnly'; |
| 161 | } |
| 162 | |
| 163 | if (isset($properties['httponly']) && $properties['httponly']) { |
| 164 | $result .= '; HttpOnly'; |
| 165 | } |
| 166 | |
| 167 | if ( |
| 168 | isset($properties['samesite']) |
| 169 | && in_array(strtolower($properties['samesite']), ['lax', 'strict', 'none'], true) |
| 170 | ) { |
| 171 | // While strtolower is needed for correct comparison, the RFC doesn't care about case |
| 172 | $result .= '; SameSite=' . $properties['samesite']; |
| 173 | } |
| 174 | |
| 175 | return $result; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Parse cookie values from header value |
| 180 | * |
| 181 | * Returns an associative array of cookie names and values |
| 182 | * |
| 183 | * @param string|array $header |
| 184 | * |
| 185 | * @return array |
| 186 | */ |
| 187 | public static function parseHeader($header): array |
| 188 | { |
| 189 | if (is_array($header)) { |
| 190 | $header = $header[0] ?? ''; |
| 191 | } |
| 192 | |
| 193 | if (!is_string($header)) { |
| 194 | throw new InvalidArgumentException('Cannot parse Cookie data. Header value must be a string.'); |
| 195 | } |
| 196 | |
| 197 | $header = rtrim($header, "\r\n"); |
| 198 | $pieces = preg_split('@[;]\s*@', $header); |
| 199 | $cookies = []; |
| 200 | |
| 201 | if (is_array($pieces)) { |
| 202 | foreach ($pieces as $cookie) { |
| 203 | $cookie = explode('=', $cookie, 2); |
| 204 | |
| 205 | if (count($cookie) === 2) { |
| 206 | $key = urldecode($cookie[0]); |
| 207 | $value = urldecode($cookie[1]); |
| 208 | |
| 209 | if (!isset($cookies[$key])) { |
| 210 | $cookies[$key] = $value; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return $cookies; |
| 217 | } |
| 218 | } |
| 219 |