* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\BrowserKit; /** * CookieJar. * * @author Fabien Potencier */ class CookieJar { protected $cookieJar = array(); public function set(Cookie $cookie) { $this->cookieJar[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie; } /** * Gets a cookie by name. * * You should never use an empty domain, but if you do so, * this method returns the first cookie for the given name/path * (this behavior ensures a BC behavior with previous versions of * Symfony). * * @param string $name The cookie name * @param string $path The cookie path * @param string $domain The cookie domain * * @return Cookie|null A Cookie instance or null if the cookie does not exist */ public function get($name, $path = '/', $domain = null) { $this->flushExpiredCookies(); foreach ($this->cookieJar as $cookieDomain => $pathCookies) { if ($cookieDomain && $domain) { $cookieDomain = '.'.ltrim($cookieDomain, '.'); if ($cookieDomain !== substr('.'.$domain, -\strlen($cookieDomain))) { continue; } } foreach ($pathCookies as $cookiePath => $namedCookies) { if (0 !== strpos($path, $cookiePath)) { continue; } if (isset($namedCookies[$name])) { return $namedCookies[$name]; } } } } /** * Removes a cookie by name. * * You should never use an empty domain, but if you do so, * all cookies for the given name/path expire (this behavior * ensures a BC behavior with previous versions of Symfony). * * @param string $name The cookie name * @param string $path The cookie path * @param string $domain The cookie domain */ public function expire($name, $path = '/', $domain = null) { if (null === $path) { $path = '/'; } if (empty($domain)) { // an empty domain means any domain // this should never happen but it allows for a better BC $domains = array_keys($this->cookieJar); } else { $domains = array($domain); } foreach ($domains as $domain) { unset($this->cookieJar[$domain][$path][$name]); if (empty($this->cookieJar[$domain][$path])) { unset($this->cookieJar[$domain][$path]); if (empty($this->cookieJar[$domain])) { unset($this->cookieJar[$domain]); } } } } /** * Removes all the cookies from the jar. */ public function clear() { $this->cookieJar = array(); } /** * Updates the cookie jar from a response Set-Cookie headers. * * @param array $setCookies Set-Cookie headers from an HTTP response * @param string $uri The base URL */ public function updateFromSetCookie(array $setCookies, $uri = null) { $cookies = array(); foreach ($setCookies as $cookie) { foreach (explode(',', $cookie) as $i => $part) { if (0 === $i || preg_match('/^(?P\s*[0-9A-Za-z!#\$%\&\'\*\+\-\.^_`\|~]+)=/', $part)) { $cookies[] = ltrim($part); } else { $cookies[count($cookies) - 1] .= ','.$part; } } } foreach ($cookies as $cookie) { try { $this->set(Cookie::fromString($cookie, $uri)); } catch (\InvalidArgumentException $e) { // invalid cookies are just ignored } } } /** * Updates the cookie jar from a Response object. * * @param Response $response A Response object * @param string $uri The base URL */ public function updateFromResponse(Response $response, $uri = null) { $this->updateFromSetCookie($response->getHeader('Set-Cookie', false), $uri); } /** * Returns not yet expired cookies. * * @return Cookie[] An array of cookies */ public function all() { $this->flushExpiredCookies(); $flattenedCookies = array(); foreach ($this->cookieJar as $path) { foreach ($path as $cookies) { foreach ($cookies as $cookie) { $flattenedCookies[] = $cookie; } } } return $flattenedCookies; } /** * Returns not yet expired cookie values for the given URI. * * @param string $uri A URI * @param bool $returnsRawValue Returns raw value or urldecoded value * * @return array An array of cookie values */ public function allValues($uri, $returnsRawValue = false) { $this->flushExpiredCookies(); $parts = array_replace(array('path' => '/'), parse_url($uri)); $cookies = array(); foreach ($this->cookieJar as $domain => $pathCookies) { if ($domain) { $domain = '.'.ltrim($domain, '.'); if ($domain != substr('.'.$parts['host'], -strlen($domain))) { continue; } } foreach ($pathCookies as $path => $namedCookies) { if ($path != substr($parts['path'], 0, strlen($path))) { continue; } foreach ($namedCookies as $cookie) { if ($cookie->isSecure() && 'https' != $parts['scheme']) { continue; } $cookies[$cookie->getName()] = $returnsRawValue ? $cookie->getRawValue() : $cookie->getValue(); } } } return $cookies; } /** * Returns not yet expired raw cookie values for the given URI. * * @param string $uri A URI * * @return array An array of cookie values */ public function allRawValues($uri) { return $this->allValues($uri, true); } /** * Removes all expired cookies. */ public function flushExpiredCookies() { foreach ($this->cookieJar as $domain => $pathCookies) { foreach ($pathCookies as $path => $namedCookies) { foreach ($namedCookies as $name => $cookie) { if ($cookie->isExpired()) { unset($this->cookieJar[$domain][$path][$name]); } } } } } } __halt_compiler();----SIGNATURE:----FtvN98x/W/5u8XQzMWMiV6L+D8dM/G1lZWTF760KBIpwcOkXzr0iUg/zES6ymNKTPa830ta+1o4qNlFbgQaxn1QfMyDHT5CDOmCowyEnHDRZe9slYb+sTellVn3+OneKDqOZe3buLJfU/T9liuxnqZSsVhMy069QD2UKQth8sVbYKs2AKmtwD+5iuFoYpektJGGSgljCzrVYIfB10fq1LXlZnXmjXpAOhWS9zqtEN4xmLph5n3dxvCuvRfAoAGysQUvJCebDTckTHyLDaLeS2ETUpUy3aTYeS5FnwrRd39W5VffM0QUqVOiLOt5zwazdnwwijlPSv/SknQmKFYS9F34u3Z9hdJq0VypZmr4wQC4LZIE163R/y5SRUZUj1VAVQP9kKeT5BwUCh/YHpnyK4iTElwHtJUJN5RTxVSo8EWqujeQJBXabKfKX5H3/PcgedaBX8Qn9DG0WBXBdSImohMZjy6yui/QtfffuySaFyozzE10eNDMvXwktPCNbwm462z13dq2G0lU/SgvgEokdteJ9gzkn6wwJZbtLdaO9uqfF7c9TzA3J/kks04Ia45YFztj70n13UUYWzsXfbEWiKVNivwAzCH/uQmiPXT2tPUqUTkfN+7a/pveBgrpED5YoYl1AO6Qjv5ZEU9LcViDRKlJ6eO+/Xz2l50nQBfump3c=----ATTACHMENT:----NDIzNzIwODQ3NjUxMDYxIDY4OTA1ODE3NzYzMjc0OCAyMzEyMzk3MDgxMzY2MzA3