Security update for Core, with self-updated composer
[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      * 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 (null !== $sameSite) {
81             $sameSite = strtolower($sameSite);
82         }
83
84         if (!in_array($sameSite, array(self::SAMESITE_LAX, self::SAMESITE_STRICT, null), true)) {
85             throw new \InvalidArgumentException('The "sameSite" parameter value is not valid.');
86         }
87
88         $this->sameSite = $sameSite;
89     }
90
91     /**
92      * Returns the cookie as a string.
93      *
94      * @return string The cookie
95      */
96     public function __toString()
97     {
98         $str = ($this->isRaw() ? $this->getName() : urlencode($this->getName())).'=';
99
100         if ('' === (string) $this->getValue()) {
101             $str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001);
102         } else {
103             $str .= $this->isRaw() ? $this->getValue() : rawurlencode($this->getValue());
104
105             if (0 !== $this->getExpiresTime()) {
106                 $str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime());
107             }
108         }
109
110         if ($this->getPath()) {
111             $str .= '; path='.$this->getPath();
112         }
113
114         if ($this->getDomain()) {
115             $str .= '; domain='.$this->getDomain();
116         }
117
118         if (true === $this->isSecure()) {
119             $str .= '; secure';
120         }
121
122         if (true === $this->isHttpOnly()) {
123             $str .= '; httponly';
124         }
125
126         if (null !== $this->getSameSite()) {
127             $str .= '; samesite='.$this->getSameSite();
128         }
129
130         return $str;
131     }
132
133     /**
134      * Gets the name of the cookie.
135      *
136      * @return string
137      */
138     public function getName()
139     {
140         return $this->name;
141     }
142
143     /**
144      * Gets the value of the cookie.
145      *
146      * @return string|null
147      */
148     public function getValue()
149     {
150         return $this->value;
151     }
152
153     /**
154      * Gets the domain that the cookie is available to.
155      *
156      * @return string|null
157      */
158     public function getDomain()
159     {
160         return $this->domain;
161     }
162
163     /**
164      * Gets the time the cookie expires.
165      *
166      * @return int
167      */
168     public function getExpiresTime()
169     {
170         return $this->expire;
171     }
172
173     /**
174      * Gets the path on the server in which the cookie will be available on.
175      *
176      * @return string
177      */
178     public function getPath()
179     {
180         return $this->path;
181     }
182
183     /**
184      * Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
185      *
186      * @return bool
187      */
188     public function isSecure()
189     {
190         return $this->secure;
191     }
192
193     /**
194      * Checks whether the cookie will be made accessible only through the HTTP protocol.
195      *
196      * @return bool
197      */
198     public function isHttpOnly()
199     {
200         return $this->httpOnly;
201     }
202
203     /**
204      * Whether this cookie is about to be cleared.
205      *
206      * @return bool
207      */
208     public function isCleared()
209     {
210         return $this->expire < time();
211     }
212
213     /**
214      * Checks if the cookie value should be sent with no url encoding.
215      *
216      * @return bool
217      */
218     public function isRaw()
219     {
220         return $this->raw;
221     }
222
223     /**
224      * Gets the SameSite attribute.
225      *
226      * @return string|null
227      */
228     public function getSameSite()
229     {
230         return $this->sameSite;
231     }
232 }