1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238:
<?php
namespace Guzzle\Plugin\Cookie\CookieJar;
use Guzzle\Plugin\Cookie\Cookie;
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Http\Message\Response;
use Guzzle\Parser\ParserRegistry;
use Guzzle\Plugin\Cookie\Exception\InvalidCookieException;
class ArrayCookieJar implements CookieJarInterface, \Serializable
{
protected $cookies = array();
protected $strictMode;
public function __construct($strictMode = false)
{
$this->strictMode = $strictMode;
}
public function setStrictMode($strictMode)
{
$this->strictMode = $strictMode;
}
public function remove($domain = null, $path = null, $name = null)
{
$cookies = $this->all($domain, $path, $name, false, false);
$this->cookies = array_filter($this->cookies, function (Cookie $cookie) use ($cookies) {
return !in_array($cookie, $cookies, true);
});
return $this;
}
public function removeTemporary()
{
$this->cookies = array_filter($this->cookies, function (Cookie $cookie) {
return !$cookie->getDiscard() && $cookie->getExpires();
});
return $this;
}
public function removeExpired()
{
$currentTime = time();
$this->cookies = array_filter($this->cookies, function (Cookie $cookie) use ($currentTime) {
return !$cookie->getExpires() || $currentTime < $cookie->getExpires();
});
return $this;
}
public function all($domain = null, $path = null, $name = null, $skipDiscardable = false, $skipExpired = true)
{
return array_values(array_filter($this->cookies, function (Cookie $cookie) use (
$domain,
$path,
$name,
$skipDiscardable,
$skipExpired
) {
return false === (($name && $cookie->getName() != $name) ||
($skipExpired && $cookie->isExpired()) ||
($skipDiscardable && ($cookie->getDiscard() || !$cookie->getExpires())) ||
($path && !$cookie->matchesPath($path)) ||
($domain && !$cookie->matchesDomain($domain)));
}));
}
public function add(Cookie $cookie)
{
$result = $cookie->validate();
if ($result !== true) {
if ($this->strictMode) {
throw new InvalidCookieException($result);
} else {
$this->removeCookieIfEmpty($cookie);
return false;
}
}
foreach ($this->cookies as $i => $c) {
if ($c->getPath() != $cookie->getPath() ||
$c->getDomain() != $cookie->getDomain() ||
$c->getPorts() != $cookie->getPorts() ||
$c->getName() != $cookie->getName()
) {
continue;
}
if (!$cookie->getDiscard() && $c->getDiscard()) {
unset($this->cookies[$i]);
continue;
}
if ($cookie->getExpires() > $c->getExpires()) {
unset($this->cookies[$i]);
continue;
}
if ($cookie->getValue() !== $c->getValue()) {
unset($this->cookies[$i]);
continue;
}
return false;
}
$this->cookies[] = $cookie;
return true;
}
public function serialize()
{
return json_encode(array_map(function (Cookie $cookie) {
return $cookie->toArray();
}, $this->all(null, null, null, true, true)));
}
public function unserialize($data)
{
$data = json_decode($data, true);
if (empty($data)) {
$this->cookies = array();
} else {
$this->cookies = array_map(function (array $cookie) {
return new Cookie($cookie);
}, $data);
}
}
public function count()
{
return count($this->cookies);
}
public function getIterator()
{
return new \ArrayIterator($this->cookies);
}
public function addCookiesFromResponse(Response $response, RequestInterface $request = null)
{
if ($cookieHeader = $response->getHeader('Set-Cookie')) {
$parser = ParserRegistry::getInstance()->getParser('cookie');
foreach ($cookieHeader as $cookie) {
if ($parsed = $request
? $parser->parseCookie($cookie, $request->getHost(), $request->getPath())
: $parser->parseCookie($cookie)
) {
foreach ($parsed['cookies'] as $key => $value) {
$row = $parsed;
$row['name'] = $key;
$row['value'] = $value;
unset($row['cookies']);
$this->add(new Cookie($row));
}
}
}
}
}
public function getMatchingCookies(RequestInterface $request)
{
$cookies = $this->all($request->getHost(), $request->getPath());
foreach ($cookies as $index => $cookie) {
if (!$cookie->matchesPort($request->getPort()) || ($cookie->getSecure() && $request->getScheme() != 'https')) {
unset($cookies[$index]);
}
};
return $cookies;
}
private function removeCookieIfEmpty(Cookie $cookie)
{
$cookieValue = $cookie->getValue();
if ($cookieValue === null || $cookieValue === '') {
$this->remove($cookie->getDomain(), $cookie->getPath(), $cookie->getName());
}
}
}