/vendor/symfony/browser-kit/Symfony/Component/BrowserKit/CookieJar.php

https://gitlab.com/xolotsoft/pumasruiz · PHP · 265 lines · 140 code · 30 blank · 95 comment · 25 complexity · 2722e49a5cf555ec0710e7c93dc4f487 MD5 · raw file

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