Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\BrowserKit; Chris@0: Chris@0: /** Chris@0: * Cookie represents an HTTP cookie. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class Cookie Chris@0: { Chris@0: /** Chris@0: * Handles dates as defined by RFC 2616 section 3.3.1, and also some other Chris@0: * non-standard, but common formats. Chris@0: */ Chris@17: private static $dateFormats = [ Chris@0: 'D, d M Y H:i:s T', Chris@0: 'D, d-M-y H:i:s T', Chris@0: 'D, d-M-Y H:i:s T', Chris@0: 'D, d-m-y H:i:s T', Chris@0: 'D, d-m-Y H:i:s T', Chris@0: 'D M j G:i:s Y', Chris@0: 'D M d H:i:s Y T', Chris@17: ]; Chris@0: Chris@0: protected $name; Chris@0: protected $value; Chris@0: protected $expires; Chris@0: protected $path; Chris@0: protected $domain; Chris@0: protected $secure; Chris@0: protected $httponly; Chris@0: protected $rawValue; Chris@0: Chris@0: /** Chris@0: * Sets a cookie. Chris@0: * Chris@13: * @param string $name The cookie name Chris@13: * @param string $value The value of the cookie Chris@13: * @param string|null $expires The time the cookie expires Chris@13: * @param string|null $path The path on the server in which the cookie will be available on Chris@13: * @param string $domain The domain that the cookie is available Chris@13: * @param bool $secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client Chris@13: * @param bool $httponly The cookie httponly flag Chris@13: * @param bool $encodedValue Whether the value is encoded or not Chris@0: */ Chris@0: public function __construct($name, $value, $expires = null, $path = null, $domain = '', $secure = false, $httponly = true, $encodedValue = false) Chris@0: { Chris@0: if ($encodedValue) { Chris@0: $this->value = urldecode($value); Chris@0: $this->rawValue = $value; Chris@0: } else { Chris@0: $this->value = $value; Chris@12: $this->rawValue = rawurlencode($value); Chris@0: } Chris@0: $this->name = $name; Chris@0: $this->path = empty($path) ? '/' : $path; Chris@0: $this->domain = $domain; Chris@0: $this->secure = (bool) $secure; Chris@0: $this->httponly = (bool) $httponly; Chris@0: Chris@0: if (null !== $expires) { Chris@0: $timestampAsDateTime = \DateTime::createFromFormat('U', $expires); Chris@0: if (false === $timestampAsDateTime) { Chris@0: throw new \UnexpectedValueException(sprintf('The cookie expiration time "%s" is not valid.', $expires)); Chris@0: } Chris@0: Chris@0: $this->expires = $timestampAsDateTime->format('U'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the HTTP representation of the Cookie. Chris@0: */ Chris@0: public function __toString() Chris@0: { Chris@0: $cookie = sprintf('%s=%s', $this->name, $this->rawValue); Chris@0: Chris@0: if (null !== $this->expires) { Chris@0: $dateTime = \DateTime::createFromFormat('U', $this->expires, new \DateTimeZone('GMT')); Chris@0: $cookie .= '; expires='.str_replace('+0000', '', $dateTime->format(self::$dateFormats[0])); Chris@0: } Chris@0: Chris@0: if ('' !== $this->domain) { Chris@0: $cookie .= '; domain='.$this->domain; Chris@0: } Chris@0: Chris@0: if ($this->path) { Chris@0: $cookie .= '; path='.$this->path; Chris@0: } Chris@0: Chris@0: if ($this->secure) { Chris@0: $cookie .= '; secure'; Chris@0: } Chris@0: Chris@0: if ($this->httponly) { Chris@0: $cookie .= '; httponly'; Chris@0: } Chris@0: Chris@0: return $cookie; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a Cookie instance from a Set-Cookie header value. Chris@0: * Chris@13: * @param string $cookie A Set-Cookie header value Chris@13: * @param string|null $url The base URL Chris@0: * Chris@0: * @return static Chris@0: * Chris@0: * @throws \InvalidArgumentException Chris@0: */ Chris@0: public static function fromString($cookie, $url = null) Chris@0: { Chris@0: $parts = explode(';', $cookie); Chris@0: Chris@0: if (false === strpos($parts[0], '=')) { Chris@0: throw new \InvalidArgumentException(sprintf('The cookie string "%s" is not valid.', $parts[0])); Chris@0: } Chris@0: Chris@0: list($name, $value) = explode('=', array_shift($parts), 2); Chris@0: Chris@17: $values = [ Chris@0: 'name' => trim($name), Chris@0: 'value' => trim($value), Chris@0: 'expires' => null, Chris@0: 'path' => '/', Chris@0: 'domain' => '', Chris@0: 'secure' => false, Chris@0: 'httponly' => false, Chris@0: 'passedRawValue' => true, Chris@17: ]; Chris@0: Chris@0: if (null !== $url) { Chris@0: if ((false === $urlParts = parse_url($url)) || !isset($urlParts['host'])) { Chris@0: throw new \InvalidArgumentException(sprintf('The URL "%s" is not valid.', $url)); Chris@0: } Chris@0: Chris@0: $values['domain'] = $urlParts['host']; Chris@0: $values['path'] = isset($urlParts['path']) ? substr($urlParts['path'], 0, strrpos($urlParts['path'], '/')) : ''; Chris@0: } Chris@0: Chris@0: foreach ($parts as $part) { Chris@0: $part = trim($part); Chris@0: Chris@0: if ('secure' === strtolower($part)) { Chris@0: // Ignore the secure flag if the original URI is not given or is not HTTPS Chris@0: if (!$url || !isset($urlParts['scheme']) || 'https' != $urlParts['scheme']) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $values['secure'] = true; Chris@0: Chris@0: continue; Chris@0: } Chris@0: Chris@0: if ('httponly' === strtolower($part)) { Chris@0: $values['httponly'] = true; Chris@0: Chris@0: continue; Chris@0: } Chris@0: Chris@17: if (2 === \count($elements = explode('=', $part, 2))) { Chris@0: if ('expires' === strtolower($elements[0])) { Chris@0: $elements[1] = self::parseDate($elements[1]); Chris@0: } Chris@0: Chris@0: $values[strtolower($elements[0])] = $elements[1]; Chris@0: } Chris@0: } Chris@0: Chris@0: return new static( Chris@0: $values['name'], Chris@0: $values['value'], Chris@0: $values['expires'], Chris@0: $values['path'], Chris@0: $values['domain'], Chris@0: $values['secure'], Chris@0: $values['httponly'], Chris@0: $values['passedRawValue'] Chris@0: ); Chris@0: } Chris@0: Chris@0: private static function parseDate($dateValue) Chris@0: { Chris@0: // trim single quotes around date if present Chris@17: if (($length = \strlen($dateValue)) > 1 && "'" === $dateValue[0] && "'" === $dateValue[$length - 1]) { Chris@0: $dateValue = substr($dateValue, 1, -1); Chris@0: } Chris@0: Chris@0: foreach (self::$dateFormats as $dateFormat) { Chris@0: if (false !== $date = \DateTime::createFromFormat($dateFormat, $dateValue, new \DateTimeZone('GMT'))) { Chris@0: return $date->format('U'); Chris@0: } Chris@0: } Chris@0: Chris@0: // attempt a fallback for unusual formatting Chris@0: if (false !== $date = date_create($dateValue, new \DateTimeZone('GMT'))) { Chris@0: return $date->format('U'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the name of the cookie. Chris@0: * Chris@0: * @return string The cookie name Chris@0: */ Chris@0: public function getName() Chris@0: { Chris@0: return $this->name; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the value of the cookie. Chris@0: * Chris@0: * @return string The cookie value Chris@0: */ Chris@0: public function getValue() Chris@0: { Chris@0: return $this->value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the raw value of the cookie. Chris@0: * Chris@0: * @return string The cookie value Chris@0: */ Chris@0: public function getRawValue() Chris@0: { Chris@0: return $this->rawValue; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the expires time of the cookie. Chris@0: * Chris@13: * @return string|null The cookie expires time Chris@0: */ Chris@0: public function getExpiresTime() Chris@0: { Chris@0: return $this->expires; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the path of the cookie. Chris@0: * Chris@0: * @return string The cookie path Chris@0: */ Chris@0: public function getPath() Chris@0: { Chris@0: return $this->path; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the domain of the cookie. Chris@0: * Chris@0: * @return string The cookie domain Chris@0: */ Chris@0: public function getDomain() Chris@0: { Chris@0: return $this->domain; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the secure flag of the cookie. Chris@0: * Chris@0: * @return bool The cookie secure flag Chris@0: */ Chris@0: public function isSecure() Chris@0: { Chris@0: return $this->secure; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the httponly flag of the cookie. Chris@0: * Chris@0: * @return bool The cookie httponly flag Chris@0: */ Chris@0: public function isHttpOnly() Chris@0: { Chris@0: return $this->httponly; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the cookie has expired. Chris@0: * Chris@0: * @return bool true if the cookie has expired, false otherwise Chris@0: */ Chris@0: public function isExpired() Chris@0: { Chris@0: return null !== $this->expires && 0 != $this->expires && $this->expires < time(); Chris@0: } Chris@0: }