From d1327dc0d28a2f747a43a793a09b3037a01ad445 Mon Sep 17 00:00:00 2001
From: Benjamin Renard <benjamin.renard@akka.eu>
Date: Thu, 11 Jan 2018 15:33:58 +0100
Subject: [PATCH] Fix RemoteData access behind a proxy

---
 CMakeLists.txt            | 12 +++++++-----
 compil.sh                 | 18 ++++++++++++++----
 scripts/DDServer.env.in   |  3 +++
 src/REMOTEDATA/CDAWEB.php | 38 +++++++++++++++++++++++++++++++++++---
 4 files changed, 59 insertions(+), 12 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 920e37a..34f96fa 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -32,15 +32,17 @@ find_package( LibXML2 REQUIRED )
 get_filename_component(NETCDFLIB_DIR ${NETCDFLIBRARIES} PATH)
 get_filename_component(DDCLIENTLIB_DIR ${DDCLIENTLIBRARIES} PATH)
 
-configure_file (
- "${CMAKE_SOURCE_DIR}/scripts/DDServer.env.in"
- "${CMAKE_SOURCE_DIR}/scripts/DDServer.env"
-)
-
 set(DDSERVICE_URL $ENV{DDSERVICE_URL})
 set(DDRESPATH $ENV{DDRESPATH})
 set(DDBASEDATA $ENV{DDBASEDATA})
 set(DDBASEINFO $ENV{DDBASEINFO})
