CookieJar.php 7.3 KB

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