| 1 |
<?php |
| 2 |
|
| 3 |
namespace GuzzleHttp\Cookie; |
| 4 |
|
| 5 |
/** |
| 6 |
* Set-Cookie object |
| 7 |
*/ |
| 8 |
class SetCookie |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @var array |
| 12 |
*/ |
| 13 |
private static $defaults = [ |
| 14 |
'Name' => null, |
| 15 |
'Value' => null, |
| 16 |
'Domain' => null, |
| 17 |
'Path' => '/', |
| 18 |
'Max-Age' => null, |
| 19 |
'Expires' => null, |
| 20 |
'Secure' => false, |
| 21 |
'Discard' => false, |
| 22 |
'HttpOnly' => false, |
| 23 |
]; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var array Cookie data |
| 27 |
*/ |
| 28 |
private $data; |
| 29 |
|
| 30 |
/** |
| 31 |
* Create a new SetCookie object from a string. |
| 32 |
* |
| 33 |
* @param string $cookie Set-Cookie header string |
| 34 |
*/ |
| 35 |
public static function fromString(string $cookie): self |
| 36 |
{ |
| 37 |
// Create the default return array |
| 38 |
$data = self::$defaults; |
| 39 |
// Explode the cookie string using a series of semicolons |
| 40 |
$pieces = \array_filter(\array_map('trim', \explode(';', $cookie))); |
| 41 |
// The name of the cookie (first kvp) must exist and include an equal sign. |
| 42 |
if (!isset($pieces[0]) || \strpos($pieces[0], '=') === false) { |
| 43 |
return new self($data); |
| 44 |
} |
| 45 |
|
| 46 |
// Add the cookie pieces into the parsed data array |
| 47 |
foreach ($pieces as $part) { |
| 48 |
$cookieParts = \explode('=', $part, 2); |
| 49 |
$key = \trim($cookieParts[0]); |
| 50 |
$value = isset($cookieParts[1]) |
| 51 |
? \trim($cookieParts[1], " \n\r\t\0\x0B") |
| 52 |
: true; |
| 53 |
|
| 54 |
// Only check for non-cookies when cookies have been found |
| 55 |
if (!isset($data['Name'])) { |
| 56 |
$data['Name'] = $key; |
| 57 |
$data['Value'] = $value; |
| 58 |
} else { |
| 59 |
foreach (\array_keys(self::$defaults) as $search) { |
| 60 |
if (!\strcasecmp($search, $key)) { |
| 61 |
if ($search === 'Max-Age') { |
| 62 |
if (is_numeric($value)) { |
| 63 |
$data[$search] = (int) $value; |
| 64 |
} |
| 65 |
} elseif ($search === 'Secure' || $search === 'Discard' || $search === 'HttpOnly') { |
| 66 |
if ($value) { |
| 67 |
$data[$search] = true; |
| 68 |
} |
| 69 |
} else { |
| 70 |
$data[$search] = $value; |
| 71 |
} |
| 72 |
continue 2; |
| 73 |
} |
| 74 |
} |
| 75 |
$data[$key] = $value; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return new self($data); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* @param array $data Array of cookie data provided by a Cookie parser |
| 84 |
*/ |
| 85 |
public function __construct(array $data = []) |
| 86 |
{ |
| 87 |
$this->data = self::$defaults; |
| 88 |
|
| 89 |
if (isset($data['Name'])) { |
| 90 |
$this->setName($data['Name']); |
| 91 |
} |
| 92 |
|
| 93 |
if (isset($data['Value'])) { |
| 94 |
$this->setValue($data['Value']); |
| 95 |
} |
| 96 |
|
| 97 |
if (isset($data['Domain'])) { |
| 98 |
$this->setDomain($data['Domain']); |
| 99 |
} |
| 100 |
|
| 101 |
if (isset($data['Path'])) { |
| 102 |
$this->setPath($data['Path']); |
| 103 |
} |
| 104 |
|
| 105 |
if (isset($data['Max-Age'])) { |
| 106 |
$this->setMaxAge($data['Max-Age']); |
| 107 |
} |
| 108 |
|
| 109 |
if (isset($data['Expires'])) { |
| 110 |
$this->setExpires($data['Expires']); |
| 111 |
} |
| 112 |
|
| 113 |
if (isset($data['Secure'])) { |
| 114 |
$this->setSecure($data['Secure']); |
| 115 |
} |
| 116 |
|
| 117 |
if (isset($data['Discard'])) { |
| 118 |
$this->setDiscard($data['Discard']); |
| 119 |
} |
| 120 |
|
| 121 |
if (isset($data['HttpOnly'])) { |
| 122 |
$this->setHttpOnly($data['HttpOnly']); |
| 123 |
} |
| 124 |
|
| 125 |
// Set the remaining values that don't have extra validation logic |
| 126 |
foreach (array_diff(array_keys($data), array_keys(self::$defaults)) as $key) { |
| 127 |
$this->data[$key] = $data[$key]; |
| 128 |
} |
| 129 |
|
| 130 |
// Extract the Expires value and turn it into a UNIX timestamp if needed |
| 131 |
$maxAge = $this->getMaxAge(); |
| 132 |
if (!$this->getExpires() && $maxAge !== null) { |
| 133 |
// Calculate the Expires date |
| 134 |
$this->setExpires(self::maxAgeToExpires($maxAge, \time())); |
| 135 |
} elseif (null !== ($expires = $this->getExpires()) && !\is_numeric($expires)) { |
| 136 |
$this->setExpires($expires); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
private static function maxAgeToExpires(int $maxAge, int $now): int |
| 141 |
{ |
| 142 |
if ($maxAge <= 0) { |
| 143 |
return $now - 1; |
| 144 |
} |
| 145 |
if ($maxAge > \PHP_INT_MAX - $now) { |
| 146 |
return \PHP_INT_MAX; |
| 147 |
} |
| 148 |
|
| 149 |
return $now + $maxAge; |
| 150 |
} |
| 151 |
|
| 152 |
public function __toString() |
| 153 |
{ |
| 154 |
$str = $this->data['Name'].'='.($this->data['Value'] ?? '').'; '; |
| 155 |
foreach ($this->data as $k => $v) { |
| 156 |
if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== false) { |
| 157 |
if ($k === 'Expires') { |
| 158 |
$str .= 'Expires='.\gmdate('D, d M Y H:i:s \G\M\T', $v).'; '; |
| 159 |
} else { |
| 160 |
$str .= ($v === true ? $k : "{$k}={$v}").'; '; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return \rtrim($str, '; '); |
| 166 |
} |
| 167 |
|
| 168 |
public function toArray(): array |
| 169 |
{ |
| 170 |
return $this->data; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Get the cookie name. |
| 175 |
* |
| 176 |
* @return string |
| 177 |
*/ |
| 178 |
public function getName() |
| 179 |
{ |
| 180 |
return $this->data['Name']; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Set the cookie name. |
| 185 |
* |
| 186 |
* @param string $name Cookie name |
| 187 |
*/ |
| 188 |
public function setName($name): void |
| 189 |
{ |
| 190 |
if (!is_string($name)) { |
| 191 |
\trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 192 |
} |
| 193 |
|
| 194 |
$this->data['Name'] = (string) $name; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Get the cookie value. |
| 199 |
* |
| 200 |
* @return string|null |
| 201 |
*/ |
| 202 |
public function getValue() |
| 203 |
{ |
| 204 |
return $this->data['Value']; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Set the cookie value. |
| 209 |
* |
| 210 |
* @param string $value Cookie value |
| 211 |
*/ |
| 212 |
public function setValue($value): void |
| 213 |
{ |
| 214 |
if (!is_string($value)) { |
| 215 |
\trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 216 |
} |
| 217 |
|
| 218 |
$this->data['Value'] = (string) $value; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Get the domain. |
| 223 |
* |
| 224 |
* @return string|null |
| 225 |
*/ |
| 226 |
public function getDomain() |
| 227 |
{ |
| 228 |
return $this->data['Domain']; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Set the domain of the cookie. |
| 233 |
* |
| 234 |
* @param string|null $domain |
| 235 |
*/ |
| 236 |
public function setDomain($domain): void |
| 237 |
{ |
| 238 |
if (!is_string($domain) && null !== $domain) { |
| 239 |
\trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 240 |
} |
| 241 |
|
| 242 |
$this->data['Domain'] = null === $domain ? null : (string) $domain; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Get the path. |
| 247 |
* |
| 248 |
* @return string |
| 249 |
*/ |
| 250 |
public function getPath() |
| 251 |
{ |
| 252 |
return $this->data['Path']; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Set the path of the cookie. |
| 257 |
* |
| 258 |
* @param string $path Path of the cookie |
| 259 |
*/ |
| 260 |
public function setPath($path): void |
| 261 |
{ |
| 262 |
if (!is_string($path)) { |
| 263 |
\trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a string to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 264 |
} |
| 265 |
|
| 266 |
$this->data['Path'] = (string) $path; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Maximum lifetime of the cookie in seconds. |
| 271 |
* |
| 272 |
* @return int|null |
| 273 |
*/ |
| 274 |
public function getMaxAge() |
| 275 |
{ |
| 276 |
return null === $this->data['Max-Age'] ? null : (int) $this->data['Max-Age']; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Set the max-age of the cookie. |
| 281 |
* |
| 282 |
* @param int|null $maxAge Max age of the cookie in seconds |
| 283 |
*/ |
| 284 |
public function setMaxAge($maxAge): void |
| 285 |
{ |
| 286 |
if (!is_int($maxAge) && null !== $maxAge) { |
| 287 |
\trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 288 |
} |
| 289 |
|
| 290 |
$this->data['Max-Age'] = $maxAge === null ? null : (int) $maxAge; |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* The UNIX timestamp when the cookie Expires. |
| 295 |
* |
| 296 |
* @return string|int|null |
| 297 |
*/ |
| 298 |
public function getExpires() |
| 299 |
{ |
| 300 |
return $this->data['Expires']; |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Set the unix timestamp for which the cookie will expire. |
| 305 |
* |
| 306 |
* @param int|string|null $timestamp Unix timestamp or any English textual datetime description. |
| 307 |
*/ |
| 308 |
public function setExpires($timestamp): void |
| 309 |
{ |
| 310 |
if (!is_int($timestamp) && !is_string($timestamp) && null !== $timestamp) { |
| 311 |
\trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing an int, string or null to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 312 |
} |
| 313 |
|
| 314 |
if (null === $timestamp) { |
| 315 |
$this->data['Expires'] = null; |
| 316 |
} elseif (\is_numeric($timestamp)) { |
| 317 |
$this->data['Expires'] = (int) $timestamp; |
| 318 |
} else { |
| 319 |
// Store unparseable dates as session cookies, not as expired cookies. |
| 320 |
$expires = \strtotime((string) $timestamp); |
| 321 |
$this->data['Expires'] = $expires === false ? null : $expires; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Get whether or not this is a secure cookie. |
| 327 |
* |
| 328 |
* @return bool |
| 329 |
*/ |
| 330 |
public function getSecure() |
| 331 |
{ |
| 332 |
return $this->data['Secure']; |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Set whether or not the cookie is secure. |
| 337 |
* |
| 338 |
* @param bool $secure Set to true or false if secure |
| 339 |
*/ |
| 340 |
public function setSecure($secure): void |
| 341 |
{ |
| 342 |
if (!is_bool($secure)) { |
| 343 |
\trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 344 |
} |
| 345 |
|
| 346 |
$this->data['Secure'] = (bool) $secure; |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Get whether or not this is a session cookie. |
| 351 |
* |
| 352 |
* @return bool|null |
| 353 |
*/ |
| 354 |
public function getDiscard() |
| 355 |
{ |
| 356 |
return $this->data['Discard']; |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Set whether or not this is a session cookie. |
| 361 |
* |
| 362 |
* @param bool $discard Set to true or false if this is a session cookie |
| 363 |
*/ |
| 364 |
public function setDiscard($discard): void |
| 365 |
{ |
| 366 |
if (!is_bool($discard)) { |
| 367 |
\trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 368 |
} |
| 369 |
|
| 370 |
$this->data['Discard'] = (bool) $discard; |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Get whether or not this is an HTTP only cookie. |
| 375 |
* |
| 376 |
* @return bool |
| 377 |
*/ |
| 378 |
public function getHttpOnly() |
| 379 |
{ |
| 380 |
return $this->data['HttpOnly']; |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Set whether or not this is an HTTP only cookie. |
| 385 |
* |
| 386 |
* @param bool $httpOnly Set to true or false if this is HTTP only |
| 387 |
*/ |
| 388 |
public function setHttpOnly($httpOnly): void |
| 389 |
{ |
| 390 |
if (!is_bool($httpOnly)) { |
| 391 |
\trigger_deprecation('guzzlehttp/guzzle', '7.4', 'Not passing a bool to %s::%s() is deprecated and will cause an error in 8.0.', __CLASS__, __FUNCTION__); |
| 392 |
} |
| 393 |
|
| 394 |
$this->data['HttpOnly'] = (bool) $httpOnly; |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Check if the cookie matches a path value. |
| 399 |
* |
| 400 |
* A request-path path-matches a given cookie-path if at least one of |
| 401 |
* the following conditions holds: |
| 402 |
* |
| 403 |
* - The cookie-path and the request-path are identical. |
| 404 |
* - The cookie-path is a prefix of the request-path, and the last |
| 405 |
* character of the cookie-path is %x2F ("/"). |
| 406 |
* - The cookie-path is a prefix of the request-path, and the first |
| 407 |
* character of the request-path that is not included in the cookie- |
| 408 |
* path is a %x2F ("/") character. |
| 409 |
* |
| 410 |
* @param string $requestPath Path to check against |
| 411 |
*/ |
| 412 |
public function matchesPath(string $requestPath): bool |
| 413 |
{ |
| 414 |
$cookiePath = $this->getPath(); |
| 415 |
|
| 416 |
// Match on exact matches or when path is the default empty "/" |
| 417 |
if ($cookiePath === '/' || $cookiePath === $requestPath) { |
| 418 |
return true; |
| 419 |
} |
| 420 |
|
| 421 |
// Ensure that the cookie-path is a prefix of the request path. |
| 422 |
if (0 !== \strpos($requestPath, $cookiePath)) { |
| 423 |
return false; |
| 424 |
} |
| 425 |
|
| 426 |
// Match if the last character of the cookie-path is "/" |
| 427 |
if (\substr($cookiePath, -1, 1) === '/') { |
| 428 |
return true; |
| 429 |
} |
| 430 |
|
| 431 |
// Match if the first character not included in cookie path is "/" |
| 432 |
return \substr($requestPath, \strlen($cookiePath), 1) === '/'; |
| 433 |
} |
| 434 |
|
| 435 |
/** |
| 436 |
* Check if the cookie matches a domain value. |
| 437 |
* |
| 438 |
* @param string $domain Domain to check against |
| 439 |
*/ |
| 440 |
public function matchesDomain(string $domain): bool |
| 441 |
{ |
| 442 |
$cookieDomain = $this->getDomain(); |
| 443 |
if (null === $cookieDomain) { |
| 444 |
return true; |
| 445 |
} |
| 446 |
|
| 447 |
// Remove the leading '.' as per spec in RFC 6265. |
| 448 |
// https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.3 |
| 449 |
$cookieDomain = \strtolower($cookieDomain); |
| 450 |
if ($cookieDomain !== '' && $cookieDomain[0] === '.') { |
| 451 |
/** @var string */ |
| 452 |
$cookieDomain = \substr($cookieDomain, 1); |
| 453 |
} |
| 454 |
if ('' === $cookieDomain) { |
| 455 |
return false; |
| 456 |
} |
| 457 |
|
| 458 |
$domain = \strtolower($domain); |
| 459 |
if ($domain === $cookieDomain) { |
| 460 |
return true; |
| 461 |
} |
| 462 |
|
| 463 |
// IP literals and numeric hosts are exact-match-only per RFC 6265. |
| 464 |
// Only the exact match above may succeed for those cookie domains. |
| 465 |
if (self::isIpAddressOrNumericHost($cookieDomain)) { |
| 466 |
return false; |
| 467 |
} |
| 468 |
|
| 469 |
// Matching the subdomain according to RFC 6265. |
| 470 |
// https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3 |
| 471 |
if (\filter_var($domain, \FILTER_VALIDATE_IP)) { |
| 472 |
return false; |
| 473 |
} |
| 474 |
|
| 475 |
return (bool) \preg_match('/\.'.\preg_quote($cookieDomain, '/').'$/', $domain); |
| 476 |
} |
| 477 |
|
| 478 |
private static function isIpAddressOrNumericHost(string $host): bool |
| 479 |
{ |
| 480 |
// Strip one root dot before detection so trailing-dot numeric hosts |
| 481 |
// still cannot be matched by subdomains. |
| 482 |
if ($host !== '' && \str_ends_with($host, '.')) { |
| 483 |
$host = \substr($host, 0, -1); |
| 484 |
} |
| 485 |
|
| 486 |
if (\str_starts_with($host, '[') && \str_ends_with($host, ']')) { |
| 487 |
$host = \substr($host, 1, -1); |
| 488 |
} |
| 489 |
|
| 490 |
if (\filter_var($host, \FILTER_VALIDATE_IP) !== false) { |
| 491 |
return true; |
| 492 |
} |
| 493 |
|
| 494 |
// Public DNS names do not have an all-numeric rightmost label; treat |
| 495 |
// those private/internal hosts as exact-match-only too. |
| 496 |
$labels = \explode('.', $host); |
| 497 |
$last = (string) \end($labels); |
| 498 |
|
| 499 |
return $last !== '' && \ctype_digit($last); |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* Check if the cookie is expired. |
| 504 |
*/ |
| 505 |
public function isExpired(): bool |
| 506 |
{ |
| 507 |
return $this->getExpires() !== null && \time() > $this->getExpires(); |
| 508 |
} |
| 509 |
|
| 510 |
/** |
| 511 |
* Check if the cookie is valid according to RFC 6265. |
| 512 |
* |
| 513 |
* @return bool|string Returns true if valid or an error message if invalid |
| 514 |
*/ |
| 515 |
public function validate() |
| 516 |
{ |
| 517 |
$name = $this->getName(); |
| 518 |
if ($name === '') { |
| 519 |
return 'The cookie name must not be empty'; |
| 520 |
} |
| 521 |
|
| 522 |
// Check if any of the invalid characters are present in the cookie name |
| 523 |
if (\preg_match( |
| 524 |
'/[\x00-\x20\x22\x28-\x29\x2c\x2f\x3a-\x40\x5c\x7b\x7d\x7f]/', |
| 525 |
$name |
| 526 |
)) { |
| 527 |
return 'Cookie name must not contain invalid characters: ASCII ' |
| 528 |
.'Control characters (0-31;127), space, tab and the ' |
| 529 |
.'following characters: ()<>@,;:\"/?={}'; |
| 530 |
} |
| 531 |
|
| 532 |
// Value must not be null. 0 and empty string are valid. Empty strings |
| 533 |
// are technically against RFC 6265, but known to happen in the wild. |
| 534 |
$value = $this->getValue(); |
| 535 |
if ($value === null) { |
| 536 |
return 'The cookie value must not be empty'; |
| 537 |
} |
| 538 |
|
| 539 |
// Domains must not be empty, but may be omitted. "0" is not a valid |
| 540 |
// internet domain, but may be used as server name in a private network. |
| 541 |
$domain = $this->getDomain(); |
| 542 |
if ($domain === '' || (null !== $domain && '' === \ltrim(\trim($domain), '.'))) { |
| 543 |
return 'The cookie domain must not be empty'; |
| 544 |
} |
| 545 |
|
| 546 |
return true; |
| 547 |
} |
| 548 |
} |