Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/http-foundation/Cookie.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 protected $name; | |
22 protected $value; | |
23 protected $domain; | |
24 protected $expire; | |
25 protected $path; | |
26 protected $secure; | |
27 protected $httpOnly; | |
28 private $raw; | |
29 private $sameSite; | |
30 | |
31 const SAMESITE_LAX = 'lax'; | |
32 const SAMESITE_STRICT = 'strict'; | |
33 | |
34 /** | |
35 * Constructor. | |
36 * | |
37 * @param string $name The name of the cookie | |
38 * @param string $value The value of the cookie | |
39 * @param int|string|\DateTimeInterface $expire The time the cookie expires | |
40 * @param string $path The path on the server in which the cookie will be available on | |
41 * @param string $domain The domain that the cookie is available to | |
42 * @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client | |
43 * @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol | |
44 * @param bool $raw Whether the cookie value should be sent with no url encoding | |
45 * @param string|null $sameSite Whether the cookie will be available for cross-site requests | |
46 * | |
47 * @throws \InvalidArgumentException | |
48 */ | |
49 public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true, $raw = false, $sameSite = null) | |
50 { | |
51 // from PHP source code | |
52 if (preg_match("/[=,; \t\r\n\013\014]/", $name)) { | |
53 throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name)); | |
54 } | |
55 | |
56 if (empty($name)) { | |
57 throw new \InvalidArgumentException('The cookie name cannot be empty.'); | |
58 } | |
59 | |
60 // convert expiration time to a Unix timestamp | |
61 if ($expire instanceof \DateTimeInterface) { | |
62 $expire = $expire->format('U'); | |
63 } elseif (!is_numeric($expire)) { | |
64 $expire = strtotime($expire); | |
65 | |
66 if (false === $expire) { | |
67 throw new \InvalidArgumentException('The cookie expiration time is not valid.'); | |
68 } | |
69 } | |
70 | |
71 $this->name = $name; | |
72 $this->value = $value; | |
73 $this->domain = $domain; | |
74 $this->expire = 0 < $expire ? (int) $expire : 0; | |
75 $this->path = empty($path) ? '/' : $path; | |
76 $this->secure = (bool) $secure; | |
77 $this->httpOnly = (bool) $httpOnly; | |
78 $this->raw = (bool) $raw; | |
79 | |
80 if (!in_array($sameSite, array(self::SAMESITE_LAX, self::SAMESITE_STRICT, null), true)) { | |
81 throw new \InvalidArgumentException('The "sameSite" parameter value is not valid.'); | |
82 } | |
83 | |
84 $this->sameSite = $sameSite; | |
85 } | |
86 | |
87 /** | |
88 * Returns the cookie as a string. | |
89 * | |
90 * @return string The cookie | |
91 */ | |
92 public function __toString() | |
93 { | |
94 $str = ($this->isRaw() ? $this->getName() : urlencode($this->getName())).'='; | |
95 | |
96 if ('' === (string) $this->getValue()) { | |
97 $str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001); | |
98 } else { | |
99 $str .= $this->isRaw() ? $this->getValue() : urlencode($this->getValue()); | |
100 | |
101 if (0 !== $this->getExpiresTime()) { | |
102 $str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime()); | |
103 } | |
104 } | |
105 | |
106 if ($this->getPath()) { | |
107 $str .= '; path='.$this->getPath(); | |
108 } | |
109 | |
110 if ($this->getDomain()) { | |
111 $str .= '; domain='.$this->getDomain(); | |
112 } | |
113 | |
114 if (true === $this->isSecure()) { | |
115 $str .= '; secure'; | |
116 } | |
117 | |
118 if (true === $this->isHttpOnly()) { | |
119 $str .= '; httponly'; | |
120 } | |
121 | |
122 if (null !== $this->getSameSite()) { | |
123 $str .= '; samesite='.$this->getSameSite(); | |
124 } | |
125 | |
126 return $str; | |
127 } | |
128 | |
129 /** | |
130 * Gets the name of the cookie. | |
131 * | |
132 * @return string | |
133 */ | |
134 public function getName() | |
135 { | |
136 return $this->name; | |
137 } | |
138 | |
139 /** | |
140 * Gets the value of the cookie. | |
141 * | |
142 * @return string|null | |
143 */ | |
144 public function getValue() | |
145 { | |
146 return $this->value; | |
147 } | |
148 | |
149 /** | |
150 * Gets the domain that the cookie is available to. | |
151 * | |
152 * @return string|null | |
153 */ | |
154 public function getDomain() | |
155 { | |
156 return $this->domain; | |
157 } | |
158 | |
159 /** | |
160 * Gets the time the cookie expires. | |
161 * | |
162 * @return int | |
163 */ | |
164 public function getExpiresTime() | |
165 { | |
166 return $this->expire; | |
167 } | |
168 | |
169 /** | |
170 * Gets the path on the server in which the cookie will be available on. | |
171 * | |
172 * @return string | |
173 */ | |
174 public function getPath() | |
175 { | |
176 return $this->path; | |
177 } | |
178 | |
179 /** | |
180 * Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client. | |
181 * | |
182 * @return bool | |
183 */ | |
184 public function isSecure() | |
185 { | |
186 return $this->secure; | |
187 } | |
188 | |
189 /** | |
190 * Checks whether the cookie will be made accessible only through the HTTP protocol. | |
191 * | |
192 * @return bool | |
193 */ | |
194 public function isHttpOnly() | |
195 { | |
196 return $this->httpOnly; | |
197 } | |
198 | |
199 /** | |
200 * Whether this cookie is about to be cleared. | |
201 * | |
202 * @return bool | |
203 */ | |
204 public function isCleared() | |
205 { | |
206 return $this->expire < time(); | |
207 } | |
208 | |
209 /** | |
210 * Checks if the cookie value should be sent with no url encoding. | |
211 * | |
212 * @return bool | |
213 */ | |
214 public function isRaw() | |
215 { | |
216 return $this->raw; | |
217 } | |
218 | |
219 /** | |
220 * Gets the SameSite attribute. | |
221 * | |
222 * @return string|null | |
223 */ | |
224 public function getSameSite() | |
225 { | |
226 return $this->sameSite; | |
227 } | |
228 } |