annotate vendor/symfony/browser-kit/Cookie.php @ 19:fa3358dc1485 tip

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