CookieCollection.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Http\Client;
  15. use Cake\Http\Cookie\CookieCollection as BaseCollection;
  16. use Cake\Http\Cookie\CookieInterface;
  17. /**
  18. * Container class for cookies used in Http\Client.
  19. *
  20. * Provides cookie jar like features for storing cookies between
  21. * requests, as well as appending cookies to new requests.
  22. *
  23. * @deprecated 3.5.0 Use Cake\Http\Cookie\CookieCollection instead.
  24. */
  25. class CookieCollection extends BaseCollection
  26. {
  27. /**
  28. * {@inheritDoc}
  29. */
  30. public function __construct(array $cookies = [])
  31. {
  32. parent::__construct($cookies);
  33. deprecationWarning('Use Cake\Http\Cookie\CookieCollection instead.');
  34. }
  35. /**
  36. * Store the cookies from a response.
  37. *
  38. * Store the cookies that haven't expired. If a cookie has been expired
  39. * and is currently stored, it will be removed.
  40. *
  41. * @param Response $response The response to read cookies from
  42. * @param string $url The request URL used for default host/path values.
  43. * @return void
  44. */
  45. public function store(Response $response, $url)
  46. {
  47. $host = parse_url($url, PHP_URL_HOST);
  48. $path = parse_url($url, PHP_URL_PATH);
  49. $path = $path ?: '/';
  50. $header = $response->getHeader('Set-Cookie');
  51. $cookies = $this->parseSetCookieHeader($header);
  52. $cookies = $this->setRequestDefaults($cookies, $host, $path);
  53. foreach ($cookies as $cookie) {
  54. $this->cookies[$cookie->getId()] = $cookie;
  55. }
  56. $this->removeExpiredCookies($host, $path);
  57. }
  58. /**
  59. * Get stored cookies for a URL.
  60. *
  61. * Finds matching stored cookies and returns a simple array
  62. * of name => value
  63. *
  64. * @param string $url The URL to find cookies for.
  65. * @return array
  66. */
  67. public function get($url)
  68. {
  69. $path = parse_url($url, PHP_URL_PATH) ?: '/';
  70. $host = parse_url($url, PHP_URL_HOST);
  71. $scheme = parse_url($url, PHP_URL_SCHEME);
  72. return $this->findMatchingCookies($scheme, $host, $path);
  73. }
  74. /**
  75. * Get all the stored cookies as arrays.
  76. *
  77. * @return array
  78. */
  79. public function getAll()
  80. {
  81. $out = [];
  82. foreach ($this->cookies as $cookie) {
  83. $out[] = $this->convertCookieToArray($cookie);
  84. }
  85. return $out;
  86. }
  87. /**
  88. * Convert the cookie into an array of its properties.
  89. *
  90. * Primarily useful where backwards compatibility is needed.
  91. *
  92. * @param \Cake\Http\Cookie\CookieInterface $cookie Cookie object.
  93. * @return array
  94. */
  95. protected function convertCookieToArray(CookieInterface $cookie)
  96. {
  97. return [
  98. 'name' => $cookie->getName(),
  99. 'value' => $cookie->getValue(),
  100. 'path' => $cookie->getPath(),
  101. 'domain' => $cookie->getDomain(),
  102. 'secure' => $cookie->isSecure(),
  103. 'httponly' => $cookie->isHttpOnly(),
  104. 'expires' => $cookie->getExpiresTimestamp()
  105. ];
  106. }
  107. }
  108. // @deprecated Add backwards compat alias.
  109. class_alias('Cake\Http\Client\CookieCollection', 'Cake\Network\Http\CookieCollection');