+set(PROXY_HOST $ENV{PROXY_HOST})
+set(PROXY_USERPWD $ENV{PROXY_USERPWD})
+
+configure_file (
+ "${CMAKE_SOURCE_DIR}/scripts/DDServer.env.in"
+ "${CMAKE_SOURCE_DIR}/scripts/DDServer.env"
+)
 
 configure_file (
  "${CMAKE_SOURCE_DIR}/src/DDSERVICES/SOAP/DDserverWeb_ini.php.in"
diff --git a/compil.sh b/compil.sh
index bb933d2..a87c9cd 100755
--- a/compil.sh
+++ b/compil.sh
@@ -1,16 +1,26 @@
 #!/bin/sh
 
 export INSTALL_DIR="/opt/local"
-export DDSERVICE_URL="http://apus.irap.omp.eu/NEWAMDA/DDService"
-export DDRESPATH="/home/budnik/DDBASE"
-export DDBASEDATA="/home/budnik/DDBASE/DATA"
-export DDBASEINFO="/home/budnik/DDBASE/INFO"
+export DDSERVICE_URL="http://amdadev.fr/DDService"
+export DDRESPATH="/var/amda-data/"
+export DDBASEDATA="/home/budnik/AMDA-NG.core/DDBASE/DATA"
+export DDBASEINFO="/home/budnik/AMDA-NG.core/DDBASE/INFO"
+#export PROXY_HOST=""
+#export PROXY_USERPWD=""
 
 echo "Install dir. : ${INSTALL_DIR}"
 echo "DDService Url : ${DDSERVICE_URL}"
 echo "DD.res path : ${DDRESPATH}"
 echo "DDBASE DATA path : ${DDBASEDATA}"
 echo "DDBASE INFO path : ${DDBASEINFO}"
+if [ -n "$PROXY_HOST" ]
+then
+	echo "Proxy host : ${PROXY_HOST}"
+fi
+if [ -n "$PROXY_USERPWD" ]
+then
+        echo "Proxy user/pwd : ${PROXY_USERPWD}"
+fi
 read -p "Press any key to continue..."
 
 export NETCDF_ROOT=$INSTALL_DIR
diff --git a/scripts/DDServer.env.in b/scripts/DDServer.env.in
index 5b95e33..7bff72a 100644
--- a/scripts/DDServer.env.in
+++ b/scripts/DDServer.env.in
@@ -8,6 +8,9 @@ LD_LIBRARY_PATH=$DDLIB/:@NETCDFLIB_DIR@:@libcdf_LIBRARY_DIR@:@USRLIB_DIR@
 DATAMANAGER=@CMAKE_INSTALL_PREFIX@/bin/DATAMANAGER
 REMOTEDATA=@CMAKE_INSTALL_PREFIX@/bin/REMOTEDATA
 CALLEXT=@CMAKE_INSTALL_PREFIX@/bin/CALLEXT
+PROXY_HOST=@PROXY_HOST@
+PROXY_USERPWD=@PROXY_USERPWD@
 
 export DDBASE DDPATH DDBASEBIN DDLIB LD_LIBRARY_PATH
 export DATAMANAGER REMOTEDATA CALLEXT
+export PROXY_HOST PROXY_USERPWD
diff --git a/src/REMOTEDATA/CDAWEB.php b/src/REMOTEDATA/CDAWEB.php
index bafb18e..aab1861 100644
--- a/src/REMOTEDATA/CDAWEB.php
+++ b/src/REMOTEDATA/CDAWEB.php
@@ -42,21 +42,50 @@ class CDAWEB extends RemoteDataCenterClass
 	{			
 		error_log("CDAWEB Proxy creation on ".date("Y-m-d\TH:i:s").PHP_EOL,3,log);
 		error_log("CDAWEB Proxy creation on ".date("Y-m-d\TH:i:s").PHP_EOL,3,err);
+
+		$this->initStreamContext();
 		
 		$this->getAllSpaseDatasets();		
 	}			 
 		
-   private function openConnection() 
-   {	
+	private function openConnection() 
+	{	
 		$this->ch = curl_init();			 
 		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
-		curl_setopt($this->ch, CURLOPT_TIMEOUT, 60); 
+		curl_setopt($this->ch, CURLOPT_TIMEOUT, 60);
+		// Add proxy definition
+		$PROXY_HOST=getenv('PROXY_HOST');
+		$PROXY_USERPWD=getenv('PROXY_USERPWD');
+		if (!empty($PROXY_HOST)) {
+			curl_setopt($this->ch, CURLOPT_PROXY, getenv('PROXY_HOST'));
+			if (!empty($PROXY_USERPWD)) {
+				curl_setopt($this->ch, CURLOPT_PROXYUSERPWD, getenv('PROXY_USERPWD'));
+			}
+		}
 	} 
 		  
 	private function closeConnection()
 	{
 		curl_close($this->ch);	
 	}
+
+	private function initStreamContext()
+	{
+		$PROXY_HOST=getenv('PROXY_HOST');
+		$PROXY_USERPWD=getenv('PROXY_USERPWD');
+		if (!empty($PROXY_HOST)) {
+			$context = array(
+				'http' => array(
+					'proxy' => "tcp://$PROXY_HOST",
+					'request_fulluri' => true,
+				),
+			);
+			if (!empty($PROXY_USERPWD)) {
+				$context['http']['header'] = "Proxy-Authorization: Basic ".base64_encode($PROXY_USERPWD);
+                        }
+			stream_context_set_default($context);
+		}
+	}
 	
 	protected function setDataViewURL()
 	{
@@ -367,6 +396,7 @@ class CDAWEB extends RemoteDataCenterClass
 	
 	protected function existsMasterCdf($dsId)
 	{
+		$this->initStreamContext();
 		$file = CDAWebConfigClass::$masterUrl.strtolower($dsId)."_00000000_v01.cdf";
 		$file_headers = @get_headers($file);
 		
@@ -383,6 +413,7 @@ class CDAWEB extends RemoteDataCenterClass
 		 
 	protected function getMasterCdf($dsId)
 	{
+		$this->initStreamContext();
 		$file = CDAWebConfigClass::$masterUrl.strtolower($dsId)."_00000000_v01.cdf";
 		$file_headers = @get_headers($file);
 		
@@ -394,6 +425,7 @@ class CDAWEB extends RemoteDataCenterClass
 	
 	public function getData($ds, $start, $stop)
 	{
+		$this->initStreamContext();
 		$this->openConnection();
 		$this->setDataViewURL(); 
 
--
libgit2 0.21.2