From 039d19acfa24b03f67f1720aca8d281686b3bff5 Mon Sep 17 00:00:00 2001
From: Benjamin Renard <benjamin.renard@akka.eu>
Date: Thu, 9 Jul 2020 13:46:25 +0200
Subject: [PATCH] Fix getParameter WS API

---
 php/WebServices/WSUserMgr.php |  5 ++---
 php/WebServices/WebServer.php | 68 +++++++++++++++++++++++++++++++++++++++++++++-----------------------
 php/classes/AmdaAction.php    |  2 +-
 php/classes/AmdaStats.php     |  2 +-
 php/classes/ProxyUtils.php    |  4 ++--
 5 files changed, 51 insertions(+), 30 deletions(-)

diff --git a/php/WebServices/WSUserMgr.php b/php/WebServices/WSUserMgr.php
index f08b5a2..63e10fe 100644
--- a/php/WebServices/WSUserMgr.php
+++ b/php/WebServices/WSUserMgr.php
@@ -17,7 +17,7 @@ class WSUserMgr extends UserMgr
 			exit(json_encode(array("error" => $msg)));
 	}
 
-	public function init($username, $password, $sessionID, $setPathOnly, $isSoap) 
+	public function initWS($username, $password, $sessionID, $setPathOnly, $isSoap) 
 	{
 		$this->user = trim($username);
 		if (isset($sessionID))
@@ -55,11 +55,10 @@ class WSUserMgr extends UserMgr
 		if ( $setPathOnly )
 			return array('success' => true);
 			
-		$this->userGrps = $this->getUserGrps();	
 		$this->makeLocalTree();
 		$this->makeRemoteTree();
 
 		return array('success' => true);
 	}
 }
-?>
\ No newline at end of file
+?>
diff --git a/php/WebServices/WebServer.php b/php/WebServices/WebServer.php
index 51acf66..b33439a 100644
--- a/php/WebServices/WebServer.php
+++ b/php/WebServices/WebServer.php
@@ -19,7 +19,7 @@ class WebServer
 		if (!is_dir(WSConfigClass::getWsResultDir())) mkdir(WSConfigClass::getWsResultDir(), 0775);
 	}
 
-	protected function init($data) 
+	protected function init($data = NULL) 
 	{
 		$this->requestTime = date('Ymd',time());
 		
@@ -57,7 +57,7 @@ class WebServer
 	private function initUserMgr($setPathOnly = false) 
 	{
 		$wsUserMgr = new WSUserMgr();
-		$wsUserMgr->init($this->userID, $this->userPWD, $this->sessionID,  $setPathOnly, $this->isSoap);
+		$wsUserMgr->initWS($this->userID, $this->userPWD, $this->sessionID,  $setPathOnly, $this->isSoap);
 		
 		$this->IPclient = $wsUserMgr->getIPClient();
 		
@@ -175,6 +175,8 @@ class WebServer
 		$xpath = new DOMXpath($orbitsXml);
 		$path = '//orbites[@mission="'.$spacecraft.'" and @coordinate_system="'.$orbitRequest['coordinateSystem'].'" and @units="'.$orbitRequest['units'].'" ] ';
 
+error_log($path);
+
 		$orbits = $xpath->query($path);
 		
 		foreach ($orbits as $orbit)
@@ -243,7 +245,8 @@ class WebServer
 						"timeformat" => $formatInfo['timeFormat'],
 						"compression" => $formatInfo['gzip']
 			);	
-			
+
+
 		if (!isset($this->requestManager))
 			$this->requestManager = new RequestManagerClass();
 		 
@@ -252,6 +255,7 @@ class WebServer
 		} catch (Exception $e) {
 			$this->throwError("executionError", "Exception detected : ".$e->getMessage()); 
 		}
