| 1 |
<?php |
| 2 |
|
| 3 |
namespace GuzzleHttp\Cookie; |
| 4 |
|
| 5 |
use Psr\Http\Message\RequestInterface; |
| 6 |
use Psr\Http\Message\ResponseInterface; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class CookieJar implements CookieJarInterface |
| 12 |
{ |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
private $cookies = []; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
private $strictMode; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public function __construct(bool $strictMode = false, array $cookieArray = []) |
| 31 |
{ |
| 32 |
$this->strictMode = $strictMode; |
| 33 |
|
| 34 |
foreach ($cookieArray as $cookie) { |
| 35 |
if (!$cookie instanceof SetCookie) { |
| 36 |
$cookie = new SetCookie($cookie); |
| 37 |
} |
| 38 |
$this->setCookie($cookie); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public static function fromArray(array $cookies, string $domain): self |
| 49 |
{ |
| 50 |
$cookieJar = new self(); |
| 51 |
foreach ($cookies as $name => $value) { |
| 52 |
$cookieJar->setCookie(new SetCookie([ |
| 53 |
'Domain' => $domain, |
| 54 |
'Name' => $name, |
| 55 |
'Value' => $value, |
| 56 |
'Discard' => true, |
| 57 |
])); |
| 58 |
} |
| 59 |
|
| 60 |
return $cookieJar; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public static function shouldPersist(SetCookie $cookie, bool $allowSessionCookies = false): bool |
| 71 |
{ |
| 72 |
if ($cookie->getExpires() || $allowSessionCookies) { |
| 73 |
if (!$cookie->getDiscard()) { |
| 74 |
return true; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function getCookieByName(string $name): ?SetCookie |
| 89 |
{ |
| 90 |
foreach ($this->cookies as $cookie) { |
| 91 |
if ($cookie->getName() !== null && \strcasecmp($cookie->getName(), $name) === 0) { |
| 92 |
return $cookie; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return null; |
| 97 |
} |
| 98 |
|
| 99 |
public function toArray(): array |
| 100 |
{ |
| 101 |
return \array_map(static function (SetCookie $cookie): array { |
| 102 |
return $cookie->toArray(); |
| 103 |
}, $this->getIterator()->getArrayCopy()); |
| 104 |
} |
| 105 |
|
| 106 |
public function clear(?string $domain = null, ?string $path = null, ?string $name = null): void |
| 107 |
{ |
| 108 |
if ($domain === null) { |
| 109 |
$this->cookies = []; |
| 110 |
|
| 111 |
return; |
| 112 |
} elseif (!$path) { |
| 113 |
$this->cookies = \array_filter( |
| 114 |
$this->cookies, |
| 115 |
static function (SetCookie $cookie) use ($domain): bool { |
| 116 |
return $cookie->getDomain() === null || !$cookie->matchesDomain($domain); |
| 117 |
} |
| 118 |
); |
| 119 |
} elseif (!$name) { |
| 120 |
$this->cookies = \array_filter( |
| 121 |
$this->cookies, |
| 122 |
static function (SetCookie $cookie) use ($path, $domain): bool { |
| 123 |
return !($cookie->getDomain() !== null |
| 124 |
&& $cookie->matchesPath($path) |
| 125 |
&& $cookie->matchesDomain($domain)); |
| 126 |
} |
| 127 |
); |
| 128 |
} else { |
| 129 |
$this->cookies = \array_filter( |
| 130 |
$this->cookies, |
| 131 |
static function (SetCookie $cookie) use ($path, $domain, $name) { |
| 132 |
return !($cookie->getDomain() !== null |
| 133 |
&& $cookie->getName() == $name |
| 134 |
&& $cookie->matchesPath($path) |
| 135 |
&& $cookie->matchesDomain($domain)); |
| 136 |
} |
| 137 |
); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
public function clearSessionCookies(): void |
| 142 |
{ |
| 143 |
$this->cookies = \array_filter( |
| 144 |
$this->cookies, |
| 145 |
static function (SetCookie $cookie): bool { |
| 146 |
return !$cookie->getDiscard() && $cookie->getExpires(); |
| 147 |
} |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
public function setCookie(SetCookie $cookie): bool |
| 152 |
{ |
| 153 |
|
| 154 |
|
| 155 |
$name = $cookie->getName(); |
| 156 |
if (!$name && $name !== '0') { |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
$result = $cookie->validate(); |
| 162 |
if ($result !== true) { |
| 163 |
if ($this->strictMode) { |
| 164 |
throw new \RuntimeException('Invalid cookie: '.$result); |
| 165 |
} |
| 166 |
$this->removeCookieIfEmpty($cookie); |
| 167 |
|
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
foreach ($this->cookies as $i => $c) { |
| 173 |
|
| 174 |
|
| 175 |
if ($c->getPath() != $cookie->getPath() |
| 176 |
|| $c->getDomain() != $cookie->getDomain() |
| 177 |
|| $c->getName() != $cookie->getName() |
| 178 |
) { |
| 179 |
continue; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
if (!$cookie->getDiscard() && $c->getDiscard()) { |
| 185 |
unset($this->cookies[$i]); |
| 186 |
continue; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
if ($cookie->getExpires() > $c->getExpires()) { |
| 192 |
unset($this->cookies[$i]); |
| 193 |
continue; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
if ($cookie->getValue() !== $c->getValue()) { |
| 198 |
unset($this->cookies[$i]); |
| 199 |
continue; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
$this->cookies[] = $cookie; |
| 207 |
|
| 208 |
return true; |
| 209 |
} |
| 210 |
|
| 211 |
public function count(): int |
| 212 |
{ |
| 213 |
return \count($this->cookies); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public function getIterator(): \ArrayIterator |
| 220 |
{ |
| 221 |
return new \ArrayIterator(\array_values($this->cookies)); |
| 222 |
} |
| 223 |
|
| 224 |
public function extractCookies(RequestInterface $request, ResponseInterface $response): void |
| 225 |
{ |
| 226 |
if ($cookieHeader = $response->getHeader('Set-Cookie')) { |
| 227 |
foreach ($cookieHeader as $cookie) { |
| 228 |
$sc = SetCookie::fromString($cookie); |
| 229 |
if (!$sc->getDomain()) { |
| 230 |
$sc->setDomain($request->getUri()->getHost()); |
| 231 |
} |
| 232 |
if (0 !== \strpos($sc->getPath(), '/')) { |
| 233 |
$sc->setPath($this->getCookiePathFromRequest($request)); |
| 234 |
} |
| 235 |
if (!$sc->matchesDomain($request->getUri()->getHost())) { |
| 236 |
continue; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
$this->setCookie($sc); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
private function getCookiePathFromRequest(RequestInterface $request): string |
| 251 |
{ |
| 252 |
$uriPath = $request->getUri()->getPath(); |
| 253 |
if ('' === $uriPath) { |
| 254 |
return '/'; |
| 255 |
} |
| 256 |
if (0 !== \strpos($uriPath, '/')) { |
| 257 |
return '/'; |
| 258 |
} |
| 259 |
if ('/' === $uriPath) { |
| 260 |
return '/'; |
| 261 |
} |
| 262 |
$lastSlashPos = \strrpos($uriPath, '/'); |
| 263 |
if (0 === $lastSlashPos || false === $lastSlashPos) { |
| 264 |
return '/'; |
| 265 |
} |
| 266 |
|
| 267 |
return \substr($uriPath, 0, $lastSlashPos); |
| 268 |
} |
| 269 |
|
| 270 |
public function withCookieHeader(RequestInterface $request): RequestInterface |
| 271 |
{ |
| 272 |
$values = []; |
| 273 |
$uri = $request->getUri(); |
| 274 |
$scheme = $uri->getScheme(); |
| 275 |
$host = $uri->getHost(); |
| 276 |
$path = $uri->getPath() ?: '/'; |
| 277 |
|
| 278 |
foreach ($this->cookies as $cookie) { |
| 279 |
if ($cookie->getDomain() !== null |
| 280 |
&& $cookie->matchesPath($path) |
| 281 |
&& $cookie->matchesDomain($host) |
| 282 |
&& !$cookie->isExpired() |
| 283 |
&& (!$cookie->getSecure() || $scheme === 'https') |
| 284 |
) { |
| 285 |
$values[] = $cookie->getName().'=' |
| 286 |
.$cookie->getValue(); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
return $values |
| 291 |
? $request->withHeader('Cookie', \implode('; ', $values)) |
| 292 |
: $request; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
private function removeCookieIfEmpty(SetCookie $cookie): void |
| 300 |
{ |
| 301 |
$cookieValue = $cookie->getValue(); |
| 302 |
if (($cookieValue === null || $cookieValue === '') && $cookie->getDomain() !== null) { |
| 303 |
$this->clear( |
| 304 |
$cookie->getDomain(), |
| 305 |
$cookie->getPath(), |
| 306 |
$cookie->getName() |
| 307 |
); |
| 308 |
} |
| 309 |
} |
| 310 |
} |