16 private $cmdPart2plus =
"";
17 private $checkGetStatements =
true;
21 $this->xapiproxy = $this->dic[
'xapiproxy'];
22 $this->request = $this->dic->http()->request();
26 $this->xapiProxyResponse = $this->xapiproxy->getXapiProxyResponse();
27 $request = $this->dic->http()->request();
28 $cmdParts = $this->xapiproxy->cmdParts();
29 $this->xapiproxy->log()->debug($this->msg(var_export($cmdParts,
true)));
30 if (count($cmdParts) === 5) {
32 if ($cmd ===
"statements") {
33 $this->handleStatementsRequest($request);
34 } elseif ($cmd ===
"activities") {
35 $this->handleActivitiesRequest($request);
36 } elseif ($cmd ===
"activities/profile") {
37 $this->handleActivitiesProfileRequest($request);
38 } elseif ($cmd ===
"activities/state") {
39 $this->handleActivitiesStateRequest($request);
40 } elseif ($cmd ===
"agents") {
41 $this->handleAgentsRequest($request);
42 } elseif ($cmd ===
"agents/profile") {
43 $this->handleAgentsProfileRequest($request);
44 } elseif ($cmd ===
"about") {
45 $this->handleAboutRequest($request);
47 $this->xapiproxy->log()->debug($this->msg(
"Wrong xApi Query: " . $request->getUri()));
48 $this->xapiProxyResponse->exitBadRequest();
51 $this->xapiproxy->log()->error($this->msg(
"Wrong xApi Query: " . $request->getUri()));
52 $this->xapiProxyResponse->exitBadRequest();
58 $this->xapiproxy->log()->debug($this->msg(
"handleStatementsRequest (" . $this->xapiproxy->method() .
"): " . $request->getUri()));
59 $method = $this->xapiproxy->method();
60 if ($method ===
"post" || $method ===
"put") {
61 $this->handlePostPutStatementsRequest($request);
62 } elseif ($method ===
"get") {
63 $this->handleGetStatementsRequest($request);
65 $this->xapiProxyResponse->exitBadRequest();
71 if ($this->xapiproxy->cmdParts()[4] ==
"") {
72 $this->xapiproxy->log()->warning($this->msg(
"unfiltered get statements requests are not allowed for security reasons"));
73 $this->xapiProxyResponse->exitBadRequest();
75 $this->xapiproxy->log()->debug($this->msg(
"handleGetStatementsRequest: " . $request->getUri()));
79 if ($this->checkGetStatements) {
83 if (isset(
$_GET[
'statementId'])) {
84 $this->xapiproxy->log()->debug($this->msg(
"single statementId requests can not be secured. It is not allowed to append any additional parameter like registration or activity (tested in LL7)"));
87 if (isset(
$_GET[
'activity'])) {
90 $this->xapiproxy->log()->debug($this->msg(
"add activity: " . $obj->getActivityId()));
91 $this->cmdPart2plus .=
"&activity=" . $obj->getActivityId() .
"&related_activities=true";
93 if (!$access->hasOutcomesAccess($authToken->getUsrId())) {
106 if (isset(
$_GET[
'registration'])) {
107 $regParam =
$_GET[
'registration'];
108 if ($regParam != $regUserObject) {
109 $this->xapiproxy->log()->debug($this->msg(
"wrong registration: " . $regParam .
" != " . $regUserObject));
113 $this->xapiproxy->log()->debug($this->msg(
"add registration: " . $regUserObject));
114 $this->cmdPart2plus .=
"®istration=" . $regUserObject;
120 $this->xapiProxyResponse->exitBadRequest();
122 $this->handleProxy($request);
125 $this->xapiproxy->log()->error($this->msg($e->getMessage()));
131 $this->xapiproxy->log()->debug($this->msg(
"handlePostPutStatementsRequest: " . $request->getUri()));
132 $body = $request->getBody()->getContents();
134 $this->xapiproxy->log()->warning($this->msg(
"empty body in handlePostPutRequest"));
135 $this->handleProxy($request);
139 $this->xapiproxy->log()->debug($this->msg(
"process statements"));
140 $retArr = $this->xapiproxy->processStatements($request, $body);
141 if (is_array($retArr)) {
142 $body = json_encode($retArr[0]);
143 $fakePostBody = $retArr[1];
147 $this->xapiproxy->log()->error($this->msg($e->getMessage()));
148 $this->xapiProxyResponse->exitProxyError();
151 $body = $this->xapiproxy->modifyBody($body);
152 $req =
new Request($request->getMethod(),$request->getUri(),$request->getHeaders(),$body);
153 $this->handleProxy(
$req, $fakePostBody);
156 $this->xapiproxy->log()->error($this->msg($e->getMessage()));
157 $this->handleProxy($request, $fakePostBody);
166 $this->xapiproxy->log()->debug($this->msg(
"handleActivitiesRequest (" . $this->xapiproxy->method() .
"): " . $request->getUri()));
167 $this->handleProxy($request);
172 $this->xapiproxy->log()->debug($this->msg(
"handleActivitiesProfileRequest (" . $this->xapiproxy->method() .
"): " . $request->getUri()));
173 $this->handleProxy($request);
178 $this->xapiproxy->log()->debug($this->msg(
"handleActivitiesStateRequest (" . $this->xapiproxy->method() .
"): " . $request->getUri()));
179 $this->handleProxy($request);
184 $this->xapiproxy->log()->debug($this->msg(
"blocked handleAgentsRequest (" . $this->xapiproxy->method() .
"): " . $request->getUri()));
185 $this->xapiProxyResponse->exitBadRequest();
190 $this->xapiproxy->log()->debug($this->msg(
"handleAgentsProfileRequest (" . $this->xapiproxy->method() .
"): " . $request->getUri()));
191 $this->handleProxy($request);
196 $this->xapiproxy->log()->debug($this->msg(
"handleAboutRequest (" . $this->xapiproxy->method() .
"): " . $request->getUri()));
197 $this->handleProxy($request);
202 $endpointDefault = $this->xapiproxy->getDefaultLrsEndpoint();
203 $endpointFallback = $this->xapiproxy->getFallbackLrsEndpoint();
205 $this->xapiproxy->log()->debug($this->msg(
"endpointDefault: " . $endpointDefault));
206 $this->xapiproxy->log()->debug($this->msg(
"endpointFallback: " . $endpointFallback));
208 $keyDefault = $this->xapiproxy->getDefaultLrsKey();
209 $secretDefault = $this->xapiproxy->getDefaultLrsSecret();
210 $authDefault =
'Basic ' . base64_encode($keyDefault .
':' . $secretDefault);
212 $hasFallback = ($endpointFallback ===
"") ? FALSE : TRUE;
215 $keyFallback = $this->xapiproxy->getFallbackLrsKey();
216 $secretFallback = $this->xapiproxy->getFallbackLrsSecret();
217 $authFallback =
'Basic ' . base64_encode($keyFallback .
':' . $secretFallback);
221 RequestOptions::VERIFY =>
true,
222 RequestOptions::CONNECT_TIMEOUT => 10,
223 RequestOptions::HTTP_ERRORS =>
false 225 $cmd = $this->xapiproxy->cmdParts()[2] . $this->cmdPart2plus;
226 $upstreamDefault = $endpointDefault.$cmd;
227 $uriDefault =
new Uri($upstreamDefault);
228 $body = $request->getBody()->getContents();
229 $reqDefault = $this->createProxyRequest($request,$uriDefault,$authDefault,$body);
232 $upstreamFallback = $endpointFallback.$cmd;
233 $uriFallback =
new Uri($upstreamFallback);
234 $reqFallback = $this->createProxyRequest($request,$uriFallback,$authFallback,$body);
237 $httpclient =
new Client();
240 'default' => $httpclient->sendAsync($reqDefault, $req_opts),
241 'fallback' => $httpclient->sendAsync($reqFallback, $req_opts)
247 $responses = Promise\Utils::settle($promises)->wait();
250 $this->xapiproxy->log()->error($this->msg($e->getMessage()));
253 $defaultOk = $this->xapiProxyResponse->checkResponse($responses[
'default'], $endpointDefault);
254 $fallbackOk = $this->xapiProxyResponse->checkResponse($responses[
'fallback'], $endpointFallback);
258 $this->xapiProxyResponse->handleResponse($reqDefault, $responses[
'default'][
'value'], $fakePostBody);
261 $this->xapiproxy->error($this->msg(
"XAPI exception from Default LRS: " . $endpointDefault .
" (sent HTTP 500 to client): " . $e->getMessage()));
262 $this->xapiProxyResponse->exitProxyError();
266 elseif ($fallbackOk) {
268 $this->xapiProxyResponse->handleResponse($reqFallback, $responses[
'fallback'][
'value'], $fakePostBody);
271 $this->xapiproxy->error($this->msg(
"XAPI exception from Default LRS: " . $endpointDefault .
" (sent HTTP 500 to client): " . $e->getMessage()));
272 $this->xapiProxyResponse->exitProxyError();
276 $this->xapiProxyResponse->exitResponseError();
281 'default' => $httpclient->sendAsync($reqDefault, $req_opts)
286 $responses = Promise\Utils::settle($promises)->wait();
289 $this->xapiproxy->log()->error($this->msg($e->getMessage()));
291 if ($this->xapiProxyResponse->checkResponse($responses[
'default'], $endpointDefault)) {
293 $this->xapiProxyResponse->handleResponse($reqDefault, $responses[
'default'][
'value'], $fakePostBody);
296 $this->xapiproxy->error($this->msg(
"XAPI exception from Default LRS: " . $endpointDefault .
" (sent HTTP 500 to client): " . $e->getMessage()));
297 $this->xapiProxyResponse->exitProxyError();
301 $this->xapiProxyResponse->exitResponseError();
309 'Cache-Control' =>
'no-cache, no-store, must-revalidate',
310 'Authorization' =>
$auth 313 if ($request->hasHeader(
'X-Experience-API-Version')) {
314 $headers[
'X-Experience-API-Version'] = $request->getHeader(
'X-Experience-API-Version');
317 if ($request->hasHeader(
'Referrer')) {
318 $headers[
'Referrer'] = $request->getHeader(
'Referrer');
321 if ($request->hasHeader(
'Content-Type')) {
322 $headers[
'Content-Type'] = $request->getHeader(
'Content-Type');
325 if ($request->hasHeader(
'Origin')) {
326 $headers[
'Origin'] = $request->getHeader(
'Origin');
329 if ($request->hasHeader(
'Content-Length')) {
330 $contentLength = $request->getHeader(
'Content-Length');
331 if (is_array($contentLength) && $contentLength[0] ===
'') {
332 $contentLength = array(0);
334 elseif ($contentLength ===
'') {
335 $contentLength = array(0);
337 $headers[
'Content-Length'] = $contentLength;
340 if ($request->hasHeader(
'Connection')) {
341 $headers[
'Connection'] = $request->getHeader(
'Connection');
346 $req =
new Request(strtoupper($request->getMethod()),$uri,$headers,$body);
351 private function msg($msg) {
352 return $this->xapiproxy->msg($msg);
static getInstance(ilObjCmiXapi $object)
handleAgentsProfileRequest($request)
handleAboutRequest($request)
static getInstanceByToken($token)
handleStatementsRequest($request)
handleActivitiesProfileRequest($request)
handleGetStatementsRequest($request)
handleActivitiesStateRequest($request)
static getCMI5RegistrationFromAuthToken(ilCmiXapiAuthToken $authToken)
handleAgentsRequest($request)
handlePostPutStatementsRequest($request)
static getInstance($a_id=0, $a_reference=true)
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
static getRegistrationFromAuthToken(ilCmiXapiAuthToken $authToken)
handleActivitiesRequest($request)
handleProxy($request, $fakePostBody=NULL)
createProxyRequest($request, $uri, $auth, $body)