Changeset 52328 for trunk/src/wp-includes/Requests/Cookie.php
- Timestamp:
- 12/06/2021 09:29:00 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/Requests/Cookie.php
r52244 r52328 3 3 * Cookie storage object 4 4 * 5 * @package Requests\Cookies 5 * @package Requests 6 * @subpackage Cookies 6 7 */ 7 8 namespace WpOrg\Requests;9 10 use WpOrg\Requests\Exception\InvalidArgument;11 use WpOrg\Requests\Iri;12 use WpOrg\Requests\Response\Headers;13 use WpOrg\Requests\Utility\CaseInsensitiveDictionary;14 use WpOrg\Requests\Utility\InputValidator;15 8 16 9 /** 17 10 * Cookie storage object 18 11 * 19 * @package Requests\Cookies 12 * @package Requests 13 * @subpackage Cookies 20 14 */ 21 class Cookie {15 class Requests_Cookie { 22 16 /** 23 17 * Cookie name. … … 40 34 * httponly. 41 35 * 42 * @var \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array Array-like object43 */ 44 public $attributes = [];36 * @var Requests_Utility_CaseInsensitiveDictionary|array Array-like object 37 */ 38 public $attributes = array(); 45 39 46 40 /** … … 52 46 * @var array 53 47 */ 54 public $flags = [];48 public $flags = array(); 55 49 56 50 /** … … 69 63 * @param string $name 70 64 * @param string $value 71 * @param array|\WpOrg\Requests\Utility\CaseInsensitiveDictionary $attributes Associative array of attribute data 72 * @param array $flags 73 * @param int|null $reference_time 74 * 75 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $name argument is not a string. 76 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $value argument is not a string. 77 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $attributes argument is not an array or iterable object with array access. 78 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $flags argument is not an array. 79 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $reference_time argument is not an integer or null. 80 */ 81 public function __construct($name, $value, $attributes = [], $flags = [], $reference_time = null) { 82 if (is_string($name) === false) { 83 throw InvalidArgument::create(1, '$name', 'string', gettype($name)); 84 } 85 86 if (is_string($value) === false) { 87 throw InvalidArgument::create(2, '$value', 'string', gettype($value)); 88 } 89 90 if (InputValidator::has_array_access($attributes) === false || InputValidator::is_iterable($attributes) === false) { 91 throw InvalidArgument::create(3, '$attributes', 'array|ArrayAccess&Traversable', gettype($attributes)); 92 } 93 94 if (is_array($flags) === false) { 95 throw InvalidArgument::create(4, '$flags', 'array', gettype($flags)); 96 } 97 98 if ($reference_time !== null && is_int($reference_time) === false) { 99 throw InvalidArgument::create(5, '$reference_time', 'integer|null', gettype($reference_time)); 100 } 101 65 * @param array|Requests_Utility_CaseInsensitiveDictionary $attributes Associative array of attribute data 66 */ 67 public function __construct($name, $value, $attributes = array(), $flags = array(), $reference_time = null) { 102 68 $this->name = $name; 103 69 $this->value = $value; 104 70 $this->attributes = $attributes; 105 $default_flags = [71 $default_flags = array( 106 72 'creation' => time(), 107 73 'last-access' => time(), 108 74 'persistent' => false, 109 75 'host-only' => true, 110 ];76 ); 111 77 $this->flags = array_merge($default_flags, $flags); 112 78 … … 117 83 118 84 $this->normalize(); 119 }120 121 /**122 * Get the cookie value123 *124 * Attributes and other data can be accessed via methods.125 */126 public function __toString() {127 return $this->value;128 85 } 129 86 … … 157 114 * Check if a cookie is valid for a given URI 158 115 * 159 * @param \WpOrg\Requests\Iri$uri URI to check116 * @param Requests_IRI $uri URI to check 160 117 * @return boolean Whether the cookie is valid for the given URI 161 118 */ 162 public function uri_matches( Iri$uri) {119 public function uri_matches(Requests_IRI $uri) { 163 120 if (!$this->domain_matches($uri->host)) { 164 121 return false; … … 175 132 * Check if a cookie is valid for a given domain 176 133 * 177 * @param string $ domainDomain to check134 * @param string $string Domain to check 178 135 * @return boolean Whether the cookie is valid for the given domain 179 136 */ 180 public function domain_matches($domain) { 181 if (is_string($domain) === false) { 182 return false; 183 } 184 137 public function domain_matches($string) { 185 138 if (!isset($this->attributes['domain'])) { 186 139 // Cookies created manually; cookies created by Requests will set … … 189 142 } 190 143 191 $ cookie_domain= $this->attributes['domain'];192 if ($ cookie_domain === $domain) {193 // The cookie domain and the passed domainare identical.144 $domain_string = $this->attributes['domain']; 145 if ($domain_string === $string) { 146 // The domain string and the string are identical. 194 147 return true; 195 148 } … … 201 154 } 202 155 203 if (strlen($ domain) <= strlen($cookie_domain)) {204 // For obvious reasons, the cookie domain cannot be a suffix if the passeddomain205 // is shorter than the cookie domain156 if (strlen($string) <= strlen($domain_string)) { 157 // For obvious reasons, the string cannot be a suffix if the domain 158 // is shorter than the domain string 206 159 return false; 207 160 } 208 161 209 if (substr($ domain, -1 * strlen($cookie_domain)) !== $cookie_domain) {210 // The cookie domain should be a suffix of the passed domain.162 if (substr($string, -1 * strlen($domain_string)) !== $domain_string) { 163 // The domain string should be a suffix of the string. 211 164 return false; 212 165 } 213 166 214 $prefix = substr($ domain, 0, strlen($domain) - strlen($cookie_domain));167 $prefix = substr($string, 0, strlen($string) - strlen($domain_string)); 215 168 if (substr($prefix, -1) !== '.') { 216 // The last character of the passed domainthat is not included in the169 // The last character of the string that is not included in the 217 170 // domain string should be a %x2E (".") character. 218 171 return false; 219 172 } 220 173 221 // The passed domainshould be a host name (i.e., not an IP address).222 return !preg_match('#^(.+\.)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $ domain);174 // The string should be a host name (i.e., not an IP address). 175 return !preg_match('#^(.+\.)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $string); 223 176 } 224 177 … … 241 194 // the path to the requested path 242 195 return true; 243 }244 245 if (is_scalar($request_path) === false) {246 return false;247 196 } 248 197 … … 369 318 370 319 /** 320 * Format a cookie for a Cookie header 321 * 322 * @codeCoverageIgnore 323 * @deprecated Use {@see Requests_Cookie::format_for_header} 324 * @return string 325 */ 326 public function formatForHeader() { 327 return $this->format_for_header(); 328 } 329 330 /** 371 331 * Format a cookie for a Set-Cookie header 372 332 * … … 379 339 $header_value = $this->format_for_header(); 380 340 if (!empty($this->attributes)) { 381 $parts = [];341 $parts = array(); 382 342 foreach ($this->attributes as $key => $value) { 383 343 // Ignore non-associative attributes … … 396 356 397 357 /** 358 * Format a cookie for a Set-Cookie header 359 * 360 * @codeCoverageIgnore 361 * @deprecated Use {@see Requests_Cookie::format_for_set_cookie} 362 * @return string 363 */ 364 public function formatForSetCookie() { 365 return $this->format_for_set_cookie(); 366 } 367 368 /** 369 * Get the cookie value 370 * 371 * Attributes and other data can be accessed via methods. 372 */ 373 public function __toString() { 374 return $this->value; 375 } 376 377 /** 398 378 * Parse a cookie string into a cookie object 399 379 * … … 402 382 * specifies some of this handling, but not in a thorough manner. 403 383 * 404 * @param string $cookie_header Cookie header value (from a Set-Cookie header) 405 * @param string $name 406 * @param int|null $reference_time 407 * @return \WpOrg\Requests\Cookie Parsed cookie object 408 * 409 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $cookie_header argument is not a string. 410 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $name argument is not a string. 411 */ 412 public static function parse($cookie_header, $name = '', $reference_time = null) { 413 if (is_string($cookie_header) === false) { 414 throw InvalidArgument::create(1, '$cookie_header', 'string', gettype($cookie_header)); 415 } 416 417 if (is_string($name) === false) { 418 throw InvalidArgument::create(2, '$name', 'string', gettype($name)); 419 } 420 421 $parts = explode(';', $cookie_header); 384 * @param string Cookie header value (from a Set-Cookie header) 385 * @return Requests_Cookie Parsed cookie object 386 */ 387 public static function parse($string, $name = '', $reference_time = null) { 388 $parts = explode(';', $string); 422 389 $kvparts = array_shift($parts); 423 390 424 391 if (!empty($name)) { 425 $value = $ cookie_header;392 $value = $string; 426 393 } 427 394 elseif (strpos($kvparts, '=') === false) { … … 440 407 $value = trim($value); 441 408 442 // Attribute key sare handled case-insensitively443 $attributes = new CaseInsensitiveDictionary();409 // Attribute key are handled case-insensitively 410 $attributes = new Requests_Utility_CaseInsensitiveDictionary(); 444 411 445 412 if (!empty($parts)) { … … 459 426 } 460 427 461 return new static($name, $value, $attributes, [], $reference_time);428 return new Requests_Cookie($name, $value, $attributes, array(), $reference_time); 462 429 } 463 430 … … 465 432 * Parse all Set-Cookie headers from request headers 466 433 * 467 * @param \WpOrg\Requests\Response\Headers $headers Headers to parse from468 * @param \WpOrg\Requests\Iri|null $origin URI for comparing cookie origins434 * @param Requests_Response_Headers $headers Headers to parse from 435 * @param Requests_IRI|null $origin URI for comparing cookie origins 469 436 * @param int|null $time Reference time for expiration calculation 470 437 * @return array 471 438 */ 472 public static function parse_from_headers( Headers $headers, Iri$origin = null, $time = null) {439 public static function parse_from_headers(Requests_Response_Headers $headers, Requests_IRI $origin = null, $time = null) { 473 440 $cookie_headers = $headers->getValues('Set-Cookie'); 474 441 if (empty($cookie_headers)) { 475 return [];476 } 477 478 $cookies = [];442 return array(); 443 } 444 445 $cookies = array(); 479 446 foreach ($cookie_headers as $header) { 480 447 $parsed = self::parse($header, '', $time); … … 525 492 return $cookies; 526 493 } 494 495 /** 496 * Parse all Set-Cookie headers from request headers 497 * 498 * @codeCoverageIgnore 499 * @deprecated Use {@see Requests_Cookie::parse_from_headers} 500 * @return array 501 */ 502 public static function parseFromHeaders(Requests_Response_Headers $headers) { 503 return self::parse_from_headers($headers); 504 } 527 505 }
Note: See TracChangeset
for help on using the changeset viewer.