Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / http-foundation / Cookie.php
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      * Creates cookie from raw header string.
36      *
37      * @param string $cookie
38      * @param bool   $decode
39      *
40      * @return static
41      */
42     public static function fromString($cookie, $decode = false)
43     {
44         $data = array(
45             'expires' => 0,
46             'path' => '/',
47             'domain' => null,
48             'secure' => false,
49             'httponly' => false,
50             'raw' => !$decode,
51             'samesite' => null,
52         );
53         foreach (explode(';', $cookie) as $part) {
54             if (false === strpos($part, '=')) {
55                 $key = trim($part);
56                 $value = true;
57             } else {
58                 list($key, $value) = explode('=', trim($part), 2);
59                 $key = trim($key);
60                 $value = trim($value);
61             }
62             if (!isset($data['name'])) {
63                 $data['name'] = $decode ? urldecode($key) : $key;
64                 $data['value'] = true === $value ? null : ($decode ? urldecode($value) : $value);
65                 continue;
66             }
67             switch ($key = strtolower($key)) {
68                 case 'name':
69                 case 'value':
70                     break;
71                 case 'max-age':
72                     $data['expires'] = time() + (int) $value;
73                     break;
74                 default:
75                     $data[$key] = $value;
76                     break;
77             }
78         }
79
80         return new static($data['name'], $data['value'], $data['expires'], $data['path'], $data['domain'], $data['secure'], $data['httponly'], $data['raw'], $data['samesite']);
81     }
82
83     /**
84      * @param string                        $name     The name of the cookie
85      * @param string|null                   $value    The value of the cookie
86      * @param int|string|\DateTimeInterface $expire   The time the cookie expires
87      * @param string                        $path     The path on the server in which the cookie will be available on
88      * @param string|null                   $domain   The domain that the cookie is available to
89      * @param bool                          $secure   Whether the cookie should only be transmitted over a secure HTTPS connection from the client
90      * @param bool                          $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
91      * @param bool                          $raw      Whether the cookie value should be sent with no url encoding
92      * @param string|null                   $sameSite Whether the cookie will be available for cross-site requests
93      *
94      * @throws \InvalidArgumentException
95      */
96     public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true, $raw = false, $sameSite = null)
97     {
98         // from PHP source code
99         if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
100             throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
101         }
102
103         if (empty($name)) {
104             throw new \InvalidArgumentException('The cookie name cannot be empty.');
105         }
106
107         // convert expiration time to a Unix timestamp
108         if ($expire instanceof \DateTimeInterface) {
109             $expire = $expire->format('U');
110         } elseif (!is_numeric($expire)) {
111             $expire = strtotime($expire);
112
113             if (false === $expire) {
114                 throw new \InvalidArgumentException('The cookie expiration time is not valid.');
115             }
116         }
117
118         $this->name = $name;
119         $this->value = $value;
120         $this->domain = $domain;
121         $this->expire = 0 < $expire ? (int) $expire : 0;
122         $this->path = empty($path) ? '/' : $path;
123         $this->secure = (bool) $secure;
124         $this->httpOnly = (bool) $httpOnly;
125         $this->raw = (bool) $raw;
126
127         if (null !== $sameSite) {
128             $sameSite = strtolower($sameSite);
129         }
130
131         if (!\in_array($sameSite, array(self::SAMESITE_LAX, self::SAMESITE_STRICT, null), true)) {
132             throw new \InvalidArgumentException('The "sameSite" parameter value is not valid.');
133         }
134
135         $this->sameSite = $sameSite;
136     }
137
138     /**
139      * Returns the cookie as a string.
140      *
141      * @return string The cookie
142      */
143     public function __toString()
144     {
145         $str = ($this->isRaw() ? $this->getName() : urlencode($this->getName())).'=';
146
147         if ('' === (string) $this->getValue()) {
148             $str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; Max-Age=0';
149         } else {
150             $str .= $this->isRaw() ? $this->getValue() : rawurlencode($this->getValue());
151
152             if (0 !== $this->getExpiresTime()) {
153                 $str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime()).'; Max-Age='.$this->getMaxAge();
154             }
155         }
156
157         if ($this->getPath()) {
158             $str .= '; path='.$this->getPath();
159         }
160
161         if ($this->getDomain()) {
162             $str .= '; domain='.$this->getDomain();
163         }
164
165         if (true === $this->isSecure()) {
166             $str .= '; secure';
167         }
168
169         if (true === $this->isHttpOnly()) {
170             $str .= '; httponly';
171         }
172
173         if (null !== $this->getSameSite()) {
174             $str .= '; samesite='.$this->getSameSite();
175         }
176
177         return $str;
178     }
179
180     /**
181      * Gets the name of the cookie.
182      *
183      * @return string
184      */
185     public function getName()
186     {
187         return $this->name;
188     }
189
190     /**
191      * Gets the value of the cookie.
192      *
193      * @return string|null
194      */
195     public function getValue()
196     {
197         return $this->value;
198     }
199
200     /**
201      * Gets the domain that the cookie is available to.
202      *
203      * @return string|null
204      */
205     public function getDomain()
206     {
207         return $this->domain;
208     }
209
210     /**
211      * Gets the time the cookie expires.
212      *
213      * @return int
214      */
215     public function getExpiresTime()
216     {
217         return $this->expire;
218     }
219
220     /**
221      * Gets the max-age attribute.
222      *
223      * @return int
224      */
225     public function getMaxAge()
226     {
227         $maxAge = $this->expire - time();
228
229         return 0 >= $maxAge ? 0 : $maxAge;
230     }
231
232     /**
233      * Gets the path on the server in which the cookie will be available on.
234      *
235      * @return string
236      */
237     public function getPath()
238     {
239         return $this->path;
240     }
241
242     /**
243      * Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
244      *
245      * @return bool
246      */
247     public function isSecure()
248     {
249         return $this->secure;
250     }
251
252     /**
253      * Checks whether the cookie will be made accessible only through the HTTP protocol.
254      *
255      * @return bool
256      */
257     public function isHttpOnly()
258     {
259         return $this->httpOnly;
260     }
261
262     /**
263      * Whether this cookie is about to be cleared.
264      *
265      * @return bool
266      */
267     public function isCleared()
268     {
269         return 0 !== $this->expire && $this->expire < time();
270     }
271
272     /**
273      * Checks if the cookie value should be sent with no url encoding.
274      *
275      * @return bool
276      */
277     public function isRaw()
278     {
279         return $this->raw;
280     }
281
282     /**
283      * Gets the SameSite attribute.
284      *
285      * @return string|null
286      */
287     public function getSameSite()
288     {
289         return $this->sameSite;
290     }
291 }