| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\GuzzleHttp; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Exception\InvalidArgumentException; |
| 6 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Handler\CurlHandler; |
| 7 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Handler\CurlMultiHandler; |
| 8 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Handler\Proxy; |
| 9 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Handler\StreamHandler; |
| 10 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\UriInterface; |
| 11 |
final class Utils |
| 12 |
{ |
| 13 |
/** |
| 14 |
* Debug function used to describe the provided value type and class. |
| 15 |
* |
| 16 |
* @param mixed $input |
| 17 |
* |
| 18 |
* @return string Returns a string containing the type of the variable and |
| 19 |
* if a class is provided, the class name. |
| 20 |
*/ |
| 21 |
public static function describeType($input) : string |
| 22 |
{ |
| 23 |
switch (\gettype($input)) { |
| 24 |
case 'object': |
| 25 |
return 'object(' . \get_class($input) . ')'; |
| 26 |
case 'array': |
| 27 |
return 'array(' . \count($input) . ')'; |
| 28 |
default: |
| 29 |
\ob_start(); |
| 30 |
\var_dump($input); |
| 31 |
// normalize float vs double |
| 32 |
/** @var string $varDumpContent */ |
| 33 |
$varDumpContent = \ob_get_clean(); |
| 34 |
return \str_replace('double(', 'float(', \rtrim($varDumpContent)); |
| 35 |
} |
| 36 |
} |
| 37 |
/** |
| 38 |
* Parses an array of header lines into an associative array of headers. |
| 39 |
* |
| 40 |
* @param iterable $lines Header lines array of strings in the following |
| 41 |
* format: "Name: Value" |
| 42 |
*/ |
| 43 |
public static function headersFromLines(iterable $lines) : array |
| 44 |
{ |
| 45 |
$headers = []; |
| 46 |
foreach ($lines as $line) { |
| 47 |
$parts = \explode(':', $line, 2); |
| 48 |
$headers[\trim($parts[0])][] = isset($parts[1]) ? \trim($parts[1]) : null; |
| 49 |
} |
| 50 |
return $headers; |
| 51 |
} |
| 52 |
/** |
| 53 |
* Returns a debug stream based on the provided variable. |
| 54 |
* |
| 55 |
* @param mixed $value Optional value |
| 56 |
* |
| 57 |
* @return resource |
| 58 |
*/ |
| 59 |
public static function debugResource($value = null) |
| 60 |
{ |
| 61 |
if (\is_resource($value)) { |
| 62 |
return $value; |
| 63 |
} |
| 64 |
if (\defined('STDOUT')) { |
| 65 |
return \STDOUT; |
| 66 |
} |
| 67 |
return Psr7\Utils::tryFopen('php://output', 'w'); |
| 68 |
} |
| 69 |
/** |
| 70 |
* Chooses and creates a default handler to use based on the environment. |
| 71 |
* |
| 72 |
* The returned handler is not wrapped by any default middlewares. |
| 73 |
* |
| 74 |
* @return callable(\Psr\Http\Message\RequestInterface, array): Promise\PromiseInterface Returns the best handler for the given system. |
| 75 |
* |
| 76 |
* @throws \RuntimeException if no viable Handler is available. |
| 77 |
*/ |
| 78 |
public static function chooseHandler() : callable |
| 79 |
{ |
| 80 |
$handler = null; |
| 81 |
if (\defined('CURLOPT_CUSTOMREQUEST') && \function_exists('curl_version') && \version_compare(\curl_version()['version'], '7.21.2') >= 0) { |
| 82 |
if (\function_exists('curl_multi_exec') && \function_exists('curl_exec')) { |
| 83 |
$handler = Proxy::wrapSync(new CurlMultiHandler(), new CurlHandler()); |
| 84 |
} elseif (\function_exists('curl_exec')) { |
| 85 |
$handler = new CurlHandler(); |
| 86 |
} elseif (\function_exists('curl_multi_exec')) { |
| 87 |
$handler = new CurlMultiHandler(); |
| 88 |
} |
| 89 |
} |
| 90 |
if (\ini_get('allow_url_fopen')) { |
| 91 |
$handler = $handler ? Proxy::wrapStreaming($handler, new StreamHandler()) : new StreamHandler(); |
| 92 |
} elseif (!$handler) { |
| 93 |
throw new \RuntimeException('GuzzleHttp requires cURL, the allow_url_fopen ini setting, or a custom HTTP handler.'); |
| 94 |
} |
| 95 |
return $handler; |
| 96 |
} |
| 97 |
/** |
| 98 |
* Get the default User-Agent string to use with Guzzle. |
| 99 |
*/ |
| 100 |
public static function defaultUserAgent() : string |
| 101 |
{ |
| 102 |
return \sprintf('GuzzleHttp/%d', ClientInterface::MAJOR_VERSION); |
| 103 |
} |
| 104 |
/** |
| 105 |
* Returns the default cacert bundle for the current system. |
| 106 |
* |
| 107 |
* First, the openssl.cafile and curl.cainfo php.ini settings are checked. |
| 108 |
* If those settings are not configured, then the common locations for |
| 109 |
* bundles found on Red Hat, CentOS, Fedora, Ubuntu, Debian, FreeBSD, OS X |
| 110 |
* and Windows are checked. If any of these file locations are found on |
| 111 |
* disk, they will be utilized. |
| 112 |
* |
| 113 |
* Note: the result of this function is cached for subsequent calls. |
| 114 |
* |
| 115 |
* @throws \RuntimeException if no bundle can be found. |
| 116 |
* |
| 117 |
* @deprecated Utils::defaultCaBundle will be removed in guzzlehttp/guzzle:8.0. This method is not needed in PHP 5.6+. |
| 118 |
*/ |
| 119 |
public static function defaultCaBundle() : string |
| 120 |
{ |
| 121 |
static $cached = null; |
| 122 |
static $cafiles = [ |
| 123 |
// Red Hat, CentOS, Fedora (provided by the ca-certificates package) |
| 124 |
'/etc/pki/tls/certs/ca-bundle.crt', |
| 125 |
// Ubuntu, Debian (provided by the ca-certificates package) |
| 126 |
'/etc/ssl/certs/ca-certificates.crt', |
| 127 |
// FreeBSD (provided by the ca_root_nss package) |
| 128 |
'/usr/local/share/certs/ca-root-nss.crt', |
| 129 |
// SLES 12 (provided by the ca-certificates package) |
| 130 |
'/var/lib/ca-certificates/ca-bundle.pem', |
| 131 |
// OS X provided by homebrew (using the default path) |
| 132 |
'/usr/local/etc/openssl/cert.pem', |
| 133 |
// Google app engine |
| 134 |
'/etc/ca-certificates.crt', |
| 135 |
// Windows? |
| 136 |
'C:\\windows\\system32\\curl-ca-bundle.crt', |
| 137 |
'C:\\windows\\curl-ca-bundle.crt', |
| 138 |
]; |
| 139 |
if ($cached) { |
| 140 |
return $cached; |
| 141 |
} |
| 142 |
if ($ca = \ini_get('openssl.cafile')) { |
| 143 |
return $cached = $ca; |
| 144 |
} |
| 145 |
if ($ca = \ini_get('curl.cainfo')) { |
| 146 |
return $cached = $ca; |
| 147 |
} |
| 148 |
foreach ($cafiles as $filename) { |
| 149 |
if (\file_exists($filename)) { |
| 150 |
return $cached = $filename; |
| 151 |
} |
| 152 |
} |
| 153 |
throw new \RuntimeException(<<<EOT |
| 154 |
No system CA bundle could be found in any of the the common system locations. |
| 155 |
PHP versions earlier than 5.6 are not properly configured to use the system's |
| 156 |
CA bundle by default. In order to verify peer certificates, you will need to |
| 157 |
supply the path on disk to a certificate bundle to the 'verify' request |
| 158 |
option: https://docs.guzzlephp.org/en/latest/request-options.html#verify. If |
| 159 |
you do not need a specific certificate bundle, then Mozilla provides a commonly |
| 160 |
used CA bundle which can be downloaded here (provided by the maintainer of |
| 161 |
cURL): https://curl.haxx.se/ca/cacert.pem. Once you have a CA bundle available |
| 162 |
on disk, you can set the 'openssl.cafile' PHP ini setting to point to the path |
| 163 |
to the file, allowing you to omit the 'verify' request option. See |
| 164 |
https://curl.haxx.se/docs/sslcerts.html for more information. |
| 165 |
EOT |
| 166 |
); |
| 167 |
} |
| 168 |
/** |
| 169 |
* Creates an associative array of lowercase header names to the actual |
| 170 |
* header casing. |
| 171 |
*/ |
| 172 |
public static function normalizeHeaderKeys(array $headers) : array |
| 173 |
{ |
| 174 |
$result = []; |
| 175 |
foreach (\array_keys($headers) as $key) { |
| 176 |
$result[\strtolower($key)] = $key; |
| 177 |
} |
| 178 |
return $result; |
| 179 |
} |
| 180 |
/** |
| 181 |
* Returns true if the provided host matches any of the no proxy areas. |
| 182 |
* |
| 183 |
* This method will strip a port from the host if it is present. Each pattern |
| 184 |
* can be matched with an exact match (e.g., "foo.com" == "foo.com") or a |
| 185 |
* partial match: (e.g., "foo.com" == "baz.foo.com" and ".foo.com" == |
| 186 |
* "baz.foo.com", but ".foo.com" != "foo.com"). |
| 187 |
* |
| 188 |
* Areas are matched in the following cases: |
| 189 |
* 1. "*" (without quotes) always matches any hosts. |
| 190 |
* 2. An exact match. |
| 191 |
* 3. The area starts with "." and the area is the last part of the host. e.g. |
| 192 |
* '.mit.edu' will match any host that ends with '.mit.edu'. |
| 193 |
* |
| 194 |
* @param string $host Host to check against the patterns. |
| 195 |
* @param string[] $noProxyArray An array of host patterns. |
| 196 |
* |
| 197 |
* @throws InvalidArgumentException |
| 198 |
*/ |
| 199 |
public static function isHostInNoProxy(string $host, array $noProxyArray) : bool |
| 200 |
{ |
| 201 |
if (\strlen($host) === 0) { |
| 202 |
throw new InvalidArgumentException('Empty host provided'); |
| 203 |
} |
| 204 |
// Strip port if present. |
| 205 |
[$host] = \explode(':', $host, 2); |
| 206 |
foreach ($noProxyArray as $area) { |
| 207 |
// Always match on wildcards. |
| 208 |
if ($area === '*') { |
| 209 |
return \true; |
| 210 |
} |
| 211 |
if (empty($area)) { |
| 212 |
// Don't match on empty values. |
| 213 |
continue; |
| 214 |
} |
| 215 |
if ($area === $host) { |
| 216 |
// Exact matches. |
| 217 |
return \true; |
| 218 |
} |
| 219 |
// Special match if the area when prefixed with ".". Remove any |
| 220 |
// existing leading "." and add a new leading ".". |
| 221 |
$area = '.' . \ltrim($area, '.'); |
| 222 |
if (\substr($host, -\strlen($area)) === $area) { |
| 223 |
return \true; |
| 224 |
} |
| 225 |
} |
| 226 |
return \false; |
| 227 |
} |
| 228 |
/** |
| 229 |
* Wrapper for json_decode that throws when an error occurs. |
| 230 |
* |
| 231 |
* @param string $json JSON data to parse |
| 232 |
* @param bool $assoc When true, returned objects will be converted |
| 233 |
* into associative arrays. |
| 234 |
* @param int $depth User specified recursion depth. |
| 235 |
* @param int $options Bitmask of JSON decode options. |
| 236 |
* |
| 237 |
* @return object|array|string|int|float|bool|null |
| 238 |
* |
| 239 |
* @throws InvalidArgumentException if the JSON cannot be decoded. |
| 240 |
* |
| 241 |
* @see https://www.php.net/manual/en/function.json-decode.php |
| 242 |
*/ |
| 243 |
public static function jsonDecode(string $json, bool $assoc = \false, int $depth = 512, int $options = 0) |
| 244 |
{ |
| 245 |
$data = \json_decode($json, $assoc, $depth, $options); |
| 246 |
if (\JSON_ERROR_NONE !== \json_last_error()) { |
| 247 |
throw new InvalidArgumentException('json_decode error: ' . \json_last_error_msg()); |
| 248 |
} |
| 249 |
return $data; |
| 250 |
} |
| 251 |
/** |
| 252 |
* Wrapper for JSON encoding that throws when an error occurs. |
| 253 |
* |
| 254 |
* @param mixed $value The value being encoded |
| 255 |
* @param int $options JSON encode option bitmask |
| 256 |
* @param int $depth Set the maximum depth. Must be greater than zero. |
| 257 |
* |
| 258 |
* @throws InvalidArgumentException if the JSON cannot be encoded. |
| 259 |
* |
| 260 |
* @see https://www.php.net/manual/en/function.json-encode.php |
| 261 |
*/ |
| 262 |
public static function jsonEncode($value, int $options = 0, int $depth = 512) : string |
| 263 |
{ |
| 264 |
$json = \json_encode($value, $options, $depth); |
| 265 |
if (\JSON_ERROR_NONE !== \json_last_error()) { |
| 266 |
throw new InvalidArgumentException('json_encode error: ' . \json_last_error_msg()); |
| 267 |
} |
| 268 |
/** @var string */ |
| 269 |
return $json; |
| 270 |
} |
| 271 |
/** |
| 272 |
* Wrapper for the hrtime() or microtime() functions |
| 273 |
* (depending on the PHP version, one of the two is used) |
| 274 |
* |
| 275 |
* @return float UNIX timestamp |
| 276 |
* |
| 277 |
* @internal |
| 278 |
*/ |
| 279 |
public static function currentTime() : float |
| 280 |
{ |
| 281 |
return (float) \function_exists('hrtime') ? \hrtime(\true) / 1000000000.0 : \microtime(\true); |
| 282 |
} |
| 283 |
/** |
| 284 |
* @throws InvalidArgumentException |
| 285 |
* |
| 286 |
* @internal |
| 287 |
*/ |
| 288 |
public static function idnUriConvert(UriInterface $uri, int $options = 0) : UriInterface |
| 289 |
{ |
| 290 |
if ($uri->getHost()) { |
| 291 |
$asciiHost = self::idnToAsci($uri->getHost(), $options, $info); |
| 292 |
if ($asciiHost === \false) { |
| 293 |
$errorBitSet = $info['errors'] ?? 0; |
| 294 |
$errorConstants = \array_filter(\array_keys(\get_defined_constants()), static function (string $name) : bool { |
| 295 |
return \substr($name, 0, 11) === 'IDNA_ERROR_'; |
| 296 |
}); |
| 297 |
$errors = []; |
| 298 |
foreach ($errorConstants as $errorConstant) { |
| 299 |
if ($errorBitSet & \constant($errorConstant)) { |
| 300 |
$errors[] = $errorConstant; |
| 301 |
} |
| 302 |
} |
| 303 |
$errorMessage = 'IDN conversion failed'; |
| 304 |
if ($errors) { |
| 305 |
$errorMessage .= ' (errors: ' . \implode(', ', $errors) . ')'; |
| 306 |
} |
| 307 |
throw new InvalidArgumentException($errorMessage); |
| 308 |
} |
| 309 |
if ($uri->getHost() !== $asciiHost) { |
| 310 |
// Replace URI only if the ASCII version is different |
| 311 |
$uri = $uri->withHost($asciiHost); |
| 312 |
} |
| 313 |
} |
| 314 |
return $uri; |
| 315 |
} |
| 316 |
/** |
| 317 |
* @internal |
| 318 |
*/ |
| 319 |
public static function getenv(string $name) : ?string |
| 320 |
{ |
| 321 |
if (isset($_SERVER[$name])) { |
| 322 |
return (string) $_SERVER[$name]; |
| 323 |
} |
| 324 |
if (\PHP_SAPI === 'cli' && ($value = \getenv($name)) !== \false && $value !== null) { |
| 325 |
return (string) $value; |
| 326 |
} |
| 327 |
return null; |
| 328 |
} |
| 329 |
/** |
| 330 |
* @return string|false |
| 331 |
*/ |
| 332 |
private static function idnToAsci(string $domain, int $options, ?array &$info = []) |
| 333 |
{ |
| 334 |
if (\function_exists('idn_to_ascii') && \defined('INTL_IDNA_VARIANT_UTS46')) { |
| 335 |
return \idn_to_ascii($domain, $options, \INTL_IDNA_VARIANT_UTS46, $info); |
| 336 |
} |
| 337 |
throw new \Error('ext-idn or symfony/polyfill-intl-idn not loaded or too old'); |
| 338 |
} |
| 339 |
} |
| 340 |
|