Changeset 52244 for trunk/src/wp-includes/Requests/Cookie.php
- Timestamp:
- 11/25/2021 01:10:30 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/Requests/Cookie.php
r50842 r52244 3 3 * Cookie storage object 4 4 * 5 * @package Requests 6 * @subpackage Cookies 5 * @package Requests\Cookies 7 6 */ 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; 8 15 9 16 /** 10 17 * Cookie storage object 11 18 * 12 * @package Requests 13 * @subpackage Cookies 19 * @package Requests\Cookies 14 20 */ 15 class Requests_Cookie {21 class Cookie { 16 22 /** 17 23 * Cookie name. … … 34 40 * httponly. 35 41 * 36 * @var Requests_Utility_CaseInsensitiveDictionary|array Array-like object37 */ 38 public $attributes = array();42 * @var \WpOrg\Requests\Utility\CaseInsensitiveDictionary|array Array-like object 43 */ 44 public $attributes = []; 39 45 40 46 /** … … 46 52 * @var array 47 53 */ 48 public $flags = array();54 public $flags = []; 49 55 50 56 /** … … 63 69 * @param string $name 64 70 * @param string $value 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) { 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 68 102 $this->name = $name; 69 103 $this->value = $value; 70 104 $this->attributes = $attributes; 71 $default_flags = array(105 $default_flags = [ 72 106 'creation' => time(), 73 107 'last-access' => time(), 74 108 'persistent' => false, 75 109 'host-only' => true, 76 );110 ]; 77 111 $this->flags = array_merge($default_flags, $flags); 78 112 … … 83 117 84 118 $this->normalize(); 119 } 120 121 /** 122 * Get the cookie value 123 * 124 * Attributes and other data can be accessed via methods. 125 */ 126 public function __toString() { 127 return $this->value; 85 128 } 86 129 … … 114 157 * Check if a cookie is valid for a given URI 115 158 * 116 * @param Requests_IRI$uri URI to check159 * @param \WpOrg\Requests\Iri $uri URI to check 117 160 * @return boolean Whether the cookie is valid for the given URI 118 161 */ 119 public function uri_matches( Requests_IRI$uri) {162 public function uri_matches(Iri $uri) { 120 163 if (!$this->domain_matches($uri->host)) { 121 164 return false; … … 132 175 * Check if a cookie is valid for a given domain 133 176 * 134 * @param string $ stringDomain to check177 * @param string $domain Domain to check 135 178 * @return boolean Whether the cookie is valid for the given domain 136 179 */ 137 public function domain_matches($string) { 180 public function domain_matches($domain) { 181 if (is_string($domain) === false) { 182 return false; 183 } 184 138 185 if (!isset($this->attributes['domain'])) { 139 186 // Cookies created manually; cookies created by Requests will set … … 142 189 } 143 190 144 $ domain_string= $this->attributes['domain'];145 if ($ domain_string === $string) {146 // The domain string and the stringare identical.191 $cookie_domain = $this->attributes['domain']; 192 if ($cookie_domain === $domain) { 193 // The cookie domain and the passed domain are identical. 147 194 return true; 148 195 } … … 154 201 } 155 202 156 if (strlen($ string) <= strlen($domain_string)) {157 // For obvious reasons, the string cannot be a suffix if thedomain158 // is shorter than the domain string159 return false; 160 } 161 162 if (substr($ string, -1 * strlen($domain_string)) !== $domain_string) {163 // The domain string should be a suffix of the string.164 return false; 165 } 166 167 $prefix = substr($ string, 0, strlen($string) - strlen($domain_string));203 if (strlen($domain) <= strlen($cookie_domain)) { 204 // For obvious reasons, the cookie domain cannot be a suffix if the passed domain 205 // is shorter than the cookie domain 206 return false; 207 } 208 209 if (substr($domain, -1 * strlen($cookie_domain)) !== $cookie_domain) { 210 // The cookie domain should be a suffix of the passed domain. 211 return false; 212 } 213 214 $prefix = substr($domain, 0, strlen($domain) - strlen($cookie_domain)); 168 215 if (substr($prefix, -1) !== '.') { 169 // The last character of the stringthat is not included in the216 // The last character of the passed domain that is not included in the 170 217 // domain string should be a %x2E (".") character. 171 218 return false; 172 219 } 173 220 174 // The stringshould 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);221 // The passed domain should 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); 176 223 } 177 224 … … 194 241 // the path to the requested path 195 242 return true; 243 } 244 245 if (is_scalar($request_path) === false) { 246 return false; 196 247 } 197 248 … … 318 369 319 370 /** 320 * Format a cookie for a Cookie header321 *322 * @codeCoverageIgnore323 * @deprecated Use {@see Requests_Cookie::format_for_header}324 * @return string325 */326 public function formatForHeader() {327 return $this->format_for_header();328 }329 330 /**331 371 * Format a cookie for a Set-Cookie header 332 372 * … … 339 379 $header_value = $this->format_for_header(); 340 380 if (!empty($this->attributes)) { 341 $parts = array();381 $parts = []; 342 382 foreach ($this->attributes as $key => $value) { 343 383 // Ignore non-associative attributes … … 356 396 357 397 /** 358 * Format a cookie for a Set-Cookie header359 *360 * @codeCoverageIgnore361 * @deprecated Use {@see Requests_Cookie::format_for_set_cookie}362 * @return string363 */364 public function formatForSetCookie() {365 return $this->format_for_set_cookie();366 }367 368 /**369 * Get the cookie value370 *371 * Attributes and other data can be accessed via methods.372 */373 public function __toString() {374 return $this->value;375 }376 377 /**378 398 * Parse a cookie string into a cookie object 379 399 * … … 382 402 * specifies some of this handling, but not in a thorough manner. 383 403 * 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); 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); 389 422 $kvparts = array_shift($parts); 390 423 391 424 if (!empty($name)) { 392 $value = $ string;425 $value = $cookie_header; 393 426 } 394 427 elseif (strpos($kvparts, '=') === false) { … … 407 440 $value = trim($value); 408 441 409 // Attribute key are handled case-insensitively410 $attributes = new Requests_Utility_CaseInsensitiveDictionary();442 // Attribute keys are handled case-insensitively 443 $attributes = new CaseInsensitiveDictionary(); 411 444 412 445 if (!empty($parts)) { … … 426 459 } 427 460 428 return new Requests_Cookie($name, $value, $attributes, array(), $reference_time);461 return new static($name, $value, $attributes, [], $reference_time); 429 462 } 430 463 … … 432 465 * Parse all Set-Cookie headers from request headers 433 466 * 434 * @param Requests_Response_Headers $headers Headers to parse from435 * @param Requests_IRI|null $origin URI for comparing cookie origins467 * @param \WpOrg\Requests\Response\Headers $headers Headers to parse from 468 * @param \WpOrg\Requests\Iri|null $origin URI for comparing cookie origins 436 469 * @param int|null $time Reference time for expiration calculation 437 470 * @return array 438 471 */ 439 public static function parse_from_headers( Requests_Response_Headers $headers, Requests_IRI$origin = null, $time = null) {472 public static function parse_from_headers(Headers $headers, Iri $origin = null, $time = null) { 440 473 $cookie_headers = $headers->getValues('Set-Cookie'); 441 474 if (empty($cookie_headers)) { 442 return array();443 } 444 445 $cookies = array();475 return []; 476 } 477 478 $cookies = []; 446 479 foreach ($cookie_headers as $header) { 447 480 $parsed = self::parse($header, '', $time); … … 492 525 return $cookies; 493 526 } 494 495 /**496 * Parse all Set-Cookie headers from request headers497 *498 * @codeCoverageIgnore499 * @deprecated Use {@see Requests_Cookie::parse_from_headers}500 * @return array501 */502 public static function parseFromHeaders(Requests_Response_Headers $headers) {503 return self::parse_from_headers($headers);504 }505 527 }
Note: See TracChangeset
for help on using the changeset viewer.