From 18cc1a4d3ec36117d38ae0b15df0f751aa2150cd Mon Sep 17 00:00:00 2001 From: ozh Date: Mon, 25 May 2015 13:07:38 +0200 Subject: [PATCH] Refactor proxy functions Easier testing. --- includes/functions-deprecated.php | 13 +++++ includes/functions-http.php | 81 ++++++++++++++++++++----------- 2 files changed, 65 insertions(+), 29 deletions(-) diff --git a/includes/functions-deprecated.php b/includes/functions-deprecated.php index a5e8429..932c927 100644 --- a/includes/functions-deprecated.php +++ b/includes/functions-deprecated.php @@ -75,3 +75,16 @@ function yourls_has_interface() { return false; return true; } + +/** + * Check if a proxy is defined for HTTP requests + * + * @uses YOURLS_PROXY + * @since 1.7 + * @deprecated 1.7.1 + * @return bool true if a proxy is defined, false otherwise + */ +function yourls_http_proxy_is_defined() { + yourls_deprecated_function( __FUNCTION__, '1.7.1', 'yourls_http_get_proxy' ); + return yourls_apply_filter( 'http_proxy_is_defined', defined( 'YOURLS_PROXY' ) ); +} diff --git a/includes/functions-http.php b/includes/functions-http.php index 290b1ae..af2d30c 100644 --- a/includes/functions-http.php +++ b/includes/functions-http.php @@ -67,14 +67,37 @@ function yourls_http_post_body( $url, $headers = array(), $data = array(), $opti } /** - * Check if a proxy is defined for HTTP requests + * Get proxy information * - * @uses YOURLS_PROXY - * @since 1.7 - * @return bool true if a proxy is defined, false otherwise + * @uses YOURLS_PROXY YOURLS_PROXY_USERNAME YOURLS_PROXY_PASSWORD + * @since 1.7.1 + * @return mixed false if no proxy is defined, or string like '10.0.0.201:3128' or array like ('10.0.0.201:3128', 'username', 'password') + */ +function yourls_http_get_proxy() { + $proxy = false; + + if( defined( 'YOURLS_PROXY' ) && !empty( 'YOURLS_PROXY' ) ) { + $proxy = YOURLS_PROXY; + // Username (?) and password can be defined as an empty string : no check for empty() + if( defined( 'YOURLS_PROXY_USERNAME' ) && defined( 'YOURLS_PROXY_PASSWORD' ) ) { + $proxy = array( YOURLS_PROXY, YOURLS_PROXY_USERNAME, YOURLS_PROXY_PASSWORD ); + } + } + + return yourls_apply_filter( 'http_get_proxy', $proxy ); +} + +/** + * Get list of hosts that should bypass the proxy + * + * @uses YOURLS_PROXY_BYPASS_HOSTS + * @since 1.7.1 + * @return mixed false if no host defined, or string like "example.com, *.mycorp.com" */ -function yourls_http_proxy_is_defined() { - return yourls_apply_filter( 'http_proxy_is_defined', defined( 'YOURLS_PROXY' ) ); +function yourls_http_get_proxy_bypass_host() { + $hosts = defined( 'YOURLS_PROXY_BYPASS_HOSTS' ) ? YOURLS_PROXY_BYPASS_HOSTS : false; + + return yourls_apply_filter( 'http_get_proxy_bypass_host', $hosts ); } /** @@ -82,9 +105,6 @@ function yourls_http_proxy_is_defined() { * * For a list of all available options, see function request() in /includes/Requests/Requests.php * - * @uses YOURLS_PROXY - * @uses YOURLS_PROXY_USERNAME - * @uses YOURLS_PROXY_PASSWORD * @since 1.7 * @return array Options */ @@ -95,13 +115,9 @@ function yourls_http_default_options() { 'follow_redirects' => true, 'redirects' => 3, ); - - if( yourls_http_proxy_is_defined() ) { - if( defined( 'YOURLS_PROXY_USERNAME' ) && defined( 'YOURLS_PROXY_PASSWORD' ) ) { - $options['proxy'] = array( YOURLS_PROXY, YOURLS_PROXY_USERNAME, YOURLS_PROXY_PASSWORD ); - } else { - $options['proxy'] = YOURLS_PROXY; - } + + if( yourls_http_get_proxy() ) { + $options['proxy'] = yourls_http_get_proxy(); } return yourls_apply_filter( 'http_default_options', $options ); @@ -143,21 +159,28 @@ function yourls_send_through_proxy( $url ) { if( in_array( $check['host'], $local ) ) return false; - if ( !defined( 'YOURLS_PROXY_BYPASS_HOSTS' ) ) - return true; - - // Check YOURLS_PROXY_BYPASS_HOSTS + $bypass = yourls_http_get_proxy_bypass_host(); + + if( $bypass === false OR $bypass === '' ) { + return true; + } + + // Build array of hosts to bypass static $bypass_hosts; static $wildcard_regex = false; if ( null == $bypass_hosts ) { - $bypass_hosts = preg_split( '|,\s*|', YOURLS_PROXY_BYPASS_HOSTS ); - - if ( false !== strpos( YOURLS_PROXY_BYPASS_HOSTS, '*' ) ) { - $wildcard_regex = array(); - foreach ( $bypass_hosts as $host ) - $wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) ); - $wildcard_regex = '/^(' . implode( '|', $wildcard_regex ) . ')$/i'; - } + $bypass_hosts = preg_split( '|\s*,\s*|', $bypass ); + + if ( false !== strpos( $bypass, '*' ) ) { + $wildcard_regex = array(); + foreach ( $bypass_hosts as $host ) { + $wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) ); + if ( false !== strpos( $host, '*' ) ) { + $wildcard_regex[] = str_replace( '\*\.', '', preg_quote( $host, '/' ) ); + } + } + $wildcard_regex = '/^(' . implode( '|', $wildcard_regex ) . ')$/i'; + } } if ( !empty( $wildcard_regex ) ) @@ -182,7 +205,7 @@ function yourls_http_request( $type, $url, $headers, $data, $options ) { $options = array_merge( yourls_http_default_options(), $options ); - if( yourls_http_proxy_is_defined() && !yourls_send_through_proxy( $url ) ) + if( yourls_http_get_proxy() && !yourls_send_through_proxy( $url ) ) unset( $options['proxy'] ); try { -- 2.45.0