ILIAS  trunk Revision v11.0_alpha-1811-gd2d5443e411
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ltiservices.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 chdir("../../../");
23 
26 
27 global $DIC;
28 
29 $path = $_SERVER['PATH_INFO'] ?? '';
30 
31 if (empty($path)) {
32  ilObjLTIConsumer::sendResponseError(500, json_encode(array('error' => "ERROR_NO_PATH_INFO")));
33 }
34 
36 
37 ilObjLTIConsumer::getLogger()->debug("lti service call $serviceName");
38 ilObjLTIConsumer::getLogger()->debug("lti service path $path");
39 
41 switch ($serviceName) {
42  case "gradeservice":
44  $service->setResourcePath($path);
45  break;
46  default:
47  ilObjLTIConsumer::sendResponseError(400, json_encode(array('error' => 'invalid_request')));
48 }
49 
51 
54 
55 if ($isGet) {
56  $response->setAccept($_SERVER['HTTP_ACCEPT'] ?? '');
57 } else {
58  $response->setContentType(isset($_SERVER['CONTENT_TYPE']) ? explode(';', $_SERVER['CONTENT_TYPE'], 2)[0] : '');
59 }
60 
61 $validRequest = false;
62 
63 $accept = $response->getAccept();
64 $contenttype = $response->getContentType();
65 $resources = $service->getResources();
67 
68 foreach ($resources as $resource) {
69  if (($isGet && !empty($accept) && (!str_contains($accept, '*/*')) &&
70  !in_array($accept, $resource->getFormats())) ||
71  ((!$isGet && !$isDelete) && !in_array($contenttype, $resource->getFormats()))) {
72  continue;
73  }
74 
75  $template = $resource->getTemplate();
76  $template = preg_replace('/\{[a-zA-Z_]+\}/', '[^/]+', $template);
77  $template = preg_replace('/\(([0-9a-zA-Z_\-,\/]+)\)/', '(\\1|)', $template);
78  $template = str_replace('/', '\/', $template);
79  if (preg_match("/^$template$/", $path) === 1) {
80  $validRequest = true;
81  $res = $resource;
82  break;
83  }
84 }
85 
86 if (!$validRequest || $res == null) {
87  $response->setCode(400);
88  $response->setReason("No handler found for $serviceName/$path $accept $contenttype");
89 } else {
90  $body = file_get_contents('php://input');
91  $response->setRequestData($body);
92  if (in_array($response->getRequestMethod(), $res->getMethods())) {
93  $res->execute($response);
94  } else {
95  $response->setCode(405);
96  }
97 }
98 $response->send();
99 
100 function getService(string &$path): string
101 {
102  $route = explode("/", $path);
103  array_shift($route); // first slash
104  $ret = array_shift($route); // service name
105  $path = "/" . implode("/", $route);
106  return $ret;
107 }
$res
Definition: ltiservices.php:66
$isDelete
Definition: ltiservices.php:53
$resources
Definition: ltiservices.php:65
if(empty($path)) $serviceName
Definition: ltiservices.php:35
static sendResponseError(int $code, string $message, $log=true)
const CONTEXT_SCORM
$validRequest
Definition: ltiservices.php:61
$accept
Definition: ltiservices.php:63
$path
Definition: ltiservices.php:29
global $DIC
Definition: ltiservices.php:27
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static initILIAS()
ilias initialisation
switch($serviceName) $response
Definition: ltiservices.php:50
$_SERVER['HTTP_HOST']
Definition: raiseError.php:26
$contenttype
Definition: ltiservices.php:64
$isGet
Definition: ltiservices.php:52
static init(string $a_type)
Init context by type.
getService(string &$path)
$service
Definition: ltiservices.php:40