comparison vendor/symfony/http-foundation/Cookie.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 7a779792577d
children c2387f117808
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
30 30
31 const SAMESITE_LAX = 'lax'; 31 const SAMESITE_LAX = 'lax';
32 const SAMESITE_STRICT = 'strict'; 32 const SAMESITE_STRICT = 'strict';
33 33
34 /** 34 /**
35 * Constructor. 35 * Creates cookie from raw header string.
36 * 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 /**
37 * @param string $name The name of the cookie 84 * @param string $name The name of the cookie
38 * @param string $value The value of the cookie 85 * @param string|null $value The value of the cookie
39 * @param int|string|\DateTimeInterface $expire The time the cookie expires 86 * @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 87 * @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 88 * @param string|null $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 89 * @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 90 * @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 91 * @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 92 * @param string|null $sameSite Whether the cookie will be available for cross-site requests
46 * 93 *
96 public function __toString() 143 public function __toString()
97 { 144 {
98 $str = ($this->isRaw() ? $this->getName() : urlencode($this->getName())).'='; 145 $str = ($this->isRaw() ? $this->getName() : urlencode($this->getName())).'=';
99 146
100 if ('' === (string) $this->getValue()) { 147 if ('' === (string) $this->getValue()) {
101 $str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001); 148 $str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; max-age=-31536001';
102 } else { 149 } else {
103 $str .= $this->isRaw() ? $this->getValue() : rawurlencode($this->getValue()); 150 $str .= $this->isRaw() ? $this->getValue() : rawurlencode($this->getValue());
104 151
105 if (0 !== $this->getExpiresTime()) { 152 if (0 !== $this->getExpiresTime()) {
106 $str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime()); 153 $str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime()).'; max-age='.$this->getMaxAge();
107 } 154 }
108 } 155 }
109 156
110 if ($this->getPath()) { 157 if ($this->getPath()) {
111 $str .= '; path='.$this->getPath(); 158 $str .= '; path='.$this->getPath();
169 { 216 {
170 return $this->expire; 217 return $this->expire;
171 } 218 }
172 219
173 /** 220 /**
221 * Gets the max-age attribute.
222 *
223 * @return int
224 */
225 public function getMaxAge()
226 {
227 return 0 !== $this->expire ? $this->expire - time() : 0;
228 }
229
230 /**
174 * Gets the path on the server in which the cookie will be available on. 231 * Gets the path on the server in which the cookie will be available on.
175 * 232 *
176 * @return string 233 * @return string
177 */ 234 */
178 public function getPath() 235 public function getPath()