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