CookieJar.php 6.8 KB

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