Open Journal Systems  3.3.0
lib/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 
13 
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 
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 
98  public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true, $raw = false, $sameSite = null)
99  {
100  // from PHP source code
101  if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
102  throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
103  }
104 
105  if (empty($name)) {
106  throw new \InvalidArgumentException('The cookie name cannot be empty.');
107  }
108 
109  // convert expiration time to a Unix timestamp
110  if ($expire instanceof \DateTimeInterface) {
111  $expire = $expire->format('U');
112  } elseif (!is_numeric($expire)) {
113  $expire = strtotime($expire);
114 
115  if (false === $expire) {
116  throw new \InvalidArgumentException('The cookie expiration time is not valid.');
117  }
118  }
119 
120  $this->name = $name;
121  $this->value = $value;
122  $this->domain = $domain;
123  $this->expire = 0 < $expire ? (int) $expire : 0;
124  $this->path = empty($path) ? '/' : $path;
125  $this->secure = (bool) $secure;
126  $this->httpOnly = (bool) $httpOnly;
127  $this->raw = (bool) $raw;
128 
129  if (null !== $sameSite) {
130  $sameSite = strtolower($sameSite);
131  }
132 
133  if (!in_array($sameSite, array(self::SAMESITE_LAX, self::SAMESITE_STRICT, null), true)) {
134  throw new \InvalidArgumentException('The "sameSite" parameter value is not valid.');
135  }
136 
137  $this->sameSite = $sameSite;
138  }
139 
145  public function __toString()
146  {
147  $str = ($this->isRaw() ? $this->getName() : urlencode($this->getName())).'=';
148 
149  if ('' === (string) $this->getValue()) {
150  $str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; max-age=-31536001';
151  } else {
152  $str .= $this->isRaw() ? $this->getValue() : rawurlencode($this->getValue());
153 
154  if (0 !== $this->getExpiresTime()) {
155  $str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime()).'; max-age='.$this->getMaxAge();
156  }
157  }
158 
159  if ($this->getPath()) {
160  $str .= '; path='.$this->getPath();
161  }
162 
163  if ($this->getDomain()) {
164  $str .= '; domain='.$this->getDomain();
165  }
166 
167  if (true === $this->isSecure()) {
168  $str .= '; secure';
169  }
170 
171  if (true === $this->isHttpOnly()) {
172  $str .= '; httponly';
173  }
174 
175  if (null !== $this->getSameSite()) {
176  $str .= '; samesite='.$this->getSameSite();
177  }
178 
179  return $str;
180  }
181 
187  public function getName()
188  {
189  return $this->name;
190  }
191 
197  public function getValue()
198  {
199  return $this->value;
200  }
201 
207  public function getDomain()
208  {
209  return $this->domain;
210  }
211 
217  public function getExpiresTime()
218  {
219  return $this->expire;
220  }
221 
227  public function getMaxAge()
228  {
229  return 0 !== $this->expire ? $this->expire - time() : 0;
230  }
231 
237  public function getPath()
238  {
239  return $this->path;
240  }
241 
247  public function isSecure()
248  {
249  return $this->secure;
250  }
251 
257  public function isHttpOnly()
258  {
259  return $this->httpOnly;
260  }
261 
267  public function isCleared()
268  {
269  return $this->expire < time();
270  }
271 
277  public function isRaw()
278  {
279  return $this->raw;
280  }
281 
287  public function getSameSite()
288  {
289  return $this->sameSite;
290  }
291 }
Symfony\Component\HttpFoundation\Cookie\$httpOnly
$httpOnly
Definition: lib/vendor/symfony/http-foundation/Cookie.php:27
Symfony\Component\HttpFoundation\Cookie\$domain
$domain
Definition: lib/vendor/symfony/http-foundation/Cookie.php:23
Symfony\Component\HttpFoundation\Cookie\$value
$value
Definition: lib/vendor/symfony/http-foundation/Cookie.php:22
Symfony\Component\HttpFoundation\Cookie\getSameSite
getSameSite()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:287
Symfony\Component\HttpFoundation\Cookie\SAMESITE_LAX
const SAMESITE_LAX
Definition: lib/vendor/symfony/http-foundation/Cookie.php:31
Symfony\Component\HttpFoundation\Cookie\getPath
getPath()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:237
Symfony\Component\HttpFoundation\Cookie\getMaxAge
getMaxAge()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:227
Symfony\Component\HttpFoundation\Cookie\isRaw
isRaw()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:277
Symfony\Component\HttpFoundation\Cookie\__construct
__construct($name, $value=null, $expire=0, $path='/', $domain=null, $secure=false, $httpOnly=true, $raw=false, $sameSite=null)
Definition: lib/vendor/symfony/http-foundation/Cookie.php:98
Symfony\Component\HttpFoundation\Cookie\isCleared
isCleared()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:267
Symfony\Component\HttpFoundation\Cookie\isHttpOnly
isHttpOnly()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:257
Symfony\Component\HttpFoundation\Cookie\getDomain
getDomain()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:207
Symfony\Component\HttpFoundation\Cookie\$expire
$expire
Definition: lib/vendor/symfony/http-foundation/Cookie.php:24
Symfony\Component\HttpFoundation\Cookie\__toString
__toString()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:145
Symfony\Component\HttpFoundation\Cookie\fromString
static fromString($cookie, $decode=false)
Definition: lib/vendor/symfony/http-foundation/Cookie.php:42
Symfony\Component\HttpFoundation\Cookie\$secure
$secure
Definition: lib/vendor/symfony/http-foundation/Cookie.php:26
Symfony\Component\HttpFoundation
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:12
Symfony\Component\HttpFoundation\Cookie\$path
$path
Definition: lib/vendor/symfony/http-foundation/Cookie.php:25
Symfony\Component\HttpFoundation\Cookie
Definition: lib/vendor/symfony/http-foundation/Cookie.php:19
Symfony\Component\HttpFoundation\Cookie\isSecure
isSecure()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:247
Symfony\Component\HttpFoundation\Cookie\getExpiresTime
getExpiresTime()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:217
Symfony\Component\HttpFoundation\Cookie\$name
$name
Definition: lib/vendor/symfony/http-foundation/Cookie.php:21
Symfony\Component\HttpFoundation\Cookie\getValue
getValue()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:197
Symfony\Component\HttpFoundation\Cookie\getName
getName()
Definition: lib/vendor/symfony/http-foundation/Cookie.php:187
Symfony\Component\HttpFoundation\Cookie\SAMESITE_STRICT
const SAMESITE_STRICT
Definition: lib/vendor/symfony/http-foundation/Cookie.php:32