ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
XapiProxy\XapiProxyRequest Class Reference
+ Collaboration diagram for XapiProxy\XapiProxyRequest:

Public Member Functions

 __construct ()
 
 handle ()
 

Private Member Functions

 handleStatementsRequest ($request)
 
 handleGetStatementsRequest ($request)
 
 handlePostPutStatementsRequest ($request)
 
 handleActivitiesRequest ($request)
 
 handleActivitiesProfileRequest ($request)
 
 handleActivitiesStateRequest ($request)
 
 handleAgentsRequest ($request)
 
 handleAgentsProfileRequest ($request)
 
 handleAboutRequest ($request)
 
 handleProxy ($request, $fakePostBody=NULL)
 
 createProxyRequest ($request, $uri, $auth, $body)
 
 msg ($msg)
 

Private Attributes

 $dic
 
 $xapiproxy
 
 $request
 
 $xapiProxyResponse
 
 $cmdPart2plus = ""
 
 $checkGetStatements = true
 

Detailed Description

Definition at line 10 of file XapiProxyRequest.php.

Constructor & Destructor Documentation

◆ __construct()

XapiProxy\XapiProxyRequest::__construct ( )

Definition at line 19 of file XapiProxyRequest.php.

References $GLOBALS.

19  {
20  $this->dic = $GLOBALS['DIC'];
21  $this->xapiproxy = $this->dic['xapiproxy'];
22  $this->request = $this->dic->http()->request();
23  }
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64

Member Function Documentation

◆ createProxyRequest()

XapiProxy\XapiProxyRequest::createProxyRequest (   $request,
  $uri,
  $auth,
  $body 
)
private

Definition at line 307 of file XapiProxyRequest.php.

References $auth, and XapiProxy\$req.

307  {
308  $headers = array(
309  'Cache-Control' => 'no-cache, no-store, must-revalidate',
310  'Authorization' => $auth
311  );
312 
313  if ($request->hasHeader('X-Experience-API-Version')) {
314  $headers['X-Experience-API-Version'] = $request->getHeader('X-Experience-API-Version');
315  }
316 
317  if ($request->hasHeader('Referrer')) {
318  $headers['Referrer'] = $request->getHeader('Referrer');
319  }
320 
321  if ($request->hasHeader('Content-Type')) {
322  $headers['Content-Type'] = $request->getHeader('Content-Type');
323  }
324 
325  if ($request->hasHeader('Origin')) {
326  $headers['Origin'] = $request->getHeader('Origin');
327  }
328 
329  if ($request->hasHeader('Content-Length')) {
330  $contentLength = $request->getHeader('Content-Length');
331  if (is_array($contentLength) && $contentLength[0] === '') {
332  $contentLength = array(0);
333  }
334  elseif ($contentLength === '') {
335  $contentLength = array(0);
336  }
337  $headers['Content-Length'] = $contentLength;
338  }
339 
340  if ($request->hasHeader('Connection')) {
341  $headers['Connection'] = $request->getHeader('Connection');
342  }
343 
344  //$this->xapiproxy->log()->debug($this->msg($body));
345 
346  $req = new Request(strtoupper($request->getMethod()),$uri,$headers,$body);
347 
348  return $req;
349  }
$auth
Definition: metadata.php:59

◆ handle()

XapiProxy\XapiProxyRequest::handle ( )

Definition at line 25 of file XapiProxyRequest.php.

25  {
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) {
31  $cmd = $cmdParts[3];
32  if ($cmd === "statements") {
34  } elseif ($cmd === "activities") {
36  } elseif ($cmd === "activities/profile") {
38  } elseif ($cmd === "activities/state") {
40  } elseif ($cmd === "agents") {
42  } elseif ($cmd === "agents/profile") {
44  } elseif ($cmd === "about") {
46  } else {
47  $this->xapiproxy->log()->debug($this->msg("Wrong xApi Query: " . $request->getUri()));
48  $this->xapiProxyResponse->exitBadRequest();
49  }
50  } else {
51  $this->xapiproxy->log()->error($this->msg("Wrong xApi Query: " . $request->getUri()));
52  $this->xapiProxyResponse->exitBadRequest();
53  }
54  }

◆ handleAboutRequest()

XapiProxy\XapiProxyRequest::handleAboutRequest (   $request)
private

Definition at line 194 of file XapiProxyRequest.php.

195  {
196  $this->xapiproxy->log()->debug($this->msg("handleAboutRequest (" . $this->xapiproxy->method() . "): " . $request->getUri()));
197  $this->handleProxy($request);
198  }
handleProxy($request, $fakePostBody=NULL)

