getDomain(); $path = $cookie->getPath(); if (! isset($this->cookies[$domain])) { $this->cookies[$domain] = []; } if (! isset($this->cookies[$domain][$path])) { $this->cookies[$domain][$path] = []; } $this->cookies[$domain][$path][$cookie->getName()] = $cookie; $this->rawCookies[] = $cookie; } else { throw new Exception\InvalidArgumentException('Supplient argument is not a valid cookie string or object'); } } /** * Parse an HTTP response, adding all the cookies set in that response * * @param Uri\Uri|string $refUri Requested URI */ public function addCookiesFromResponse(Response $response, $refUri) { $cookieHdrs = $response->getHeaders()->get('Set-Cookie'); if (is_array($cookieHdrs) || $cookieHdrs instanceof ArrayIterator) { foreach ($cookieHdrs as $cookie) { $this->addCookie($cookie, $refUri); } } elseif (is_string($cookieHdrs)) { $this->addCookie($cookieHdrs, $refUri); } } /** * Get all cookies in the cookie jar as an array * * @param int $retAs Whether to return cookies as objects of \Laminas\Http\Header\SetCookie or as strings * @return array|string */ public function getAllCookies($retAs = self::COOKIE_OBJECT) { return $this->_flattenCookiesArray($this->cookies, $retAs); } /** * Return an array of all cookies matching a specific request according to the request URI, * whether session cookies should be sent or not, and the time to consider as "now" when * checking cookie expiry time. * * @param string|Uri\Uri $uri URI to check against (secure, domain, path) * @param bool $matchSessionCookies Whether to send session cookies * @param int $retAs Whether to return cookies as objects of \Laminas\Http\Header\Cookie or as strings * @param int $now Override the current time when checking for expiry time * @throws Exception\InvalidArgumentException If invalid URI specified. * @return array|string */ public function getMatchingCookies( $uri, $matchSessionCookies = true, $retAs = self::COOKIE_OBJECT, $now = null ) { if (is_string($uri)) { $uri = Uri\UriFactory::factory($uri, 'http'); } elseif (! $uri instanceof Uri\Uri) { throw new Exception\InvalidArgumentException('Invalid URI string or object passed'); } $host = $uri->getHost(); if (empty($host)) { throw new Exception\InvalidArgumentException('Invalid URI specified; does not contain a host'); } // First, reduce the array of cookies to only those matching domain and path $cookies = $this->_matchDomain($host); $cookies = $this->_matchPath($cookies, $uri->getPath()); $cookies = $this->_flattenCookiesArray($cookies, self::COOKIE_OBJECT); // Next, run Cookie->match on all cookies to check secure, time and session matching $ret = []; foreach ($cookies as $cookie) { if ($cookie->match($uri, $matchSessionCookies, $now)) { $ret[] = $cookie; } } // Now, use self::_flattenCookiesArray again - only to convert to the return format ;) $ret = $this->_flattenCookiesArray($ret, $retAs); return $ret; } /** * Get a specific cookie according to a URI and name * * @param Uri\Uri|string $uri The uri (domain and path) to match * @param string $cookieName The cookie's name * @param int $retAs Whether to return cookies as objects of \Laminas\Http\Header\SetCookie or as strings * @throws Exception\InvalidArgumentException If invalid URI specified or invalid $retAs value. * @return SetCookie|string */ public function getCookie($uri, $cookieName, $retAs = self::COOKIE_OBJECT) { if (is_string($uri)) { $uri = Uri\UriFactory::factory($uri, 'http'); } elseif (! $uri instanceof Uri\Uri) { throw new Exception\InvalidArgumentException('Invalid URI specified'); } $host = $uri->getHost(); if (empty($host)) { throw new Exception\InvalidArgumentException('Invalid URI specified; host missing'); } // Get correct cookie path $path = $uri->getPath() ?? ''; $lastSlashPos = strrpos($path, '/') ?: 0; $path = substr($path, 0, $lastSlashPos); if (! $path) { $path = '/'; } if (isset($this->cookies[$uri->getHost()][$path][$cookieName])) { $cookie = $this->cookies[$uri->getHost()][$path][$cookieName]; switch ($retAs) { case self::COOKIE_OBJECT: return $cookie; case self::COOKIE_STRING_ARRAY: case self::COOKIE_STRING_CONCAT: return $cookie->__toString(); default: throw new Exception\InvalidArgumentException(sprintf( 'Invalid value passed for $retAs: %s', $retAs )); } } return false; } /** * Helper function to recursively flatten an array. Should be used when exporting the * cookies array (or parts of it) * * @param SetCookie|array $ptr * @param int $retAs What value to return * @return array|string */ // @codingStandardsIgnoreStart protected function _flattenCookiesArray($ptr, $retAs = self::COOKIE_OBJECT) { // @codingStandardsIgnoreEnd if (is_array($ptr)) { $ret = $retAs === self::COOKIE_STRING_CONCAT ? '' : []; foreach ($ptr as $item) { if ($retAs === self::COOKIE_STRING_CONCAT) { $ret .= $this->_flattenCookiesArray($item, $retAs); } else { $ret = array_merge($ret, $this->_flattenCookiesArray($item, $retAs)); } } return $ret; } elseif ($ptr instanceof SetCookie) { switch ($retAs) { case self::COOKIE_STRING_ARRAY: return [$ptr->__toString()]; case self::COOKIE_STRING_CONCAT: return $ptr->__toString(); case self::COOKIE_OBJECT: default: return [$ptr]; } } } /** * Return a subset of the cookies array matching a specific domain * * @param string $domain * @return array */ // @codingStandardsIgnoreStart protected function _matchDomain($domain) { // @codingStandardsIgnoreEnd $ret = []; foreach (array_keys($this->cookies) as $cdom) { if (SetCookie::matchCookieDomain($cdom, $domain)) { $ret[$cdom] = $this->cookies[$cdom]; } } return $ret; } /** * Return a subset of a domain-matching cookies that also match a specified path * * @param array $domains * @param string $path * @return array */ // @codingStandardsIgnoreStart protected function _matchPath($domains, $path) { // @codingStandardsIgnoreEnd $ret = []; foreach ($domains as $dom => $pathsArray) { foreach (array_keys($pathsArray) as $cpath) { if (SetCookie::matchCookiePath($cpath, $path)) { if (! isset($ret[$dom])) { $ret[$dom] = []; } $ret[$dom][$cpath] = $pathsArray[$cpath]; } } } return $ret; } /** * Create a new Cookies object and automatically load into it all the * cookies set in a Response object. If $uri is set, it will be * considered as the requested URI for setting default domain and path * of the cookie. * * @param Response $response HTTP Response object * @param Uri\Uri|string $refUri The requested URI * @return static * @todo Add the $uri functionality. */ public static function fromResponse(Response $response, $refUri) { $jar = new static(); $jar->addCookiesFromResponse($response, $refUri); return $jar; } /** * Tells if the array of cookies is empty * * @return bool */ public function isEmpty() { return count($this) === 0; } /** * Empties the cookieJar of any cookie * * @return $this */ public function reset() { $this->cookies = $this->rawCookies = []; return $this; } }