From d9d226bb98700375d44f0c1ab92d938c9c9ab01f Mon Sep 17 00:00:00 2001
From: Cerdic <cedric@yterium.com>
Date: Fri, 28 Sep 2018 09:26:41 +0000
Subject: [PATCH] Fix #4086 : detection des exceptions au proxy, prendre en
 compte les domaines parents ce qui permet de placer une exception sur une
 extension ou un domaine et qui s'appliquera a tous les sous, sous-sous,
 sous-sous-sous-domaines etc...

---
 ecrire/inc/distant.php | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/ecrire/inc/distant.php b/ecrire/inc/distant.php
index 2e4b541df1..58a44adda9 100644
--- a/ecrire/inc/distant.php
+++ b/ecrire/inc/distant.php
@@ -1236,16 +1236,37 @@ function need_proxy($host, $http_proxy = null, $http_noproxy = null) {
 	if (is_null($http_proxy)) {
 		$http_proxy = isset($GLOBALS['meta']['http_proxy']) ? $GLOBALS['meta']['http_proxy'] : null;
 	}
+	// rien a faire si pas de proxy :)
+	if (is_null($http_proxy) or !$http_proxy = trim($http_proxy)) {
+		return '';
+	}
+
 	if (is_null($http_noproxy)) {
 		$http_noproxy = isset($GLOBALS['meta']['http_noproxy']) ? $GLOBALS['meta']['http_noproxy'] : null;
 	}
+	// si pas d'exception, on retourne le proxy
+	if (is_null($http_noproxy) or !$http_noproxy = trim($http_noproxy)) {
+		return $http_proxy;
+	}
+
+	// si le host ou l'un des domaines parents est dans $http_noproxy on fait exception
+	// $http_noproxy peut contenir plusieurs domaines separes par des espaces ou retour ligne
+	$http_noproxy = str_replace("\n", " ", $http_noproxy);
+	$http_noproxy = str_replace("\r", " ", $http_noproxy);
+	$http_noproxy = " $http_noproxy ";
+	$domain = $host;
+	do {
+		if (strpos($http_noproxy, " $domain ") !== false) {
+			return '';
+		}
 
-	$domain = substr($host, strpos($host, '.'));
+		$domain = explode('.', $domain);
+		array_shift($domain);
+		$domain = implode('.', $domain);
+	} while (strpos($domain, '.') !== false);
 
-	return ($http_proxy
-		and (strpos(" $http_noproxy ", " $host ") === false
-			and (strpos(" $http_noproxy ", " $domain ") === false)))
-		? $http_proxy : '';
+	// ok c'est pas une exception
+	return $http_proxy;
 }
 
 
-- 
GitLab