PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 2.8.16
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v2.8.16
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / vendor / symfony / browser-kit / Cookie.php

Cookie.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 2.8.16, at vendor/symfony/browser-kit/Cookie.php

320 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\BrowserKit;
13
14 /**
15 * Cookie represents an HTTP cookie.
16 *
17 * @author Fabien Potencier <[email protected]>
18 */
19 class Cookie
20 {
21 /**
22 * Handles dates as defined by RFC 2616 section 3.3.1, and also some other
23 * non-standard, but common formats.
24 */
25 private const DATE_FORMATS = [
26 'D, d M Y H:i:s T',
27 'D, d-M-y H:i:s T',
28 'D, d-M-Y H:i:s T',
29 'D, d-m-y H:i:s T',
30 'D, d-m-Y H:i:s T',
31 'D M j G:i:s Y',
32 'D M d H:i:s Y T',
33 ];
34
35 protected $name;
36 protected $value;
37 protected $expires;
38 protected $path;
39 protected $domain;
40 protected $secure;
41 protected $httponly;
42 protected $rawValue;
43 private $samesite;
44
45 /**
46 * Sets a cookie.
47 *
48 * @param string $name The cookie name
49 * @param string|null $value The value of the cookie
50 * @param string|null $expires The time the cookie expires
51 * @param string|null $path The path on the server in which the cookie will be available on
52 * @param string $domain The domain that the cookie is available
53 * @param bool $secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client
54 * @param bool $httponly The cookie httponly flag
55 * @param bool $encodedValue Whether the value is encoded or not
56 * @param string|null $samesite The cookie samesite attribute
57 */
58 public function __construct(string $name, ?string $value, string $expires = null, string $path = null, string $domain = '', bool $secure = false, bool $httponly = true, bool $encodedValue = false, string $samesite = null)
59 {
60 if ($encodedValue) {
61 $this->value = urldecode($value);
62 $this->rawValue = $value;
63 } else {
64 $this->value = $value;
65 $this->rawValue = rawurlencode($value ?? '');
66 }
67 $this->name = $name;
68 $this->path = empty($path) ? '/' : $path;
69 $this->domain = $domain;
70 $this->secure = $secure;
71 $this->httponly = $httponly;
72 $this->samesite = $samesite;
73
74 if (null !== $expires) {
75 $timestampAsDateTime = \DateTime::createFromFormat('U', $expires);
76 if (false === $timestampAsDateTime) {
77 throw new \UnexpectedValueException(sprintf('The cookie expiration time "%s" is not valid.', $expires));
78 }
79
80 $this->expires = $timestampAsDateTime->format('U');
81 }
82 }
83
84 /**
85 * Returns the HTTP representation of the Cookie.
86 *
87 * @return string
88 */
89 public function __toString()
90 {
91 $cookie = sprintf('%s=%s', $this->name, $this->rawValue);
92
93 if (null !== $this->expires) {
94 $dateTime = \DateTime::createFromFormat('U', $this->expires, new \DateTimeZone('GMT'));
95 $cookie .= '; expires='.str_replace('+0000', '', $dateTime->format(self::DATE_FORMATS[0]));
96 }
97
98 if ('' !== $this->domain) {
99 $cookie .= '; domain='.$this->domain;
100 }
101
102 if ($this->path) {
103 $cookie .= '; path='.$this->path;
104 }
105
106 if ($this->secure) {
107 $cookie .= '; secure';
108 }
109
110 if ($this->httponly) {
111 $cookie .= '; httponly';
112 }
113
114 if (null !== $this->samesite) {
115 $cookie .= '; samesite='.$this->samesite;
116 }
117
118 return $cookie;
119 }
120
121 /**
122 * Creates a Cookie instance from a Set-Cookie header value.
123 *
124 * @return static
125 *
126 * @throws \InvalidArgumentException
127 */
128 public static function fromString(string $cookie, string $url = null)
129 {
130 $parts = explode(';', $cookie);
131
132 if (!str_contains($parts[0], '=')) {
133 throw new \InvalidArgumentException(sprintf('The cookie string "%s" is not valid.', $parts[0]));
134 }
135
136 [$name, $value] = explode('=', array_shift($parts), 2);
137
138 $values = [
139 'name' => trim($name),
140 'value' => trim($value),
141 'expires' => null,
142 'path' => '/',
143 'domain' => '',
144 'secure' => false,
145 'httponly' => false,
146 'passedRawValue' => true,
147 'samesite' => null,
148 ];
149
150 if (null !== $url) {
151 if ((false === $urlParts = parse_url($url)) || !isset($urlParts['host'])) {
152 throw new \InvalidArgumentException(sprintf('The URL "%s" is not valid.', $url));
153 }
154
155 $values['domain'] = $urlParts['host'];
156 $values['path'] = isset($urlParts['path']) ? substr($urlParts['path'], 0, strrpos($urlParts['path'], '/')) : '';
157 }
158
159 foreach ($parts as $part) {
160 $part = trim($part);
161
162 if ('secure' === strtolower($part)) {
163 // Ignore the secure flag if the original URI is not given or is not HTTPS
164 if (!$url || !isset($urlParts['scheme']) || 'https' != $urlParts['scheme']) {
165 continue;
166 }
167
168 $values['secure'] = true;
169
170 continue;
171 }
172
173 if ('httponly' === strtolower($part)) {
174 $values['httponly'] = true;
175
176 continue;
177 }
178
179 if (2 === \count($elements = explode('=', $part, 2))) {
180 if ('expires' === strtolower($elements[0])) {
181 $elements[1] = self::parseDate($elements[1]);
182 }
183
184 $values[strtolower($elements[0])] = $elements[1];
185 }
186 }
187
188 return new static(
189 $values['name'],
190 $values['value'],
191 $values['expires'],
192 $values['path'],
193 $values['domain'],
194 $values['secure'],
195 $values['httponly'],
196 $values['passedRawValue'],
197 $values['samesite']
198 );
199 }
200
201 private static function parseDate(string $dateValue): ?string
202 {
203 // trim single quotes around date if present
204 if (($length = \strlen($dateValue)) > 1 && "'" === $dateValue[0] && "'" === $dateValue[$length - 1]) {
205 $dateValue = substr($dateValue, 1, -1);
206 }
207
208 foreach (self::DATE_FORMATS as $dateFormat) {
209 if (false !== $date = \DateTime::createFromFormat($dateFormat, $dateValue, new \DateTimeZone('GMT'))) {
210 return $date->format('U');
211 }
212 }
213
214 // attempt a fallback for unusual formatting
215 if (false !== $date = date_create($dateValue, new \DateTimeZone('GMT'))) {
216 return $date->format('U');
217 }
218
219 return null;
220 }
221
222 /**
223 * Gets the name of the cookie.
224 *
225 * @return string
226 */
227 public function getName()
228 {
229 return $this->name;
230 }
231
232 /**
233 * Gets the value of the cookie.
234 *
235 * @return string
236 */
237 public function getValue()
238 {
239 return $this->value;
240 }
241
242 /**
243 * Gets the raw value of the cookie.
244 *
245 * @return string
246 */
247 public function getRawValue()
248 {
249 return $this->rawValue;
250 }
251
252 /**
253 * Gets the expires time of the cookie.
254 *
255 * @return string|null
256 */
257 public function getExpiresTime()
258 {
259 return $this->expires;
260 }
261
262 /**
263 * Gets the path of the cookie.
264 *
265 * @return string
266 */
267 public function getPath()
268 {
269 return $this->path;
270 }
271
272 /**
273 * Gets the domain of the cookie.
274 *
275 * @return string
276 */
277 public function getDomain()
278 {
279 return $this->domain;
280 }
281
282 /**
283 * Returns the secure flag of the cookie.
284 *
285 * @return bool
286 */
287 public function isSecure()
288 {
289 return $this->secure;
290 }
291
292 /**
293 * Returns the httponly flag of the cookie.
294 *
295 * @return bool
296 */
297 public function isHttpOnly()
298 {
299 return $this->httponly;
300 }
301
302 /**
303 * Returns true if the cookie has expired.
304 *
305 * @return bool
306 */
307 public function isExpired()
308 {
309 return null !== $this->expires && 0 != $this->expires && $this->expires <= time();
310 }
311
312 /**
313 * Gets the samesite attribute of the cookie.
314 */
315 public function getSameSite(): ?string
316 {
317 return $this->samesite;
318 }
319 }
320