Changeset 52244 for trunk/src/wp-includes/Requests/Iri.php
- Timestamp:
- 11/25/2021 01:10:30 AM (5 years ago)
- File:
-
- 1 moved
-
trunk/src/wp-includes/Requests/Iri.php (moved) (moved from trunk/src/wp-includes/Requests/IRI.php ) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/Requests/Iri.php
r52243 r52244 3 3 * IRI parser/serialiser/normaliser 4 4 * 5 * @package Requests 6 * @subpackage Utilities 5 * @package Requests\Utilities 7 6 */ 7 8 namespace WpOrg\Requests; 9 10 use WpOrg\Requests\Exception; 11 use WpOrg\Requests\Exception\InvalidArgument; 12 use WpOrg\Requests\Ipv6; 13 use WpOrg\Requests\Port; 14 use WpOrg\Requests\Utility\InputValidator; 8 15 9 16 /** … … 39 46 * POSSIBILITY OF SUCH DAMAGE. 40 47 * 41 * @package Requests 42 * @subpackage Utilities 48 * @package Requests\Utilities 43 49 * @author Geoffrey Sneddon 44 50 * @author Steve Minutillo 45 51 * @copyright 2007-2009 Geoffrey Sneddon and Steve Minutillo 46 * @license http ://www.opensource.org/licenses/bsd-license.php52 * @license https://opensource.org/licenses/bsd-license.php 47 53 * @link http://hg.gsnedders.com/iri/ 48 54 * 49 55 * @property string $iri IRI we're working with 50 * @property-read string $uri IRI in URI form, {@see to_uri}56 * @property-read string $uri IRI in URI form, {@see \WpOrg\Requests\IRI::to_uri()} 51 57 * @property string $scheme Scheme part of the IRI 52 58 * @property string $authority Authority part, formatted for a URI (userinfo + host + port) … … 64 70 * @property string $ifragment Fragment part of the IRI (after '#') 65 71 */ 66 class Requests_IRI{72 class Iri { 67 73 /** 68 74 * Scheme … … 124 130 protected $normalization = array( 125 131 'acap' => array( 126 'port' => 674132 'port' => Port::ACAP, 127 133 ), 128 134 'dict' => array( 129 'port' => 2628135 'port' => Port::DICT, 130 136 ), 131 137 'file' => array( 132 'ihost' => 'localhost' 138 'ihost' => 'localhost', 133 139 ), 134 140 'http' => array( 135 'port' => 80,141 'port' => Port::HTTP, 136 142 ), 137 143 'https' => array( 138 'port' => 443,144 'port' => Port::HTTPS, 139 145 ), 140 146 ); … … 241 247 * Create a new IRI object, from a specified string 242 248 * 243 * @param string|null $iri 249 * @param string|Stringable|null $iri 250 * 251 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $iri argument is not a string, Stringable or null. 244 252 */ 245 253 public function __construct($iri = null) { 254 if ($iri !== null && InputValidator::is_string_or_stringable($iri) === false) { 255 throw InvalidArgument::create(1, '$iri', 'string|Stringable|null', gettype($iri)); 256 } 257 246 258 $this->set_iri($iri); 247 259 } … … 252 264 * Returns false if $base is not absolute, otherwise an IRI. 253 265 * 254 * @param Requests_IRI|string $base (Absolute) Base IRI255 * @param Requests_IRI|string $relative Relative IRI256 * @return Requests_IRI|false266 * @param \WpOrg\Requests\Iri|string $base (Absolute) Base IRI 267 * @param \WpOrg\Requests\Iri|string $relative Relative IRI 268 * @return \WpOrg\Requests\Iri|false 257 269 */ 258 270 public static function absolutize($base, $relative) { 259 if (!($relative instanceof Requests_IRI)) {260 $relative = new Requests_IRI($relative);271 if (!($relative instanceof self)) { 272 $relative = new self($relative); 261 273 } 262 274 if (!$relative->is_valid()) { … … 267 279 } 268 280 269 if (!($base instanceof Requests_IRI)) {270 $base = new Requests_IRI($base);281 if (!($base instanceof self)) { 282 $base = new self($base); 271 283 } 272 284 if ($base->scheme === null || !$base->is_valid()) { … … 280 292 } 281 293 else { 282 $target = new Requests_IRI;294 $target = new self; 283 295 $target->scheme = $base->scheme; 284 296 $target->iuserinfo = $base->iuserinfo; … … 331 343 $has_match = preg_match('/^((?P<scheme>[^:\/?#]+):)?(\/\/(?P<authority>[^\/?#]*))?(?P<path>[^?#]*)(\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$/', $iri, $match); 332 344 if (!$has_match) { 333 throw new Requests_Exception('Cannot parse supplied IRI', 'iri.cannot_parse', $iri);345 throw new Exception('Cannot parse supplied IRI', 'iri.cannot_parse', $iri); 334 346 } 335 347 … … 414 426 * Replace invalid character with percent encoding 415 427 * 416 * @param string $ stringInput string428 * @param string $text Input string 417 429 * @param string $extra_chars Valid characters not in iunreserved or 418 430 * iprivate (this is ASCII-only) … … 420 432 * @return string 421 433 */ 422 protected function replace_invalid_with_pct_encoding($ string, $extra_chars, $iprivate = false) {434 protected function replace_invalid_with_pct_encoding($text, $extra_chars, $iprivate = false) { 423 435 // Normalize as many pct-encoded sections as possible 424 $ string = preg_replace_callback('/(?:%[A-Fa-f0-9]{2})+/', array($this, 'remove_iunreserved_percent_encoded'), $string);436 $text = preg_replace_callback('/(?:%[A-Fa-f0-9]{2})+/', array($this, 'remove_iunreserved_percent_encoded'), $text); 425 437 426 438 // Replace invalid percent characters 427 $ string = preg_replace('/%(?![A-Fa-f0-9]{2})/', '%25', $string);439 $text = preg_replace('/%(?![A-Fa-f0-9]{2})/', '%25', $text); 428 440 429 441 // Add unreserved and % to $extra_chars (the latter is safe because all … … 433 445 // Now replace any bytes that aren't allowed with their pct-encoded versions 434 446 $position = 0; 435 $strlen = strlen($ string);436 while (($position += strspn($ string, $extra_chars, $position)) < $strlen) {437 $value = ord($ string[$position]);447 $strlen = strlen($text); 448 while (($position += strspn($text, $extra_chars, $position)) < $strlen) { 449 $value = ord($text[$position]); 438 450 439 451 // Start position … … 472 484 if ($position + $length <= $strlen) { 473 485 for ($position++; $remaining; $position++) { 474 $value = ord($ string[$position]);486 $value = ord($text[$position]); 475 487 476 488 // Check that the byte is valid, then add it to the character: … … 523 535 524 536 for ($j = $start; $j <= $position; $j++) { 525 $ string = substr_replace($string, sprintf('%%%02X', ord($string[$j])), $j, 1);537 $text = substr_replace($text, sprintf('%%%02X', ord($text[$j])), $j, 1); 526 538 $j += 2; 527 539 $position += 2; … … 531 543 } 532 544 533 return $ string;545 return $text; 534 546 } 535 547 … … 540 552 * encoded characters in iunreserved 541 553 * 542 * @param array $ match PCRE match554 * @param array $regex_match PCRE match 543 555 * @return string Replacement 544 556 */ 545 protected function remove_iunreserved_percent_encoded($ match) {557 protected function remove_iunreserved_percent_encoded($regex_match) { 546 558 // As we just have valid percent encoded sequences we can just explode 547 559 // and ignore the first member of the returned array (an empty string). 548 $bytes = explode('%', $ match[0]);560 $bytes = explode('%', $regex_match[0]); 549 561 550 562 // Initialize the new string (this is what will be returned) and that … … 722 734 return true; 723 735 } 736 737 $iri = (string) $iri; 738 724 739 if (isset($cache[$iri])) { 725 740 list($this->scheme, … … 734 749 } 735 750 736 $parsed = $this->parse_iri( (string)$iri);751 $parsed = $this->parse_iri($iri); 737 752 738 753 $return = $this->set_scheme($parsed['scheme']) … … 864 879 } 865 880 if (substr($ihost, 0, 1) === '[' && substr($ihost, -1) === ']') { 866 if ( Requests_IPv6::check_ipv6(substr($ihost, 1, -1))) {867 $this->ihost = '[' . Requests_IPv6::compress(substr($ihost, 1, -1)) . ']';881 if (Ipv6::check_ipv6(substr($ihost, 1, -1))) { 882 $this->ihost = '[' . Ipv6::compress(substr($ihost, 1, -1)) . ']'; 868 883 } 869 884 else { … … 986 1001 * Convert an IRI to a URI (or parts thereof) 987 1002 * 988 * @param string|bool IRI to convert (or false from {@see get_iri})1003 * @param string|bool $iri IRI to convert (or false from {@see \WpOrg\Requests\IRI::get_iri()}) 989 1004 * @return string|false URI if IRI is valid, false otherwise. 990 1005 */ 991 protected function to_uri($ string) {992 if (!is_string($ string)) {1006 protected function to_uri($iri) { 1007 if (!is_string($iri)) { 993 1008 return false; 994 1009 } … … 1000 1015 1001 1016 $position = 0; 1002 $strlen = strlen($ string);1003 while (($position += strcspn($ string, $non_ascii, $position)) < $strlen) {1004 $ string = substr_replace($string, sprintf('%%%02X', ord($string[$position])), $position, 1);1017 $strlen = strlen($iri); 1018 while (($position += strcspn($iri, $non_ascii, $position)) < $strlen) { 1019 $iri = substr_replace($iri, sprintf('%%%02X', ord($iri[$position])), $position, 1); 1005 1020 $position += 3; 1006 1021 $strlen += 2; 1007 1022 } 1008 1023 1009 return $ string;1024 return $iri; 1010 1025 } 1011 1026
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)