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:
16: use Cake\Http\Cookie\CookieCollection as BaseCollection;
17: use Cake\Http\Cookie\CookieInterface;
18:
19: /**
20: * Container class for cookies used in Http\Client.
21: *
22: * Provides cookie jar like features for storing cookies between
23: * requests, as well as appending cookies to new requests.
24: *
25: * @deprecated 3.5.0 Use Cake\Http\Cookie\CookieCollection instead.
26: */
27: class CookieCollection extends BaseCollection
28: {
29: /**
30: * {@inheritDoc}
31: */
32: public function __construct(array $cookies = [])
33: {
34: parent::__construct($cookies);
35:
36: deprecationWarning('Use Cake\Http\Cookie\CookieCollection instead.');
37: }
38:
39: /**
40: * Store the cookies from a response.
41: *
42: * Store the cookies that haven't expired. If a cookie has been expired
43: * and is currently stored, it will be removed.
44: *
45: * @param Response $response The response to read cookies from
46: * @param string $url The request URL used for default host/path values.
47: * @return void
48: */
49: public function store(Response $response, $url)
50: {
51: $host = parse_url($url, PHP_URL_HOST);
52: $path = parse_url($url, PHP_URL_PATH);
53: $path = $path ?: '/';
54:
55: $header = $response->getHeader('Set-Cookie');
56: $cookies = $this->parseSetCookieHeader($header);
57: $cookies = $this->setRequestDefaults($cookies, $host, $path);
58: foreach ($cookies as $cookie) {
59: $this->cookies[$cookie->getId()] = $cookie;
60: }
61: $this->removeExpiredCookies($host, $path);
62: }
63:
64: /**
65: * Get stored cookies for a URL.
66: *
67: * Finds matching stored cookies and returns a simple array
68: * of name => value
69: *
70: * @param string $url The URL to find cookies for.
71: * @return array
72: */
73: public function get($url)
74: {
75: $path = parse_url($url, PHP_URL_PATH) ?: '/';
76: $host = parse_url($url, PHP_URL_HOST);
77: $scheme = parse_url($url, PHP_URL_SCHEME);
78:
79: return $this->findMatchingCookies($scheme, $host, $path);
80: }
81:
82: /**
83: * Get all the stored cookies as arrays.
84: *
85: * @return array
86: */
87: public function getAll()
88: {
89: $out = [];
90: foreach ($this->cookies as $cookie) {
91: $out[] = $this->convertCookieToArray($cookie);
92: }
93:
94: return $out;
95: }
96:
97: /**
98: * Convert the cookie into an array of its properties.
99: *
100: * Primarily useful where backwards compatibility is needed.
101: *
102: * @param \Cake\Http\Cookie\CookieInterface $cookie Cookie object.
103: * @return array
104: */
105: protected function convertCookieToArray(CookieInterface $cookie)
106: {
107: return [
108: 'name' => $cookie->getName(),
109: 'value' => $cookie->getValue(),
110: 'path' => $cookie->getPath(),
111: 'domain' => $cookie->getDomain(),
112: 'secure' => $cookie->isSecure(),
113: 'httponly' => $cookie->isHttpOnly(),
114: 'expires' => $cookie->getExpiresTimestamp()
115: ];
116: }
117: }
118:
119: // @deprecated 3.4.0 Add backwards compat alias.
120: class_alias('Cake\Http\Client\CookieCollection', 'Cake\Network\Http\CookieCollection');
121: