diff --git includes/common.inc includes/common.inc index b340f02..4c3b6c4 100644 --- includes/common.inc +++ includes/common.inc @@ -781,7 +781,18 @@ function drupal_http_request($url, array $options = array()) { $result->code = -1002; return $result; } - + + $proxy_server = variable_get('proxy_server', ''); + if ((strlen($proxy_server) > 0) && (FALSE == _matches_proxy_exceptions($uri['host']))) { + $use_proxy = TRUE; + $proxy_port = variable_get('proxy_port', 8080); + $proxy_username = variable_get('proxy_username', ''); + $proxy_password = variable_get('proxy_password', ''); + } + else { + $use_proxy = FALSE; + } + timer_start(__FUNCTION__); // Merge the default options. @@ -798,7 +809,12 @@ function drupal_http_request($url, array $options = array()) { case 'feed': $port = isset($uri['port']) ? $uri['port'] : 80; $host = $uri['host'] . ($port != 80 ? ':' . $port : ''); - $fp = @fsockopen($uri['host'], $port, $errno, $errstr, $options['timeout']); + if ($use_proxy) { + $fp = @fsockopen($proxy_server, $proxy_port, $errno, $errstr, $options['timeout']); + } + else { + $fp = @fsockopen($uri['host'], $port, $errno, $errstr, $options['timeout']); + } break; case 'https': // Note: Only works when PHP is compiled with OpenSSL support. @@ -829,9 +845,14 @@ function drupal_http_request($url, array $options = array()) { } // Construct the path to act on. - $path = isset($uri['path']) ? $uri['path'] : '/'; - if (isset($uri['query'])) { - $path .= '?' . $uri['query']; + if ($use_proxy) { + $path = $url; + } + else { + $path = isset($uri['path']) ? $uri['path'] : '/'; + if (isset($uri['query'])) { + $path .= '?' . $uri['query']; + } } // Merge the default headers. @@ -857,6 +878,11 @@ function drupal_http_request($url, array $options = array()) { if (isset($uri['user'])) { $options['headers']['Authorization'] = 'Basic ' . base64_encode($uri['user'] . (!empty($uri['pass']) ? ":" . $uri['pass'] : '')); } + + // If proxy authorisation is required the set the header. + if ($use_proxy && !empty($proxy_username)) { + $options['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($proxy_username . (!empty($proxy_password) ? ":" . $proxy_password : '')); + } // If the database prefix is being used by SimpleTest to run the tests in a copied // database then set the user-agent header to the database prefix so that any @@ -992,6 +1018,33 @@ function drupal_http_request($url, array $options = array()) { return $result; } + +/** + * Checks host against proxy exceptions + * + * @param $host + * A string containing the host part of the requested URI. + * @return + * - TRUE if one of the proxy exceptions equals or is part of $host. + * - FALSE if none of the proxy exceptions equals or is part of $host. + */ +function _matches_proxy_exceptions($host) { + $rv = FALSE; + + $proxy_exceptions = variable_get('proxy_exceptions', ''); + if (FALSE == empty($proxy_exceptions)) { + $patterns = explode(",", $proxy_exceptions); + foreach ($patterns as $pattern) { + $pattern = trim($pattern, " "); + + if (strstr($host, $pattern)) { + $rv = TRUE; + break; + } + } + } + return $rv; +} /** * @} End of "HTTP handling". */ @@ -5139,9 +5192,6 @@ function drupal_render_cache_by_query($query, $function, $expire = CACHE_TEMPORA } /** - - -/** * Helper function for building cache ids. * * @param $granularity diff --git sites/default/default.settings.php sites/default/default.settings.php index 2cee981..6c00208 100644 --- sites/default/default.settings.php +++ sites/default/default.settings.php @@ -407,3 +407,16 @@ ini_set('session.cookie_lifetime', 2000000); * Remove the leading hash signs to disable. */ # $conf['allow_authorize_operations'] = FALSE; + +/** + * External Access proxy settings: + * + * If your drupal site requires that external internet access goes through a + * web proxy then you can enter the necessary settings here. Currently only + * BASIC authentication is supported if using the username and password fields. + */ +# $conf['proxy_server'] = "proxy.example.com"; +# $conf['proxy_port'] = 8080; +# $conf['proxy_username'] = ""; +# $conf['proxy_password'] = ""; +# $conf['proxy_exceptions'] = "127.0.0.1,localhost";