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