/
SBproxy.class.php
215 lines (166 loc) · 6.25 KB
/
SBproxy.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
class SBproxy{
/* Configuration Parameters (not tested) */
const LS_ID = '';
const WSDL = 'http://ilabs.cti.ac.at/iLabServiceBroker/iLabServiceBroker.asmx?WSDL';
/* Variables */
private $client = NULL;
/* Constructor */
function __construct($SBcouponID, $SBpasskey){
//Test if WSDL exists
$wsdlContent = file_get_contents(self::WSDL);
//echo '$wsdlContent:'.$wsdlContent.'__END!';
//TEST: undeklarierte variable ausgeben
//echo $acacacac;
if(empty($wsdlContent)){
throw new Exception($this->genXMLresp('exception','Server is not responding.'));
}else{
$checkWSDL = simplexml_load_string($wsdlContent);
if($checkWSDL === false){
throw new Exception($this->genXMLresp('exception','Unable to load WSDL-file correctly.'));
}else{
//Create new client
$this->client = new SoapClient(self::WSDL, array('exceptions' => 0, 'trace' => 1, 'soap_version' => SOAP_1_2));
//Set SOAP-header
$hNamespace = 'http://ilab.mit.edu';
$hContent = array('couponID' => $SBcouponID,
'couponPassKey' => $SBpasskey);
$header = new SOAPHeader($hNamespace, 'sbAuthHeader', $hContent);
$this->client->__setSoapHeaders($header);
}
}
}//END OF function __construct()
/* Methods */
/** Generate responses in XML-format **/
private function genXMLresp($type,$msg,$operation=NULL){
//XML declaration
$resp = '<?xml version="1.0" encoding="utf-8"?>';
//open root element
if(is_null($operation)){
$resp .= '<SBresponse type="'.$type.'">';
}else{
$resp .= '<SBresponse type="'.$type.'" operation="'.$operation.'">';
}
//append all warnings and notices from custom error-handler
//TODO:error_handler muss in eigene Klasse
//if(!isset($_SESSION['error_cnt'])){
$resp .= '<errors />';
//}else{
// $resp .= '<errors cnt="'.$_SESSION['error_cnt'].'">'.$_SESSION['error_str'].'</errors>';
// unset($_SESSION['error_cnt']);
// unset($_SESSION['error_str']);
//}
//append actual message
if($type == 'exception'){
$resp .= '<exception>'.$msg.'</exception>';
}else{
$resp .= $msg;
}
//close root element
$resp .= '</SBresponse>';
//return
return $resp;
}//END of private function genXMLresp()
/** Generic function for all needed SOAP calls **/
public function soapCall($operation, $parameters = NULL){
//var_dump($operation);
//set client parameter depending on $operation
switch($operation){
case 'Cancel':
case 'GetExperimentStatus':
case 'RetrieveResult':
if($parameters !== NULL){
$params = array('experimentID' => $parameters['experimentID']);
} else {
return $this->genXMLresp('exception','Parameters missing.');
}
break;
case 'GetLabConfiguration':
case 'GetLabStatus':
$params = array('labServerID' => self::LS_ID);
break;
case 'Submit':
if($parameters !== NULL){
$params = array(
'labServerID' => self::LS_ID,
'experimentSpecification' => $parameters['experimentSpecification'],
'priorityHint' => 0,
'emailNotification' => false);
} else {
return $this->genXMLresp('exception','Parameters missing.');
}
break;
default:
return $this->genXMLresp('exception','SOAP operation unknown.');
break;
}
//make a SOAP call
//TODO: try out what happens, if server not responding (make local WSDL file and change name)
$SOAPresp = $this->client->__soapCall($operation, array('parameters' => $params));
//check for soap:Fault
if (is_soap_fault($SOAPresp)) {
$resp_type = 'soapFault';
//TODO: $result durch $SOAPresp ersetzen. Im Moment dient es zum Test des Error-Handlers...
$msg = '<soapFault><faultcode>'.$SOAPresp->faultcode.'</faultcode><faultstring>'.$SOAPresp->faultstring.'</faultstring></soapFault>';
// Faultactor not always defined... causes Notice message!
//+ '<faultactor>'.$SOAPresp->faultactor.'</faultactor><detail>'.$SOAPresp->detail.'</detail>'
//+ '</soapFault>';
//DEBUG
//$msg .= PHP_EOL . PHP_EOL .'<SubmitSpec>'. PHP_EOL . htmlentities(str_replace('><','>'.PHP_EOL.'<',$this->genSubmitSpec($settings))) . PHP_EOL . '</SubmitSpec>'.PHP_EOL . PHP_EOL;
$getLastRequest = $this->client->__getLastRequest();
//$sxe = new SimpleXMLElement($getLastRequest);
//$child = $sxe->children('Envelope',TRUE)->children();
//echo '<pre>'.$getLastRequest.'</pre>';
//$msg .= PHP_EOL . PHP_EOL . '<getLastRequest>'.htmlentities(str_replace('><','>'.PHP_EOL.'<',$getLastRequest)).'</getLastRequest>';//cannot be parsed in JS
//$msg .= '<lastRequest>'.$sxe.'</lastRequest>';
//DEBUG END
//create return message
return $this->genXMLresp($resp_type,$msg,$operation);
} else {//success
$resp_type = 'success';//TODO: Current types exception,soapFault und success. Maybe use 'soapEnvelope' instead of 'success'...
$prop = $operation . 'Result';
require_once(dirname(__FILE__).'/XMLSerializer.inc.php');
//$myLogFile = fopen(dirname(__FILE__).'/LogFile.txt','w') or DIE('Unable to open file!');
//fwrite($myLogFile, '$prop:' . PHP_EOL . $prop . PHP_EOL . PHP_EOL);
//Check if var is object
if( is_object($SOAPresp->$prop) ) {
/*ob_start();
var_dump($SOAPresp->$prop);//no echo inside ob_start/end
$txt = ob_get_contents();
ob_end_clean();
fwrite($myLogFile, 'var_dump($SOAPresp->$prop):' . PHP_EOL . $txt . PHP_EOL);//*/
//var_dump($SOAPresp->$prop);
//---------------
$xml_generator = new XMLSerializer();
$std_class = json_decode(json_encode($SOAPresp->$prop));
$xml = $xml_generator->generateValidXmlFromObj($std_class);
echo $xml;
//fwrite($myLogFile, 'var_dump($SOAPresp->$prop):' . PHP_EOL . $xml . PHP_EOL);
//---------------
//---------------
} else if( is_string($SOAPresp->$prop) ) {
$txt = $SOAPresp->$prop;
echo $txt;
//fwrite($myLogFile, 'var_dump($SOAPresp->$prop):' . PHP_EOL . $txt . PHP_EOL);
}
//echo $SOAPresp->$prop;
//$sxe = new SimpleXMLElement( $SOAPresp->GetLabConfigurationResult );
//$child = $sxe->children('labConfiguration',TRUE)->children();
//$msg = $child->asXML();
//strip response out of soap:Body
//echo $this->client->__getLastResponse().'<br><br>';
//$sxe = new SimpleXMLElement($this->client->__getLastResponse());
//echo $sxe.'\n';
//$child = $sxe->children('soap',TRUE)->children();
//test
//echo 'child:'.$child.'<<end';
//SOAP-Body as response
//$msg = $child->asXML();
//Close File
//fclose($myLogFile);
}
//create return message
//return $this->genXMLresp($resp_type,$msg,$operation);
}//END OF public function soapCall()
}//END OF class SOAP
?>