From b96e9021601bbc8bc4cf7684bc5943309c92c364 Mon Sep 17 00:00:00 2001 From: Flavio Copes Date: Mon, 22 Feb 2016 18:18:37 +0100 Subject: [PATCH] Allow authentication for proxy --- system/src/Grav/Common/GPM/Response.php | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/system/src/Grav/Common/GPM/Response.php b/system/src/Grav/Common/GPM/Response.php index 0e57ec0c6..e16298ec0 100644 --- a/system/src/Grav/Common/GPM/Response.php +++ b/system/src/Grav/Common/GPM/Response.php @@ -201,7 +201,15 @@ private static function getFopen() // if proxy set add that $proxy_url = self::getGrav()['config']->get('system.proxy_url'); if ($proxy_url) { - $options['fopen']['proxy'] = $proxy_url; + $parsed_url = parse_url($proxy_url); + + $options['fopen']['proxy'] = ($parsed_url['scheme'] ?: 'http') . '://' . $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''); + $options['fopen']['request_fulluri'] = true; + + if (isset($parsed_url['user']) && isset($parsed_url['pass'])) { + $auth = base64_encode($parsed_url['user'] . ':' . $parsed_url['pass']); + $options['fopen']['header'] = "Proxy-Authorization: Basic $auth"; + } } if ($callback) { @@ -269,7 +277,18 @@ private static function curlExecFollow($ch, $options, $callback) // if proxy set add that $proxy_url = self::getGrav()['config']->get('system.proxy_url'); if ($proxy_url) { - $options['curl'][CURLOPT_PROXY] = $proxy_url; + $parsed_url = parse_url($proxy_url); + + $options['curl'][CURLOPT_PROXY] = $parsed_url['host']; + $options['curl'][CURLOPT_PROXYTYPE] = 'HTTP'; + + if (isset($parsed_url['port'])) { + $options['curl'][CURLOPT_PROXYPORT] = $parsed_url['port']; + } + + if (isset($parsed_url['user']) && isset($parsed_url['pass'])) { + $options['curl'][CURLOPT_PROXYUSERPWD] = $parsed_url['user'] . ':' . $parsed_url['pass']; + } } // no open_basedir set, we can proceed normally @@ -278,7 +297,7 @@ private static function curlExecFollow($ch, $options, $callback) return curl_exec($ch); } - $max_redirects = isset($options['curl'][CURLOPT_MAXREDIRS]) ? $options['curl'][CURLOPT_MAXREDIRS] : 3; + $max_redirects = isset($options['curl'][CURLOPT_MAXREDIRS]) ? $options['curl'][CURLOPT_MAXREDIRS] : 3; $options['curl'][CURLOPT_FOLLOWLOCATION] = false; // open_basedir set but no redirects to follow, we can disable followlocation and proceed normally