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