Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Cookie.php 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. private $sameSite;
  27. private $secureDefault = false;
  28. const SAMESITE_NONE = 'none';
  29. const SAMESITE_LAX = 'lax';
  30. const SAMESITE_STRICT = 'strict';
  31. /**
  32. * Creates cookie from raw header string.
  33. *
  34. * @param string $cookie
  35. * @param bool $decode
  36. *
  37. * @return static
  38. */
  39. public static function fromString($cookie, $decode = false)
  40. {
  41. $data = [
  42. 'expires' => 0,
  43. 'path' => '/',
  44. 'domain' => null,
  45. 'secure' => false,
  46. 'httponly' => false,
  47. 'raw' => !$decode,
  48. 'samesite' => null,
  49. ];
  50. $parts = HeaderUtils::split($cookie, ';=');
  51. $part = array_shift($parts);
  52. $name = $decode ? urldecode($part[0]) : $part[0];
  53. $value = isset($part[1]) ? ($decode ? urldecode($part[1]) : $part[1]) : null;
  54. $data = HeaderUtils::combine($parts) + $data;
  55. if (isset($data['max-age'])) {
  56. $data['expires'] = time() + (int) $data['max-age'];
  57. }
  58. return new static($name, $value, $data['expires'], $data['path'], $data['domain'], $data['secure'], $data['httponly'], $data['raw'], $data['samesite']);
  59. }
  60. public static function create(string $name, string $value = null, $expire = 0, ?string $path = '/', string $domain = null, bool $secure = null, bool $httpOnly = true, bool $raw = false, ?string $sameSite = self::SAMESITE_LAX): self
  61. {
  62. return new self($name, $value, $expire, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
  63. }
  64. /**
  65. * @param string $name The name of the cookie
  66. * @param string|null $value The value of the cookie
  67. * @param int|string|\DateTimeInterface $expire The time the cookie expires
  68. * @param string $path The path on the server in which the cookie will be available on
  69. * @param string|null $domain The domain that the cookie is available to
  70. * @param bool|null $secure Whether the client should send back the cookie only over HTTPS or null to auto-enable this when the request is already using HTTPS
  71. * @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
  72. * @param bool $raw Whether the cookie value should be sent with no url encoding
  73. * @param string|null $sameSite Whether the cookie will be available for cross-site requests
  74. *
  75. * @throws \InvalidArgumentException
  76. */
  77. public function __construct(string $name, string $value = null, $expire = 0, ?string $path = '/', string $domain = null, ?bool $secure = false, bool $httpOnly = true, bool $raw = false, string $sameSite = null)
  78. {
  79. if (9 > \func_num_args()) {
  80. @trigger_error(sprintf('The default value of the "$secure" and "$samesite" arguments of "%s"\'s constructor will respectively change from "false" to "null" and from "null" to "lax" in Symfony 5.0, you should define their values explicitly or use "Cookie::create()" instead.', __METHOD__), E_USER_DEPRECATED);
  81. }
  82. // from PHP source code
  83. if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
  84. throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
  85. }
  86. if (empty($name)) {
  87. throw new \InvalidArgumentException('The cookie name cannot be empty.');
  88. }
  89. // convert expiration time to a Unix timestamp
  90. if ($expire instanceof \DateTimeInterface) {
  91. $expire = $expire->format('U');
  92. } elseif (!is_numeric($expire)) {
  93. $expire = strtotime($expire);
  94. if (false === $expire) {
  95. throw new \InvalidArgumentException('The cookie expiration time is not valid.');
  96. }
  97. }
  98. $this->name = $name;
  99. $this->value = $value;
  100. $this->domain = $domain;
  101. $this->expire = 0 < $expire ? (int) $expire : 0;
  102. $this->path = empty($path) ? '/' : $path;
  103. $this->secure = $secure;
  104. $this->httpOnly = $httpOnly;
  105. $this->raw = $raw;
  106. if ('' === $sameSite) {
  107. $sameSite = null;
  108. } elseif (null !== $sameSite) {
  109. $sameSite = strtolower($sameSite);
  110. }
  111. if (!\in_array($sameSite, [self::SAMESITE_LAX, self::SAMESITE_STRICT, self::SAMESITE_NONE, null], true)) {
  112. throw new \InvalidArgumentException('The "sameSite" parameter value is not valid.');
  113. }
  114. $this->sameSite = $sameSite;
  115. }
  116. /**
  117. * Returns the cookie as a string.
  118. *
  119. * @return string The cookie
  120. */
  121. public function __toString()
  122. {
  123. $str = ($this->isRaw() ? $this->getName() : urlencode($this->getName())).'=';
  124. if ('' === (string) $this->getValue()) {
  125. $str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; Max-Age=0';
  126. } else {
  127. $str .= $this->isRaw() ? $this->getValue() : rawurlencode($this->getValue());
  128. if (0 !== $this->getExpiresTime()) {
  129. $str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime()).'; Max-Age='.$this->getMaxAge();
  130. }
  131. }
  132. if ($this->getPath()) {
  133. $str .= '; path='.$this->getPath();
  134. }
  135. if ($this->getDomain()) {
  136. $str .= '; domain='.$this->getDomain();
  137. }
  138. if (true === $this->isSecure()) {
  139. $str .= '; secure';
  140. }
  141. if (true === $this->isHttpOnly()) {
  142. $str .= '; httponly';
  143. }
  144. if (null !== $this->getSameSite()) {
  145. $str .= '; samesite='.$this->getSameSite();
  146. }
  147. return $str;
  148. }
  149. /**
  150. * Gets the name of the cookie.
  151. *
  152. * @return string
  153. */
  154. public function getName()
  155. {
  156. return $this->name;
  157. }
  158. /**
  159. * Gets the value of the cookie.
  160. *
  161. * @return string|null
  162. */
  163. public function getValue()
  164. {
  165. return $this->value;
  166. }
  167. /**
  168. * Gets the domain that the cookie is available to.
  169. *
  170. * @return string|null
  171. */
  172. public function getDomain()
  173. {
  174. return $this->domain;
  175. }
  176. /**
  177. * Gets the time the cookie expires.
  178. *
  179. * @return int
  180. */
  181. public function getExpiresTime()
  182. {
  183. return $this->expire;
  184. }
  185. /**
  186. * Gets the max-age attribute.
  187. *
  188. * @return int
  189. */
  190. public function getMaxAge()
  191. {
  192. $maxAge = $this->expire - time();
  193. return 0 >= $maxAge ? 0 : $maxAge;
  194. }
  195. /**
  196. * Gets the path on the server in which the cookie will be available on.
  197. *
  198. * @return string
  199. */
  200. public function getPath()
  201. {
  202. return $this->path;
  203. }
  204. /**
  205. * Checks whether the cookie should only be transmitted over a secure HTTPS connection from the client.
  206. *
  207. * @return bool
  208. */
  209. public function isSecure()
  210. {
  211. return $this->secure ?? $this->secureDefault;
  212. }
  213. /**
  214. * Checks whether the cookie will be made accessible only through the HTTP protocol.
  215. *
  216. * @return bool
  217. */
  218. public function isHttpOnly()
  219. {
  220. return $this->httpOnly;
  221. }
  222. /**
  223. * Whether this cookie is about to be cleared.
  224. *
  225. * @return bool
  226. */
  227. public function isCleared()
  228. {
  229. return 0 !== $this->expire && $this->expire < time();
  230. }
  231. /**
  232. * Checks if the cookie value should be sent with no url encoding.
  233. *
  234. * @return bool
  235. */
  236. public function isRaw()
  237. {
  238. return $this->raw;
  239. }
  240. /**
  241. * Gets the SameSite attribute.
  242. *
  243. * @return string|null
  244. */
  245. public function getSameSite()
  246. {
  247. return $this->sameSite;
  248. }
  249. /**
  250. * @param bool $default The default value of the "secure" flag when it is set to null
  251. */
  252. public function setSecureDefault(bool $default): void
  253. {
  254. $this->secureDefault = $default;
  255. }
  256. }