class-wp-http-proxy.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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, eg. *.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. * @since 2.8.0
  46. *
  47. * @use WP_PROXY_HOST
  48. * @use WP_PROXY_PORT
  49. *
  50. * @return bool
  51. */
  52. public function is_enabled() {
  53. return defined( 'WP_PROXY_HOST' ) && defined( 'WP_PROXY_PORT' );
  54. }
  55. /**
  56. * Whether authentication should be used.
  57. *
  58. * @since 2.8.0
  59. *
  60. * @use WP_PROXY_USERNAME
  61. * @use WP_PROXY_PASSWORD
  62. *
  63. * @return bool
  64. */
  65. public function use_authentication() {
  66. return defined( 'WP_PROXY_USERNAME' ) && defined( 'WP_PROXY_PASSWORD' );
  67. }
  68. /**
  69. * Retrieve the host for the proxy server.
  70. *
  71. * @since 2.8.0
  72. *
  73. * @return string
  74. */
  75. public function host() {
  76. if ( defined( 'WP_PROXY_HOST' ) ) {
  77. return WP_PROXY_HOST;
  78. }
  79. return '';
  80. }
  81. /**
  82. * Retrieve the port for the proxy server.
  83. *
  84. * @since 2.8.0
  85. *
  86. * @return string
  87. */
  88. public function port() {
  89. if ( defined( 'WP_PROXY_PORT' ) ) {
  90. return WP_PROXY_PORT;
  91. }
  92. return '';
  93. }
  94. /**
  95. * Retrieve the username for proxy authentication.
  96. *
  97. * @since 2.8.0
  98. *
  99. * @return string
  100. */
  101. public function username() {
  102. if ( defined( 'WP_PROXY_USERNAME' ) ) {
  103. return WP_PROXY_USERNAME;
  104. }
  105. return '';
  106. }
  107. /**
  108. * Retrieve the password for proxy authentication.
  109. *
  110. * @since 2.8.0
  111. *
  112. * @return string
  113. */
  114. public function password() {
  115. if ( defined( 'WP_PROXY_PASSWORD' ) ) {
  116. return WP_PROXY_PASSWORD;
  117. }
  118. return '';
  119. }
  120. /**
  121. * Retrieve authentication string for proxy authentication.
  122. *
  123. * @since 2.8.0
  124. *
  125. * @return string
  126. */
  127. public function authentication() {
  128. return $this->username() . ':' . $this->password();
  129. }
  130. /**
  131. * Retrieve header string for proxy authentication.
  132. *
  133. * @since 2.8.0
  134. *
  135. * @return string
  136. */
  137. public function authentication_header() {
  138. return 'Proxy-Authorization: Basic ' . base64_encode( $this->authentication() );
  139. }
  140. /**
  141. * Determines whether the request should be sent through a proxy.
  142. *
  143. * We want to keep localhost and the site URL from being sent through the proxy, because
  144. * some proxies can not handle this. We also have the constant available for defining other
  145. * hosts that won't be sent through the proxy.
  146. *
  147. * @since 2.8.0
  148. *
  149. * @staticvar array|null $bypass_hosts
  150. * @staticvar array $wildcard_regex
  151. *
  152. * @param string $uri URI to check.
  153. * @return bool True, to send through the proxy and false if, the proxy should not be used.
  154. */
  155. public function send_through_proxy( $uri ) {
  156. /*
  157. * parse_url() only handles http, https type URLs, and will emit E_WARNING on failure.
  158. * This will be displayed on sites, which is not reasonable.
  159. */
  160. $check = @parse_url( $uri );
  161. // Malformed URL, can not process, but this could mean ssl, so let through anyway.
  162. if ( $check === false ) {
  163. return true;
  164. }
  165. $home = parse_url( get_option( 'siteurl' ) );
  166. /**
  167. * Filters whether to preempt sending the request through the proxy.
  168. *
  169. * Returning false will bypass the proxy; returning true will send
  170. * the request through the proxy. Returning null bypasses the filter.
  171. *
  172. * @since 3.5.0
  173. *
  174. * @param bool|null $override Whether to override the request result. Default null.
  175. * @param string $uri URL to check.
  176. * @param array $check Associative array result of parsing the request URI.
  177. * @param array $home Associative array result of parsing the site URL.
  178. */
  179. $result = apply_filters( 'pre_http_send_through_proxy', null, $uri, $check, $home );
  180. if ( ! is_null( $result ) ) {
  181. return $result;
  182. }
  183. if ( 'localhost' == $check['host'] || ( isset( $home['host'] ) && $home['host'] == $check['host'] ) ) {
  184. return false;
  185. }
  186. if ( ! defined( 'WP_PROXY_BYPASS_HOSTS' ) ) {
  187. return true;
  188. }
  189. static $bypass_hosts = null;
  190. static $wildcard_regex = array();
  191. if ( null === $bypass_hosts ) {
  192. $bypass_hosts = preg_split( '|,\s*|', WP_PROXY_BYPASS_HOSTS );
  193. if ( false !== strpos( WP_PROXY_BYPASS_HOSTS, '*' ) ) {
  194. $wildcard_regex = array();
  195. foreach ( $bypass_hosts as $host ) {
  196. $wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) );
  197. }
  198. $wildcard_regex = '/^(' . implode( '|', $wildcard_regex ) . ')$/i';
  199. }
  200. }
  201. if ( ! empty( $wildcard_regex ) ) {
  202. return ! preg_match( $wildcard_regex, $check['host'] );
  203. } else {
  204. return ! in_array( $check['host'], $bypass_hosts );
  205. }
  206. }
  207. }