PluginProbe
Media Cloud Sync / 1.2.5
Media Cloud Sync v1.2.5
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Cookie / CookieJar.php

CookieJar.php in Media Cloud Sync 1.2.5, at includes/sdk/s3/GuzzleHttp/Cookie/CookieJar.php

252 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Cookie;
4
5 use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface;
6 use Dudlewebs\WPMCS\s3\Psr\Http\Message\ResponseInterface;
7 /**
8 * Cookie jar that stores cookies as an array
9 */
10 class CookieJar implements 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 SetCookie) {
28 $cookie = new 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 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(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 (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 (SetCookie $cookie) use($domain) {
105 return !$cookie->matchesDomain($domain);
106 });
107 } elseif (!$name) {
108 $this->cookies = \array_filter($this->cookies, function (SetCookie $cookie) use($path, $domain) {
109 return !($cookie->matchesPath($path) && $cookie->matchesDomain($domain));
110 });
111 } else {
112 $this->cookies = \array_filter($this->cookies, function (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 (SetCookie $cookie) {
120 return !$cookie->getDiscard() && $cookie->getExpires();
121 });
122 }
123 public function setCookie(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(RequestInterface $request, ResponseInterface $response)
180 {
181 if ($cookieHeader = $response->getHeader('Set-Cookie')) {
182 foreach ($cookieHeader as $cookie) {
183 $sc = 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 if (!$sc->matchesDomain($request->getUri()->getHost())) {
191 continue;
192 }
193 // Note: At this point `$sc->getDomain()` being a public suffix should
194 // be rejected, but we don't want to pull in the full PSL dependency.
195 $this->setCookie($sc);
196 }
197 }
198 }
199 /**
200 * Computes cookie path following RFC 6265 section 5.1.4
201 *
202 * @link https://tools.ietf.org/html/rfc6265#section-5.1.4
203 *
204 * @param RequestInterface $request
205 * @return string
206 */
207 private function getCookiePathFromRequest(RequestInterface $request)
208 {
209 $uriPath = $request->getUri()->getPath();
210 if ('' === $uriPath) {
211 return '/';
212 }
213 if (0 !== \strpos($uriPath, '/')) {
214 return '/';
215 }
216 if ('/' === $uriPath) {
217 return '/';
218 }
219 if (0 === ($lastSlashPos = \strrpos($uriPath, '/'))) {
220 return '/';
221 }
222 return \substr($uriPath, 0, $lastSlashPos);
223 }
224 public function withCookieHeader(RequestInterface $request)
225 {
226 $values = [];
227 $uri = $request->getUri();
228 $scheme = $uri->getScheme();
229 $host = $uri->getHost();
230 $path = $uri->getPath() ?: '/';
231 foreach ($this->cookies as $cookie) {
232 if ($cookie->matchesPath($path) && $cookie->matchesDomain($host) && !$cookie->isExpired() && (!$cookie->getSecure() || $scheme === 'https')) {
233 $values[] = $cookie->getName() . '=' . $cookie->getValue();
234 }
235 }
236 return $values ? $request->withHeader('Cookie', \implode('; ', $values)) : $request;
237 }
238 /**
239 * If a cookie already exists and the server asks to set it again with a
240 * null value, the cookie must be deleted.
241 *
242 * @param SetCookie $cookie
243 */
244 private function removeCookieIfEmpty(SetCookie $cookie)
245 {
246 $cookieValue = $cookie->getValue();
247 if ($cookieValue === null || $cookieValue === '') {
248 $this->clear($cookie->getDomain(), $cookie->getPath(), $cookie->getName());
249 }
250 }
251 }
252