◆ handleActivitiesProfileRequest()

XapiProxy\XapiProxyRequest::handleActivitiesProfileRequest (   $request)
private

Definition at line 170 of file XapiProxyRequest.php.

171  {
172  $this->xapiproxy->log()->debug($this->msg("handleActivitiesProfileRequest (" . $this->xapiproxy->method() . "): " . $request->getUri()));
173  $this->handleProxy($request);
174  }
handleProxy($request, $fakePostBody=NULL)

◆ handleActivitiesRequest()

XapiProxy\XapiProxyRequest::handleActivitiesRequest (   $request)
private

Definition at line 162 of file XapiProxyRequest.php.

163  {
164 // $this->xapiproxy->log()->debug($this->msg("blocked handleActivitiesRequest (" . $this->xapiproxy->method() . "): " . $request->getUri()));
165 // $this->xapiProxyResponse->exitBadRequest();
166  $this->xapiproxy->log()->debug($this->msg("handleActivitiesRequest (" . $this->xapiproxy->method() . "): " . $request->getUri()));
167  $this->handleProxy($request);
168  }
handleProxy($request, $fakePostBody=NULL)

◆ handleActivitiesStateRequest()

XapiProxy\XapiProxyRequest::handleActivitiesStateRequest (   $request)
private

Definition at line 176 of file XapiProxyRequest.php.

177  {
178  $this->xapiproxy->log()->debug($this->msg("handleActivitiesStateRequest (" . $this->xapiproxy->method() . "): " . $request->getUri()));
179  $this->handleProxy($request);
180  }
handleProxy($request, $fakePostBody=NULL)

◆ handleAgentsProfileRequest()

XapiProxy\XapiProxyRequest::handleAgentsProfileRequest (   $request)
private

Definition at line 188 of file XapiProxyRequest.php.

189  {
190  $this->xapiproxy->log()->debug($this->msg("handleAgentsProfileRequest (" . $this->xapiproxy->method() . "): " . $request->getUri()));
191  $this->handleProxy($request);
192  }
handleProxy($request, $fakePostBody=NULL)

◆ handleAgentsRequest()

XapiProxy\XapiProxyRequest::handleAgentsRequest (   $request)
private

Definition at line 182 of file XapiProxyRequest.php.

183  {
184  $this->xapiproxy->log()->debug($this->msg("blocked handleAgentsRequest (" . $this->xapiproxy->method() . "): " . $request->getUri()));
185  $this->xapiProxyResponse->exitBadRequest();
186  }

◆ handleGetStatementsRequest()

XapiProxy\XapiProxyRequest::handleGetStatementsRequest (   $request)
private

Definition at line 69 of file XapiProxyRequest.php.

References $_GET, Vendor\Package\$e, ilObjCmiXapi\CONT_TYPE_CMI5, ilCmiXapiUser\getCMI5RegistrationFromAuthToken(), ilCmiXapiAccess\getInstance(), ilObjCmiXapi\getInstance(), ilCmiXapiAuthToken\getInstanceByToken(), and ilCmiXapiUser\getRegistrationFromAuthToken().

70  {
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();
74  }
75  $this->xapiproxy->log()->debug($this->msg("handleGetStatementsRequest: " . $request->getUri()));
76 
77  try {
78  $badRequest = false;
79  if ($this->checkGetStatements) {
80  $authToken = \ilCmiXapiAuthToken::getInstanceByToken($this->xapiproxy->token());
81  $obj = \ilObjCmiXapi::getInstance($authToken->getRefId(), true);
82  $access = \ilCmiXapiAccess::getInstance($obj);
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)"));
85  // single statementId can not be handled. it is not allowed to append a registration on single statement requests (tested in LL7)
86  } else {
87  if (isset($_GET['activity'])) {
88  // ToDo: how this can be verified? the object only knows the top activityId
89  } else {
90  $this->xapiproxy->log()->debug($this->msg("add activity: " . $obj->getActivityId()));
91  $this->cmdPart2plus .= "&activity=" . $obj->getActivityId() . "&related_activities=true";
92  }
93  if (!$access->hasOutcomesAccess($authToken->getUsrId())) {
94  // ToCheck
95  /*
96  if (!$access->hasStatementsAccess()) {
97  $this->xapiproxy->log()->warning($this->msg("statements access is not enabled"));
98  $this->xapiProxyResponse->exitBadRequest();
99  }
100  */
101  if ($obj->getContentType() == \ilObjCmiXapi::CONT_TYPE_CMI5) {
102  $regUserObject = \ilCmiXapiUser::getCMI5RegistrationFromAuthToken($authToken);
103  } else {
104  $regUserObject = \ilCmiXapiUser::getRegistrationFromAuthToken($authToken);
105  }
106  if (isset($_GET['registration'])) {
107  $regParam = $_GET['registration'];
108  if ($regParam != $regUserObject) {
109  $this->xapiproxy->log()->debug($this->msg("wrong registration: " . $regParam . " != " . $regUserObject));
110  $badRequest = true;
111  }
112  } else { // add registration
113  $this->xapiproxy->log()->debug($this->msg("add registration: " . $regUserObject));
114  $this->cmdPart2plus .= "&registration=" . $regUserObject;
115  }
116  }
117  }
118  }
119  if ($badRequest) {
120  $this->xapiProxyResponse->exitBadRequest();
121  } else {
122  $this->handleProxy($request);
123  }
124  } catch (\Exception $e) {
125  $this->xapiproxy->log()->error($this->msg($e->getMessage()));
126  }
127  }
static getInstance(ilObjCmiXapi $object)
$_GET["client_id"]
static getCMI5RegistrationFromAuthToken(ilCmiXapiAuthToken $authToken)
static getInstance($a_id=0, $a_reference=true)
static getRegistrationFromAuthToken(ilCmiXapiAuthToken $authToken)
handleProxy($request, $fakePostBody=NULL)
+ Here is the call graph for this function:

