| 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp\Cookie;
|
|---|
| 3 |
|
|---|
| 4 | /**
|
|---|
| 5 | * Set-Cookie object
|
|---|
| 6 | */
|
|---|
| 7 | class SetCookie
|
|---|
| 8 | {
|
|---|
| 9 | /** @var array */
|
|---|
| 10 | private static $defaults = [
|
|---|
| 11 | 'Name' => null,
|
|---|
| 12 | 'Value' => null,
|
|---|
| 13 | 'Domain' => null,
|
|---|
| 14 | 'Path' => '/',
|
|---|
| 15 | 'Max-Age' => null,
|
|---|
| 16 | 'Expires' => null,
|
|---|
| 17 | 'Secure' => false,
|
|---|
| 18 | 'Discard' => false,
|
|---|
| 19 | 'HttpOnly' => false
|
|---|
| 20 | ];
|
|---|
| 21 |
|
|---|
| 22 | /** @var array Cookie data */
|
|---|
| 23 | private $data;
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * Create a new SetCookie object from a string
|
|---|
| 27 | *
|
|---|
| 28 | * @param string $cookie Set-Cookie header string
|
|---|
| 29 | *
|
|---|
| 30 | * @return self
|
|---|
| 31 | */
|
|---|
| 32 | public static function fromString($cookie)
|
|---|
| 33 | {
|
|---|
| 34 | // Create the default return array
|
|---|
| 35 | $data = self::$defaults;
|
|---|
| 36 | // Explode the cookie string using a series of semicolons
|
|---|
| 37 | $pieces = array_filter(array_map('trim', explode(';', $cookie)));
|
|---|
| 38 | // The name of the cookie (first kvp) must include an equal sign.
|
|---|
| 39 | if (empty($pieces) || !strpos($pieces[0], '=')) {
|
|---|
| 40 | return new self($data);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | // Add the cookie pieces into the parsed data array
|
|---|
| 44 | foreach ($pieces as $part) {
|
|---|
| 45 |
|
|---|
| 46 | $cookieParts = explode('=', $part, 2);
|
|---|
| 47 | $key = trim($cookieParts[0]);
|
|---|
| 48 | $value = isset($cookieParts[1])
|
|---|
| 49 | ? trim($cookieParts[1], " \n\r\t\0\x0B\"")
|
|---|
| 50 | : true;
|
|---|
| 51 |
|
|---|
| 52 | // Only check for non-cookies when cookies have been found
|
|---|
| 53 | if (empty($data['Name'])) {
|
|---|
| 54 | $data['Name'] = $key;
|
|---|
| 55 | $data['Value'] = $value;
|
|---|
| 56 | } else {
|
|---|
| 57 | foreach (array_keys(self::$defaults) as $search) {
|
|---|
| 58 | if (!strcasecmp($search, $key)) {
|
|---|
| 59 | $data[$search] = $value;
|
|---|
| 60 | continue 2;
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | $data[$key] = $value;
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | return new self($data);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * @param array $data Array of cookie data provided by a Cookie parser
|
|---|
| 72 | */
|
|---|
| 73 | public function __construct(array $data = [])
|
|---|
| 74 | {
|
|---|
| 75 | $this->data = array_replace(self::$defaults, $data);
|
|---|
| 76 | // Extract the Expires value and turn it into a UNIX timestamp if needed
|
|---|
| 77 | if (!$this->getExpires() && $this->getMaxAge()) {
|
|---|
| 78 | // Calculate the Expires date
|
|---|
| 79 | $this->setExpires(time() + $this->getMaxAge());
|
|---|
| 80 | } elseif ($this->getExpires() && !is_numeric($this->getExpires())) {
|
|---|
| 81 | $this->setExpires($this->getExpires());
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | public function __toString()
|
|---|
| 86 | {
|
|---|
| 87 | $str = $this->data['Name'] . '=' . $this->data['Value'] . '; ';
|
|---|
| 88 | foreach ($this->data as $k => $v) {
|
|---|
| 89 | if ($k != 'Name' && $k != 'Value' && $v !== null && $v !== false) {
|
|---|
| 90 | if ($k == 'Expires') {
|
|---|
| 91 | $str .= 'Expires=' . gmdate('D, d M Y H:i:s \G\M\T', $v) . '; ';
|
|---|
| 92 | } else {
|
|---|
| 93 | $str .= ($v === true ? $k : "{$k}={$v}") . '; ';
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | return rtrim($str, '; ');
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | public function toArray()
|
|---|
| 102 | {
|
|---|
| 103 | return $this->data;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * Get the cookie name
|
|---|
| 108 | *
|
|---|
| 109 | * @return string
|
|---|
| 110 | */
|
|---|
| 111 | public function getName()
|
|---|
| 112 | {
|
|---|
| 113 | return $this->data['Name'];
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /**
|
|---|
| 117 | * Set the cookie name
|
|---|
| 118 | *
|
|---|
| 119 | * @param string $name Cookie name
|
|---|
| 120 | */
|
|---|
| 121 | public function setName($name)
|
|---|
| 122 | {
|
|---|
| 123 | $this->data['Name'] = $name;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /**
|
|---|
| 127 | * Get the cookie value
|
|---|
| 128 | *
|
|---|
| 129 | * @return string
|
|---|
| 130 | */
|
|---|
| 131 | public function getValue()
|
|---|
| 132 | {
|
|---|
| 133 | return $this->data['Value'];
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | * Set the cookie value
|
|---|
| 138 | *
|
|---|
| 139 | * @param string $value Cookie value
|
|---|
| 140 | */
|
|---|
| 141 | public function setValue($value)
|
|---|
| 142 | {
|
|---|
| 143 | $this->data['Value'] = $value;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /**
|
|---|
| 147 | * Get the domain
|
|---|
| 148 | *
|
|---|
| 149 | * @return string|null
|
|---|
| 150 | */
|
|---|
| 151 | public function getDomain()
|
|---|
| 152 | {
|
|---|
| 153 | return $this->data['Domain'];
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /**
|
|---|
| 157 | * Set the domain of the cookie
|
|---|
| 158 | *
|
|---|
| 159 | * @param string $domain
|
|---|
| 160 | */
|
|---|
| 161 | public function setDomain($domain)
|
|---|
| 162 | {
|
|---|
| 163 | $this->data['Domain'] = $domain;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /**
|
|---|
| 167 | * Get the path
|
|---|
| 168 | *
|
|---|
| 169 | * @return string
|
|---|
| 170 | */
|
|---|
| 171 | public function getPath()
|
|---|
| 172 | {
|
|---|
| 173 | return $this->data['Path'];
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | /**
|
|---|
| 177 | * Set the path of the cookie
|
|---|
| 178 | *
|
|---|
| 179 | * @param string $path Path of the cookie
|
|---|
| 180 | */
|
|---|
| 181 | public function setPath($path)
|
|---|
| 182 | {
|
|---|
| 183 | $this->data['Path'] = $path;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /**
|
|---|
| 187 | * Maximum lifetime of the cookie in seconds
|
|---|
| 188 | *
|
|---|
| 189 | * @return int|null
|
|---|
| 190 | */
|
|---|
| 191 | public function getMaxAge()
|
|---|
| 192 | {
|
|---|
| 193 | return $this->data['Max-Age'];
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /**
|
|---|
| 197 | * Set the max-age of the cookie
|
|---|
| 198 | *
|
|---|
| 199 | * @param int $maxAge Max age of the cookie in seconds
|
|---|
| 200 | */
|
|---|
| 201 | public function setMaxAge($maxAge)
|
|---|
| 202 | {
|
|---|
| 203 | $this->data['Max-Age'] = $maxAge;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /**
|
|---|
| 207 | * The UNIX timestamp when the cookie Expires
|
|---|
| 208 | *
|
|---|
| 209 | * @return mixed
|
|---|
| 210 | */
|
|---|
| 211 | public function getExpires()
|
|---|
| 212 | {
|
|---|
| 213 | return $this->data['Expires'];
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | /**
|
|---|
| 217 | * Set the unix timestamp for which the cookie will expire
|
|---|
| 218 | *
|
|---|
| 219 | * @param int $timestamp Unix timestamp
|
|---|
| 220 | */
|
|---|
| 221 | public function setExpires($timestamp)
|
|---|
| 222 | {
|
|---|
| 223 | $this->data['Expires'] = is_numeric($timestamp)
|
|---|
| 224 | ? (int) $timestamp
|
|---|
| 225 | : strtotime($timestamp);
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /**
|
|---|
| 229 | * Get whether or not this is a secure cookie
|
|---|
| 230 | *
|
|---|
| 231 | * @return null|bool
|
|---|
| 232 | */
|
|---|
| 233 | public function getSecure()
|
|---|
| 234 | {
|
|---|
| 235 | return $this->data['Secure'];
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | /**
|
|---|
| 239 | * Set whether or not the cookie is secure
|
|---|
| 240 | *
|
|---|
| 241 | * @param bool $secure Set to true or false if secure
|
|---|
| 242 | */
|
|---|
| 243 | public function setSecure($secure)
|
|---|
| 244 | {
|
|---|
| 245 | $this->data['Secure'] = $secure;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | /**
|
|---|
| 249 | * Get whether or not this is a session cookie
|
|---|
| 250 | *
|
|---|
| 251 | * @return null|bool
|
|---|
| 252 | */
|
|---|
| 253 | public function getDiscard()
|
|---|
| 254 | {
|
|---|
| 255 | return $this->data['Discard'];
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | /**
|
|---|
| 259 | * Set whether or not this is a session cookie
|
|---|
| 260 | *
|
|---|
| 261 | * @param bool $discard Set to true or false if this is a session cookie
|
|---|
| 262 | */
|
|---|
| 263 | public function setDiscard($discard)
|
|---|
| 264 | {
|
|---|
| 265 | $this->data['Discard'] = $discard;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | /**
|
|---|
| 269 | * Get whether or not this is an HTTP only cookie
|
|---|
| 270 | *
|
|---|
| 271 | * @return bool
|
|---|
| 272 | */
|
|---|
| 273 | public function getHttpOnly()
|
|---|
| 274 | {
|
|---|
| 275 | return $this->data['HttpOnly'];
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | /**
|
|---|
| 279 | * Set whether or not this is an HTTP only cookie
|
|---|
| 280 | *
|
|---|
| 281 | * @param bool $httpOnly Set to true or false if this is HTTP only
|
|---|
| 282 | */
|
|---|
| 283 | public function setHttpOnly($httpOnly)
|
|---|
| 284 | {
|
|---|
| 285 | $this->data['HttpOnly'] = $httpOnly;
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | /**
|
|---|
| 289 | * Check if the cookie matches a path value
|
|---|
| 290 | *
|
|---|
| 291 | * @param string $path Path to check against
|
|---|
| 292 | *
|
|---|
| 293 | * @return bool
|
|---|
| 294 | */
|
|---|
| 295 | public function matchesPath($path)
|
|---|
| 296 | {
|
|---|
| 297 | return !$this->getPath() || 0 === stripos($path, $this->getPath());
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | /**
|
|---|
| 301 | * Check if the cookie matches a domain value
|
|---|
| 302 | *
|
|---|
| 303 | * @param string $domain Domain to check against
|
|---|
| 304 | *
|
|---|
| 305 | * @return bool
|
|---|
| 306 | */
|
|---|
| 307 | public function matchesDomain($domain)
|
|---|
| 308 | {
|
|---|
| 309 | // Remove the leading '.' as per spec in RFC 6265.
|
|---|
| 310 | // http://tools.ietf.org/html/rfc6265#section-5.2.3
|
|---|
| 311 | $cookieDomain = ltrim($this->getDomain(), '.');
|
|---|
| 312 |
|
|---|
| 313 | // Domain not set or exact match.
|
|---|
| 314 | if (!$cookieDomain || !strcasecmp($domain, $cookieDomain)) {
|
|---|
| 315 | return true;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | // Matching the subdomain according to RFC 6265.
|
|---|
| 319 | // http://tools.ietf.org/html/rfc6265#section-5.1.3
|
|---|
| 320 | if (filter_var($domain, FILTER_VALIDATE_IP)) {
|
|---|
| 321 | return false;
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | return (bool) preg_match('/\.' . preg_quote($cookieDomain) . '$/i', $domain);
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | /**
|
|---|
| 328 | * Check if the cookie is expired
|
|---|
| 329 | *
|
|---|
| 330 | * @return bool
|
|---|
| 331 | */
|
|---|
| 332 | public function isExpired()
|
|---|
| 333 | {
|
|---|
| 334 | return $this->getExpires() && time() > $this->getExpires();
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | /**
|
|---|
| 338 | * Check if the cookie is valid according to RFC 6265
|
|---|
| 339 | *
|
|---|
| 340 | * @return bool|string Returns true if valid or an error message if invalid
|
|---|
| 341 | */
|
|---|
| 342 | public function validate()
|
|---|
| 343 | {
|
|---|
| 344 | // Names must not be empty, but can be 0
|
|---|
| 345 | $name = $this->getName();
|
|---|
| 346 | if (empty($name) && !is_numeric($name)) {
|
|---|
| 347 | return 'The cookie name must not be empty';
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | // Check if any of the invalid characters are present in the cookie name
|
|---|
| 351 | if (preg_match('/[\x00-\x20\x22\x28-\x29\x2c\x2f\x3a-\x40\x5b-\x5d\x7b\x7d\x7f]/', $name)) {
|
|---|
| 352 | return 'Cookie name must not contain invalid characters: ASCII Control characters (0-31;127), space, tab and the following characters: ()<>@,;:\"/[]?={}';
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | // Value must not be empty, but can be 0
|
|---|
| 356 | $value = $this->getValue();
|
|---|
| 357 | if (empty($value) && !is_numeric($value)) {
|
|---|
| 358 | return 'The cookie value must not be empty';
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | // Domains must not be empty, but can be 0
|
|---|
| 362 | // A "0" is not a valid internet domain, but may be used as server name
|
|---|
| 363 | // in a private network.
|
|---|
| 364 | $domain = $this->getDomain();
|
|---|
| 365 | if (empty($domain) && !is_numeric($domain)) {
|
|---|
| 366 | return 'The cookie domain must not be empty';
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | return true;
|
|---|
| 370 | }
|
|---|
| 371 | }
|
|---|