PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Buzz/Cookie/Cookie.php

http://github.com/kriswallsmith/Buzz
PHP | 214 lines | 137 code | 33 blank | 44 comment | 18 complexity | 59a44ee45493285da732aa17214acd18 MD5 | raw file
  1. <?php
  2. namespace Buzz\Cookie;
  3. use Buzz\Message;
  4. class Cookie
  5. {
  6. const ATTR_DOMAIN = 'domain';
  7. const ATTR_PATH = 'path';
  8. const ATTR_SECURE = 'secure';
  9. const ATTR_MAX_AGE = 'max-age';
  10. const ATTR_EXPIRES = 'expires';
  11. protected $name;
  12. protected $value;
  13. protected $attributes = array();
  14. protected $createdAt;
  15. /**
  16. * Constructor.
  17. */
  18. public function __construct()
  19. {
  20. $this->createdAt = time();
  21. }
  22. /**
  23. * Returns true if the current cookie matches the supplied request.
  24. *
  25. * @return boolean
  26. */
  27. public function matchesRequest(Message\Request $request)
  28. {
  29. // domain
  30. if (!$this->matchesDomain(parse_url($request->getHost(), PHP_URL_HOST))) {
  31. return false;
  32. }
  33. // path
  34. if (!$this->matchesPath($request->getResource())) {
  35. return false;
  36. }
  37. // secure
  38. if ($this->hasAttribute(static::ATTR_SECURE) && !$request->isSecure()) {
  39. return false;
  40. }
  41. return true;
  42. }
  43. /**
  44. * Returns true of the current cookie has expired.
  45. *
  46. * Checks the max-age and expires attributes.
  47. *
  48. * @return boolean Whether the current cookie has expired
  49. */
  50. public function isExpired()
  51. {
  52. $maxAge = $this->getAttribute(static::ATTR_MAX_AGE);
  53. if ($maxAge && time() - $this->getCreatedAt() > $maxAge) {
  54. return true;
  55. }
  56. $expires = $this->getAttribute(static::ATTR_EXPIRES);
  57. if ($expires && strtotime($expires) < time()) {
  58. return true;
  59. }
  60. return false;
  61. }
  62. /**
  63. * Returns true if the current cookie matches the supplied domain.
  64. *
  65. * @param string $domain A domain hostname
  66. *
  67. * @return boolean
  68. */
  69. public function matchesDomain($domain)
  70. {
  71. $cookieDomain = $this->getAttribute(static::ATTR_DOMAIN);
  72. if (0 === strpos($cookieDomain, '.')) {
  73. $pattern = '/\b'.preg_quote(substr($cookieDomain, 1), '/').'$/i';
  74. return (boolean) preg_match($pattern, $domain);
  75. } else {
  76. return 0 == strcasecmp($cookieDomain, $domain);
  77. }
  78. }
  79. /**
  80. * Returns true if the current cookie matches the supplied path.
  81. *
  82. * @param string $path A path
  83. *
  84. * @return boolean
  85. */
  86. public function matchesPath($path)
  87. {
  88. $needle = $this->getAttribute(static::ATTR_PATH);
  89. return null === $needle || 0 === strpos($path, $needle);
  90. }
  91. /**
  92. * Populates the current cookie with data from the supplied Set-Cookie header.
  93. *
  94. * @param string $header A Set-Cookie header
  95. * @param string $issuingDomain The domain that issued the header
  96. */
  97. public function fromSetCookieHeader($header, $issuingDomain)
  98. {
  99. list($this->name, $header) = explode('=', $header, 2);
  100. if (false === strpos($header, ';')) {
  101. $this->value = $header;
  102. $header = null;
  103. } else {
  104. list($this->value, $header) = explode(';', $header, 2);
  105. }
  106. $this->clearAttributes();
  107. foreach (array_map('trim', explode(';', trim($header))) as $pair) {
  108. if (false === strpos($pair, '=')) {
  109. $name = $pair;
  110. $value = null;
  111. } else {
  112. list($name, $value) = explode('=', $pair);
  113. }
  114. $this->setAttribute($name, $value);
  115. }
  116. if (!$this->getAttribute(static::ATTR_DOMAIN)) {
  117. $this->setAttribute(static::ATTR_DOMAIN, $issuingDomain);
  118. }
  119. }
  120. /**
  121. * Formats a Cookie header for the current cookie.
  122. *
  123. * @return string An HTTP request Cookie header
  124. */
  125. public function toCookieHeader()
  126. {
  127. return 'Cookie: '.$this->getName().'='.$this->getValue();
  128. }
  129. public function setName($name)
  130. {
  131. $this->name = $name;
  132. }
  133. public function getName()
  134. {
  135. return $this->name;
  136. }
  137. public function setValue($value)
  138. {
  139. $this->value = $value;
  140. }
  141. public function getValue()
  142. {
  143. return $this->value;
  144. }
  145. public function setAttributes(array $attributes)
  146. {
  147. // attributes are case insensitive
  148. $this->attributes = array_change_key_case($attributes);
  149. }
  150. public function setAttribute($name, $value)
  151. {
  152. $this->attributes[strtolower($name)] = $value;
  153. }
  154. public function getAttributes()
  155. {
  156. return $this->attributes;
  157. }
  158. public function getAttribute($name)
  159. {
  160. $name = strtolower($name);
  161. if (isset($this->attributes[$name])) {
  162. return $this->attributes[$name];
  163. }
  164. }
  165. public function hasAttribute($name)
  166. {
  167. return array_key_exists($name, $this->attributes);
  168. }
  169. public function clearAttributes()
  170. {
  171. $this->setAttributes(array());
  172. }
  173. public function setCreatedAt($createdAt)
  174. {
  175. $this->createdAt = $createdAt;
  176. }
  177. public function getCreatedAt()
  178. {
  179. return $this->createdAt;
  180. }
  181. }