PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.4
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.4
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / guzzle / src / functions.php

functions.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.4, at vendor_prefixed/guzzlehttp/guzzle/src/functions.php

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