16 private $cmdPart2plus =
"";
17 private $checkGetStatements =
true;
22 $this->xapiproxy = $this->dic[
'xapiproxy'];
23 $this->request = $this->dic->http()->request();
28 $this->xapiProxyResponse = $this->xapiproxy->getXapiProxyResponse();
29 $request = $this->dic->http()->request();
30 $cmdParts = $this->xapiproxy->cmdParts();
31 $this->xapiproxy->log()->debug($this->msg(var_export($cmdParts,
true)));
32 if (count($cmdParts) === 5) {
34 if ($cmd ===
"statements") {
35 $this->handleStatementsRequest($request);
36 } elseif ($cmd ===
"activities") {
37 $this->handleActivitiesRequest($request);
38 } elseif ($cmd ===
"activities/profile") {
39 $this->handleActivitiesProfileRequest($request);
40 } elseif ($cmd ===
"activities/state") {
41 $this->handleActivitiesStateRequest($request);
42 } elseif ($cmd ===
"agents") {
43 $this->handleAgentsRequest($request);
44 } elseif ($cmd ===
"agents/profile") {
45 $this->handleAgentsProfileRequest($request);
46 } elseif ($cmd ===
"about") {
47 $this->handleAboutRequest($request);
49 $this->xapiproxy->log()->debug($this->msg(
"Wrong xApi Query: " . $request->getUri()));
50 $this->xapiProxyResponse->exitBadRequest();
53 $this->xapiproxy->log()->error($this->msg(
"Wrong xApi Query: " . $request->getUri()));
54 $this->xapiProxyResponse->exitBadRequest();
60 $this->xapiproxy->log()->debug($this->msg(
"handleStatementsRequest (" . $this->xapiproxy->method() .
"): " . $request->getUri()));
61 $method = $this->xapiproxy->method();
62 if ($method ===
"post" || $method ===
"put") {
63 $this->handlePostPutStatementsRequest($request);
64 } elseif ($method ===
"get") {
65 $this->handleGetStatementsRequest($request);
67 $this->xapiProxyResponse->exitBadRequest();
73 if ($this->xapiproxy->cmdParts()[4] ==
"") {
74 $this->xapiproxy->log()->warning($this->msg(
"unfiltered get statements requests are not allowed for security reasons"));
75 $this->xapiProxyResponse->exitBadRequest();
77 $this->xapiproxy->log()->debug($this->msg(
"handleGetStatementsRequest: " . $request->getUri()));
81 if ($this->checkGetStatements) {
85 if (isset(
$_GET[
'statementId'])) {
86 $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)"));
89 if (isset(
$_GET[
'activity'])) {
92 $this->xapiproxy->log()->debug($this->msg(
"add activity: " . $obj->getActivityId()));
93 $this->cmdPart2plus .=
"&activity=" . $obj->getActivityId() .
"&related_activities=true";
95 if (!$access->hasOutcomesAccess($authToken->getUsrId())) {
108 if (isset(
$_GET[
'registration'])) {
109 $regParam =
$_GET[
'registration'];
110 if ($regParam != $regUserObject) {
111 $this->xapiproxy->log()->debug($this->msg(
"wrong registration: " . $regParam .
" != " . $regUserObject));
115 $this->xapiproxy->log()->debug($this->msg(
"add registration: " . $regUserObject));
116 $this->cmdPart2plus .=
"®istration=" . $regUserObject;
122 $this->xapiProxyResponse->exitBadRequest();
124 $this->handleProxy($request);
127 $this->xapiproxy->log()->error($this->msg($e->getMessage()));
133 $this->xapiproxy->log()->debug($this->msg(
"handlePostPutStatementsRequest: " . $request->getUri()));
134 $body = $request->getBody()->getContents();
136 $this->xapiproxy->log()->warning($this->msg(
"empty body in handlePostPutRequest"));
137 $this->handleProxy($request);
140 $this->xapiproxy->log()->debug($this->msg(
"process statements"));
141 $retArr = $this->xapiproxy->processStatements($request, $body);
142 if (is_array($retArr)) {
143 $body = json_encode($retArr[0]);
144 $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);
155 $this->xapiproxy->log()->error($this->msg($e->getMessage()));
156 $this->handleProxy($request, $fakePostBody);
165 $this->xapiproxy->log()->debug($this->msg(
"handleActivitiesRequest (" . $this->xapiproxy->method() .
"): " . $request->getUri()));
166 $this->handleProxy($request);
171 $this->xapiproxy->log()->debug($this->msg(
"handleActivitiesProfileRequest (" . $this->xapiproxy->method() .
"): " . $request->getUri()));
172 $this->handleProxy($request);
177 $this->xapiproxy->log()->debug($this->msg(
"handleActivitiesStateRequest (" . $this->xapiproxy->method() .
"): " . $request->getUri()));
178 $this->handleProxy($request);
183 $this->xapiproxy->log()->debug($this->msg(
"blocked handleAgentsRequest (" . $this->xapiproxy->method() .
"): " . $request->getUri()));
184 $this->xapiProxyResponse->exitBadRequest();
189 $this->xapiproxy->log()->debug($this->msg(
"handleAgentsProfileRequest (" . $this->xapiproxy->method() .
"): " . $request->getUri()));
190 $this->handleProxy($request);
195 $this->xapiproxy->log()->debug($this->msg(
"handleAboutRequest (" . $this->xapiproxy->method() .
"): " . $request->getUri()));
196 $this->handleProxy($request);
201 $endpointDefault = $this->xapiproxy->getDefaultLrsEndpoint();
202 $endpointFallback = $this->xapiproxy->getFallbackLrsEndpoint();
204 $this->xapiproxy->log()->debug($this->msg(
"endpointDefault: " . $endpointDefault));
205 $this->xapiproxy->log()->debug($this->msg(
"endpointFallback: " . $endpointFallback));
207 $keyDefault = $this->xapiproxy->getDefaultLrsKey();
208 $secretDefault = $this->xapiproxy->getDefaultLrsSecret();
209 $authDefault =
'Basic ' . base64_encode($keyDefault .
':' . $secretDefault);
211 $hasFallback = ($endpointFallback ===
"") ?
false :
true;
214 $keyFallback = $this->xapiproxy->getFallbackLrsKey();
215 $secretFallback = $this->xapiproxy->getFallbackLrsSecret();
216 $authFallback =
'Basic ' . base64_encode($keyFallback .
':' . $secretFallback);
220 RequestOptions::VERIFY =>
true,
221 RequestOptions::CONNECT_TIMEOUT => 10,
222 RequestOptions::HTTP_ERRORS =>
false 224 $cmd = $this->xapiproxy->cmdParts()[2] . $this->cmdPart2plus;
225 $upstreamDefault = $endpointDefault . $cmd;
226 $uriDefault =
new Uri($upstreamDefault);
227 $body = $request->getBody()->getContents();
228 $reqDefault = $this->createProxyRequest($request, $uriDefault, $authDefault, $body);
231 $upstreamFallback = $endpointFallback . $cmd;
232 $uriFallback =
new Uri($upstreamFallback);
233 $reqFallback = $this->createProxyRequest($request, $uriFallback, $authFallback, $body);
236 $httpclient =
new Client();
239 'default' => $httpclient->sendAsync($reqDefault, $req_opts),
240 'fallback' => $httpclient->sendAsync($reqFallback, $req_opts)
246 $responses = Promise\Utils::settle($promises)->wait();
248 $this->xapiproxy->log()->error($this->msg($e->getMessage()));
251 $defaultOk = $this->xapiProxyResponse->checkResponse($responses[
'default'], $endpointDefault);
252 $fallbackOk = $this->xapiProxyResponse->checkResponse($responses[
'fallback'], $endpointFallback);
256 $this->xapiProxyResponse->handleResponse($reqDefault, $responses[
'default'][
'value'], $fakePostBody);
258 $this->xapiproxy->error($this->msg(
"XAPI exception from Default LRS: " . $endpointDefault .
" (sent HTTP 500 to client): " . $e->getMessage()));
259 $this->xapiProxyResponse->exitProxyError();
261 } elseif ($fallbackOk) {
263 $this->xapiProxyResponse->handleResponse($reqFallback, $responses[
'fallback'][
'value'], $fakePostBody);
265 $this->xapiproxy->error($this->msg(
"XAPI exception from Default LRS: " . $endpointDefault .
" (sent HTTP 500 to client): " . $e->getMessage()));
266 $this->xapiProxyResponse->exitProxyError();
269 $this->xapiProxyResponse->exitResponseError();
273 'default' => $httpclient->sendAsync($reqDefault, $req_opts)
278 $responses = Promise\Utils::settle($promises)->wait();
280 $this->xapiproxy->log()->error($this->msg($e->getMessage()));
282 if ($this->xapiProxyResponse->checkResponse($responses[
'default'], $endpointDefault)) {
284 $this->xapiProxyResponse->handleResponse($reqDefault, $responses[
'default'][
'value'], $fakePostBody);
286 $this->xapiproxy->error($this->msg(
"XAPI exception from Default LRS: " . $endpointDefault .
" (sent HTTP 500 to client): " . $e->getMessage()));
287 $this->xapiProxyResponse->exitProxyError();
290 $this->xapiProxyResponse->exitResponseError();
299 'Cache-Control' =>
'no-cache, no-store, must-revalidate',
300 'Authorization' =>
$auth 303 if ($request->hasHeader(
'X-Experience-API-Version')) {
304 $headers[
'X-Experience-API-Version'] = $request->getHeader(
'X-Experience-API-Version');
307 if ($request->hasHeader(
'Referrer')) {
308 $headers[
'Referrer'] = $request->getHeader(
'Referrer');
311 if ($request->hasHeader(
'Content-Type')) {
312 $headers[
'Content-Type'] = $request->getHeader(
'Content-Type');
315 if ($request->hasHeader(
'Origin')) {
316 $headers[
'Origin'] = $request->getHeader(
'Origin');
319 if ($request->hasHeader(
'Content-Length')) {
320 $contentLength = $request->getHeader(
'Content-Length');
321 if (is_array($contentLength) && $contentLength[0] ===
'') {
322 $contentLength = array(0);
323 } elseif ($contentLength ===
'') {
324 $contentLength = array(0);
326 $headers[
'Content-Length'] = $contentLength;
329 if ($request->hasHeader(
'Connection')) {
330 $headers[
'Connection'] = $request->getHeader(
'Connection');
335 $req =
new Request(strtoupper($request->getMethod()), $uri, $headers, $body);
340 private function msg($msg)
342 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']
handleProxy($request, $fakePostBody=null)
static getRegistrationFromAuthToken(ilCmiXapiAuthToken $authToken)
handleActivitiesRequest($request)
createProxyRequest($request, $uri, $auth, $body)