AmdaClient.php 8.54 KB
<?php

/**  @class AmdaClient
 *    @version $Id: AmdaClient.php 2888 2015-04-29 15:25:42Z elena $
 *    @brief Client of DD Web Server
 *
 */

  ini_set("soap.wsdl_cache_enabled", "0");
  ini_set('soap.wsdl_cache_ttl',0);

class AmdaClient {

	public $client;
 
	function __construct(){	 
		try {
			$this->client = new SoapClient(DD_WSDL);
		}
		catch  (SoapFault $exception) {
			$msg = $exception->faultstring.PHP_EOL;
			error_log($msg, 1, email);
			exit($msg);
		}
		// reset log file
		if (file_exists(log)) unlink(log);

		date_default_timezone_set('UTC');
	}

	public function base64url_encode($data) {
		return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
	} 

	/* Just copy URL to Info/Bases.xml */	 
	public function getAvailableExternalBases() {
		try {
			$extBases = $this->client->getAvailableExternalBases();
		}
		catch  (SoapFault $exception) {
			$msg = $exception->faultstring.PHP_EOL;
			error_log($msg, 1, email);
			exit($msg);
		}

		if ($extBases === "NOEXTERNALBASES") {
			error_log($extBases.PHP_EOL, 1, email);
			return $extBases;
		}

		$extBasesDom = new DomDocument("1.0");
		if (!$extBasesDom->load($extBases)) {
			$msg = "Can't load Bases.xml".PHP_EOL;
			error_log($msg, 1, email);
			exit($msg);
		}

// 		$External = $extBasesDom->getElementsByTagName("External")->item(0);
// 		if ($External == NULL) {
// 			$msg = "no External Tag found in Bases.xml".PHP_EOL;
// 			error_log($msg, 1, email);
// 			exit($msg);
// 		}

		$extBasesNames = $extBasesDom->getElementsByTagName("dataCenter");
		if ($extBasesNames->length === 0) {
			$msg = "no external bases found in Bases.xml".PHP_EOL;
			error_log($msg, 1, email);
			exit($msg);
		}
/*
		if (!$this->updateOnly) {
			if (!is_dir(RemoteData)) mkdir(RemoteData, 0775, true);
			chgrp(RemoteData, APACHE_USER);                
			chmod(RemoteData, 0775);
			if (!is_dir(RemoteData.'PARAMS')) mkdir(RemoteData.'PARAMS', 0775, true);
			chgrp(RemoteData.'PARAMS', APACHE_USER);                
			chmod(RemoteData.'PARAMS', 0775);
			
			$xsl = new DOMDocument("1.0");
			$xsl->load(XMLPATH."basesRaw2bases.xsl");

			$xslt = new XSLTProcessor();
			$xslt->importStylesheet($xsl);

			if (!$xslt->transformToDoc($extBasesDom)->save(RemoteData."Bases.xml")){
				$msg = "problem while transforming Bases.xml".PHP_EOL;
				error_log($msg, 1, email);
				exit($msg);
			}
		}*/
		return $extBasesNames; 
	}
	 
	/* Just copy URL to LocalData/Amda_Users.xml */
	 
// 	public function getUserGroups() {
// 		try {
// 			$group_info = $this->client->getUserGroups();
// 		}
// 		catch  (SoapFault $exception) {
// 			exit("Problems with DD Web Services".PHP_EOL);
// 		}
// 		if ($group_info === "NOUSERGROUPSSPECIFIED") return false;
// 		 
// 		if (!file_exists(LocalData)) mkdir(LocalData, 0755, true);
// 		
// 		if (!copy($group_info,LocalData."AMDA_Users.xml"))
// 				exit("problem with copying AMDA_Users info".PHP_EOL);
// 
// 		return true;
// 	}
// 	

	/* Get list of VI with TimeRestriction in DDBASE */
	public function getDatasetsWithTimeRestriction() {
		try {
			$restrictions = $this->client->getDatasetsWithTimeRestriction();
		}
		catch  (SoapFault $exception) {
			error_log($exception->faultstring.PHP_EOL, 1, email);
			return FALSE;
		}
		return $restrictions;
	}

	/* Get Time Restriction from VI in DDBASE */
	public function getTimeRestriction($dataSet) {
		try {
				$restrictions = $this->client->getTimeRestriction($dataSet);
		}
		catch  (SoapFault $exception) {                     
				error_log($exception->faultstring.PHP_EOL, 1, email);
			return FALSE;
		}
		if ($restrictions['success'] === FALSE) {
				error_log('TimeRestriction: '.$dataSet.' '.$restrictions['days'], 1, email);
			return FALSE;
		}
								
		return $restrictions;
				
	}

	/* Just copy URL to DATA/$BaseID/base.xml */ 
	public function getAvailableExternalData($BaseID) {
		try {
			$extData = $this->client->getAvailableExternalData($BaseID);
		}
		catch  (SoapFault $exception) {
			$msg = $exception->faultstring.PHP_EOL;
			if (!$this->updateOnly) error_log($msg, 1, email);
			exit($msg);
		}

		if ($extData === "NOEXTERNALDATA") return false;
		//drwxrwxr-x
		if (!$this->updateOnly) {
			if (!file_exists(RemoteData.$BaseID)) mkdir(RemoteData.$BaseID, 0775, true);
			chgrp(RemoteData.$BaseID, APACHE_USER);                
			chmod(RemoteData.$BaseID, 0775);
		}

		if (!copy($extData,RemoteData.$BaseID."/base.xml"))  {
			$msg = "problem with copying base.xml for ".$BaseID.PHP_EOL;
			error_log($msg, 1, email);
			exit($msg);
		}
    
		return true; 
	}

