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