Cookie.php 6.0 KB

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