PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

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