| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\GuzzleHttp; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Exception\InvalidArgumentException; |
| 6 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\UriInterface; |
| 7 |
use Dudlewebs\WPMCS\s3\Symfony\Polyfill\Intl\Idn\Idn; |
| 8 |
final class Utils |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Wrapper for the hrtime() or microtime() functions |
| 12 |
* (depending on the PHP version, one of the two is used) |
| 13 |
* |
| 14 |
* @return float|mixed UNIX timestamp |
| 15 |
* |
| 16 |
* @internal |
| 17 |
*/ |
| 18 |
public static function currentTime() |
| 19 |
{ |
| 20 |
return \function_exists('hrtime') ? \hrtime(\true) / 1000000000.0 : \microtime(\true); |
| 21 |
} |
| 22 |
/** |
| 23 |
* @param int $options |
| 24 |
* |
| 25 |
* @return UriInterface |
| 26 |
* @throws InvalidArgumentException |
| 27 |
* |
| 28 |
* @internal |
| 29 |
*/ |
| 30 |
public static function idnUriConvert(UriInterface $uri, $options = 0) |
| 31 |
{ |
| 32 |
if ($uri->getHost()) { |
| 33 |
$asciiHost = self::idnToAsci($uri->getHost(), $options, $info); |
| 34 |
if ($asciiHost === \false) { |
| 35 |
$errorBitSet = isset($info['errors']) ? $info['errors'] : 0; |
| 36 |
$errorConstants = \array_filter(\array_keys(\get_defined_constants()), function ($name) { |
| 37 |
return \substr($name, 0, 11) === 'IDNA_ERROR_'; |
| 38 |
}); |
| 39 |
$errors = []; |
| 40 |
foreach ($errorConstants as $errorConstant) { |
| 41 |
if ($errorBitSet & \constant($errorConstant)) { |
| 42 |
$errors[] = $errorConstant; |
| 43 |
} |
| 44 |
} |
| 45 |
$errorMessage = 'IDN conversion failed'; |
| 46 |
if ($errors) { |
| 47 |
$errorMessage .= ' (errors: ' . \implode(', ', $errors) . ')'; |
| 48 |
} |
| 49 |
throw new InvalidArgumentException($errorMessage); |
| 50 |
} else { |
| 51 |
if ($uri->getHost() !== $asciiHost) { |
| 52 |
// Replace URI only if the ASCII version is different |
| 53 |
$uri = $uri->withHost($asciiHost); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
return $uri; |
| 58 |
} |
| 59 |
/** |
| 60 |
* @param string $domain |
| 61 |
* @param int $options |
| 62 |
* @param array $info |
| 63 |
* |
| 64 |
* @return string|false |
| 65 |
*/ |
| 66 |
private static function idnToAsci($domain, $options, &$info = []) |
| 67 |
{ |
| 68 |
if (\preg_match('%^[ -~]+$%', $domain) === 1) { |
| 69 |
return $domain; |
| 70 |
} |
| 71 |
if (\extension_loaded('intl') && \defined('INTL_IDNA_VARIANT_UTS46')) { |
| 72 |
return \idn_to_ascii($domain, $options, \INTL_IDNA_VARIANT_UTS46, $info); |
| 73 |
} |
| 74 |
/* |
| 75 |
* The Idn class is marked as @internal. Verify that class and method exists. |
| 76 |
*/ |
| 77 |
if (\method_exists(Idn::class, 'idn_to_ascii')) { |
| 78 |
return Idn::idn_to_ascii($domain, $options, Idn::INTL_IDNA_VARIANT_UTS46, $info); |
| 79 |
} |
| 80 |
throw new \RuntimeException('ext-intl or symfony/polyfill-intl-idn not loaded or too old'); |
| 81 |
} |
| 82 |
} |
| 83 |
|