123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- namespace Symfony\Component\BrowserKit;
- class CookieJar
- {
- protected $cookieJar = [];
- public function set(Cookie $cookie)
- {
- $this->cookieJar[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
- }
-
- 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];
- }
- }
- }
- }
-
- public function expire($name, $path = '/', $domain = null)
- {
- if (null === $path) {
- $path = '/';
- }
- if (empty($domain)) {
-
-
- $domains = array_keys($this->cookieJar);
- } else {
- $domains = [$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]);
- }
- }
- }
- }
-
- public function clear()
- {
- $this->cookieJar = [];
- }
-
- public function updateFromSetCookie(array $setCookies, $uri = null)
- {
- $cookies = [];
- foreach ($setCookies as $cookie) {
- foreach (explode(',', $cookie) as $i => $part) {
- if (0 === $i || preg_match('/^(?P<token>\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) {
-
- }
- }
- }
-
- public function updateFromResponse(Response $response, $uri = null)
- {
- $this->updateFromSetCookie($response->getHeader('Set-Cookie', false), $uri);
- }
-
- public function all()
- {
- $this->flushExpiredCookies();
- $flattenedCookies = [];
- foreach ($this->cookieJar as $path) {
- foreach ($path as $cookies) {
- foreach ($cookies as $cookie) {
- $flattenedCookies[] = $cookie;
- }
- }
- }
- return $flattenedCookies;
- }
-
- public function allValues($uri, $returnsRawValue = false)
- {
- $this->flushExpiredCookies();
- $parts = array_replace(['path' => '/'], parse_url($uri));
- $cookies = [];
- 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;
- }
-
- public function allRawValues($uri)
- {
- return $this->allValues($uri, true);
- }
-
- 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]);
- }
- }
- }
- }
- }
- }
|