No Description

Cookie.php 4.9KB

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