class-wp-http-proxy.php 5.8 KB

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