/wp-includes/class-wp-http-proxy.php

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