PageRenderTime 24ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/guzzlehttp/guzzle/src/functions.php

https://gitlab.com/reasonat/test8
PHP | 329 lines | 209 code | 27 blank | 93 comment | 18 complexity | 0f28c4525f3ba3b7edbd20916f0dff8e MD5 | raw file
  1. <?php
  2. namespace GuzzleHttp;
  3. use GuzzleHttp\Handler\CurlHandler;
  4. use GuzzleHttp\Handler\CurlMultiHandler;
  5. use GuzzleHttp\Handler\Proxy;
  6. use GuzzleHttp\Handler\StreamHandler;
  7. /**
  8. * Expands a URI template
  9. *
  10. * @param string $template URI template
  11. * @param array $variables Template variables
  12. *
  13. * @return string
  14. */
  15. function uri_template($template, array $variables)
  16. {
  17. if (extension_loaded('uri_template')) {
  18. // @codeCoverageIgnoreStart
  19. return \uri_template($template, $variables);
  20. // @codeCoverageIgnoreEnd
  21. }
  22. static $uriTemplate;
  23. if (!$uriTemplate) {
  24. $uriTemplate = new UriTemplate();
  25. }
  26. return $uriTemplate->expand($template, $variables);
  27. }
  28. /**
  29. * Debug function used to describe the provided value type and class.
  30. *
  31. * @param mixed $input
  32. *
  33. * @return string Returns a string containing the type of the variable and
  34. * if a class is provided, the class name.
  35. */
  36. function describe_type($input)
  37. {
  38. switch (gettype($input)) {
  39. case 'object':
  40. return 'object(' . get_class($input) . ')';
  41. case 'array':
  42. return 'array(' . count($input) . ')';
  43. default:
  44. ob_start();
  45. var_dump($input);
  46. // normalize float vs double
  47. return str_replace('double(', 'float(', rtrim(ob_get_clean()));
  48. }
  49. }
  50. /**
  51. * Parses an array of header lines into an associative array of headers.
  52. *
  53. * @param array $lines Header lines array of strings in the following
  54. * format: "Name: Value"
  55. * @return array
  56. */
  57. function headers_from_lines($lines)
  58. {
  59. $headers = [];
  60. foreach ($lines as $line) {
  61. $parts = explode(':', $line, 2);
  62. $headers[trim($parts[0])][] = isset($parts[1])
  63. ? trim($parts[1])
  64. : 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. * @throws \RuntimeException if no viable Handler is available.
  90. * @return callable Returns the best handler for the given system.
  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
  104. ? Proxy::wrapStreaming($handler, new StreamHandler())
  105. : new StreamHandler();
  106. } elseif (!$handler) {
  107. throw new \RuntimeException('GuzzleHttp requires cURL, the '
  108. . 'allow_url_fopen ini setting, or a custom HTTP handler.');
  109. }
  110. return $handler;
  111. }
  112. /**
  113. * Get the default User-Agent string to use with Guzzle
  114. *
  115. * @return string
  116. */
  117. function default_user_agent()
  118. {
  119. static $defaultAgent = '';
  120. if (!$defaultAgent) {
  121. $defaultAgent = 'GuzzleHttp/' . Client::VERSION;
  122. if (extension_loaded('curl') && function_exists('curl_version')) {
  123. $defaultAgent .= ' curl/' . \curl_version()['version'];
  124. }
  125. $defaultAgent .= ' PHP/' . PHP_VERSION;
  126. }
  127. return $defaultAgent;
  128. }
  129. /**
  130. * Returns the default cacert bundle for the current system.
  131. *
  132. * First, the openssl.cafile and curl.cainfo php.ini settings are checked.
  133. * If those settings are not configured, then the common locations for
  134. * bundles found on Red Hat, CentOS, Fedora, Ubuntu, Debian, FreeBSD, OS X
  135. * and Windows are checked. If any of these file locations are found on
  136. * disk, they will be utilized.
  137. *
  138. * Note: the result of this function is cached for subsequent calls.
  139. *
  140. * @return string
  141. * @throws \RuntimeException if no bundle can be found.
  142. */
  143. function default_ca_bundle()
  144. {
  145. static $cached = null;
  146. static $cafiles = [
  147. // Red Hat, CentOS, Fedora (provided by the ca-certificates package)
  148. '/etc/pki/tls/certs/ca-bundle.crt',
  149. // Ubuntu, Debian (provided by the ca-certificates package)
  150. '/etc/ssl/certs/ca-certificates.crt',
  151. // FreeBSD (provided by the ca_root_nss package)
  152. '/usr/local/share/certs/ca-root-nss.crt',
  153. // OS X provided by homebrew (using the default path)
  154. '/usr/local/etc/openssl/cert.pem',
  155. // Google app engine
  156. '/etc/ca-certificates.crt',
  157. // Windows?
  158. 'C:\\windows\\system32\\curl-ca-bundle.crt',
  159. 'C:\\windows\\curl-ca-bundle.crt',
  160. ];
  161. if ($cached) {
  162. return $cached;
  163. }
  164. if ($ca = ini_get('openssl.cafile')) {
  165. return $cached = $ca;
  166. }
  167. if ($ca = ini_get('curl.cainfo')) {
  168. return $cached = $ca;
  169. }
  170. foreach ($cafiles as $filename) {
  171. if (file_exists($filename)) {
  172. return $cached = $filename;
  173. }
  174. }
  175. throw new \RuntimeException(<<< EOT
  176. No system CA bundle could be found in any of the the common system locations.
  177. PHP versions earlier than 5.6 are not properly configured to use the system's
  178. CA bundle by default. In order to verify peer certificates, you will need to
  179. supply the path on disk to a certificate bundle to the 'verify' request
  180. option: http://docs.guzzlephp.org/en/latest/clients.html#verify. If you do not
  181. need a specific certificate bundle, then Mozilla provides a commonly used CA
  182. bundle which can be downloaded here (provided by the maintainer of cURL):
  183. https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt. Once
  184. you have a CA bundle available on disk, you can set the 'openssl.cafile' PHP
  185. ini setting to point to the path to the file, allowing you to omit the 'verify'
  186. request option. See http://curl.haxx.se/docs/sslcerts.html for more
  187. information.
  188. EOT
  189. );
  190. }
  191. /**
  192. * Creates an associative array of lowercase header names to the actual
  193. * header casing.
  194. *
  195. * @param array $headers
  196. *
  197. * @return array
  198. */
  199. function normalize_header_keys(array $headers)
  200. {
  201. $result = [];
  202. foreach (array_keys($headers) as $key) {
  203. $result[strtolower($key)] = $key;
  204. }
  205. return $result;
  206. }
  207. /**
  208. * Returns true if the provided host matches any of the no proxy areas.
  209. *
  210. * This method will strip a port from the host if it is present. Each pattern
  211. * can be matched with an exact match (e.g., "foo.com" == "foo.com") or a
  212. * partial match: (e.g., "foo.com" == "baz.foo.com" and ".foo.com" ==
  213. * "baz.foo.com", but ".foo.com" != "foo.com").
  214. *
  215. * Areas are matched in the following cases:
  216. * 1. "*" (without quotes) always matches any hosts.
  217. * 2. An exact match.
  218. * 3. The area starts with "." and the area is the last part of the host. e.g.
  219. * '.mit.edu' will match any host that ends with '.mit.edu'.
  220. *
  221. * @param string $host Host to check against the patterns.
  222. * @param array $noProxyArray An array of host patterns.
  223. *
  224. * @return bool
  225. */
  226. function is_host_in_noproxy($host, array $noProxyArray)
  227. {
  228. if (strlen($host) === 0) {
  229. throw new \InvalidArgumentException('Empty host provided');
  230. }
  231. // Strip port if present.
  232. if (strpos($host, ':')) {
  233. $host = explode($host, ':', 2)[0];
  234. }
  235. foreach ($noProxyArray as $area) {
  236. // Always match on wildcards.
  237. if ($area === '*') {
  238. return true;
  239. } elseif (empty($area)) {
  240. // Don't match on empty values.
  241. continue;
  242. } elseif ($area === $host) {
  243. // Exact matches.
  244. return true;
  245. } else {
  246. // Special match if the area when prefixed with ".". Remove any
  247. // existing leading "." and add a new leading ".".
  248. $area = '.' . ltrim($area, '.');
  249. if (substr($host, -(strlen($area))) === $area) {
  250. return true;
  251. }
  252. }
  253. }
  254. return false;
  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 mixed
  266. * @throws \InvalidArgumentException if the JSON cannot be decoded.
  267. * @link http://www.php.net/manual/en/function.json-decode.php
  268. */
  269. function json_decode($json, $assoc = false, $depth = 512, $options = 0)
  270. {
  271. $data = \json_decode($json, $assoc, $depth, $options);
  272. if (JSON_ERROR_NONE !== json_last_error()) {
  273. throw new \InvalidArgumentException(
  274. '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 \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 \InvalidArgumentException(
  294. 'json_encode error: ' . json_last_error_msg());
  295. }
  296. return $json;
  297. }