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