+
 			
 		if (!$downloadResult['success']) {
 			$this->throwError("serverError", $downloadResult['message']);
@@ -262,7 +266,7 @@ class WebServer
 		} elseif ($downloadResult['status'] == 'done') 
 		{
 			$this->deleteProcess($downloadResult['id']);
-			return array('success' => true, 'status' => 'done', 'dataFileURLs' => WSConfigClass::getUrl().$downloadResult['result']);
+			return array('success' => true, 'status' => 'done', 'dataFileURLs' => WSConfigClass::getUrl().$downloadResult['result'], 'exectime' => $downloadResult['exectime']);
 		} else {
 			return ['success' => false, 'message' => 'Unknown status ' . $downloadResult['status']];
 		} 
@@ -386,6 +390,7 @@ class WebServer
 */
 	public function getParameter($data) 
 	{
+error_log("BRE =============================> getParameter");
 		$res = $this->init($data);
     
 		if (!$res['success']){
@@ -395,6 +400,14 @@ class WebServer
 		$this->initUserMgr();
 
 		$vars = $res['vars'];
+
+		if (empty($vars["startTime"])) {
+			$this->throwError("requestError", "Missing startTime definition");
+		}
+
+		if (empty($vars["stopTime"])) {
+			$this->throwError("requestError", "Missing stopTime definition");
+		}
 		
 		if (is_numeric($vars["startTime"])) {
 			$this->checkInputTime($vars["startTime"],$vars["stopTime"]);
@@ -405,37 +418,40 @@ class WebServer
 			$this->checkInputTime(strtotime($vars["startTime"]),strtotime($vars["stopTime"])); 
 		}
 		
-		if (!$vars["timeFormat"]) {
+		if (empty($vars["timeFormat"])) {
 			$timeFormat = "ISO8601";
 		}
 		else {
 			$timeFormat = $vars["timeFormat"];
 		}
-		
+
+
+		if (empty($vars["parameterID"])) {
+			$this->throwError("requestError", "Missing parameterID definition");
+		}
 		$paramId = array();
 		array_push($paramId, $vars["parameterID"]);
 
-		if (!$vars["gzip"])
+		if (empty($vars["gzip"]))
 			$gzip = 0;
 		else
-			$gzip = $vars["gzip"];
+			$gzip = ($vars["gzip"] == 1);
 
-		if (!$vars["stream"])
-			$stream = 0;
-		else
-			$stream = $vars["stream"];
+		$sampling = !empty($vars["sampling"]) ? $vars["sampling"] : NULL;
+
+		$outputFormat = !empty($vars["outputFormat"]) ? $vars["outputFormat"] : 'ASCII'; 
 			
 		$this->service = strtolower(__FUNCTION__);
 
 		$res = $this->doDownloadRequest(
-					array("startTime" => $vars["startTime"], "stopTime" => $vars["stopTime"], "sampling" => $vars["sampling"]),
+					array("startTime" => $vars["startTime"], "stopTime" => $vars["stopTime"], "sampling" => $sampling),
 					array("params" => $paramId),
-					array("format" => $vars["outputFormat"], "timeFormat"=> $timeFormat, "gzip"=>$gzip, "stream"=>$stream));
+					array("format" => $outputFormat, "timeFormat"=> $timeFormat, "gzip"=>$gzip));
  
 		if ($res['success']) 
 			return $res;
 	
-		$this->throwError("serverError", $res['message']);   	
+		$this->throwError("serverError", $res['message']); 
 	}
 	
 /*
@@ -535,17 +551,12 @@ class WebServer
 		else
 			$gzip = $vars["gzip"];
 
-		if (!$vars["stream"])
-			$stream = 0;
-		else
-			$stream = $vars["stream"];
-		
 		$this->service = strtolower(__FUNCTION__);
 
 		$res = $this->doDownloadRequest(
 					array("startTime" => $vars["startTime"], "stopTime" => $vars["stopTime"], "sampling" => $sampling),
 					array("params" => $paramId),
-					array("format" => $vars["outputFormat"], "timeFormat"=> $timeFormat, "gzip"=>$gzip, "stream"=>$stream));
+					array("format" => $vars["outputFormat"], "timeFormat"=> $timeFormat, "gzip"=>$gzip));
   
 		if ($res['success']) return $res;
 
@@ -571,6 +582,17 @@ class WebServer
 		catch (Exception $e) 
 		{
 			// after first getStatus() call  process is deleted
+			/*$obj = array('id' => $id);
+
+			try
+			{
+				$res = $this->requestManager->runWSRequest($this->userID, $this->IPclient, FunctionTypeEnumClass::PROCESSDELETE, null, $id);
+			}
+			catch (Exception $e)
+			{
+				
+			}
+
 			$jobsManager = new WSJobsManagerClass();
 			
 			try 
@@ -587,7 +609,7 @@ class WebServer
 			catch (Exception $e) 
 			{
 				$this->throwError("getResultFromProcessIdError", "Exception detected : ".$e->getMessage());
-			}
+			}*/
 		}
 		
 		if (!$res['success']) {
@@ -708,7 +730,7 @@ class WebServer
 		$res = $this->doDownloadRequest(
 					array("startTime" => $vars["startTime"], "stopTime" => $vars["stopTime"], "sampling" => $vars["sampling"]),
 					array("params" => $paramId),
-					array("format" => $vars["outputFormat"], "timeFormat"=> $timeFormat, "gzip"=>$gzip, "stream"=>$stream));
+					array("format" => $vars["outputFormat"], "timeFormat"=> $timeFormat, "gzip"=>$gzip));
  	 
 		if ($res['success']) return $res;
  
