Cookie.php 4.9 KB

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