wp/wp-includes/class-wp-http-proxy.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    50 	 * @use WP_PROXY_PORT
    50 	 * @use WP_PROXY_PORT
    51 	 *
    51 	 *
    52 	 * @return bool
    52 	 * @return bool
    53 	 */
    53 	 */
    54 	public function is_enabled() {
    54 	public function is_enabled() {
    55 		return defined('WP_PROXY_HOST') && defined('WP_PROXY_PORT');
    55 		return defined( 'WP_PROXY_HOST' ) && defined( 'WP_PROXY_PORT' );
    56 	}
    56 	}
    57 
    57 
    58 	/**
    58 	/**
    59 	 * Whether authentication should be used.
    59 	 * Whether authentication should be used.
    60 	 *
    60 	 *
    64 	 * @use WP_PROXY_PASSWORD
    64 	 * @use WP_PROXY_PASSWORD
    65 	 *
    65 	 *
    66 	 * @return bool
    66 	 * @return bool
    67 	 */
    67 	 */
    68 	public function use_authentication() {
    68 	public function use_authentication() {
    69 		return defined('WP_PROXY_USERNAME') && defined('WP_PROXY_PASSWORD');
    69 		return defined( 'WP_PROXY_USERNAME' ) && defined( 'WP_PROXY_PASSWORD' );
    70 	}
    70 	}
    71 
    71 
    72 	/**
    72 	/**
    73 	 * Retrieve the host for the proxy server.
    73 	 * Retrieve the host for the proxy server.
    74 	 *
    74 	 *
    75 	 * @since 2.8.0
    75 	 * @since 2.8.0
    76 	 *
    76 	 *
    77 	 * @return string
    77 	 * @return string
    78 	 */
    78 	 */
    79 	public function host() {
    79 	public function host() {
    80 		if ( defined('WP_PROXY_HOST') )
    80 		if ( defined( 'WP_PROXY_HOST' ) ) {
    81 			return WP_PROXY_HOST;
    81 			return WP_PROXY_HOST;
       
    82 		}
    82 
    83 
    83 		return '';
    84 		return '';
    84 	}
    85 	}
    85 
    86 
    86 	/**
    87 	/**
    89 	 * @since 2.8.0
    90 	 * @since 2.8.0
    90 	 *
    91 	 *
    91 	 * @return string
    92 	 * @return string
    92 	 */
    93 	 */
    93 	public function port() {
    94 	public function port() {
    94 		if ( defined('WP_PROXY_PORT') )
    95 		if ( defined( 'WP_PROXY_PORT' ) ) {
    95 			return WP_PROXY_PORT;
    96 			return WP_PROXY_PORT;
       
    97 		}
    96 
    98 
    97 		return '';
    99 		return '';
    98 	}
   100 	}
    99 
   101 
   100 	/**
   102 	/**
   103 	 * @since 2.8.0
   105 	 * @since 2.8.0
   104 	 *
   106 	 *
   105 	 * @return string
   107 	 * @return string
   106 	 */
   108 	 */
   107 	public function username() {
   109 	public function username() {
   108 		if ( defined('WP_PROXY_USERNAME') )
   110 		if ( defined( 'WP_PROXY_USERNAME' ) ) {
   109 			return WP_PROXY_USERNAME;
   111 			return WP_PROXY_USERNAME;
       
   112 		}
   110 
   113 
   111 		return '';
   114 		return '';
   112 	}
   115 	}
   113 
   116 
   114 	/**
   117 	/**
   117 	 * @since 2.8.0
   120 	 * @since 2.8.0
   118 	 *
   121 	 *
   119 	 * @return string
   122 	 * @return string
   120 	 */
   123 	 */
   121 	public function password() {
   124 	public function password() {
   122 		if ( defined('WP_PROXY_PASSWORD') )
   125 		if ( defined( 'WP_PROXY_PASSWORD' ) ) {
   123 			return WP_PROXY_PASSWORD;
   126 			return WP_PROXY_PASSWORD;
       
   127 		}
   124 
   128 
   125 		return '';
   129 		return '';
   126 	}
   130 	}
   127 
   131 
   128 	/**
   132 	/**
   165 	public function send_through_proxy( $uri ) {
   169 	public function send_through_proxy( $uri ) {
   166 		/*
   170 		/*
   167 		 * parse_url() only handles http, https type URLs, and will emit E_WARNING on failure.
   171 		 * parse_url() only handles http, https type URLs, and will emit E_WARNING on failure.
   168 		 * This will be displayed on sites, which is not reasonable.
   172 		 * This will be displayed on sites, which is not reasonable.
   169 		 */
   173 		 */
   170 		$check = @parse_url($uri);
   174 		$check = @parse_url( $uri );
   171 
   175 
   172 		// Malformed URL, can not process, but this could mean ssl, so let through anyway.
   176 		// Malformed URL, can not process, but this could mean ssl, so let through anyway.
   173 		if ( $check === false )
   177 		if ( $check === false ) {
   174 			return true;
   178 			return true;
   175 
   179 		}
   176 		$home = parse_url( get_option('siteurl') );
   180 
       
   181 		$home = parse_url( get_option( 'siteurl' ) );
   177 
   182 
   178 		/**
   183 		/**
   179 		 * Filters whether to preempt sending the request through the proxy server.
   184 		 * Filters whether to preempt sending the request through the proxy server.
   180 		 *
   185 		 *
   181 		 * Returning false will bypass the proxy; returning true will send
   186 		 * Returning false will bypass the proxy; returning true will send
   187 		 * @param string $uri      URL to check.
   192 		 * @param string $uri      URL to check.
   188 		 * @param array  $check    Associative array result of parsing the URI.
   193 		 * @param array  $check    Associative array result of parsing the URI.
   189 		 * @param array  $home     Associative array result of parsing the site URL.
   194 		 * @param array  $home     Associative array result of parsing the site URL.
   190 		 */
   195 		 */
   191 		$result = apply_filters( 'pre_http_send_through_proxy', null, $uri, $check, $home );
   196 		$result = apply_filters( 'pre_http_send_through_proxy', null, $uri, $check, $home );
   192 		if ( ! is_null( $result ) )
   197 		if ( ! is_null( $result ) ) {
   193 			return $result;
   198 			return $result;
   194 
   199 		}
   195 		if ( 'localhost' == $check['host'] || ( isset( $home['host'] ) && $home['host'] == $check['host'] ) )
   200 
       
   201 		if ( 'localhost' == $check['host'] || ( isset( $home['host'] ) && $home['host'] == $check['host'] ) ) {
   196 			return false;
   202 			return false;
   197 
   203 		}
   198 		if ( !defined('WP_PROXY_BYPASS_HOSTS') )
   204 
       
   205 		if ( ! defined( 'WP_PROXY_BYPASS_HOSTS' ) ) {
   199 			return true;
   206 			return true;
   200 
   207 		}
   201 		static $bypass_hosts = null;
   208 
       
   209 		static $bypass_hosts   = null;
   202 		static $wildcard_regex = array();
   210 		static $wildcard_regex = array();
   203 		if ( null === $bypass_hosts ) {
   211 		if ( null === $bypass_hosts ) {
   204 			$bypass_hosts = preg_split('|,\s*|', WP_PROXY_BYPASS_HOSTS);
   212 			$bypass_hosts = preg_split( '|,\s*|', WP_PROXY_BYPASS_HOSTS );
   205 
   213 
   206 			if ( false !== strpos(WP_PROXY_BYPASS_HOSTS, '*') ) {
   214 			if ( false !== strpos( WP_PROXY_BYPASS_HOSTS, '*' ) ) {
   207 				$wildcard_regex = array();
   215 				$wildcard_regex = array();
   208 				foreach ( $bypass_hosts as $host )
   216 				foreach ( $bypass_hosts as $host ) {
   209 					$wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) );
   217 					$wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) );
   210 				$wildcard_regex = '/^(' . implode('|', $wildcard_regex) . ')$/i';
   218 				}
       
   219 				$wildcard_regex = '/^(' . implode( '|', $wildcard_regex ) . ')$/i';
   211 			}
   220 			}
   212 		}
   221 		}
   213 
   222 
   214 		if ( !empty($wildcard_regex) )
   223 		if ( ! empty( $wildcard_regex ) ) {
   215 			return !preg_match($wildcard_regex, $check['host']);
   224 			return ! preg_match( $wildcard_regex, $check['host'] );
   216 		else
   225 		} else {
   217 			return !in_array( $check['host'], $bypass_hosts );
   226 			return ! in_array( $check['host'], $bypass_hosts );
       
   227 		}
   218 	}
   228 	}
   219 }
   229 }