PluginProbe
Media Cloud Sync / 1.0.2
Media Cloud Sync v1.0.2
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Utils.php

Utils.php in Media Cloud Sync 1.0.2, at includes/sdk/s3/GuzzleHttp/Utils.php

83 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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