| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class Cookie |
| 20 |
{ |
| 21 |
public const SAMESITE_NONE = 'none'; |
| 22 |
public const SAMESITE_LAX = 'lax'; |
| 23 |
public const SAMESITE_STRICT = 'strict'; |
| 24 |
|
| 25 |
protected int $expire; |
| 26 |
protected string $path; |
| 27 |
|
| 28 |
private ?string $sameSite = null; |
| 29 |
private bool $secureDefault = false; |
| 30 |
|
| 31 |
private const RESERVED_CHARS_LIST = "=,; \t\r\n\v\f"; |
| 32 |
private const RESERVED_CHARS_FROM = ['=', ',', ';', ' ', "\t", "\r", "\n", "\v", "\f"]; |
| 33 |
private const RESERVED_CHARS_TO = ['%3D', '%2C', '%3B', '%20', '%09', '%0D', '%0A', '%0B', '%0C']; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public static function fromString(string $cookie, bool $decode = false): static |
| 39 |
{ |
| 40 |
$data = [ |
| 41 |
'expires' => 0, |
| 42 |
'path' => '/', |
| 43 |
'domain' => null, |
| 44 |
'secure' => false, |
| 45 |
'httponly' => false, |
| 46 |
'raw' => !$decode, |
| 47 |
'samesite' => null, |
| 48 |
'partitioned' => false, |
| 49 |
]; |
| 50 |
|
| 51 |
$parts = HeaderUtils::split($cookie, ';='); |
| 52 |
$part = array_shift($parts); |
| 53 |
|
| 54 |
$name = $decode ? urldecode($part[0]) : $part[0]; |
| 55 |
$value = isset($part[1]) ? ($decode ? urldecode($part[1]) : $part[1]) : null; |
| 56 |
|
| 57 |
$data = HeaderUtils::combine($parts) + $data; |
| 58 |
$data['expires'] = self::expiresTimestamp($data['expires']); |
| 59 |
|
| 60 |
if (isset($data['max-age']) && ($data['max-age'] > 0 || $data['expires'] > time())) { |
| 61 |
$data['expires'] = time() + (int) $data['max-age']; |
| 62 |
} |
| 63 |
|
| 64 |
return new static($name, $value, $data['expires'], $data['path'], $data['domain'], $data['secure'], $data['httponly'], $data['raw'], $data['samesite'], $data['partitioned']); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public static function create(string $name, ?string $value = null, int|string|\DateTimeInterface $expire = 0, ?string $path = '/', ?string $domain = null, ?bool $secure = null, bool $httpOnly = true, bool $raw = false, ?string $sameSite = self::SAMESITE_LAX, bool $partitioned = false): self |
| 73 |
{ |
| 74 |
return new self($name, $value, $expire, $path, $domain, $secure, $httpOnly, $raw, $sameSite, $partitioned); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function __construct( |
| 91 |
protected string $name, |
| 92 |
protected ?string $value = null, |
| 93 |
int|string|\DateTimeInterface $expire = 0, |
| 94 |
?string $path = '/', |
| 95 |
protected ?string $domain = null, |
| 96 |
protected ?bool $secure = null, |
| 97 |
protected bool $httpOnly = true, |
| 98 |
private bool $raw = false, |
| 99 |
?string $sameSite = self::SAMESITE_LAX, |
| 100 |
private bool $partitioned = false, |
| 101 |
) { |
| 102 |
|
| 103 |
if ($raw && false !== strpbrk($name, self::RESERVED_CHARS_LIST)) { |
| 104 |
throw new \InvalidArgumentException(\sprintf('The cookie name "%s" contains invalid characters.', $name)); |
| 105 |
} |
| 106 |
|
| 107 |
if (!$name) { |
| 108 |
throw new \InvalidArgumentException('The cookie name cannot be empty.'); |
| 109 |
} |
| 110 |
|
| 111 |
$this->expire = self::expiresTimestamp($expire); |
| 112 |
$this->path = $path ?: '/'; |
| 113 |
$this->sameSite = $this->withSameSite($sameSite)->sameSite; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function withValue(?string $value): static |
| 120 |
{ |
| 121 |
$cookie = clone $this; |
| 122 |
$cookie->value = $value; |
| 123 |
|
| 124 |
return $cookie; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public function withDomain(?string $domain): static |
| 131 |
{ |
| 132 |
$cookie = clone $this; |
| 133 |
$cookie->domain = $domain; |
| 134 |
|
| 135 |
return $cookie; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function withExpires(int|string|\DateTimeInterface $expire = 0): static |
| 142 |
{ |
| 143 |
$cookie = clone $this; |
| 144 |
$cookie->expire = self::expiresTimestamp($expire); |
| 145 |
|
| 146 |
return $cookie; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
private static function expiresTimestamp(int|string|\DateTimeInterface $expire = 0): int |
| 153 |
{ |
| 154 |
|
| 155 |
if ($expire instanceof \DateTimeInterface) { |
| 156 |
$expire = $expire->format('U'); |
| 157 |
} elseif (!is_numeric($expire)) { |
| 158 |
$expire = strtotime($expire); |
| 159 |
|
| 160 |
if (false === $expire) { |
| 161 |
throw new \InvalidArgumentException('The cookie expiration time is not valid.'); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return 0 < $expire ? (int) $expire : 0; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public function withPath(string $path): static |
| 172 |
{ |
| 173 |
$cookie = clone $this; |
| 174 |
$cookie->path = '' === $path ? '/' : $path; |
| 175 |
|
| 176 |
return $cookie; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function withSecure(bool $secure = true): static |
| 183 |
{ |
| 184 |
$cookie = clone $this; |
| 185 |
$cookie->secure = $secure; |
| 186 |
|
| 187 |
return $cookie; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public function withHttpOnly(bool $httpOnly = true): static |
| 194 |
{ |
| 195 |
$cookie = clone $this; |
| 196 |
$cookie->httpOnly = $httpOnly; |
| 197 |
|
| 198 |
return $cookie; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public function withRaw(bool $raw = true): static |
| 205 |
{ |
| 206 |
if ($raw && false !== strpbrk($this->name, self::RESERVED_CHARS_LIST)) { |
| 207 |
throw new \InvalidArgumentException(\sprintf('The cookie name "%s" contains invalid characters.', $this->name)); |
| 208 |
} |
| 209 |
|
| 210 |
$cookie = clone $this; |
| 211 |
$cookie->raw = $raw; |
| 212 |
|
| 213 |
return $cookie; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public function withSameSite(?string $sameSite): static |
| 222 |
{ |
| 223 |
if ('' === $sameSite) { |
| 224 |
$sameSite = null; |
| 225 |
} elseif (null !== $sameSite) { |
| 226 |
$sameSite = strtolower($sameSite); |
| 227 |
} |
| 228 |
|
| 229 |
if (!\in_array($sameSite, [self::SAMESITE_LAX, self::SAMESITE_STRICT, self::SAMESITE_NONE, null], true)) { |
| 230 |
throw new \InvalidArgumentException('The "sameSite" parameter value is not valid.'); |
| 231 |
} |
| 232 |
|
| 233 |
$cookie = clone $this; |
| 234 |
$cookie->sameSite = $sameSite; |
| 235 |
|
| 236 |
return $cookie; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public function withPartitioned(bool $partitioned = true): static |
| 243 |
{ |
| 244 |
$cookie = clone $this; |
| 245 |
$cookie->partitioned = $partitioned; |
| 246 |
|
| 247 |
return $cookie; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
public function __toString(): string |
| 254 |
{ |
| 255 |
if ($this->isRaw()) { |
| 256 |
$str = $this->getName(); |
| 257 |
} else { |
| 258 |
$str = str_replace(self::RESERVED_CHARS_FROM, self::RESERVED_CHARS_TO, $this->getName()); |
| 259 |
} |
| 260 |
|
| 261 |
$str .= '='; |
| 262 |
|
| 263 |
if ('' === (string) $this->getValue()) { |
| 264 |
$str .= 'deleted; expires='.gmdate('D, d M Y H:i:s T', time() - 31536001).'; Max-Age=0'; |
| 265 |
} else { |
| 266 |
$str .= $this->isRaw() ? $this->getValue() : rawurlencode($this->getValue()); |
| 267 |
|
| 268 |
if (0 !== $this->getExpiresTime()) { |
| 269 |
$str .= '; expires='.gmdate('D, d M Y H:i:s T', $this->getExpiresTime()).'; Max-Age='.$this->getMaxAge(); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
if ($this->getPath()) { |
| 274 |
$str .= '; path='.$this->getPath(); |
| 275 |
} |
| 276 |
|
| 277 |
if ($this->getDomain()) { |
| 278 |
$str .= '; domain='.$this->getDomain(); |
| 279 |
} |
| 280 |
|
| 281 |
if ($this->isSecure()) { |
| 282 |
$str .= '; secure'; |
| 283 |
} |
| 284 |
|
| 285 |
if ($this->isHttpOnly()) { |
| 286 |
$str .= '; httponly'; |
| 287 |
} |
| 288 |
|
| 289 |
if (null !== $this->getSameSite()) { |
| 290 |
$str .= '; samesite='.$this->getSameSite(); |
| 291 |
} |
| 292 |
|
| 293 |
if ($this->isPartitioned()) { |
| 294 |
$str .= '; partitioned'; |
| 295 |
} |
| 296 |
|
| 297 |
return $str; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public function getName(): string |
| 304 |
{ |
| 305 |
return $this->name; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
public function getValue(): ?string |
| 312 |
{ |
| 313 |
return $this->value; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public function getDomain(): ?string |
| 320 |
{ |
| 321 |
return $this->domain; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
public function getExpiresTime(): int |
| 328 |
{ |
| 329 |
return $this->expire; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public function getMaxAge(): int |
| 336 |
{ |
| 337 |
$maxAge = $this->expire - time(); |
| 338 |
|
| 339 |
return max(0, $maxAge); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public function getPath(): string |
| 346 |
{ |
| 347 |
return $this->path; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public function isSecure(): bool |
| 354 |
{ |
| 355 |
return $this->secure ?? $this->secureDefault; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public function isHttpOnly(): bool |
| 362 |
{ |
| 363 |
return $this->httpOnly; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
public function isCleared(): bool |
| 370 |
{ |
| 371 |
return 0 !== $this->expire && $this->expire < time(); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public function isRaw(): bool |
| 378 |
{ |
| 379 |
return $this->raw; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public function isPartitioned(): bool |
| 386 |
{ |
| 387 |
return $this->partitioned; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public function getSameSite(): ?string |
| 394 |
{ |
| 395 |
return $this->sameSite; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
public function setSecureDefault(bool $default): void |
| 402 |
{ |
| 403 |
$this->secureDefault = $default; |
| 404 |
} |
| 405 |
} |