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