/vendor/symfony/src/Symfony/Component/BrowserKit/Cookie.php

https://github.com/nguyennamtien/TaskBoxx · PHP · 287 lines · 142 code · 36 blank · 109 comment · 18 complexity · c118336d2f8c3825104d8ab6d56ca871 MD5 · raw file

  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\BrowserKit;
  11. /**
  12. * Cookie represents an HTTP cookie.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. *
  16. * @api
  17. */
  18. class Cookie
  19. {
  20. const DATE_FORMAT = 'D, d-M-Y H:i:s T';
  21. protected $name;
  22. protected $value;
  23. protected $expires;
  24. protected $path;
  25. protected $domain;
  26. protected $secure;
  27. protected $httponly;
  28. protected $rawValue;
  29. /**
  30. * Sets a cookie.
  31. *
  32. * @param string $name The cookie name
  33. * @param string $value The value of the cookie
  34. * @param string $expires The time the cookie expires
  35. * @param string $path The path on the server in which the cookie will be available on
  36. * @param string $domain The domain that the cookie is available
  37. * @param Boolean $secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client
  38. * @param Boolean $httponly The cookie httponly flag
  39. * @param Boolean $encodedValue Whether the value is encoded or not
  40. *
  41. * @api
  42. */
  43. public function __construct($name, $value, $expires = null, $path = null, $domain = '', $secure = false, $httponly = true, $encodedValue = false)
  44. {
  45. if ($encodedValue) {
  46. $this->value = urldecode($value);
  47. $this->rawValue = $value;
  48. } else {
  49. $this->value = $value;
  50. $this->rawValue = urlencode($value);
  51. }
  52. $this->name = $name;
  53. $this->expires = null === $expires ? null : (integer) $expires;
  54. $this->path = empty($path) ? null : $path;
  55. $this->domain = $domain;
  56. $this->secure = (Boolean) $secure;
  57. $this->httponly = (Boolean) $httponly;
  58. }
  59. /**
  60. * Returns the HTTP representation of the Cookie.
  61. *
  62. * @return string The HTTP representation of the Cookie
  63. *
  64. * @api
  65. */
  66. public function __toString()
  67. {
  68. $cookie = sprintf('%s=%s', $this->name, $this->rawValue);
  69. if (null !== $this->expires) {
  70. $cookie .= '; expires='.substr(\DateTime::createFromFormat('U', $this->expires, new \DateTimeZone('UTC'))->format(static::DATE_FORMAT), 0, -5);
  71. }
  72. if ('' !== $this->domain) {
  73. $cookie .= '; domain='.$this->domain;
  74. }
  75. if (null !== $this->path) {
  76. $cookie .= '; path='.$this->path;
  77. }
  78. if ($this->secure) {
  79. $cookie .= '; secure';
  80. }
  81. if ($this->httponly) {
  82. $cookie .= '; httponly';
  83. }
  84. return $cookie;
  85. }
  86. /**
  87. * Creates a Cookie instance from a Set-Cookie header value.
  88. *
  89. * @param string $cookie A Set-Cookie header value
  90. * @param string $url The base URL
  91. *
  92. * @return Cookie A Cookie instance
  93. *
  94. * @api
  95. */
  96. static public function fromString($cookie, $url = null)
  97. {
  98. $parts = explode(';', $cookie);
  99. if (false === strpos($parts[0], '=')) {
  100. throw new \InvalidArgumentException('The cookie string "%s" is not valid.');
  101. }
  102. list($name, $value) = explode('=', array_shift($parts), 2);
  103. $values = array(
  104. 'name' => trim($name),
  105. 'value' => trim($value),
  106. 'expires' => null,
  107. 'path' => null,
  108. 'domain' => '',
  109. 'secure' => false,
  110. 'httponly' => false,
  111. 'passedRawValue' => true,
  112. );
  113. if (null !== $url) {
  114. if ((false === $urlParts = parse_url($url)) || !isset($urlParts['host']) || !isset($urlParts['path'])) {
  115. throw new \InvalidArgumentException(sprintf('The URL "%s" is not valid.', $url));
  116. }
  117. $parts = array_merge($urlParts, $parts);
  118. $values['domain'] = $parts['host'];
  119. $values['path'] = substr($parts['path'], 0, strrpos($parts['path'], '/'));
  120. }
  121. foreach ($parts as $part) {
  122. $part = trim($part);
  123. if ('secure' === strtolower($part)) {
  124. $values['secure'] = true;
  125. continue;
  126. }
  127. if ('httponly' === strtolower($part)) {
  128. $values['httponly'] = true;
  129. continue;
  130. }
  131. if (2 === count($elements = explode('=', $part, 2))) {
  132. if ('expires' === $elements[0]) {
  133. if (false === $date = \DateTime::createFromFormat(static::DATE_FORMAT, $elements[1], new \DateTimeZone('UTC'))) {
  134. throw new \InvalidArgumentException(sprintf('The expires part of cookie is not valid (%s).', $elements[1]));
  135. }
  136. $elements[1] = $date->getTimestamp();
  137. }
  138. $values[strtolower($elements[0])] = $elements[1];
  139. }
  140. }
  141. return new static(
  142. $values['name'],
  143. $values['value'],
  144. $values['expires'],
  145. $values['path'],
  146. $values['domain'],
  147. $values['secure'],
  148. $values['httponly'],
  149. $values['passedRawValue']
  150. );
  151. }
  152. /**
  153. * Gets the name of the cookie.
  154. *
  155. * @return string The cookie name
  156. *
  157. * @api
  158. */
  159. public function getName()
  160. {
  161. return $this->name;
  162. }
  163. /**
  164. * Gets the value of the cookie.
  165. *
  166. * @return string The cookie value
  167. *
  168. * @api
  169. */
  170. public function getValue()
  171. {
  172. return $this->value;
  173. }
  174. /**
  175. * Gets the raw value of the cookie.
  176. *
  177. * @return string The cookie value
  178. *
  179. * @api
  180. */
  181. public function getRawValue()
  182. {
  183. return $this->rawValue;
  184. }
  185. /**
  186. * Gets the expires time of the cookie.
  187. *
  188. * @return string The cookie expires time
  189. *
  190. * @api
  191. */
  192. public function getExpiresTime()
  193. {
  194. return $this->expires;
  195. }
  196. /**
  197. * Gets the path of the cookie.
  198. *
  199. * @return string The cookie path
  200. *
  201. * @api
  202. */
  203. public function getPath()
  204. {
  205. return null !== $this->path ? $this->path : '/';
  206. }
  207. /**
  208. * Gets the domain of the cookie.
  209. *
  210. * @return string The cookie domain
  211. *
  212. * @api
  213. */
  214. public function getDomain()
  215. {
  216. return $this->domain;
  217. }
  218. /**
  219. * Returns the secure flag of the cookie.
  220. *
  221. * @return Boolean The cookie secure flag
  222. *
  223. * @api
  224. */
  225. public function isSecure()
  226. {
  227. return $this->secure;
  228. }
  229. /**
  230. * Returns the httponly flag of the cookie.
  231. *
  232. * @return Boolean The cookie httponly flag
  233. *
  234. * @api
  235. */
  236. public function isHttpOnly()
  237. {
  238. return $this->httponly;
  239. }
  240. /**
  241. * Returns true if the cookie has expired.
  242. *
  243. * @return Boolean true if the cookie has expired, false otherwise
  244. *
  245. * @api
  246. */
  247. public function isExpired()
  248. {
  249. return (null !== $this->expires) && $this->expires < time();
  250. }
  251. }