	function days2MonthDay($oldDate, $addDay){
		list($year, $day, $hour, $min, $sec) = sscanf($oldDate, "%04d%03d%02d%02d%02d");

		if ($addDay) $day++;
		$newDate = date("Y/m/d",strtotime("+$day days",strtotime("$year-01-01 $hour:$min:$sec")));

		return $newDate;
	}
   
	/* Update Remote Bases Start - Stop */

	public function updateRemoteStartStop($extBaseID){

		$domName = RemoteData.$extBaseID."/base.xml";
		$dom = new DomDocument();
		if (!$dom->load($domName)){
			$msg = "Cannot load file ".$domName.PHP_EOL;
			print_r($msg);
			error_log($msg,1,email);
			return false;;
		}
		 
		$VIs = $dom -> getElementsByTagName("dataset");
		foreach ($VIs as $vi) {
			$name = $vi->getAttribute('xml:id');
			// Call AMDA WebService
			try {
				$res =  $this->client->getRemoteStartStop($extBaseID, $name);
				print_r($name.' : '.$res.PHP_EOL);
				error_log($name.' : '.$res.PHP_EOL,3,log);
				if ($res !== "NOEXTERNALDATA" && $res !== "NODATASET")
				$vi->setAttribute('desc', $res);
			}
			catch  (SoapFault $exception)
			{
				$msg = $exception->faultstring." : DataSet ".$name." Start-Stop can't be updated".PHP_EOL;
				print_r($msg);
				if (!$this->updateOnly) error_log($msg,1,email);
			}
		}
		$dom->save($domName);
		 
		return true;
	}

	/* Check Remote Bases Connection */
// 	public function checkRemoteConnection($extBaseID){
// 		try {
// 			$res =  $this->client->checkRemoteConnection($extBaseID);
// 
// 		}
// 		catch  (SoapFault $exception)
// 		{
// 			print_r($exception->faultstring.PHP_EOL);
// 			error_log($exception->faultstring.PHP_EOL,1,email);
// 			return false;
// 		}
// 
// 		return $res;
// 	}
	 
	/* Get user info from login */
	public function getUserInfo($login) { 
		try {
			$data_array = array(
				"login" => $login,
				"timestamp" => time(),
			);
			$data = $this->base64url_encode(json_encode($data_array));
			$check = md5($data.DDSERVICE_PUBLICKEY.DDSERVICE_PRIVATEKEY);
			$info = $this->client->getUserInfo($data,DDSERVICE_PUBLICKEY,$check);
		}
		catch  (SoapFault $exception) {
			return array('success' => false, 'message' => $exception->faultstring);
		} 
		return $info;
	}

    public function resetUserPwd($login, $crt_pwd, $new_pwd) {
        try {
			$data_array = array(
				"login" => $login,
                "crtpwd" => $crt_pwd,
                "newpwd" => $new_pwd,
				"timestamp" => time(),
			);
			$data = $this->base64url_encode(json_encode($data_array));
			$check = md5($data.DDSERVICE_PUBLICKEY.DDSERVICE_PRIVATEKEY);
			$result = $this->client->resetUserPwd($data,DDSERVICE_PUBLICKEY,$check);
		}
		catch  (SoapFault $exception) {
			return array('success' => false, 'message' => $exception->faultstring);
		} 
        if (!$result) {
            return array('success' => false, 'message' => "Error during password modification");
        }
		return array('success' => true);
    }

	/* Create a new user */
	public function createUser($login, $pwd, $first_name, $last_name, $email) {
		$result = FALSE;
		try {
			$data_array = array(
				"login" => $login,
				"pwd"   => $pwd,
				"first_name" => $first_name,
				"last_name"  => $last_name,
				"email"      => $email,
				"timestamp" => time(),
			);
			$data = $this->base64url_encode(json_encode($data_array));
			$check = md5($data.DDSERVICE_PUBLICKEY.DDSERVICE_PRIVATEKEY);
			$result = $this->client->createUser($data,DDSERVICE_PUBLICKEY,$check);
		}
		catch  (SoapFault $exception) {
		}
		return $result;
	}
	
	/* Get data set  info  */
// 	public function getInfo($viID, $infoID) {
// 		try {
// 			$info = $this->client->getInfo($viID, $infoID);
// 		}
// 		catch  (SoapFault $exception) {
// 			return array('success' => false, 'message' => $exception->faultstring);
// 		} 
// 	return $info;
// }

/* Recursively modify rights & grp */
// 	public   function fsmodify($obj) {
// 		chgrp($obj, APACHE_USER); 
// 		chmod($obj, is_dir($obj) ? 0775 : 0664);               
// 	}
// 
// 	public function fsmodifyr($dir) 
// 	{
// 		if($objs = glob($dir."/*")) {        
// 			foreach($objs as $obj) {
// 				$this->fsmodify($obj);
// 				if(is_dir($obj)) $this->fsmodifyr($obj);
// 			}
// 		}
// 		return $this->fsmodify($dir);
// 	}
 
}