4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\BrowserKit;
15 * Cookie represents an HTTP cookie.
17 * @author Fabien Potencier <fabien@symfony.com>
22 * Handles dates as defined by RFC 2616 section 3.3.1, and also some other
23 * non-standard, but common formats.
27 private static $dateFormats = array(
49 * @param string $name The cookie name
50 * @param string $value The value of the cookie
51 * @param string $expires The time the cookie expires
52 * @param string $path The path on the server in which the cookie will be available on
53 * @param string $domain The domain that the cookie is available
54 * @param bool $secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client
55 * @param bool $httponly The cookie httponly flag
56 * @param bool $encodedValue Whether the value is encoded or not
58 public function __construct($name, $value, $expires = null, $path = null, $domain = '', $secure = false, $httponly = true, $encodedValue = false)
61 $this->value = urldecode($value);
62 $this->rawValue = $value;
64 $this->value = $value;
65 $this->rawValue = urlencode($value);
68 $this->path = empty($path) ? '/' : $path;
69 $this->domain = $domain;
70 $this->secure = (bool) $secure;
71 $this->httponly = (bool) $httponly;
73 if (null !== $expires) {
74 $timestampAsDateTime = \DateTime::createFromFormat('U', $expires);
75 if (false === $timestampAsDateTime) {
76 throw new \UnexpectedValueException(sprintf('The cookie expiration time "%s" is not valid.', $expires));
79 $this->expires = $timestampAsDateTime->format('U');
84 * Returns the HTTP representation of the Cookie.
86 * @return string The HTTP representation of the Cookie
88 * @throws \UnexpectedValueException
90 public function __toString()
92 $cookie = sprintf('%s=%s', $this->name, $this->rawValue);
94 if (null !== $this->expires) {
95 $dateTime = \DateTime::createFromFormat('U', $this->expires, new \DateTimeZone('GMT'));
96 $cookie .= '; expires='.str_replace('+0000', '', $dateTime->format(self::$dateFormats[0]));
99 if ('' !== $this->domain) {
100 $cookie .= '; domain='.$this->domain;
104 $cookie .= '; path='.$this->path;
108 $cookie .= '; secure';
111 if ($this->httponly) {
112 $cookie .= '; httponly';
119 * Creates a Cookie instance from a Set-Cookie header value.
121 * @param string $cookie A Set-Cookie header value
122 * @param string $url The base URL
126 * @throws \InvalidArgumentException
128 public static function fromString($cookie, $url = null)
130 $parts = explode(';', $cookie);
132 if (false === strpos($parts[0], '=')) {
133 throw new \InvalidArgumentException(sprintf('The cookie string "%s" is not valid.', $parts[0]));
136 list($name, $value) = explode('=', array_shift($parts), 2);
139 'name' => trim($name),
140 'value' => trim($value),
146 'passedRawValue' => true,
150 if ((false === $urlParts = parse_url($url)) || !isset($urlParts['host'])) {
151 throw new \InvalidArgumentException(sprintf('The URL "%s" is not valid.', $url));
154 $values['domain'] = $urlParts['host'];
155 $values['path'] = isset($urlParts['path']) ? substr($urlParts['path'], 0, strrpos($urlParts['path'], '/')) : '';
158 foreach ($parts as $part) {
161 if ('secure' === strtolower($part)) {
162 // Ignore the secure flag if the original URI is not given or is not HTTPS
163 if (!$url || !isset($urlParts['scheme']) || 'https' != $urlParts['scheme']) {
167 $values['secure'] = true;
172 if ('httponly' === strtolower($part)) {
173 $values['httponly'] = true;
178 if (2 === count($elements = explode('=', $part, 2))) {
179 if ('expires' === strtolower($elements[0])) {
180 $elements[1] = self::parseDate($elements[1]);
183 $values[strtolower($elements[0])] = $elements[1];
195 $values['passedRawValue']
199 private static function parseDate($dateValue)
201 // trim single quotes around date if present
202 if (($length = strlen($dateValue)) > 1 && "'" === $dateValue[0] && "'" === $dateValue[$length - 1]) {
203 $dateValue = substr($dateValue, 1, -1);
206 foreach (self::$dateFormats as $dateFormat) {
207 if (false !== $date = \DateTime::createFromFormat($dateFormat, $dateValue, new \DateTimeZone('GMT'))) {
208 return $date->format('U');
212 // attempt a fallback for unusual formatting
213 if (false !== $date = date_create($dateValue, new \DateTimeZone('GMT'))) {
214 return $date->format('U');
219 * Gets the name of the cookie.
221 * @return string The cookie name
223 public function getName()
229 * Gets the value of the cookie.
231 * @return string The cookie value
233 public function getValue()
239 * Gets the raw value of the cookie.
241 * @return string The cookie value
243 public function getRawValue()
245 return $this->rawValue;
249 * Gets the expires time of the cookie.
251 * @return string The cookie expires time
253 public function getExpiresTime()
255 return $this->expires;
259 * Gets the path of the cookie.
261 * @return string The cookie path
263 public function getPath()
269 * Gets the domain of the cookie.
271 * @return string The cookie domain
273 public function getDomain()
275 return $this->domain;
279 * Returns the secure flag of the cookie.
281 * @return bool The cookie secure flag
283 public function isSecure()
285 return $this->secure;
289 * Returns the httponly flag of the cookie.
291 * @return bool The cookie httponly flag
293 public function isHttpOnly()
295 return $this->httponly;
299 * Returns true if the cookie has expired.
301 * @return bool true if the cookie has expired, false otherwise
303 public function isExpired()
305 return null !== $this->expires && 0 != $this->expires && $this->expires < time();