diff --git a/php/classes/AmdaAction.php b/php/classes/AmdaAction.php
index 3f87a31..53d9fd8 100644
--- a/php/classes/AmdaAction.php
+++ b/php/classes/AmdaAction.php
@@ -933,7 +933,7 @@ class AmdaAction
 		if (isset($obj->username) && isset($obj->password) && isset($obj->sessionID))
 		{
 			$dd = new WSUserMgr();
-			$dd->init($obj->username,$obj->password,$obj->sessionID, false);
+			$dd->initWS($obj->username,$obj->password,$obj->sessionID, false, false);
 		}
 		else {
 			$dd = new UserMgr();
diff --git a/php/classes/AmdaStats.php b/php/classes/AmdaStats.php
index f8ee7d2..21522a2 100644
--- a/php/classes/AmdaStats.php
+++ b/php/classes/AmdaStats.php
@@ -12,7 +12,7 @@ class AmdaStats {
 	public $usersToExclude = array('bouchemit', 'impex');
 	public $success = true;
 	private $user = null;
-	private $task = array('request'=>'plot','condition'=>'mining', 'download'=>'print','statistics'=>'statistics');
+	private $task = array('request'=>'plot','condition'=>'mining', 'download'=>'print','statistics'=>'statistics','getparameter'=>'ws_print');
 	
 	public function __construct($user) {
 		
diff --git a/php/classes/ProxyUtils.php b/php/classes/ProxyUtils.php
index fe59425..2a75b2c 100644
--- a/php/classes/ProxyUtils.php
+++ b/php/classes/ProxyUtils.php
@@ -2,7 +2,7 @@
 
 class ProxyUtils {
 	static function addProxyForCurl(&$ch) {
-		if ( defined(PROXY_HOST) ) {
+		if ( defined("PROXY_HOST") ) {
 			$proxy_host = PROXY_HOST;
 			if (!empty($proxy_host)) {
 				curl_setopt($ch, CURLOPT_PROXY, "http://".$proxy_host);
@@ -15,7 +15,7 @@ class ProxyUtils {
 	}
 
 	static function getStreamContextWithProxy() {
-		if (!defined(PROXY_HOST))
+		if (!defined("PROXY_HOST"))
 					return;
 		$proxy_host = PROXY_HOST;
 		if (empty($proxy_host))
--
libgit2 0.21.2