ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ltiservices.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
23 chdir("../../");
24 
25 require_once("Services/Init/classes/class.ilInitialisation.php");
26 
29 
30 global $DIC;
31 
32 $path = $_SERVER['PATH_INFO'] ?? '';
33 
34 if (empty($path)) {
35  ilObjLTIConsumer::sendResponseError(500, json_encode(array('error' => "ERROR_NO_PATH_INFO")));
36 }
37 
39 
40 ilObjLTIConsumer::getLogger()->debug("lti service call $serviceName");
41 ilObjLTIConsumer::getLogger()->debug("lti service path $path");
42 
43 $service = null;
44 switch ($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 
58 if ($isGet) {
59  $response->setAccept($_SERVER['HTTP_ACCEPT'] ?? '');
60 } else {
61  $response->setContentType(isset($_SERVER['CONTENT_TYPE']) ? explode(';', $_SERVER['CONTENT_TYPE'], 2)[0] : '');
62 }
63 
64 $validRequest = false;
65 
66 $accept = $response->getAccept();
67 $contenttype = $response->getContentType();
68 $resources = $service->getResources();
69 $res = null;
70 
71 foreach ($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 
89 if (!$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 
103 function 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 }
$res
Definition: ltiservices.php:69
$isDelete
Definition: ltiservices.php:56
$resources
Definition: ltiservices.php:68
if(empty($path)) $serviceName
Definition: ltiservices.php:38
static sendResponseError(int $code, string $message, $log=true)
const CONTEXT_SCORM
$validRequest
Definition: ltiservices.php:64
$accept
Definition: ltiservices.php:66
$path
Definition: ltiservices.php:32
global $DIC
Definition: ltiservices.php:30
static initILIAS()
ilias initialisation
switch($serviceName) $response
Definition: ltiservices.php:53
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10
$contenttype
Definition: ltiservices.php:67
$isGet
Definition: ltiservices.php:55
static init(string $a_type)
Init context by type.
getService(string &$path)
$service
Definition: ltiservices.php:43