CookieJar.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\BrowserKit;
  11. /**
  12. * CookieJar.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class CookieJar
  17. {
  18. protected $cookieJar = [];
  19. public function set(Cookie $cookie)
  20. {
  21. $this->cookieJar[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
  22. }
  23. /**
  24. * Gets a cookie by name.
  25. *
  26. * You should never use an empty domain, but if you do so,
  27. * this method returns the first cookie for the given name/path
  28. * (this behavior ensures a BC behavior with previous versions of
  29. * Symfony).
  30. *
  31. * @return Cookie|null A Cookie instance or null if the cookie does not exist
  32. */
  33. public function get(string $name, string $path = '/', string $domain = null)
  34. {
  35. $this->flushExpiredCookies();
  36. foreach ($this->cookieJar as $cookieDomain => $pathCookies) {
  37. if ($cookieDomain && $domain) {
  38. $cookieDomain = '.'.ltrim($cookieDomain, '.');
  39. if ($cookieDomain !== substr('.'.$domain, -\strlen($cookieDomain))) {
  40. continue;
  41. }
  42. }
  43. foreach ($pathCookies as $cookiePath => $namedCookies) {
  44. if (0 !== strpos($path, $cookiePath)) {
  45. continue;
  46. }
  47. if (isset($namedCookies[$name])) {
  48. return $namedCookies[$name];
  49. }
  50. }
  51. }
  52. return null;
  53. }
  54. /**
  55. * Removes a cookie by name.
  56. *
  57. * You should never use an empty domain, but if you do so,
  58. * all cookies for the given name/path expire (this behavior
  59. * ensures a BC behavior with previous versions of Symfony).
  60. */
  61. public function expire(string $name, ?string $path = '/', string $domain = null)
  62. {
  63. if (null === $path) {
  64. $path = '/';
  65. }
  66. if (empty($domain)) {
  67. // an empty domain means any domain
  68. // this should never happen but it allows for a better BC
  69. $domains = array_keys($this->cookieJar);
  70. } else {
  71. $domains = [$domain];
  72. }
  73. foreach ($domains as $domain) {
  74. unset($this->cookieJar[$domain][$path][$name]);
  75. if (empty($this->cookieJar[$domain][$path])) {
  76. unset($this->cookieJar[$domain][$path]);
  77. if (empty($this->cookieJar[$domain])) {
  78. unset($this->cookieJar[$domain]);
  79. }
  80. }
  81. }
  82. }
  83. /**
  84. * Removes all the cookies from the jar.
  85. */
  86. public function clear()
  87. {
  88. $this->cookieJar = [];
  89. }
  90. /**
  91. * Updates the cookie jar from a response Set-Cookie headers.
  92. *
  93. * @param string[] $setCookies Set-Cookie headers from an HTTP response
  94. */
  95. public function updateFromSetCookie(array $setCookies, string $uri = null)
  96. {
  97. $cookies = [];
  98. foreach ($setCookies as $cookie) {
  99. foreach (explode(',', $cookie) as $i => $part) {
  100. if (0 === $i || preg_match('/^(?P<token>\s*[0-9A-Za-z!#\$%\&\'\*\+\-\.^_`\|~]+)=/', $part)) {
  101. $cookies[] = ltrim($part);
  102. } else {
  103. $cookies[\count($cookies) - 1] .= ','.$part;
  104. }
  105. }
  106. }
  107. foreach ($cookies as $cookie) {
  108. try {
  109. $this->set(Cookie::fromString($cookie, $uri));
  110. } catch (\InvalidArgumentException $e) {
  111. // invalid cookies are just ignored
  112. }
  113. }
  114. }
  115. /**
  116. * Updates the cookie jar from a Response object.
  117. */
  118. public function updateFromResponse(Response $response, string $uri = null)
  119. {
  120. $this->updateFromSetCookie($response->getHeader('Set-Cookie', false), $uri);
  121. }
  122. /**
  123. * Returns not yet expired cookies.
  124. *
  125. * @return Cookie[] An array of cookies
  126. */
  127. public function all()
  128. {
  129. $this->flushExpiredCookies();
  130. $flattenedCookies = [];
  131. foreach ($this->cookieJar as $path) {
  132. foreach ($path as $cookies) {
  133. foreach ($cookies as $cookie) {
  134. $flattenedCookies[] = $cookie;
  135. }
  136. }
  137. }
  138. return $flattenedCookies;
  139. }
  140. /**
  141. * Returns not yet expired cookie values for the given URI.
  142. *
  143. * @return array An array of cookie values
  144. */
  145. public function allValues(string $uri, bool $returnsRawValue = false)
  146. {
  147. $this->flushExpiredCookies();
  148. $parts = array_replace(['path' => '/'], parse_url($uri));
  149. $cookies = [];
  150. foreach ($this->cookieJar as $domain => $pathCookies) {
  151. if ($domain) {
  152. $domain = '.'.ltrim($domain, '.');
  153. if ($domain != substr('.'.$parts['host'], -\strlen($domain))) {
  154. continue;
  155. }
  156. }
  157. foreach ($pathCookies as $path => $namedCookies) {
  158. if ($path != substr($parts['path'], 0, \strlen($path))) {
  159. continue;
  160. }
  161. foreach ($namedCookies as $cookie) {
  162. if ($cookie->isSecure() && 'https' != $parts['scheme']) {
  163. continue;
  164. }
  165. $cookies[$cookie->getName()] = $returnsRawValue ? $cookie->getRawValue() : $cookie->getValue();
  166. }
  167. }
  168. }
  169. return $cookies;
  170. }
  171. /**
  172. * Returns not yet expired raw cookie values for the given URI.
  173. *
  174. * @return array An array of cookie values
  175. */
  176. public function allRawValues(string $uri)
  177. {
  178. return $this->allValues($uri, true);
  179. }
  180. /**
  181. * Removes all expired cookies.
  182. */
  183. public function flushExpiredCookies()
  184. {
  185. foreach ($this->cookieJar as $domain => $pathCookies) {
  186. foreach ($pathCookies as $path => $namedCookies) {
  187. foreach ($namedCookies as $name => $cookie) {
  188. if ($cookie->isExpired()) {
  189. unset($this->cookieJar[$domain][$path][$name]);
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }