/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/Cookie.php

https://gitlab.com/pr0055/symfonypizza · PHP · 203 lines · 99 code · 25 blank · 79 comment · 12 complexity · 2f23b7e90a6a2f2f9dd1a226876146a0 MD5 · raw file

  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. /**
  27. * Constructor.
  28. *
  29. * @param string $name The name of the cookie
  30. * @param string $value The value of the cookie
  31. * @param int|string|\DateTimeInterface $expire The time the cookie expires
  32. * @param string $path The path on the server in which the cookie will be available on
  33. * @param string $domain The domain that the cookie is available to
  34. * @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client
  35. * @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
  36. * @param bool $raw Whether the cookie value should be sent with no url encoding
  37. *
  38. * @throws \InvalidArgumentException
  39. */
  40. public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true, $raw = false)
  41. {
  42. // from PHP source code
  43. if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
  44. throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
  45. }
  46. if (empty($name)) {
  47. throw new \InvalidArgumentException('The cookie name cannot be empty.');
  48. }
  49. // convert expiration time to a Unix timestamp
  50. if ($expire instanceof \DateTimeInterface) {
  51. $expire = $expire->format('U');
  52. } elseif (!is_numeric($expire)) {
  53. $expire = strtotime($expire);
  54. if (false === $expire || -1 === $expire) {
  55. throw new \InvalidArgumentException('The cookie expiration time is not valid.');
  56. }
  57. }
  58. $this->name = $name;
  59. $this->value = $value;
  60. $this->domain = $domain;
  61. $this->expire = $expire;
  62. $this->path = empty($path) ? '/' : $path;
  63. $this->secure = (bool) $secure;
  64. $this->httpOnly = (bool) $httpOnly;
  65. $this->raw = (bool) $raw;
  66. }
  67. /**
  68. * Returns the cookie as a string.
  69. *
  70. * @return string The cookie
  71. */
  72. public function __toString()
  73. {
  74. $str = urlencode($this->getName()).'=';
  75. if ('' === (string) $this->getValue()) {
  76. $str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001);
  77. } else {
  78. $str .= urlencode($this->getValue());
  79. if ($this->getExpiresTime() !== 0) {
  80. $str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime());
  81. }
  82. }
  83. if ($this->path) {
  84. $str .= '; path='.$this->path;
  85. }
  86. if ($this->getDomain()) {
  87. $str .= '; domain='.$this->getDomain();
  88. }
  89. if (true === $this->isSecure()) {
  90. $str .= '; secure';
  91. }
  92. if (true === $this->isHttpOnly()) {
  93. $str .= '; httponly';
  94. }
  95. return $str;
  96. }
  97. /**
  98. * Gets the name of the cookie.
  99. *
  100. * @return string
  101. */
  102. public function getName()
  103. {
  104. return $this->name;
  105. }
  106. /**
  107. * Gets the value of the cookie.
  108. *
  109. * @return string
  110. */
  111. public function getValue()
  112. {
  113. return $this->value;
  114. }
  115. /**
  116. * Gets the domain that the cookie is available to.
  117. *
  118. * @return string
  119. */
  120. public function getDomain()
  121. {
  122. return $this->domain;
  123. }
  124. /**
  125. * Gets the time the cookie expires.
  126. *
  127. * @return int
  128. */
  129. public function getExpiresTime()
  130. {
  131. return $this->expire;
  132. }
  133. /**
  134. * Gets the path on the server in which the cookie will be available on.
  135. *
  136. * @return string
  137. */
  138. public function getPath()
  139. {
  140. return $this->path;
  141. }
  142. /**
  143. * Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
  144. *
  145. * @return bool
  146. */
  147. public function isSecure()
  148. {
  149. return $this->secure;
  150. }
  151. /**
  152. * Checks whether the cookie will be made accessible only through the HTTP protocol.
  153. *
  154. * @return bool
  155. */
  156. public function isHttpOnly()
  157. {
  158. return $this->httpOnly;
  159. }
  160. /**
  161. * Whether this cookie is about to be cleared.
  162. *
  163. * @return bool
  164. */
  165. public function isCleared()
  166. {
  167. return $this->expire < time();
  168. }
  169. /**
  170. * Checks if the cookie value should be sent with no url encoding.
  171. *
  172. * @return bool
  173. */
  174. public function isRaw()
  175. {
  176. return $this->raw;
  177. }
  178. }