| 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp\Cookie;
|
|---|
| 3 |
|
|---|
| 4 | use Psr\Http\Message\RequestInterface;
|
|---|
| 5 | use Psr\Http\Message\ResponseInterface;
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * Cookie jar that stores cookies an an array
|
|---|
| 9 | */
|
|---|
| 10 | class CookieJar implements CookieJarInterface
|
|---|
| 11 | {
|
|---|
| 12 | /** @var SetCookie[] Loaded cookie data */
|
|---|
| 13 | private $cookies = [];
|
|---|
| 14 |
|
|---|
| 15 | /** @var bool */
|
|---|
| 16 | private $strictMode;
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * @param bool $strictMode Set to true to throw exceptions when invalid
|
|---|
| 20 | * cookies are added to the cookie jar.
|
|---|
| 21 | * @param array $cookieArray Array of SetCookie objects or a hash of arrays
|
|---|
| 22 | * that can be used with the SetCookie constructor
|
|---|
| 23 | */
|
|---|
| 24 | public function __construct($strictMode = false, $cookieArray = [])
|
|---|
| 25 | {
|
|---|
| 26 | $this->strictMode = $strictMode;
|
|---|
| 27 |
|
|---|
| 28 | foreach ($cookieArray as $cookie) {
|
|---|
| 29 | if (!($cookie instanceof SetCookie)) {
|
|---|
| 30 | $cookie = new SetCookie($cookie);
|
|---|
| 31 | }
|
|---|
| 32 | $this->setCookie($cookie);
|
|---|
| 33 | }
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * Create a new Cookie jar from an associative array and domain.
|
|---|
| 38 | *
|
|---|
| 39 | * @param array $cookies Cookies to create the jar from
|
|---|
| 40 | * @param string $domain Domain to set the cookies to
|
|---|
| 41 | *
|
|---|
| 42 | * @return self
|
|---|
| 43 | */
|
|---|
| 44 | public static function fromArray(array $cookies, $domain)
|
|---|
| 45 | {
|
|---|
| 46 | $cookieJar = new self();
|
|---|
| 47 | foreach ($cookies as $name => $value) {
|
|---|
| 48 | $cookieJar->setCookie(new SetCookie([
|
|---|
| 49 | 'Domain' => $domain,
|
|---|
| 50 | 'Name' => $name,
|
|---|
| 51 | 'Value' => $value,
|
|---|
| 52 | 'Discard' => true
|
|---|
| 53 | ]));
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | return $cookieJar;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * Quote the cookie value if it is not already quoted and it contains
|
|---|
| 61 | * problematic characters.
|
|---|
| 62 | *
|
|---|
| 63 | * @param string $value Value that may or may not need to be quoted
|
|---|
| 64 | *
|
|---|
| 65 | * @return string
|
|---|
| 66 | */
|
|---|
| 67 | public static function getCookieValue($value)
|
|---|
| 68 | {
|
|---|
| 69 | if (substr($value, 0, 1) !== '"' &&
|
|---|
| 70 | substr($value, -1, 1) !== '"' &&
|
|---|
| 71 | strpbrk($value, ';,')
|
|---|
| 72 | ) {
|
|---|
| 73 | $value = '"' . $value . '"';
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | return $value;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | public function toArray()
|
|---|
| 80 | {
|
|---|
| 81 | return array_map(function (SetCookie $cookie) {
|
|---|
| 82 | return $cookie->toArray();
|
|---|
| 83 | }, $this->getIterator()->getArrayCopy());
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | public function clear($domain = null, $path = null, $name = null)
|
|---|
| 87 | {
|
|---|
| 88 | if (!$domain) {
|
|---|
| 89 | $this->cookies = [];
|
|---|
| 90 | return;
|
|---|
| 91 | } elseif (!$path) {
|
|---|
| 92 | $this->cookies = array_filter(
|
|---|
| 93 | $this->cookies,
|
|---|
| 94 | function (SetCookie $cookie) use ($path, $domain) {
|
|---|
| 95 | return !$cookie->matchesDomain($domain);
|
|---|
| 96 | }
|
|---|
| 97 | );
|
|---|
| 98 | } elseif (!$name) {
|
|---|
| 99 | $this->cookies = array_filter(
|
|---|
| 100 | $this->cookies,
|
|---|
| 101 | function (SetCookie $cookie) use ($path, $domain) {
|
|---|
| 102 | return !($cookie->matchesPath($path) &&
|
|---|
| 103 | $cookie->matchesDomain($domain));
|
|---|
| 104 | }
|
|---|
| 105 | );
|
|---|
| 106 | } else {
|
|---|
| 107 | $this->cookies = array_filter(
|
|---|
| 108 | $this->cookies,
|
|---|
| 109 | function (SetCookie $cookie) use ($path, $domain, $name) {
|
|---|
| 110 | return !($cookie->getName() == $name &&
|
|---|
| 111 | $cookie->matchesPath($path) &&
|
|---|
| 112 | $cookie->matchesDomain($domain));
|
|---|
| 113 | }
|
|---|
| 114 | );
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | public function clearSessionCookies()
|
|---|
| 119 | {
|
|---|
| 120 | $this->cookies = array_filter(
|
|---|
| 121 | $this->cookies,
|
|---|
| 122 | function (SetCookie $cookie) {
|
|---|
| 123 | return !$cookie->getDiscard() && $cookie->getExpires();
|
|---|
| 124 | }
|
|---|
| 125 | );
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | public function setCookie(SetCookie $cookie)
|
|---|
| 129 | {
|
|---|
| 130 | // Only allow cookies with set and valid domain, name, value
|
|---|
| 131 | $result = $cookie->validate();
|
|---|
| 132 | if ($result !== true) {
|
|---|
| 133 | if ($this->strictMode) {
|
|---|
| 134 | throw new \RuntimeException('Invalid cookie: ' . $result);
|
|---|
| 135 | } else {
|
|---|
| 136 | $this->removeCookieIfEmpty($cookie);
|
|---|
| 137 | return false;
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | // Resolve conflicts with previously set cookies
|
|---|
| 142 | foreach ($this->cookies as $i => $c) {
|
|---|
| 143 |
|
|---|
| 144 | // Two cookies are identical, when their path, and domain are
|
|---|
| 145 | // identical.
|
|---|
| 146 | if ($c->getPath() != $cookie->getPath() ||
|
|---|
| 147 | $c->getDomain() != $cookie->getDomain() ||
|
|---|
| 148 | $c->getName() != $cookie->getName()
|
|---|
| 149 | ) {
|
|---|
| 150 | continue;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | // The previously set cookie is a discard cookie and this one is
|
|---|
| 154 | // not so allow the new cookie to be set
|
|---|
| 155 | if (!$cookie->getDiscard() && $c->getDiscard()) {
|
|---|
| 156 | unset($this->cookies[$i]);
|
|---|
| 157 | continue;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | // If the new cookie's expiration is further into the future, then
|
|---|
| 161 | // replace the old cookie
|
|---|
| 162 | if ($cookie->getExpires() > $c->getExpires()) {
|
|---|
| 163 | unset($this->cookies[$i]);
|
|---|
| 164 | continue;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | // If the value has changed, we better change it
|
|---|
| 168 | if ($cookie->getValue() !== $c->getValue()) {
|
|---|
| 169 | unset($this->cookies[$i]);
|
|---|
| 170 | continue;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | // The cookie exists, so no need to continue
|
|---|
| 174 | return false;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | $this->cookies[] = $cookie;
|
|---|
| 178 |
|
|---|
| 179 | return true;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | public function count()
|
|---|
| 183 | {
|
|---|
| 184 | return count($this->cookies);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | public function getIterator()
|
|---|
| 188 | {
|
|---|
| 189 | return new \ArrayIterator(array_values($this->cookies));
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | public function extractCookies(
|
|---|
| 193 | RequestInterface $request,
|
|---|
| 194 | ResponseInterface $response
|
|---|
| 195 | ) {
|
|---|
| 196 | if ($cookieHeader = $response->getHeader('Set-Cookie')) {
|
|---|
| 197 | foreach ($cookieHeader as $cookie) {
|
|---|
| 198 | $sc = SetCookie::fromString($cookie);
|
|---|
| 199 | if (!$sc->getDomain()) {
|
|---|
| 200 | $sc->setDomain($request->getUri()->getHost());
|
|---|
| 201 | }
|
|---|
| 202 | $this->setCookie($sc);
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | public function withCookieHeader(RequestInterface $request)
|
|---|
| 208 | {
|
|---|
| 209 | $values = [];
|
|---|
| 210 | $uri = $request->getUri();
|
|---|
| 211 | $scheme = $uri->getScheme();
|
|---|
| 212 | $host = $uri->getHost();
|
|---|
| 213 | $path = $uri->getPath() ?: '/';
|
|---|
| 214 |
|
|---|
| 215 | foreach ($this->cookies as $cookie) {
|
|---|
| 216 | if ($cookie->matchesPath($path) &&
|
|---|
| 217 | $cookie->matchesDomain($host) &&
|
|---|
| 218 | !$cookie->isExpired() &&
|
|---|
| 219 | (!$cookie->getSecure() || $scheme == 'https')
|
|---|
| 220 | ) {
|
|---|
| 221 | $values[] = $cookie->getName() . '='
|
|---|
| 222 | . self::getCookieValue($cookie->getValue());
|
|---|
| 223 | }
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | return $values
|
|---|
| 227 | ? $request->withHeader('Cookie', implode('; ', $values))
|
|---|
| 228 | : $request;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | /**
|
|---|
| 232 | * If a cookie already exists and the server asks to set it again with a
|
|---|
| 233 | * null value, the cookie must be deleted.
|
|---|
| 234 | *
|
|---|
| 235 | * @param SetCookie $cookie
|
|---|
| 236 | */
|
|---|
| 237 | private function removeCookieIfEmpty(SetCookie $cookie)
|
|---|
| 238 | {
|
|---|
| 239 | $cookieValue = $cookie->getValue();
|
|---|
| 240 | if ($cookieValue === null || $cookieValue === '') {
|
|---|
| 241 | $this->clear(
|
|---|
| 242 | $cookie->getDomain(),
|
|---|
| 243 | $cookie->getPath(),
|
|---|
| 244 | $cookie->getName()
|
|---|
| 245 | );
|
|---|
| 246 | }
|
|---|
| 247 | }
|
|---|
| 248 | }
|
|---|