weekly release 3.11.8+
[moodle.git] / mod / lti / services.php
blobb1fdca6b67bc48acc86e1ff70ea1c845f88e79ba
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This file contains a controller for receiving LTI service requests
20 * @package mod_lti
21 * @copyright 2014 Vital Source Technologies http://vitalsource.com
22 * @author Stephen Vickers
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 define('NO_DEBUG_DISPLAY', true);
27 define('NO_MOODLE_COOKIES', true);
29 require_once(__DIR__ . '/../../config.php');
30 require_once($CFG->dirroot . '/mod/lti/locallib.php');
33 $response = new \mod_lti\local\ltiservice\response();
35 $isget = $response->get_request_method() === mod_lti\local\ltiservice\resource_base::HTTP_GET;
36 $isdelete = $response->get_request_method() === mod_lti\local\ltiservice\resource_base::HTTP_DELETE;
38 if ($isget) {
39 $response->set_accept(isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : '');
40 } else {
41 $response->set_content_type(isset($_SERVER['CONTENT_TYPE']) ? explode(';', $_SERVER['CONTENT_TYPE'], 2)[0] : '');
44 $ok = false;
45 $path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
47 $accept = $response->get_accept();
48 $contenttype = $response->get_content_type();
50 $services = lti_get_services();
51 foreach ($services as $service) {
52 $resources = $service->get_resources();
53 foreach ($resources as $resource) {
54 if (($isget && !empty($accept) && (strpos($accept, '*/*') === false) &&
55 !in_array($accept, $resource->get_formats())) ||
56 ((!$isget && !$isdelete) && !in_array($contenttype, $resource->get_formats()))) {
57 continue;
59 $template = $resource->get_template();
60 $template = preg_replace('/{config_type}/', '(toolproxy|tool)', $template);
61 $template = preg_replace('/\{[a-zA-Z_]+\}/', '[^/]+', $template);
62 $template = preg_replace('/\(([0-9a-zA-Z_\-,\/]+)\)/', '(\\1|)', $template);
63 $template = str_replace('/', '\/', $template);
64 if (preg_match("/^{$template}$/", $path) === 1) {
65 $ok = true;
66 break 2;
70 if (!$ok) {
71 $response->set_code(400);
72 $response->set_reason("No handler found for {$path} {$accept} {$contenttype}");
73 } else {
74 $body = file_get_contents('php://input');
75 $response->set_request_data($body);
76 if (in_array($response->get_request_method(), $resource->get_methods())) {
77 $resource->execute($response);
78 } else {
79 $response->set_code(405);
82 $response->send();