annotate vendor/symfony/browser-kit/Cookie.php @ 2:92f882872392

Trusted hosts, + remove migration modules
author Chris Cannam
date Tue, 05 Dec 2017 09:26:43 +0000
parents 4c8ae668cc8c
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\BrowserKit;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Cookie represents an HTTP cookie.
Chris@0 16 *
Chris@0 17 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 18 */
Chris@0 19 class Cookie
Chris@0 20 {
Chris@0 21 /**
Chris@0 22 * Handles dates as defined by RFC 2616 section 3.3.1, and also some other
Chris@0 23 * non-standard, but common formats.
Chris@0 24 *
Chris@0 25 * @var array
Chris@0 26 */
Chris@0 27 private static $dateFormats = array(
Chris@0 28 'D, d M Y H:i:s T',
Chris@0 29 'D, d-M-y H:i:s T',
Chris@0 30 'D, d-M-Y H:i:s T',
Chris@0 31 'D, d-m-y H:i:s T',
Chris@0 32 'D, d-m-Y H:i:s T',
Chris@0 33 'D M j G:i:s Y',
Chris@0 34 'D M d H:i:s Y T',
Chris@0 35 );
Chris@0 36
Chris@0 37 protected $name;
Chris@0 38 protected $value;
Chris@0 39 protected $expires;
Chris@0 40 protected $path;
Chris@0 41 protected $domain;
Chris@0 42 protected $secure;
Chris@0 43 protected $httponly;
Chris@0 44 protected $rawValue;
Chris@0 45
Chris@0 46 /**
Chris@0 47 * Sets a cookie.
Chris@0 48 *
Chris@0 49 * @param string $name The cookie name
Chris@0 50 * @param string $value The value of the cookie
Chris@0 51 * @param string $expires The time the cookie expires
Chris@0 52 * @param string $path The path on the server in which the cookie will be available on
Chris@0 53 * @param string $domain The domain that the cookie is available
Chris@0 54 * @param bool $secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client
Chris@0 55 * @param bool $httponly The cookie httponly flag
Chris@0 56 * @param bool $encodedValue Whether the value is encoded or not
Chris@0 57 */
Chris@0 58 public function __construct($name, $value, $expires = null, $path = null, $domain = '', $secure = false, $httponly = true, $encodedValue = false)
Chris@0 59 {
Chris@0 60 if ($encodedValue) {
Chris@0 61 $this->value = urldecode($value);
Chris@0 62 $this->rawValue = $value;
Chris@0 63 } else {
Chris@0 64 $this->value = $value;
Chris@0 65 $this->rawValue = urlencode($value);
Chris@0 66 }
Chris@0 67 $this->name = $name;
Chris@0 68 $this->path = empty($path) ? '/' : $path;
Chris@0 69 $this->domain = $domain;
Chris@0 70 $this->secure = (bool) $secure;
Chris@0 71 $this->httponly = (bool) $httponly;
Chris@0 72
Chris@0 73 if (null !== $expires) {
Chris@0 74 $timestampAsDateTime = \DateTime::createFromFormat('U', $expires);
Chris@0 75 if (false === $timestampAsDateTime) {
Chris@0 76 throw new \UnexpectedValueException(sprintf('The cookie expiration time "%s" is not valid.', $expires));
Chris@0 77 }
Chris@0 78
Chris@0 79 $this->expires = $timestampAsDateTime->format('U');
Chris@0 80 }
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * Returns the HTTP representation of the Cookie.
Chris@0 85 *
Chris@0 86 * @return string The HTTP representation of the Cookie
Chris@0 87 *
Chris@0 88 * @throws \UnexpectedValueException
Chris@0 89 */
Chris@0 90 public function __toString()
Chris@0 91 {
Chris@0 92 $cookie = sprintf('%s=%s', $this->name, $this->rawValue);
Chris@0 93
Chris@0 94 if (null !== $this->expires) {
Chris@0 95 $dateTime = \DateTime::createFromFormat('U', $this->expires, new \DateTimeZone('GMT'));
Chris@0 96 $cookie .= '; expires='.str_replace('+0000', '', $dateTime->format(self::$dateFormats[0]));
Chris@0 97 }
Chris@0 98
Chris@0 99 if ('' !== $this->domain) {
Chris@0 100 $cookie .= '; domain='.$this->domain;
Chris@0 101 }
Chris@0 102
Chris@0 103 if ($this->path) {
Chris@0 104 $cookie .= '; path='.$this->path;
Chris@0 105 }
Chris@0 106
Chris@0 107 if ($this->secure) {
Chris@0 108 $cookie .= '; secure';
Chris@0 109 }
Chris@0 110
Chris@0 111 if ($this->httponly) {
Chris@0 112 $cookie .= '; httponly';
Chris@0 113 }
Chris@0 114
Chris@0 115 return $cookie;
Chris@0 116 }
Chris@0 117
Chris@0 118 /**
Chris@0 119 * Creates a Cookie instance from a Set-Cookie header value.
Chris@0 120 *
Chris@0 121 * @param string $cookie A Set-Cookie header value
Chris@0 122 * @param string $url The base URL
Chris@0 123 *
Chris@0 124 * @return static
Chris@0 125 *
Chris@0 126 * @throws \InvalidArgumentException
Chris@0 127 */
Chris@0 128 public static function fromString($cookie, $url = null)
Chris@0 129 {
Chris@0 130 $parts = explode(';', $cookie);
Chris@0 131
Chris@0 132 if (false === strpos($parts[0], '=')) {
Chris@0 133 throw new \InvalidArgumentException(sprintf('The cookie string "%s" is not valid.', $parts[0]));
Chris@0 134 }
Chris@0 135
Chris@0 136 list($name, $value) = explode('=', array_shift($parts), 2);
Chris@0 137
Chris@0 138 $values = array(
Chris@0 139 'name' => trim($name),
Chris@0 140 'value' => trim($value),
Chris@0 141 'expires' => null,
Chris@0 142 'path' => '/',
Chris@0 143 'domain' => '',
Chris@0 144 'secure' => false,
Chris@0 145 'httponly' => false,
Chris@0 146 'passedRawValue' => true,
Chris@0 147 );
Chris@0 148
Chris@0 149 if (null !== $url) {
Chris@0 150 if ((false === $urlParts = parse_url($url)) || !isset($urlParts['host'])) {
Chris@0 151 throw new \InvalidArgumentException(sprintf('The URL "%s" is not valid.', $url));
Chris@0 152 }
Chris@0 153
Chris@0 154 $values['domain'] = $urlParts['host'];
Chris@0 155 $values['path'] = isset($urlParts['path']) ? substr($urlParts['path'], 0, strrpos($urlParts['path'], '/')) : '';
Chris@0 156 }
Chris@0 157
Chris@0 158 foreach ($parts as $part) {
Chris@0 159 $part = trim($part);
Chris@0 160
Chris@0 161 if ('secure' === strtolower($part)) {
Chris@0 162 // Ignore the secure flag if the original URI is not given or is not HTTPS
Chris@0 163 if (!$url || !isset($urlParts['scheme']) || 'https' != $urlParts['scheme']) {
Chris@0 164 continue;
Chris@0 165 }
Chris@0 166
Chris@0 167 $values['secure'] = true;
Chris@0 168
Chris@0 169 continue;
Chris@0 170 }
Chris@0 171
Chris@0 172 if ('httponly' === strtolower($part)) {
Chris@0 173 $values['httponly'] = true;
Chris@0 174
Chris@0 175 continue;
Chris@0 176 }
Chris@0 177
Chris@0 178 if (2 === count($elements = explode('=', $part, 2))) {
Chris@0 179 if ('expires' === strtolower($elements[0])) {
Chris@0 180 $elements[1] = self::parseDate($elements[1]);
Chris@0 181 }
Chris@0 182
Chris@0 183 $values[strtolower($elements[0])] = $elements[1];
Chris@0 184 }
Chris@0 185 }
Chris@0 186
Chris@0 187 return new static(
Chris@0 188 $values['name'],
Chris@0 189 $values['value'],
Chris@0 190 $values['expires'],
Chris@0 191 $values['path'],
Chris@0 192 $values['domain'],
Chris@0 193 $values['secure'],
Chris@0 194 $values['httponly'],
Chris@0 195 $values['passedRawValue']
Chris@0 196 );
Chris@0 197 }
Chris@0 198
Chris@0 199 private static function parseDate($dateValue)
Chris@0 200 {
Chris@0 201 // trim single quotes around date if present
Chris@0 202 if (($length = strlen($dateValue)) > 1 && "'" === $dateValue[0] && "'" === $dateValue[$length - 1]) {
Chris@0 203 $dateValue = substr($dateValue, 1, -1);
Chris@0 204 }
Chris@0 205
Chris@0 206 foreach (self::$dateFormats as $dateFormat) {
Chris@0 207 if (false !== $date = \DateTime::createFromFormat($dateFormat, $dateValue, new \DateTimeZone('GMT'))) {
Chris@0 208 return $date->format('U');
Chris@0 209 }
Chris@0 210 }
Chris@0 211
Chris@0 212 // attempt a fallback for unusual formatting
Chris@0 213 if (false !== $date = date_create($dateValue, new \DateTimeZone('GMT'))) {
Chris@0 214 return $date->format('U');
Chris@0 215 }
Chris@0 216 }
Chris@0 217
Chris@0 218 /**
Chris@0 219 * Gets the name of the cookie.
Chris@0 220 *
Chris@0 221 * @return string The cookie name
Chris@0 222 */
Chris@0 223 public function getName()
Chris@0 224 {
Chris@0 225 return $this->name;
Chris@0 226 }
Chris@0 227
Chris@0 228 /**
Chris@0 229 * Gets the value of the cookie.
Chris@0 230 *
Chris@0 231 * @return string The cookie value
Chris@0 232 */
Chris@0 233 public function getValue()
Chris@0 234 {
Chris@0 235 return $this->value;
Chris@0 236 }
Chris@0 237
Chris@0 238 /**
Chris@0 239 * Gets the raw value of the cookie.
Chris@0 240 *
Chris@0 241 * @return string The cookie value
Chris@0 242 */
Chris@0 243 public function getRawValue()
Chris@0 244 {
Chris@0 245 return $this->rawValue;
Chris@0 246 }
Chris@0 247
Chris@0 248 /**
Chris@0 249 * Gets the expires time of the cookie.
Chris@0 250 *
Chris@0 251 * @return string The cookie expires time
Chris@0 252 */
Chris@0 253 public function getExpiresTime()
Chris@0 254 {
Chris@0 255 return $this->expires;
Chris@0 256 }
Chris@0 257
Chris@0 258 /**
Chris@0 259 * Gets the path of the cookie.
Chris@0 260 *
Chris@0 261 * @return string The cookie path
Chris@0 262 */
Chris@0 263 public function getPath()
Chris@0 264 {
Chris@0 265 return $this->path;
Chris@0 266 }
Chris@0 267
Chris@0 268 /**
Chris@0 269 * Gets the domain of the cookie.
Chris@0 270 *
Chris@0 271 * @return string The cookie domain
Chris@0 272 */
Chris@0 273 public function getDomain()
Chris@0 274 {
Chris@0 275 return $this->domain;
Chris@0 276 }
Chris@0 277
Chris@0 278 /**
Chris@0 279 * Returns the secure flag of the cookie.
Chris@0 280 *
Chris@0 281 * @return bool The cookie secure flag
Chris@0 282 */
Chris@0 283 public function isSecure()
Chris@0 284 {
Chris@0 285 return $this->secure;
Chris@0 286 }
Chris@0 287
Chris@0 288 /**
Chris@0 289 * Returns the httponly flag of the cookie.
Chris@0 290 *
Chris@0 291 * @return bool The cookie httponly flag
Chris@0 292 */
Chris@0 293 public function isHttpOnly()
Chris@0 294 {
Chris@0 295 return $this->httponly;
Chris@0 296 }
Chris@0 297
Chris@0 298 /**
Chris@0 299 * Returns true if the cookie has expired.
Chris@0 300 *
Chris@0 301 * @return bool true if the cookie has expired, false otherwise
Chris@0 302 */
Chris@0 303 public function isExpired()
Chris@0 304 {
Chris@0 305 return null !== $this->expires && 0 != $this->expires && $this->expires < time();
Chris@0 306 }
Chris@0 307 }