CookieJar.php 6.8 KB

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