暂无描述

class-wp-http-proxy.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * HTTP API: WP_HTTP_Proxy class
  4. *
  5. * @package WordPress
  6. * @subpackage HTTP
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to implement HTTP API proxy support.
  11. *
  12. * There are caveats to proxy support. It requires that defines be made in the wp-config.php file to
  13. * enable proxy support. There are also a few filters that plugins can hook into for some of the
  14. * constants.
  15. *
  16. * Please note that only BASIC authentication is supported by most transports.
  17. * cURL MAY support more methods (such as NTLM authentication) depending on your environment.
  18. *
  19. * The constants are as follows:
  20. * <ol>
  21. * <li>WP_PROXY_HOST - Enable proxy support and host for connecting.</li>
  22. * <li>WP_PROXY_PORT - Proxy port for connection. No default, must be defined.</li>
  23. * <li>WP_PROXY_USERNAME - Proxy username, if it requires authentication.</li>
  24. * <li>WP_PROXY_PASSWORD - Proxy password, if it requires authentication.</li>
  25. * <li>WP_PROXY_BYPASS_HOSTS - Will prevent the hosts in this list from going through the proxy.
  26. * You do not need to have localhost and the site host in this list, because they will not be passed
  27. * through the proxy. The list should be presented in a comma separated list, wildcards using * are supported. Example: *.wordpress.org</li>
  28. * </ol>
  29. *
  30. * An example can be as seen below.
  31. *
  32. * define('WP_PROXY_HOST', '192.168.84.101');
  33. * define('WP_PROXY_PORT', '8080');
  34. * define('WP_PROXY_BYPASS_HOSTS', 'localhost, www.example.com, *.wordpress.org');
  35. *
  36. * @link https://core.trac.wordpress.org/ticket/4011 Proxy support ticket in WordPress.
  37. * @link https://core.trac.wordpress.org/ticket/14636 Allow wildcard domains in WP_PROXY_BYPASS_HOSTS
  38. *
  39. * @since 2.8.0
  40. */
  41. class WP_HTTP_Proxy {
  42. /**
  43. * Whether proxy connection should be used.
  44. *
  45. * Constants which control this behaviour:
  46. *
  47. * - `WP_PROXY_HOST`
  48. * - `WP_PROXY_PORT`
  49. *
  50. * @since 2.8.0
  51. *
  52. * @return bool
  53. */
  54. public function is_enabled() {
  55. return defined( 'WP_PROXY_HOST' ) && defined( 'WP_PROXY_PORT' );
  56. }
  57. /**
  58. * Whether authentication should be used.
  59. *
  60. * Constants which control this behaviour:
  61. *
  62. * - `WP_PROXY_USERNAME`
  63. * - `WP_PROXY_PASSWORD`
  64. *
  65. * @since 2.8.0
  66. *
  67. * @return bool
  68. */
  69. public function use_authentication() {
  70. return defined( 'WP_PROXY_USERNAME' ) && defined( 'WP_PROXY_PASSWORD' );
  71. }
  72. /**
  73. * Retrieve the host for the proxy server.
  74. *
  75. * @since 2.8.0
  76. *
  77. * @return string
  78. */
  79. public function host() {
  80. if ( defined( 'WP_PROXY_HOST' ) ) {
  81. return WP_PROXY_HOST;
  82. }
  83. return '';
  84. }
  85. /**
  86. * Retrieve the port for the proxy server.
  87. *
  88. * @since 2.8.0
  89. *
  90. * @return string
  91. */
  92. public function port() {
  93. if ( defined( 'WP_PROXY_PORT' ) ) {
  94. return WP_PROXY_PORT;
  95. }
  96. return '';
  97. }
  98. /**
  99. * Retrieve the username for proxy authentication.
  100. *
  101. * @since 2.8.0
  102. *
  103. * @return string
  104. */
  105. public function username() {
  106. if ( defined( 'WP_PROXY_USERNAME' ) ) {
  107. return WP_PROXY_USERNAME;
  108. }
  109. return '';
  110. }
  111. /**
  112. * Retrieve the password for proxy authentication.
  113. *
  114. * @since 2.8.0
  115. *
  116. * @return string
  117. */
  118. public function password() {
  119. if ( defined( 'WP_PROXY_PASSWORD' ) ) {
  120. return WP_PROXY_PASSWORD;
  121. }
  122. return '';
  123. }
  124. /**
  125. * Retrieve authentication string for proxy authentication.
  126. *
  127. * @since 2.8.0
  128. *
  129. * @return string
  130. */
  131. public function authentication() {
  132. return $this->username() . ':' . $this->password();
  133. }
  134. /**
  135. * Retrieve header string for proxy authentication.
  136. *
  137. * @since 2.8.0
  138. *
  139. * @return string
  140. */
  141. public function authentication_header() {
  142. return 'Proxy-Authorization: Basic ' . base64_encode( $this->authentication() );
  143. }
  144. /**
  145. * Determines whether the request should be sent through a proxy.
  146. *
  147. * We want to keep localhost and the site URL from being sent through the proxy, because
  148. * some proxies can not handle this. We also have the constant available for defining other
  149. * hosts that won't be sent through the proxy.
  150. *
  151. * @since 2.8.0
  152. *
  153. * @param string $uri URL of the request.
  154. * @return bool Whether to send the request through the proxy.
  155. */
  156. public function send_through_proxy( $uri ) {
  157. $check = parse_url( $uri );
  158. // Malformed URL, can not process, but this could mean ssl, so let through anyway.
  159. if ( false === $check ) {
  160. return true;
  161. }
  162. $home = parse_url( get_option( 'siteurl' ) );
  163. /**
  164. * Filters whether to preempt sending the request through the proxy.
  165. *
  166. * Returning false will bypass the proxy; returning true will send
  167. * the request through the proxy. Returning null bypasses the filter.
  168. *
  169. * @since 3.5.0
  170. *
  171. * @param bool|null $override Whether to send the request through the proxy. Default null.
  172. * @param string $uri URL of the request.
  173. * @param array $check Associative array result of parsing the request URL with `parse_url()`.
  174. * @param array $home Associative array result of parsing the site URL with `parse_url()`.
  175. */
  176. $result = apply_filters( 'pre_http_send_through_proxy', null, $uri, $check, $home );
  177. if ( ! is_null( $result ) ) {
  178. return $result;
  179. }
  180. if ( 'localhost' === $check['host'] || ( isset( $home['host'] ) && $home['host'] === $check['host'] ) ) {
  181. return false;
  182. }
  183. if ( ! defined( 'WP_PROXY_BYPASS_HOSTS' ) ) {
  184. return true;
  185. }
  186. static $bypass_hosts = null;
  187. static $wildcard_regex = array();
  188. if ( null === $bypass_hosts ) {
  189. $bypass_hosts = preg_split( '|,\s*|', WP_PROXY_BYPASS_HOSTS );
  190. if ( false !== strpos( WP_PROXY_BYPASS_HOSTS, '*' ) ) {
  191. $wildcard_regex = array();
  192. foreach ( $bypass_hosts as $host ) {
  193. $wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) );
  194. }
  195. $wildcard_regex = '/^(' . implode( '|', $wildcard_regex ) . ')$/i';
  196. }
  197. }
  198. if ( ! empty( $wildcard_regex ) ) {
  199. return ! preg_match( $wildcard_regex, $check['host'] );
  200. } else {
  201. return ! in_array( $check['host'], $bypass_hosts, true );
  202. }
  203. }
  204. }