Cookie.php 4.9 KB

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