Blame view

php/classes/AmdaClient.php 7.83 KB
16035364   Benjamin Renard   First commit
1
2
3
<?php

/**  @class AmdaClient
aa94fd24   elena   Merge with last svn
4
 *    @version $Id: AmdaClient.php 2888 2015-04-29 15:25:42Z elena $
16035364   Benjamin Renard   First commit
5
6
7
8
9
 *    @brief Client of DD Web Server
 *
 */

  ini_set("soap.wsdl_cache_enabled", "0");
6d5dcc15   Myriam Bouchemit   IP address of man...
10
  ini_set('soap.wsdl_cache_ttl',0);
16035364   Benjamin Renard   First commit
11
12
13
14

class AmdaClient {

	public $client;
db4f24d1   Elena.Budnik   redmine #5385, #4...
15
16
 
	function __construct(){	 
16035364   Benjamin Renard   First commit
17
		try {
db4f24d1   Elena.Budnik   redmine #5385, #4...
18
			$this->client = new SoapClient(DD_WSDL);
16035364   Benjamin Renard   First commit
19
20
21
22
23
24
25
		}
		catch  (SoapFault $exception) {
			$msg = $exception->faultstring.PHP_EOL;
			error_log($msg, 1, email);
			exit($msg);
		}
		// reset log file
db4f24d1   Elena.Budnik   redmine #5385, #4...
26
		if (file_exists(log)) unlink(log);
16035364   Benjamin Renard   First commit
27

db4f24d1   Elena.Budnik   redmine #5385, #4...
28
		date_default_timezone_set('UTC');
16035364   Benjamin Renard   First commit
29
30
	}

1e8c4961   Benjamin Renard   Automatic user re...
31
32
33
34
	public function base64url_encode($data) {
		return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
	} 

db4f24d1   Elena.Budnik   redmine #5385, #4...
35
	/* Just copy URL to Info/Bases.xml */	 
16035364   Benjamin Renard   First commit
36
	public function getAvailableExternalBases() {
16035364   Benjamin Renard   First commit
37
38
39
40
41
42
43
44
45
46
		try {
			$extBases = $this->client->getAvailableExternalBases();
		}
		catch  (SoapFault $exception) {
			$msg = $exception->faultstring.PHP_EOL;
			error_log($msg, 1, email);
			exit($msg);
		}

		if ($extBases === "NOEXTERNALBASES") {
db4f24d1   Elena.Budnik   redmine #5385, #4...
47
			error_log($extBases.PHP_EOL, 1, email);
16035364   Benjamin Renard   First commit
48
49
50
51
52
53
			return $extBases;
		}

		$extBasesDom = new DomDocument("1.0");
		if (!$extBasesDom->load($extBases)) {
			$msg = "Can't load Bases.xml".PHP_EOL;
db4f24d1   Elena.Budnik   redmine #5385, #4...
54
			error_log($msg, 1, email);
16035364   Benjamin Renard   First commit
55
56
57
			exit($msg);
		}

db4f24d1   Elena.Budnik   redmine #5385, #4...
58
59
60
61
62
63
// 		$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);
// 		}
16035364   Benjamin Renard   First commit
64

db4f24d1   Elena.Budnik   redmine #5385, #4...
65
		$extBasesNames = $extBasesDom->getElementsByTagName("dataCenter");
16035364   Benjamin Renard   First commit
66
67
		if ($extBasesNames->length === 0) {
			$msg = "no external bases found in Bases.xml".PHP_EOL;
db4f24d1   Elena.Budnik   redmine #5385, #4...
68
			error_log($msg, 1, email);
16035364   Benjamin Renard   First commit
69
70
			exit($msg);
		}
db4f24d1   Elena.Budnik   redmine #5385, #4...
71
/*
16035364   Benjamin Renard   First commit
72
		if (!$this->updateOnly) {
db4f24d1   Elena.Budnik   redmine #5385, #4...
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
			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; 
16035364   Benjamin Renard   First commit
93
94
95
96
	}
	 
	/* Just copy URL to LocalData/Amda_Users.xml */
	 
db4f24d1   Elena.Budnik   redmine #5385, #4...
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 	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;
// 	}
// 	
3afe4f59   Benjamin Renard   Add datasets time...
114
115
116
117
118
119
120
121
122
123
124
125
126

	/* 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;
	}

db4f24d1   Elena.Budnik   redmine #5385, #4...
127
128
	/* Get Time Restriction from VI in DDBASE */
	public function getTimeRestriction($dataSet) {
16035364   Benjamin Renard   First commit
129
		try {
db4f24d1   Elena.Budnik   redmine #5385, #4...
130
				$restrictions = $this->client->getTimeRestriction($dataSet);
16035364   Benjamin Renard   First commit
131
		}
db4f24d1   Elena.Budnik   redmine #5385, #4...
132
133
134
		catch  (SoapFault $exception) {                     
				error_log($exception->faultstring.PHP_EOL, 1, email);
			return FALSE;
16035364   Benjamin Renard   First commit
135
		}
db4f24d1   Elena.Budnik   redmine #5385, #4...
136
137
138
		if ($restrictions['success'] === FALSE) {
				error_log('TimeRestriction: '.$dataSet.' '.$restrictions['days'], 1, email);
			return FALSE;
16035364   Benjamin Renard   First commit
139
		}
db4f24d1   Elena.Budnik   redmine #5385, #4...
140
141
142
								
		return $restrictions;
				
16035364   Benjamin Renard   First commit
143
	}
16035364   Benjamin Renard   First commit
144

db4f24d1   Elena.Budnik   redmine #5385, #4...
145
	/* Just copy URL to DATA/$BaseID/base.xml */ 
16035364   Benjamin Renard   First commit
146
	public function getAvailableExternalData($BaseID) {
16035364   Benjamin Renard   First commit
147
148
149
150
151
152
153
154
155
156
		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;
db4f24d1   Elena.Budnik   redmine #5385, #4...
157
158
159
160
161
162
		//drwxrwxr-x
		if (!$this->updateOnly) {
			if (!file_exists(RemoteData.$BaseID)) mkdir(RemoteData.$BaseID, 0775, true);
			chgrp(RemoteData.$BaseID, APACHE_USER);                
			chmod(RemoteData.$BaseID, 0775);
		}
16035364   Benjamin Renard   First commit
163
164
165

		if (!copy($extData,RemoteData.$BaseID."/base.xml"))  {
			$msg = "problem with copying base.xml for ".$BaseID.PHP_EOL;
db4f24d1   Elena.Budnik   redmine #5385, #4...
166
			error_log($msg, 1, email);
16035364   Benjamin Renard   First commit
167
168
169
			exit($msg);
		}
    
db4f24d1   Elena.Budnik   redmine #5385, #4...
170
		return true; 
16035364   Benjamin Renard   First commit
171
172
	}

db4f24d1   Elena.Budnik   redmine #5385, #4...
173
	function days2MonthDay($oldDate, $addDay){
16035364   Benjamin Renard   First commit
174
175
		list($year, $day, $hour, $min, $sec) = sscanf($oldDate, "%04d%03d%02d%02d%02d");

db4f24d1   Elena.Budnik   redmine #5385, #4...
176
177
		if ($addDay) $day++;
		$newDate = date("Y/m/d",strtotime("+$day days",strtotime("$year-01-01 $hour:$min:$sec")));
16035364   Benjamin Renard   First commit
178
179
180

		return $newDate;
	}
db4f24d1   Elena.Budnik   redmine #5385, #4...
181
   
16035364   Benjamin Renard   First commit
182
183
184
185
186
187
188
189
190
	/* 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);
db4f24d1   Elena.Budnik   redmine #5385, #4...
191
			error_log($msg,1,email);
16035364   Benjamin Renard   First commit
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
			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 */
db4f24d1   Elena.Budnik   redmine #5385, #4...
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// 	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;
// 	}
16035364   Benjamin Renard   First commit
233
234
235
236
	 
	/* Get user info from login */
	public function getUserInfo($login) { 
		try {
1e8c4961   Benjamin Renard   Automatic user re...
237
238
239
240
241
242
243
			$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);
16035364   Benjamin Renard   First commit
244
245
246
		}
		catch  (SoapFault $exception) {
			return array('success' => false, 'message' => $exception->faultstring);
db4f24d1   Elena.Budnik   redmine #5385, #4...
247
		} 
16035364   Benjamin Renard   First commit
248
249
		return $info;
	}
1e8c4961   Benjamin Renard   Automatic user re...
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

	/* 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;
	}
db4f24d1   Elena.Budnik   redmine #5385, #4...
271
	
16035364   Benjamin Renard   First commit
272
	/* Get data set  info  */
db4f24d1   Elena.Budnik   redmine #5385, #4...
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// 	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);
// 	}
16035364   Benjamin Renard   First commit
299
 
16035364   Benjamin Renard   First commit
300
}
aa94fd24   elena   Merge with last svn