annotate vendor/symfony/http-foundation/Cookie.php @ 12:7a779792577d

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