◆ handlePostPutStatementsRequest()

XapiProxy\XapiProxyRequest::handlePostPutStatementsRequest (   $request)
private

Definition at line 129 of file XapiProxyRequest.php.

References Vendor\Package\$e, and XapiProxy\$req.

130  {
131  $this->xapiproxy->log()->debug($this->msg("handlePostPutStatementsRequest: " . $request->getUri()));
132  $body = $request->getBody()->getContents();
133  if (empty($body)) {
134  $this->xapiproxy->log()->warning($this->msg("empty body in handlePostPutRequest"));
135  $this->handleProxy($request);
136  }
137  else {
138  try {
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]); // new body with allowed statements
143  $fakePostBody = $retArr[1]; // fake post php array of ALL statments as if all statements were processed
144  }
145  }
146  catch(\Exception $e) {
147  $this->xapiproxy->log()->error($this->msg($e->getMessage()));
148  $this->xapiProxyResponse->exitProxyError();
149  }
150  try {
151  $body = $this->xapiproxy->modifyBody($body);
152  $req = new Request($request->getMethod(),$request->getUri(),$request->getHeaders(),$body);
153  $this->handleProxy($req, $fakePostBody);
154  }
155  catch(\Exception $e) {
156  $this->xapiproxy->log()->error($this->msg($e->getMessage()));
157  $this->handleProxy($request, $fakePostBody);
158  }
159  }
160  }
handleProxy($request, $fakePostBody=NULL)

◆ handleProxy()

XapiProxy\XapiProxyRequest::handleProxy (   $request,
  $fakePostBody = NULL 
)
private

Definition at line 200 of file XapiProxyRequest.php.

References Vendor\Package\$e.

