PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.2
28.6 28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 All 130 releases
wordpress-seo / vendor_prefixed / guzzlehttp / guzzle / src / Cookie / CookieJar.php

CookieJar.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.2, at vendor_prefixed/guzzlehttp/guzzle/src/Cookie/CookieJar.php

247 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YoastSEO_Vendor\GuzzleHttp\Cookie;
4
5 use YoastSEO_Vendor\Psr\Http\Message\RequestInterface;
6 use YoastSEO_Vendor\Psr\Http\Message\ResponseInterface;
7 /**
8 * Cookie jar that stores cookies as an array
9 */
10 class CookieJar implements \YoastSEO_Vendor\GuzzleHttp\Cookie\CookieJarInterface
11 {
12 /** @var SetCookie[] Loaded cookie data */
13 private $cookies = [];
14 /** @var bool */
15 private $strictMode;
16 /**
17 * @param bool $strictMode Set to true to throw exceptions when invalid
18 * cookies are added to the cookie jar.
19 * @param array $cookieArray Array of SetCookie objects or a hash of
20 * arrays that can be used with the SetCookie
21 * constructor
22 */
23 public function __construct($strictMode = \false, $cookieArray = [])
24 {
25 $this->strictMode = $strictMode;
26 foreach ($cookieArray as $cookie) {
27 if (!$cookie instanceof \YoastSEO_Vendor\GuzzleHttp\Cookie\SetCookie) {
28 $cookie = new \YoastSEO_Vendor\GuzzleHttp\Cookie\SetCookie($cookie);
29 }
30 $this->setCookie($cookie);
31 }
32 }
33 /**
34 * Create a new Cookie jar from an associative array and domain.
35 *
36 * @param array $cookies Cookies to create the jar from
37 * @param string $domain Domain to set the cookies to
38 *
39 * @return self
40 */
41 public static function fromArray(array $cookies, $domain)
42 {
43 $cookieJar = new self();
44 foreach ($cookies as $name => $value) {
45 $cookieJar->setCookie(new \YoastSEO_Vendor\GuzzleHttp\Cookie\SetCookie(['Domain' => $domain, 'Name' => $name, 'Value' => $value, 'Discard' => \true]));
46 }
47 return $cookieJar;
48 }
49 /**
50 * @deprecated
51 */
52 public static function getCookieValue($value)
53 {
54 return $value;
55 }
56 /**
57 * Evaluate if this cookie should be persisted to storage
58 * that survives between requests.
59 *
60 * @param SetCookie $cookie Being evaluated.
61 * @param bool $allowSessionCookies If we should persist session cookies
62 * @return bool
63 */
64 public static function shouldPersist(\YoastSEO_Vendor\GuzzleHttp\Cookie\SetCookie $cookie, $allowSessionCookies = \false)
65 {
66 if ($cookie->getExpires() || $allowSessionCookies) {
67 if (!$cookie->getDiscard()) {
68 return \true;
69 }
70 }
71 return \false;
72 }
73 /**
74 * Finds and returns the cookie based on the name
75 *
76 * @param string $name cookie name to search for
77 * @return SetCookie|null cookie that was found or null if not found
78 */
79 public function getCookieByName($name)
80 {
81 // don't allow a non string name
82 if ($name === null || !\is_scalar($name)) {
83 return null;
84 }
85 foreach ($this->cookies as $cookie) {
86 if ($cookie->getName() !== null && \strcasecmp($cookie->getName(), $name) === 0) {
87 return $cookie;
88 }
89 }
90 return null;
91 }
92 public function toArray()
93 {
94 return \array_map(function (\YoastSEO_Vendor\GuzzleHttp\Cookie\SetCookie $cookie) {
95 return $cookie->toArray();
96 }, $this->getIterator()->getArrayCopy());
97 }
98 public function clear($domain = null, $path = null, $name = null)
99 {
100 if (!$domain) {
101 $this->cookies = [];
102 return;
103 } elseif (!$path) {
104 $this->cookies = \array_filter($this->cookies, function (\YoastSEO_Vendor\GuzzleHttp\Cookie\SetCookie $cookie) use($domain) {
105 return !$cookie->matchesDomain($domain);
106 });
107 } elseif (!$name) {
108 $this->cookies = \array_filter($this->cookies, function (\YoastSEO_Vendor\GuzzleHttp\Cookie\SetCookie $cookie) use($path, $domain) {
109 return !($cookie->matchesPath($path) && $cookie->matchesDomain($domain));
110 });
111 } else {
112 $this->cookies = \array_filter($this->cookies, function (\YoastSEO_Vendor\GuzzleHttp\Cookie\SetCookie $cookie) use($path, $domain, $name) {
113 return !($cookie->getName() == $name && $cookie->matchesPath($path) && $cookie->matchesDomain($domain));
114 });
115 }
116 }
117 public function clearSessionCookies()
118 {
119 $this->cookies = \array_filter($this->cookies, function (\YoastSEO_Vendor\GuzzleHttp\Cookie\SetCookie $cookie) {
120 return !$cookie->getDiscard() && $cookie->getExpires();
121 });
122 }
123 public function setCookie(\YoastSEO_Vendor\GuzzleHttp\Cookie\SetCookie $cookie)
124 {
125 // If the name string is empty (but not 0), ignore the set-cookie
126 // string entirely.
127 $name = $cookie->getName();
128 if (!$name && $name !== '0') {
129 return \false;
130 }
131 // Only allow cookies with set and valid domain, name, value
132 $result = $cookie->validate();
133 if ($result !== \true) {
134 if ($this->strictMode) {
135 throw new \RuntimeException('Invalid cookie: ' . $result);
136 } else {
137 $this->removeCookieIfEmpty($cookie);
138 return \false;
139 }
140 }
141 // Resolve conflicts with previously set cookies
142 foreach ($this->cookies as $i => $c) {
143 // Two cookies are identical, when their path, and domain are
144 // identical.
145 if ($c->getPath() != $cookie->getPath() || $c->getDomain() != $cookie->getDomain() || $c->getName() != $cookie->getName()) {
146 continue;
147 }
148 // The previously set cookie is a discard cookie and this one is
149 // not so allow the new cookie to be set
150 if (!$cookie->getDiscard() && $c->getDiscard()) {
151 unset($this->cookies[$i]);
152 continue;
153 }
154 // If the new cookie's expiration is further into the future, then
155 // replace the old cookie
156 if ($cookie->getExpires() > $c->getExpires()) {
157 unset($this->cookies[$i]);
158 continue;
159 }
160 // If the value has changed, we better change it
161 if ($cookie->getValue() !== $c->getValue()) {
162 unset($this->cookies[$i]);
163 continue;
164 }
165 // The cookie exists, so no need to continue
166 return \false;
167 }
168 $this->cookies[] = $cookie;
169 return \true;
170 }
171 public function count()
172 {
173 return \count($this->cookies);
174 }
175 public function getIterator()
176 {
177 return new \ArrayIterator(\array_values($this->cookies));
178 }
179 public function extractCookies(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, \YoastSEO_Vendor\Psr\Http\Message\ResponseInterface $response)
180 {
181 if ($cookieHeader = $response->getHeader('Set-Cookie')) {
182 foreach ($cookieHeader as $cookie) {
183 $sc = \YoastSEO_Vendor\GuzzleHttp\Cookie\SetCookie::fromString($cookie);
184 if (!$sc->getDomain()) {
185 $sc->setDomain($request->getUri()->getHost());
186 }
187 if (0 !== \strpos($sc->getPath(), '/')) {
188 $sc->setPath($this->getCookiePathFromRequest($request));
189 }
190 $this->setCookie($sc);
191 }
192 }
193 }
194 /**
195 * Computes cookie path following RFC 6265 section 5.1.4
196 *
197 * @link https://tools.ietf.org/html/rfc6265#section-5.1.4
198 *
199 * @param RequestInterface $request
200 * @return string
201 */
202 private function getCookiePathFromRequest(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request)
203 {
204 $uriPath = $request->getUri()->getPath();
205 if ('' === $uriPath) {
206 return '/';
207 }
208 if (0 !== \strpos($uriPath, '/')) {
209 return '/';
210 }
211 if ('/' === $uriPath) {
212 return '/';
213 }
214 if (0 === ($lastSlashPos = \strrpos($uriPath, '/'))) {
215 return '/';
216 }
217 return \substr($uriPath, 0, $lastSlashPos);
218 }
219 public function withCookieHeader(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request)
220 {
221 $values = [];
222 $uri = $request->getUri();
223 $scheme = $uri->getScheme();
224 $host = $uri->getHost();
225 $path = $uri->getPath() ?: '/';
226 foreach ($this->cookies as $cookie) {
227 if ($cookie->matchesPath($path) && $cookie->matchesDomain($host) && !$cookie->isExpired() && (!$cookie->getSecure() || $scheme === 'https')) {
228 $values[] = $cookie->getName() . '=' . $cookie->getValue();
229 }
230 }
231 return $values ? $request->withHeader('Cookie', \implode('; ', $values)) : $request;
232 }
233 /**
234 * If a cookie already exists and the server asks to set it again with a
235 * null value, the cookie must be deleted.
236 *
237 * @param SetCookie $cookie
238 */
239 private function removeCookieIfEmpty(\YoastSEO_Vendor\GuzzleHttp\Cookie\SetCookie $cookie)
240 {
241 $cookieValue = $cookie->getValue();
242 if ($cookieValue === null || $cookieValue === '') {
243 $this->clear($cookie->getDomain(), $cookie->getPath(), $cookie->getName());
244 }
245 }
246 }
247