| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
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\HttpFoundation;
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* Represents a cookie.
|
|
|
16 |
*
|
|
|
17 |
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
|
|
18 |
*/
|
|
|
19 |
class Cookie
|
|
|
20 |
{
|
|
|
21 |
public const SAMESITE_NONE = 'none';
|
|
|
22 |
public const SAMESITE_LAX = 'lax';
|
|
|
23 |
public const SAMESITE_STRICT = 'strict';
|
|
|
24 |
|
|
|
25 |
protected $name;
|
|
|
26 |
protected $value;
|
|
|
27 |
protected $domain;
|
|
|
28 |
protected $expire;
|
|
|
29 |
protected $path;
|
|
|
30 |
protected $secure;
|
|
|
31 |
protected $httpOnly;
|
|
|
32 |
|
|
|
33 |
private bool $raw;
|
|
|
34 |
private ?string $sameSite = null;
|
|
|
35 |
private bool $secureDefault = false;
|
|
|
36 |
|
|
|
37 |
private const RESERVED_CHARS_LIST = "=,; \t\r\n\v\f";
|
|
|
38 |
private const RESERVED_CHARS_FROM = ['=', ',', ';', ' ', "\t", "\r", "\n", "\v", "\f"];
|
|
|
39 |
private const RESERVED_CHARS_TO = ['%3D', '%2C', '%3B', '%20', '%09', '%0D', '%0A', '%0B', '%0C'];
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Creates cookie from raw header string.
|
|
|
43 |
*/
|
|
|
44 |
public static function fromString(string $cookie, bool $decode = false): static
|
|
|
45 |
{
|
|
|
46 |
$data = [
|
|
|
47 |
'expires' => 0,
|
|
|
48 |
'path' => '/',
|
|
|
49 |
'domain' => null,
|
|
|
50 |
'secure' => false,
|
|
|
51 |
'httponly' => false,
|
|
|
52 |
'raw' => !$decode,
|
|
|
53 |
'samesite' => null,
|
|
|
54 |
];
|
|
|
55 |
|
|
|
56 |
$parts = HeaderUtils::split($cookie, ';=');
|
|
|
57 |
$part = array_shift($parts);
|
|
|
58 |
|
|
|
59 |
$name = $decode ? urldecode($part[0]) : $part[0];
|
|
|
60 |
$value = isset($part[1]) ? ($decode ? urldecode($part[1]) : $part[1]) : null;
|
|
|
61 |
|
|
|
62 |
$data = HeaderUtils::combine($parts) + $data;
|
|
|
63 |
$data['expires'] = self::expiresTimestamp($data['expires']);
|
|
|
64 |
|
|
|
65 |
if (isset($data['max-age']) && ($data['max-age'] > 0 || $data['expires'] > time())) {
|
|
|
66 |
$data['expires'] = time() + (int) $data['max-age'];
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
return new static($name, $value, $data['expires'], $data['path'], $data['domain'], $data['secure'], $data['httponly'], $data['raw'], $data['samesite']);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* @see self::__construct
|
|
|
74 |
*
|
|
|
75 |
* @param self::SAMESITE_*|''|null $sameSite
|
|
|
76 |
*/
|
|
|
77 |
public static function create(string $name, string $value = null, int|string|\DateTimeInterface $expire = 0, ?string $path = '/', string $domain = null, bool $secure = null, bool $httpOnly = true, bool $raw = false, ?string $sameSite = self::SAMESITE_LAX): self
|
|
|
78 |
{
|
|
|
79 |
return new self($name, $value, $expire, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* @param string $name The name of the cookie
|
|
|
84 |
* @param string|null $value The value of the cookie
|
|
|
85 |
* @param int|string|\DateTimeInterface $expire The time the cookie expires
|
|
|
86 |
* @param string $path The path on the server in which the cookie will be available on
|
|
|
87 |
* @param string|null $domain The domain that the cookie is available to
|
|
|
88 |
* @param bool|null $secure Whether the client should send back the cookie only over HTTPS or null to auto-enable this when the request is already using HTTPS
|
|
|
89 |
* @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
|
|
|
90 |
* @param bool $raw Whether the cookie value should be sent with no url encoding
|
|
|
91 |
* @param self::SAMESITE_*|''|null $sameSite Whether the cookie will be available for cross-site requests
|
|
|
92 |
*
|
|
|
93 |
* @throws \InvalidArgumentException
|
|
|
94 |
*/
|
|
|
95 |
public function __construct(string $name, string $value = null, int|string|\DateTimeInterface $expire = 0, ?string $path = '/', string $domain = null, bool $secure = null, bool $httpOnly = true, bool $raw = false, ?string $sameSite = self::SAMESITE_LAX)
|
|
|
96 |
{
|
|
|
97 |
// from PHP source code
|
|
|
98 |
if ($raw && false !== strpbrk($name, self::RESERVED_CHARS_LIST)) {
|
|
|
99 |
throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
if (empty($name)) {
|
|
|
103 |
throw new \InvalidArgumentException('The cookie name cannot be empty.');
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
$this->name = $name;
|
|
|
107 |
$this->value = $value;
|
|
|
108 |
$this->domain = $domain;
|
|
|
109 |
$this->expire = self::expiresTimestamp($expire);
|
|
|
110 |
$this->path = empty($path) ? '/' : $path;
|
|
|
111 |
$this->secure = $secure;
|
|
|
112 |
$this->httpOnly = $httpOnly;
|
|
|
113 |
$this->raw = $raw;
|
|
|
114 |
$this->sameSite = $this->withSameSite($sameSite)->sameSite;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Creates a cookie copy with a new value.
|
|
|
119 |
*/
|
|
|
120 |
public function withValue(?string $value): static
|
|
|
121 |
{
|
|
|
122 |
$cookie = clone $this;
|
|
|
123 |
$cookie->value = $value;
|
|
|
124 |
|
|
|
125 |
return $cookie;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Creates a cookie copy with a new domain that the cookie is available to.
|
|
|
130 |
*/
|
|
|
131 |
public function withDomain(?string $domain): static
|
|
|
132 |
{
|
|
|
133 |
$cookie = clone $this;
|
|
|
134 |
$cookie->domain = $domain;
|
|
|
135 |
|
|
|
136 |
return $cookie;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* Creates a cookie copy with a new time the cookie expires.
|
|
|
141 |
*/
|
|
|
142 |
public function withExpires(int|string|\DateTimeInterface $expire = 0): static
|
|
|
143 |
{
|
|
|
144 |
$cookie = clone $this;
|
|
|
145 |
$cookie->expire = self::expiresTimestamp($expire);
|
|
|
146 |
|
|
|
147 |
return $cookie;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Converts expires formats to a unix timestamp.
|
|
|
152 |
*/
|
|
|
153 |
private static function expiresTimestamp(int|string|\DateTimeInterface $expire = 0): int
|
|
|
154 |
{
|
|
|
155 |
// convert expiration time to a Unix timestamp
|
|
|
156 |
if ($expire instanceof \DateTimeInterface) {
|
|
|
157 |
$expire = $expire->format('U');
|
|
|
158 |
} elseif (!is_numeric($expire)) {
|
|
|
159 |
$expire = strtotime($expire);
|
|
|
160 |
|
|
|
161 |
if (false === $expire) {
|
|
|
162 |
throw new \InvalidArgumentException('The cookie expiration time is not valid.');
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
return 0 < $expire ? (int) $expire : 0;
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* Creates a cookie copy with a new path on the server in which the cookie will be available on.
|
|
|
171 |
*/
|
|
|
172 |
public function withPath(string $path): static
|
|
|
173 |
{
|
|
|
174 |
$cookie = clone $this;
|
|
|
175 |
$cookie->path = '' === $path ? '/' : $path;
|
|
|
176 |
|
|
|
177 |
return $cookie;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
* Creates a cookie copy that only be transmitted over a secure HTTPS connection from the client.
|
|
|
182 |
*/
|
|
|
183 |
public function withSecure(bool $secure = true): static
|
|
|
184 |
{
|
|
|
185 |
$cookie = clone $this;
|
|
|
186 |
$cookie->secure = $secure;
|
|
|
187 |
|
|
|
188 |
return $cookie;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* Creates a cookie copy that be accessible only through the HTTP protocol.
|
|
|
193 |
*/
|
|
|
194 |
public function withHttpOnly(bool $httpOnly = true): static
|
|
|
195 |
{
|
|
|
196 |
$cookie = clone $this;
|
|
|
197 |
$cookie->httpOnly = $httpOnly;
|
|
|
198 |
|
|
|
199 |
return $cookie;
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
/**
|
|
|
203 |
* Creates a cookie copy that uses no url encoding.
|
|
|
204 |
*/
|
|
|
205 |
public function withRaw(bool $raw = true): static
|
|
|
206 |
{
|
|
|
207 |
if ($raw && false !== strpbrk($this->name, self::RESERVED_CHARS_LIST)) {
|
|
|
208 |
throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $this->name));
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
$cookie = clone $this;
|
|
|
212 |
$cookie->raw = $raw;
|
|
|
213 |
|
|
|
214 |
return $cookie;
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
/**
|
|
|
218 |
* Creates a cookie copy with SameSite attribute.
|
|
|
219 |
*
|
|
|
220 |
* @param self::SAMESITE_*|''|null $sameSite
|
|
|
221 |
*/
|
|
|
222 |
public function withSameSite(?string $sameSite): static
|
|
|
223 |
{
|
|
|
224 |
if ('' === $sameSite) {
|
|
|
225 |
$sameSite = null;
|
|
|
226 |
} elseif (null !== $sameSite) {
|
|
|
227 |
$sameSite = strtolower($sameSite);
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
if (!\in_array($sameSite, [self::SAMESITE_LAX, self::SAMESITE_STRICT, self::SAMESITE_NONE, null], true)) {
|
|
|
231 |
throw new \InvalidArgumentException('The "sameSite" parameter value is not valid.');
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
$cookie = clone $this;
|
|
|
235 |
$cookie->sameSite = $sameSite;
|
|
|
236 |
|
|
|
237 |
return $cookie;
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
/**
|
|
|
241 |
* Returns the cookie as a string.
|
|
|
242 |
*/
|
|
|
243 |
public function __toString(): string
|
|
|
244 |
{
|
|
|
245 |
if ($this->isRaw()) {
|
|
|
246 |
$str = $this->getName();
|
|
|
247 |
} else {
|
|
|
248 |
$str = str_replace(self::RESERVED_CHARS_FROM, self::RESERVED_CHARS_TO, $this->getName());
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
$str .= '=';
|
|
|
252 |
|
|
|
253 |
if ('' === (string) $this->getValue()) {
|
|
|
254 |
$str .= 'deleted; expires='.gmdate('D, d M Y H:i:s T', time() - 31536001).'; Max-Age=0';
|
|
|
255 |
} else {
|
|
|
256 |
$str .= $this->isRaw() ? $this->getValue() : rawurlencode($this->getValue());
|
|
|
257 |
|
|
|
258 |
if (0 !== $this->getExpiresTime()) {
|
|
|
259 |
$str .= '; expires='.gmdate('D, d M Y H:i:s T', $this->getExpiresTime()).'; Max-Age='.$this->getMaxAge();
|
|
|
260 |
}
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
if ($this->getPath()) {
|
|
|
264 |
$str .= '; path='.$this->getPath();
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
if ($this->getDomain()) {
|
|
|
268 |
$str .= '; domain='.$this->getDomain();
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
if (true === $this->isSecure()) {
|
|
|
272 |
$str .= '; secure';
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
if (true === $this->isHttpOnly()) {
|
|
|
276 |
$str .= '; httponly';
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
if (null !== $this->getSameSite()) {
|
|
|
280 |
$str .= '; samesite='.$this->getSameSite();
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
return $str;
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
/**
|
|
|
287 |
* Gets the name of the cookie.
|
|
|
288 |
*/
|
|
|
289 |
public function getName(): string
|
|
|
290 |
{
|
|
|
291 |
return $this->name;
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
/**
|
|
|
295 |
* Gets the value of the cookie.
|
|
|
296 |
*/
|
|
|
297 |
public function getValue(): ?string
|
|
|
298 |
{
|
|
|
299 |
return $this->value;
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
/**
|
|
|
303 |
* Gets the domain that the cookie is available to.
|
|
|
304 |
*/
|
|
|
305 |
public function getDomain(): ?string
|
|
|
306 |
{
|
|
|
307 |
return $this->domain;
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
/**
|
|
|
311 |
* Gets the time the cookie expires.
|
|
|
312 |
*/
|
|
|
313 |
public function getExpiresTime(): int
|
|
|
314 |
{
|
|
|
315 |
return $this->expire;
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
/**
|
|
|
319 |
* Gets the max-age attribute.
|
|
|
320 |
*/
|
|
|
321 |
public function getMaxAge(): int
|
|
|
322 |
{
|
|
|
323 |
$maxAge = $this->expire - time();
|
|
|
324 |
|
|
|
325 |
return 0 >= $maxAge ? 0 : $maxAge;
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
/**
|
|
|
329 |
* Gets the path on the server in which the cookie will be available on.
|
|
|
330 |
*/
|
|
|
331 |
public function getPath(): string
|
|
|
332 |
{
|
|
|
333 |
return $this->path;
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
/**
|
|
|
337 |
* Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
|
|
|
338 |
*/
|
|
|
339 |
public function isSecure(): bool
|
|
|
340 |
{
|
|
|
341 |
return $this->secure ?? $this->secureDefault;
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
/**
|
|
|
345 |
* Checks whether the cookie will be made accessible only through the HTTP protocol.
|
|
|
346 |
*/
|
|
|
347 |
public function isHttpOnly(): bool
|
|
|
348 |
{
|
|
|
349 |
return $this->httpOnly;
|
|
|
350 |
}
|
|
|
351 |
|
|
|
352 |
/**
|
|
|
353 |
* Whether this cookie is about to be cleared.
|
|
|
354 |
*/
|
|
|
355 |
public function isCleared(): bool
|
|
|
356 |
{
|
|
|
357 |
return 0 !== $this->expire && $this->expire < time();
|
|
|
358 |
}
|
|
|
359 |
|
|
|
360 |
/**
|
|
|
361 |
* Checks if the cookie value should be sent with no url encoding.
|
|
|
362 |
*/
|
|
|
363 |
public function isRaw(): bool
|
|
|
364 |
{
|
|
|
365 |
return $this->raw;
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
/**
|
|
|
369 |
* @return self::SAMESITE_*|null
|
|
|
370 |
*/
|
|
|
371 |
public function getSameSite(): ?string
|
|
|
372 |
{
|
|
|
373 |
return $this->sameSite;
|
|
|
374 |
}
|
|
|
375 |
|
|
|
376 |
/**
|
|
|
377 |
* @param bool $default The default value of the "secure" flag when it is set to null
|
|
|
378 |
*/
|
|
|
379 |
public function setSecureDefault(bool $default): void
|
|
|
380 |
{
|
|
|
381 |
$this->secureDefault = $default;
|
|
|
382 |
}
|
|
|
383 |
}
|