Cookie.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. * @api
  17. */
  18. class Cookie
  19. {
  20. protected $name;
  21. protected $value;
  22. protected $domain;
  23. protected $expire;
  24. protected $path;
  25. protected $secure;
  26. protected $httpOnly;
  27. /**
  28. * Constructor.
  29. *
  30. * @param string $name The name of the cookie
  31. * @param string $value The value of the cookie
  32. * @param integer|string|\DateTime $expire The time the cookie expires
  33. * @param string $path The path on the server in which the cookie will be available on
  34. * @param string $domain The domain that the cookie is available to
  35. * @param Boolean $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client
  36. * @param Boolean $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
  37. *
  38. * @api
  39. */
  40. public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true)
  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 \DateTime) {
  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 = (Boolean) $secure;
  64. $this->httpOnly = (Boolean) $httpOnly;
  65. }
  66. public function __toString()
  67. {
  68. $str = urlencode($this->getName()).'=';
  69. if ('' === (string) $this->getValue()) {
  70. $str .= 'deleted; expires='.gmdate("D, d-M-Y H:i:s T", time() - 31536001);
  71. } else {
  72. $str .= urlencode($this->getValue());
  73. if ($this->getExpiresTime() !== 0) {
  74. $str .= '; expires='.gmdate("D, d-M-Y H:i:s T", $this->getExpiresTime());
  75. }
  76. }
  77. if ('/' !== $this->path) {
  78. $str .= '; path='.$this->path;
  79. }
  80. if (null !== $this->getDomain()) {
  81. $str .= '; domain='.$this->getDomain();
  82. }
  83. if (true === $this->isSecure()) {
  84. $str .= '; secure';
  85. }
  86. if (true === $this->isHttpOnly()) {
  87. $str .= '; httponly';
  88. }
  89. return $str;
  90. }
  91. /**
  92. * Gets the name of the cookie.
  93. *
  94. * @return string
  95. *
  96. * @api
  97. */
  98. public function getName()
  99. {
  100. return $this->name;
  101. }
  102. /**
  103. * Gets the value of the cookie.
  104. *
  105. * @return string
  106. *
  107. * @api
  108. */
  109. public function getValue()
  110. {
  111. return $this->value;
  112. }
  113. /**
  114. * Gets the domain that the cookie is available to.
  115. *
  116. * @return string
  117. *
  118. * @api
  119. */
  120. public function getDomain()
  121. {
  122. return $this->domain;
  123. }
  124. /**
  125. * Gets the time the cookie expires.
  126. *
  127. * @return integer
  128. *
  129. * @api
  130. */
  131. public function getExpiresTime()
  132. {
  133. return $this->expire;
  134. }
  135. /**
  136. * Gets the path on the server in which the cookie will be available on.
  137. *
  138. * @return string
  139. *
  140. * @api
  141. */
  142. public function getPath()
  143. {
  144. return $this->path;
  145. }
  146. /**
  147. * Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
  148. *
  149. * @return Boolean
  150. *
  151. * @api
  152. */
  153. public function isSecure()
  154. {
  155. return $this->secure;
  156. }
  157. /**
  158. * Checks whether the cookie will be made accessible only through the HTTP protocol.
  159. *
  160. * @return Boolean
  161. *
  162. * @api
  163. */
  164. public function isHttpOnly()
  165. {
  166. return $this->httpOnly;
  167. }
  168. /**
  169. * Whether this cookie is about to be cleared
  170. *
  171. * @return Boolean
  172. *
  173. * @api
  174. */
  175. public function isCleared()
  176. {
  177. return $this->expire < time();
  178. }
  179. }