| 1 |
<?php |
| 2 |
|
| 3 |
namespace GuzzleHttp\Cookie; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
class SetCookie |
| 9 |
{ |
| 10 |
|
| 11 |
|
| 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 |
|
| 27 |
|
| 28 |
private $data; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public static function fromString(string $cookie): self |
| 36 |
{ |
| 37 |
|
| 38 |
$data = self::$defaults; |
| 39 |
|
| 40 |
$pieces = \array_filter(\array_map('trim', \explode(';', $cookie))); |
| 41 |
|
| 42 |
if (!isset($pieces[0]) || \strpos($pieces[0], '=') === false) { |
| 43 |
return new self($data); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 126 |
foreach (array_diff(array_keys($data), array_keys(self::$defaults)) as $key) { |
| 127 |
$this->data[$key] = $data[$key]; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
if (!$this->getExpires() && $this->getMaxAge()) { |
| 132 |
|
| 133 |
$this->setExpires(\time() + $this->getMaxAge()); |
| 134 |
} elseif (null !== ($expires = $this->getExpires()) && !\is_numeric($expires)) { |
| 135 |
$this->setExpires($expires); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public function __toString() |
| 140 |
{ |
| 141 |
$str = $this->data['Name'].'='.($this->data['Value'] ?? '').'; '; |
| 142 |
foreach ($this->data as $k => $v) { |
| 143 |
if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== false) { |
| 144 |
if ($k === 'Expires') { |
| 145 |
$str .= 'Expires='.\gmdate('D, d M Y H:i:s \G\M\T', $v).'; '; |
| 146 |
} else { |
| 147 |
$str .= ($v === true ? $k : "{$k}={$v}").'; '; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return \rtrim($str, '; '); |
| 153 |
} |
| 154 |
|
| 155 |
public function toArray(): array |
| 156 |
{ |
| 157 |
return $this->data; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function getName() |
| 166 |
{ |
| 167 |
return $this->data['Name']; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public function setName($name): void |
| 176 |
{ |
| 177 |
if (!is_string($name)) { |
| 178 |
\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__); |
| 179 |
} |
| 180 |
|
| 181 |
$this->data['Name'] = (string) $name; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function getValue() |
| 190 |
{ |
| 191 |
return $this->data['Value']; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public function setValue($value): void |
| 200 |
{ |
| 201 |
if (!is_string($value)) { |
| 202 |
\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__); |
| 203 |
} |
| 204 |
|
| 205 |
$this->data['Value'] = (string) $value; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public function getDomain() |
| 214 |
{ |
| 215 |
return $this->data['Domain']; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public function setDomain($domain): void |
| 224 |
{ |
| 225 |
if (!is_string($domain) && null !== $domain) { |
| 226 |
\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__); |
| 227 |
} |
| 228 |
|
| 229 |
$this->data['Domain'] = null === $domain ? null : (string) $domain; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public function getPath() |
| 238 |
{ |
| 239 |
return $this->data['Path']; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public function setPath($path): void |
| 248 |
{ |
| 249 |
if (!is_string($path)) { |
| 250 |
\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__); |
| 251 |
} |
| 252 |
|
| 253 |
$this->data['Path'] = (string) $path; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public function getMaxAge() |
| 262 |
{ |
| 263 |
return null === $this->data['Max-Age'] ? null : (int) $this->data['Max-Age']; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public function setMaxAge($maxAge): void |
| 272 |
{ |
| 273 |
if (!is_int($maxAge) && null !== $maxAge) { |
| 274 |
\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__); |
| 275 |
} |
| 276 |
|
| 277 |
$this->data['Max-Age'] = $maxAge === null ? null : (int) $maxAge; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public function getExpires() |
| 286 |
{ |
| 287 |
return $this->data['Expires']; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public function setExpires($timestamp): void |
| 296 |
{ |
| 297 |
if (!is_int($timestamp) && !is_string($timestamp) && null !== $timestamp) { |
| 298 |
\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__); |
| 299 |
} |
| 300 |
|
| 301 |
if (null === $timestamp) { |
| 302 |
$this->data['Expires'] = null; |
| 303 |
} elseif (\is_numeric($timestamp)) { |
| 304 |
$this->data['Expires'] = (int) $timestamp; |
| 305 |
} else { |
| 306 |
|
| 307 |
$expires = \strtotime((string) $timestamp); |
| 308 |
$this->data['Expires'] = $expires === false ? null : $expires; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
public function getSecure() |
| 318 |
{ |
| 319 |
return $this->data['Secure']; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
public function setSecure($secure): void |
| 328 |
{ |
| 329 |
if (!is_bool($secure)) { |
| 330 |
\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__); |
| 331 |
} |
| 332 |
|
| 333 |
$this->data['Secure'] = (bool) $secure; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
public function getDiscard() |
| 342 |
{ |
| 343 |
return $this->data['Discard']; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public function setDiscard($discard): void |
| 352 |
{ |
| 353 |
if (!is_bool($discard)) { |
| 354 |
\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__); |
| 355 |
} |
| 356 |
|
| 357 |
$this->data['Discard'] = (bool) $discard; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
public function getHttpOnly() |
| 366 |
{ |
| 367 |
return $this->data['HttpOnly']; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public function setHttpOnly($httpOnly): void |
| 376 |
{ |
| 377 |
if (!is_bool($httpOnly)) { |
| 378 |
\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__); |
| 379 |
} |
| 380 |
|
| 381 |
$this->data['HttpOnly'] = (bool) $httpOnly; |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
public function matchesPath(string $requestPath): bool |
| 400 |
{ |
| 401 |
$cookiePath = $this->getPath(); |
| 402 |
|
| 403 |
|
| 404 |
if ($cookiePath === '/' || $cookiePath == $requestPath) { |
| 405 |
return true; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
if (0 !== \strpos($requestPath, $cookiePath)) { |
| 410 |
return false; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
if (\substr($cookiePath, -1, 1) === '/') { |
| 415 |
return true; |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
return \substr($requestPath, \strlen($cookiePath), 1) === '/'; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
public function matchesDomain(string $domain): bool |
| 428 |
{ |
| 429 |
$cookieDomain = $this->getDomain(); |
| 430 |
if (null === $cookieDomain) { |
| 431 |
return true; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
$cookieDomain = \ltrim(\strtolower($cookieDomain), '.'); |
| 437 |
|
| 438 |
$domain = \strtolower($domain); |
| 439 |
|
| 440 |
|
| 441 |
if ('' === $cookieDomain || $domain === $cookieDomain) { |
| 442 |
return true; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
if (\filter_var($domain, \FILTER_VALIDATE_IP)) { |
| 448 |
return false; |
| 449 |
} |
| 450 |
|
| 451 |
return (bool) \preg_match('/\.'.\preg_quote($cookieDomain, '/').'$/', $domain); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
public function isExpired(): bool |
| 458 |
{ |
| 459 |
return $this->getExpires() !== null && \time() > $this->getExpires(); |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
public function validate() |
| 468 |
{ |
| 469 |
$name = $this->getName(); |
| 470 |
if ($name === '') { |
| 471 |
return 'The cookie name must not be empty'; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
if (\preg_match( |
| 476 |
'/[\x00-\x20\x22\x28-\x29\x2c\x2f\x3a-\x40\x5c\x7b\x7d\x7f]/', |
| 477 |
$name |
| 478 |
)) { |
| 479 |
return 'Cookie name must not contain invalid characters: ASCII ' |
| 480 |
.'Control characters (0-31;127), space, tab and the ' |
| 481 |
.'following characters: ()<>@,;:\"/?={}'; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
$value = $this->getValue(); |
| 487 |
if ($value === null) { |
| 488 |
return 'The cookie value must not be empty'; |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
$domain = $this->getDomain(); |
| 494 |
if ($domain === '') { |
| 495 |
return 'The cookie domain must not be empty'; |
| 496 |
} |
| 497 |
|
| 498 |
return true; |
| 499 |
} |
| 500 |
} |