Open Monograph Press  3.3.0
guzzlehttp/guzzle/src/functions.php
1 <?php
2 namespace GuzzleHttp;
3 
8 
17 function uri_template($template, array $variables)
18 {
19  if (extension_loaded('uri_template')) {
20  // @codeCoverageIgnoreStart
21  return \uri_template($template, $variables);
22  // @codeCoverageIgnoreEnd
23  }
24 
25  static $uriTemplate;
26  if (!$uriTemplate) {
27  $uriTemplate = new UriTemplate();
28  }
29 
30  return $uriTemplate->expand($template, $variables);
31 }
32 
41 function describe_type($input)
42 {
43  switch (gettype($input)) {
44  case 'object':
45  return 'object(' . get_class($input) . ')';
46  case 'array':
47  return 'array(' . count($input) . ')';
48  default:
49  ob_start();
50  var_dump($input);
51  // normalize float vs double
52  return str_replace('double(', 'float(', rtrim(ob_get_clean()));
53  }
54 }
55 
63 function headers_from_lines($lines)
64 {
65  $headers = [];
66 
67  foreach ($lines as $line) {
68  $parts = explode(':', $line, 2);
69  $headers[trim($parts[0])][] = isset($parts[1])
70  ? trim($parts[1])
71  : null;
72  }
73 
74  return $headers;
75 }
76 
84 function debug_resource($value = null)
85 {
86  if (is_resource($value)) {
87  return $value;
88  } elseif (defined('STDOUT')) {
89  return STDOUT;
90  }
91 
92  return fopen('php://output', 'w');
93 }
94 
103 function choose_handler()
104 {
105  $handler = null;
106  if (function_exists('curl_multi_exec') && function_exists('curl_exec')) {
107  $handler = Proxy::wrapSync(new CurlMultiHandler(), new CurlHandler());
108  } elseif (function_exists('curl_exec')) {
109  $handler = new CurlHandler();
110  } elseif (function_exists('curl_multi_exec')) {
111  $handler = new CurlMultiHandler();
112  }
113 
114  if (ini_get('allow_url_fopen')) {
115  $handler = $handler
116  ? Proxy::wrapStreaming($handler, new StreamHandler())
117  : new StreamHandler();
118  } elseif (!$handler) {
119  throw new \RuntimeException('GuzzleHttp requires cURL, the '
120  . 'allow_url_fopen ini setting, or a custom HTTP handler.');
121  }
122 
123  return $handler;
124 }
125 
132 {
133  static $defaultAgent = '';
134 
135  if (!$defaultAgent) {
136  $defaultAgent = 'GuzzleHttp/' . Client::VERSION;
137  if (extension_loaded('curl') && function_exists('curl_version')) {
138  $defaultAgent .= ' curl/' . \curl_version()['version'];
139  }
140  $defaultAgent .= ' PHP/' . PHP_VERSION;
141  }
142 
143  return $defaultAgent;
144 }
145 
161 {
162  static $cached = null;
163  static $cafiles = [
164  // Red Hat, CentOS, Fedora (provided by the ca-certificates package)
165  '/etc/pki/tls/certs/ca-bundle.crt',
166  // Ubuntu, Debian (provided by the ca-certificates package)
167  '/etc/ssl/certs/ca-certificates.crt',
168  // FreeBSD (provided by the ca_root_nss package)
169  '/usr/local/share/certs/ca-root-nss.crt',
170  // SLES 12 (provided by the ca-certificates package)
171  '/var/lib/ca-certificates/ca-bundle.pem',
172  // OS X provided by homebrew (using the default path)
173  '/usr/local/etc/openssl/cert.pem',
174  // Google app engine
175  '/etc/ca-certificates.crt',
176  // Windows?
177  'C:\\windows\\system32\\curl-ca-bundle.crt',
178  'C:\\windows\\curl-ca-bundle.crt',
179  ];
180 
181  if ($cached) {
182  return $cached;
183  }
184 
185  if ($ca = ini_get('openssl.cafile')) {
186  return $cached = $ca;
187  }
188 
189  if ($ca = ini_get('curl.cainfo')) {
190  return $cached = $ca;
191  }
192 
193  foreach ($cafiles as $filename) {
194  if (file_exists($filename)) {
195  return $cached = $filename;
196  }
197  }
198 
199  throw new \RuntimeException(
200  <<< EOT
201 No system CA bundle could be found in any of the the common system locations.
202 PHP versions earlier than 5.6 are not properly configured to use the system's
203 CA bundle by default. In order to verify peer certificates, you will need to
204 supply the path on disk to a certificate bundle to the 'verify' request
205 option: http://docs.guzzlephp.org/en/latest/clients.html#verify. If you do not
206 need a specific certificate bundle, then Mozilla provides a commonly used CA
207 bundle which can be downloaded here (provided by the maintainer of cURL):
208 https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt. Once
209 you have a CA bundle available on disk, you can set the 'openssl.cafile' PHP
210 ini setting to point to the path to the file, allowing you to omit the 'verify'
211 request option. See http://curl.haxx.se/docs/sslcerts.html for more
212 information.
213 EOT
214  );
215 }
216 
225 function normalize_header_keys(array $headers)
226 {
227  $result = [];
228  foreach (array_keys($headers) as $key) {
229  $result[strtolower($key)] = $key;
230  }
231 
232  return $result;
233 }
234 
254 function is_host_in_noproxy($host, array $noProxyArray)
255 {
256  if (strlen($host) === 0) {
257  throw new \InvalidArgumentException('Empty host provided');
258  }
259 
260  // Strip port if present.
261  if (strpos($host, ':')) {
262  $host = explode($host, ':', 2)[0];
263  }
264 
265  foreach ($noProxyArray as $area) {
266  // Always match on wildcards.
267  if ($area === '*') {
268  return true;
269  } elseif (empty($area)) {
270  // Don't match on empty values.
271  continue;
272  } elseif ($area === $host) {
273  // Exact matches.
274  return true;
275  } else {
276  // Special match if the area when prefixed with ".". Remove any
277  // existing leading "." and add a new leading ".".
278  $area = '.' . ltrim($area, '.');
279  if (substr($host, -(strlen($area))) === $area) {
280  return true;
281  }
282  }
283  }
284 
285  return false;
286 }
287 
301 function json_decode($json, $assoc = false, $depth = 512, $options = 0)
302 {
303  $data = \json_decode($json, $assoc, $depth, $options);
304  if (JSON_ERROR_NONE !== json_last_error()) {
306  'json_decode error: ' . json_last_error_msg()
307  );
308  }
309 
310  return $data;
311 }
312 
324 function json_encode($value, $options = 0, $depth = 512)
325 {
326  $json = \json_encode($value, $options, $depth);
327  if (JSON_ERROR_NONE !== json_last_error()) {
329  'json_encode error: ' . json_last_error_msg()
330  );
331  }
332 
333  return $json;
334 }
GuzzleHttp
Definition: paymethod/paypal/vendor/guzzlehttp/guzzle/src/Client.php:2
GuzzleHttp\Handler\CurlMultiHandler
Definition: CurlMultiHandler.php:18
GuzzleHttp\Promise\any
any($promises)
Definition: guzzlehttp/promises/src/functions.php:293
GuzzleHttp\UriTemplate
Definition: vendor/guzzlehttp/guzzle/src/UriTemplate.php:9
GuzzleHttp\Handler\Proxy\wrapStreaming
static wrapStreaming(callable $default, callable $streaming)
Definition: Proxy.php:45
GuzzleHttp\debug_resource
debug_resource($value=null)
Definition: guzzlehttp/guzzle/src/functions.php:84
GuzzleHttp\describe_type
describe_type($input)
Definition: guzzlehttp/guzzle/src/functions.php:41
GuzzleHttp\Handler\Proxy\wrapSync
static wrapSync(callable $default, callable $sync)
Definition: Proxy.php:21
GuzzleHttp\json_decode
json_decode($json, $assoc=false, $depth=512, $options=0)
Definition: guzzlehttp/guzzle/src/functions.php:301
GuzzleHttp\headers_from_lines
headers_from_lines($lines)
Definition: guzzlehttp/guzzle/src/functions.php:63
GuzzleHttp\choose_handler
choose_handler()
Definition: guzzlehttp/guzzle/src/functions.php:103
GuzzleHttp\Handler\CurlHandler
Definition: CurlHandler.php:14
GuzzleHttp\uri_template
uri_template($template, array $variables)
Definition: guzzlehttp/guzzle/src/functions.php:17
GuzzleHttp\ClientInterface\VERSION
const VERSION
Definition: vendor/guzzlehttp/guzzle/src/ClientInterface.php:18
GuzzleHttp\default_user_agent
default_user_agent()
Definition: guzzlehttp/guzzle/src/functions.php:131
GuzzleHttp\Handler\StreamHandler
Definition: StreamHandler.php:18
GuzzleHttp\Handler\Proxy
Definition: Proxy.php:10
GuzzleHttp\json_encode
json_encode($value, $options=0, $depth=512)
Definition: guzzlehttp/guzzle/src/functions.php:324
GuzzleHttp\default_ca_bundle
default_ca_bundle()
Definition: guzzlehttp/guzzle/src/functions.php:160
GuzzleHttp\Exception\InvalidArgumentException
Definition: vendor/guzzlehttp/guzzle/src/Exception/InvalidArgumentException.php:5