200  {
201 
202  $endpointDefault = $this->xapiproxy->getDefaultLrsEndpoint();
203  $endpointFallback = $this->xapiproxy->getFallbackLrsEndpoint();
204 
205  $this->xapiproxy->log()->debug($this->msg("endpointDefault: " . $endpointDefault));
206  $this->xapiproxy->log()->debug($this->msg("endpointFallback: " . $endpointFallback));
207 
208  $keyDefault = $this->xapiproxy->getDefaultLrsKey();
209  $secretDefault = $this->xapiproxy->getDefaultLrsSecret();
210  $authDefault = 'Basic ' . base64_encode($keyDefault . ':' . $secretDefault);
211 
212  $hasFallback = ($endpointFallback === "") ? FALSE : TRUE;
213 
214  if ($hasFallback) {
215  $keyFallback = $this->xapiproxy->getFallbackLrsKey();
216  $secretFallback = $this->xapiproxy->getFallbackLrsSecret();
217  $authFallback = 'Basic ' . base64_encode($keyFallback . ':' . $secretFallback);
218  }
219 
220  $req_opts = array(
221  RequestOptions::VERIFY => true,
222  RequestOptions::CONNECT_TIMEOUT => 10,
223  RequestOptions::HTTP_ERRORS => false
224  );
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);
230 
231  if ($hasFallback) {
232  $upstreamFallback = $endpointFallback.$cmd;
233  $uriFallback = new Uri($upstreamFallback);
234  $reqFallback = $this->createProxyRequest($request,$uriFallback,$authFallback,$body);
235  }
236 
237  $httpclient = new Client();
238  if ($hasFallback) {
239  $promises = [
240  'default' => $httpclient->sendAsync($reqDefault, $req_opts),
241  'fallback' => $httpclient->sendAsync($reqFallback, $req_opts)
242  ];
243 
244  // this would throw first ConnectionException
245  // $responses = Promise\unwrap($promises);
246  try {
247  $responses = Promise\Utils::settle($promises)->wait();
248  }
249  catch(\Exception $e) {
250  $this->xapiproxy->log()->error($this->msg($e->getMessage()));
251  }
252 
253  $defaultOk = $this->xapiProxyResponse->checkResponse($responses['default'], $endpointDefault);
254  $fallbackOk = $this->xapiProxyResponse->checkResponse($responses['fallback'], $endpointFallback);
255 
256  if ($defaultOk) {
257  try {
258  $this->xapiProxyResponse->handleResponse($reqDefault, $responses['default']['value'], $fakePostBody);
259  }
260  catch (\Exception $e) {
261  $this->xapiproxy->error($this->msg("XAPI exception from Default LRS: " . $endpointDefault . " (sent HTTP 500 to client): " . $e->getMessage()));
262  $this->xapiProxyResponse->exitProxyError();
263  }
264 
265  }
266  elseif ($fallbackOk) {
267  try {
268  $this->xapiProxyResponse->handleResponse($reqFallback, $responses['fallback']['value'], $fakePostBody);
269  }
270  catch (\Exception $e) {
271  $this->xapiproxy->error($this->msg("XAPI exception from Default LRS: " . $endpointDefault . " (sent HTTP 500 to client): " . $e->getMessage()));
272  $this->xapiProxyResponse->exitProxyError();
273  }
274  }
275  else {
276  $this->xapiProxyResponse->exitResponseError();
277  }
278  }
279  else {
280  $promises = [
281  'default' => $httpclient->sendAsync($reqDefault, $req_opts)
282  ];
283  // this would throw first ConnectionException
284  // $responses = Promise\unwrap($promises);
285  try {
286  $responses = Promise\Utils::settle($promises)->wait();
287  }
288  catch(\Exception $e) {
289  $this->xapiproxy->log()->error($this->msg($e->getMessage()));
290  }
291  if ($this->xapiProxyResponse->checkResponse($responses['default'], $endpointDefault)) {
292  try {
293  $this->xapiProxyResponse->handleResponse($reqDefault, $responses['default']['value'], $fakePostBody);
294  }
295  catch(\Exception $e) {
296  $this->xapiproxy->error($this->msg("XAPI exception from Default LRS: " . $endpointDefault . " (sent HTTP 500 to client): " . $e->getMessage()));
297  $this->xapiProxyResponse->exitProxyError();
298  }
299  }
300  else {
301  $this->xapiProxyResponse->exitResponseError();
302  }
303  }
304  }
createProxyRequest($request, $uri, $auth, $body)

◆ handleStatementsRequest()

XapiProxy\XapiProxyRequest::handleStatementsRequest (   $request)
private

Definition at line 56 of file XapiProxyRequest.php.

57  {
58  $this->xapiproxy->log()->debug($this->msg("handleStatementsRequest (" . $this->xapiproxy->method() . "): " . $request->getUri()));
59  $method = $this->xapiproxy->method();
60  if ($method === "post" || $method === "put") {
62  } elseif ($method === "get") {
64  } else {
65  $this->xapiProxyResponse->exitBadRequest();
66  }
67  }

◆ msg()

XapiProxy\XapiProxyRequest::msg (   $msg)
private

Definition at line 351 of file XapiProxyRequest.php.

351  {
352  return $this->xapiproxy->msg($msg);
353  }

Field Documentation

◆ $checkGetStatements

XapiProxy\XapiProxyRequest::$checkGetStatements = true
private

Definition at line 17 of file XapiProxyRequest.php.

◆ $cmdPart2plus

XapiProxy\XapiProxyRequest::$cmdPart2plus = ""
private

Definition at line 16 of file XapiProxyRequest.php.

◆ $dic

XapiProxy\XapiProxyRequest::$dic
private

Definition at line 12 of file XapiProxyRequest.php.

◆ $request

XapiProxy\XapiProxyRequest::$request
private

Definition at line 14 of file XapiProxyRequest.php.

◆ $xapiproxy

XapiProxy\XapiProxyRequest::$xapiproxy
private

Definition at line 13 of file XapiProxyRequest.php.

◆ $xapiProxyResponse

XapiProxy\XapiProxyRequest::$xapiProxyResponse
private

Definition at line 15 of file XapiProxyRequest.php.


The documentation for this class was generated from the following file: