Sin descripción

functions.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. use Psr\Http\Message\StreamInterface;
  8. /**
  9. * Expands a URI template
  10. *
  11. * @param string $template URI template
  12. * @param array $variables Template variables
  13. *
  14. * @return string
  15. */
  16. function uri_template($template, array $variables)
  17. {
  18. if (extension_loaded('uri_template')) {
  19. // @codeCoverageIgnoreStart
  20. return \uri_template($template, $variables);
  21. // @codeCoverageIgnoreEnd
  22. }
  23. static $uriTemplate;
  24. if (!$uriTemplate) {
  25. $uriTemplate = new UriTemplate();
  26. }
  27. return $uriTemplate->expand($template, $variables);
  28. }
  29. /**
  30. * Debug function used to describe the provided value type and class.
  31. *
  32. * @param mixed $input
  33. *
  34. * @return string Returns a string containing the type of the variable and
  35. * if a class is provided, the class name.
  36. */
  37. function describe_type($input)
  38. {
  39. switch (gettype($input)) {
  40. case 'object':
  41. return 'object(' . get_class($input) . ')';
  42. case 'array':
  43. return 'array(' . count($input) . ')';
  44. default:
  45. ob_start();
  46. var_dump($input);
  47. // normalize float vs double
  48. return str_replace('double(', 'float(', rtrim(ob_get_clean()));
  49. }
  50. }
  51. /**
  52. * Parses an array of header lines into an associative array of headers.
  53. *
  54. * @param array $lines Header lines array of strings in the following
  55. * format: "Name: Value"
  56. * @return array
  57. */
  58. function headers_from_lines($lines)
  59. {
  60. $headers = [];
  61. foreach ($lines as $line) {
  62. $parts = explode(':', $line, 2);
  63. $headers[trim($parts[0])][] = isset($parts[1])
  64. ? trim($parts[1])
  65. : null;
  66. }
  67. return $headers;
  68. }
  69. /**
  70. * Returns a debug stream based on the provided variable.
  71. *
  72. * @param mixed $value Optional value
  73. *
  74. * @return resource
  75. */
  76. function debug_resource($value = null)
  77. {
  78. if (is_resource($value)) {
  79. return $value;
  80. } elseif (defined('STDOUT')) {
  81. return STDOUT;
  82. }
  83. return fopen('php://output', 'w');
  84. }
  85. /**
  86. * Chooses and creates a default handler to use based on the environment.
  87. *
  88. * The returned handler is not wrapped by any default middlewares.
  89. *
  90. * @throws \RuntimeException if no viable Handler is available.
  91. * @return callable Returns the best handler for the given system.
  92. */
  93. function choose_handler()
  94. {
  95. $handler = null;
  96. if (extension_loaded('curl')) {
  97. $handler = Proxy::wrapSync(new CurlMultiHandler(), new CurlHandler());
  98. }
  99. if (ini_get('allow_url_fopen')) {
  100. $handler = $handler
  101. ? Proxy::wrapStreaming($handler, new StreamHandler())
  102. : new StreamHandler();
  103. } elseif (!$handler) {
  104. throw new \RuntimeException('GuzzleHttp requires cURL, the '
  105. . '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. // OS X provided by homebrew (using the default path)
  151. '/usr/local/etc/openssl/cert.pem',
  152. // Google app engine
  153. '/etc/ca-certificates.crt',
  154. // Windows?
  155. 'C:\\windows\\system32\\curl-ca-bundle.crt',
  156. 'C:\\windows\\curl-ca-bundle.crt',
  157. ];
  158. if ($cached) {
  159. return $cached;
  160. }
  161. if ($ca = ini_get('openssl.cafile')) {
  162. return $cached = $ca;
  163. }
  164. if ($ca = ini_get('curl.cainfo')) {
  165. return $cached = $ca;
  166. }
  167. foreach ($cafiles as $filename) {
  168. if (file_exists($filename)) {
  169. return $cached = $filename;
  170. }
  171. }
  172. throw new \RuntimeException(<<< EOT
  173. No system CA bundle could be found in any of the the common system locations.
  174. PHP versions earlier than 5.6 are not properly configured to use the system's
  175. CA bundle by default. In order to verify peer certificates, you will need to
  176. supply the path on disk to a certificate bundle to the 'verify' request
  177. option: http://docs.guzzlephp.org/en/latest/clients.html#verify. If you do not
  178. need a specific certificate bundle, then Mozilla provides a commonly used CA
  179. bundle which can be downloaded here (provided by the maintainer of cURL):
  180. https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt. Once
  181. you have a CA bundle available on disk, you can set the 'openssl.cafile' PHP
  182. ini setting to point to the path to the file, allowing you to omit the 'verify'
  183. request option. See http://curl.haxx.se/docs/sslcerts.html for more
  184. information.
  185. EOT
  186. );
  187. }
  188. /**
  189. * Creates an associative array of lowercase header names to the actual
  190. * header casing.
  191. *
  192. * @param array $headers
  193. *
  194. * @return array
  195. */
  196. function normalize_header_keys(array $headers)
  197. {
  198. $result = [];
  199. foreach (array_keys($headers) as $key) {
  200. $result[strtolower($key)] = $key;
  201. }
  202. return $result;
  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 array $noProxyArray An array of host patterns.
  220. *
  221. * @return bool
  222. */
  223. function is_host_in_noproxy($host, array $noProxyArray)
  224. {
  225. if (strlen($host) === 0) {
  226. throw new \InvalidArgumentException('Empty host provided');
  227. }
  228. // Strip port if present.
  229. if (strpos($host, ':')) {
  230. $host = explode($host, ':', 2)[0];
  231. }
  232. foreach ($noProxyArray as $area) {
  233. // Always match on wildcards.
  234. if ($area === '*') {
  235. return true;
  236. } elseif (empty($area)) {
  237. // Don't match on empty values.
  238. continue;
  239. } elseif ($area === $host) {
  240. // Exact matches.
  241. return true;
  242. } else {
  243. // Special match if the area when prefixed with ".". Remove any
  244. // existing leading "." and add a new leading ".".
  245. $area = '.' . ltrim($area, '.');
  246. if (substr($host, -(strlen($area))) === $area) {
  247. return true;
  248. }
  249. }
  250. }
  251. return false;
  252. }