CookieJar.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. * @param string $name The cookie name
  32. * @param string $path The cookie path
  33. * @param string $domain The cookie domain
  34. *
  35. * @return Cookie|null A Cookie instance or null if the cookie does not exist
  36. */
  37. public function get($name, $path = '/', $domain = null)
  38. {
  39. $this->flushExpiredCookies();
  40. foreach ($this->cookieJar as $cookieDomain => $pathCookies) {
  41. if ($cookieDomain && $domain) {
  42. $cookieDomain = '.'.ltrim($cookieDomain, '.');
  43. if ($cookieDomain !== substr('.'.$domain, -\strlen($cookieDomain))) {
  44. continue;
  45. }
  46. }
  47. foreach ($pathCookies as $cookiePath => $namedCookies) {
  48. if (0 !== strpos($path, $cookiePath)) {
  49. continue;
  50. }
  51. if (isset($namedCookies[$name])) {
  52. return $namedCookies[$name];
  53. }
  54. }
  55. }
  56. return null;
  57. }
  58. /**
  59. * Removes a cookie by name.
  60. *
  61. * You should never use an empty domain, but if you do so,
  62. * all cookies for the given name/path expire (this behavior
  63. * ensures a BC behavior with previous versions of Symfony).
  64. *
  65. * @param string $name The cookie name
  66. * @param string $path The cookie path
  67. * @param string $domain The cookie domain
  68. */
  69. public function expire($name, $path = '/', $domain = null)
  70. {
  71. if (null === $path) {
  72. $path = '/';
  73. }
  74. if (empty($domain)) {
  75. // an empty domain means any domain
  76. // this should never happen but it allows for a better BC
  77. $domains = array_keys($this->cookieJar);
  78. } else {
  79. $domains = [$domain];
  80. }
  81. foreach ($domains as $domain) {
  82. unset($this->cookieJar[$domain][$path][$name]);
  83. if (empty($this->cookieJar[$domain][$path])) {
  84. unset($this->cookieJar[$domain][$path]);
  85. if (empty($this->cookieJar[$domain])) {
  86. unset($this->cookieJar[$domain]);
  87. }
  88. }
  89. }
  90. }
  91. /**
  92. * Removes all the cookies from the jar.
  93. */
  94. public function clear()
  95. {
  96. $this->cookieJar = [];
  97. }
  98. /**
  99. * Updates the cookie jar from a response Set-Cookie headers.
  100. *
  101. * @param string[] $setCookies Set-Cookie headers from an HTTP response
  102. * @param string $uri The base URL
  103. */
  104. public function updateFromSetCookie(array $setCookies, $uri = null)
  105. {
  106. $cookies = [];
  107. foreach ($setCookies as $cookie) {
  108. foreach (explode(',', $cookie) as $i => $part) {
  109. if (0 === $i || preg_match('/^(?P<token>\s*[0-9A-Za-z!#\$%\&\'\*\+\-\.^_`\|~]+)=/', $part)) {
  110. $cookies[] = ltrim($part);
  111. } else {
  112. $cookies[\count($cookies) - 1] .= ','.$part;
  113. }
  114. }
  115. }
  116. foreach ($cookies as $cookie) {
  117. try {
  118. $this->set(Cookie::fromString($cookie, $uri));
  119. } catch (\InvalidArgumentException $e) {
  120. // invalid cookies are just ignored
  121. }
  122. }
  123. }
  124. /**
  125. * Updates the cookie jar from a Response object.
  126. *
  127. * @param Response $response A Response object
  128. * @param string $uri The base URL
  129. */
  130. public function updateFromResponse(Response $response, $uri = null)
  131. {
  132. $this->updateFromSetCookie($response->getHeader('Set-Cookie', false), $uri);
  133. }
  134. /**
  135. * Returns not yet expired cookies.
  136. *
  137. * @return Cookie[] An array of cookies
  138. */
  139. public function all()
  140. {
  141. $this->flushExpiredCookies();
  142. $flattenedCookies = [];
  143. foreach ($this->cookieJar as $path) {
  144. foreach ($path as $cookies) {
  145. foreach ($cookies as $cookie) {
  146. $flattenedCookies[] = $cookie;
  147. }
  148. }
  149. }
  150. return $flattenedCookies;
  151. }
  152. /**
  153. * Returns not yet expired cookie values for the given URI.
  154. *
  155. * @param string $uri A URI
  156. * @param bool $returnsRawValue Returns raw value or urldecoded value
  157. *
  158. * @return array An array of cookie values
  159. */
  160. public function allValues($uri, $returnsRawValue = false)
  161. {
  162. $this->flushExpiredCookies();
  163. $parts = array_replace(['path' => '/'], parse_url($uri));
  164. $cookies = [];
  165. foreach ($this->cookieJar as $domain => $pathCookies) {
  166. if ($domain) {
  167. $domain = '.'.ltrim($domain, '.');
  168. if ($domain != substr('.'.$parts['host'], -\strlen($domain))) {
  169. continue;
  170. }
  171. }
  172. foreach ($pathCookies as $path => $namedCookies) {
  173. if ($path != substr($parts['path'], 0, \strlen($path))) {
  174. continue;
  175. }
  176. foreach ($namedCookies as $cookie) {
  177. if ($cookie->isSecure() && 'https' != $parts['scheme']) {
  178. continue;
  179. }
  180. $cookies[$cookie->getName()] = $returnsRawValue ? $cookie->getRawValue() : $cookie->getValue();
  181. }
  182. }
  183. }
  184. return $cookies;
  185. }
  186. /**
  187. * Returns not yet expired raw cookie values for the given URI.
  188. *
  189. * @param string $uri A URI
  190. *
  191. * @return array An array of cookie values
  192. */
  193. public function allRawValues($uri)
  194. {
  195. return $this->allValues($uri, true);
  196. }
  197. /**
  198. * Removes all expired cookies.
  199. */
  200. public function flushExpiredCookies()
  201. {
  202. foreach ($this->cookieJar as $domain => $pathCookies) {
  203. foreach ($pathCookies as $path => $namedCookies) {
  204. foreach ($namedCookies as $name => $cookie) {
  205. if ($cookie->isExpired()) {
  206. unset($this->cookieJar[$domain][$path][$name]);
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }