weekly release 3.5.6+
[moodle.git] / mod / lti / services.php
blob547ea0e1347b44e5b5006c201a4ebec743590342
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() == 'GET';
36 $isdelete = $response->get_request_method() == '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']) ? $_SERVER['CONTENT_TYPE'] : '');
44 $ok = false;
45 $path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
47 $accept = $response->get_accept();
49 $services = lti_get_services();
50 foreach ($services as $service) {
51 $resources = $service->get_resources();
52 foreach ($resources as $resource) {
53 if (($isget && !empty($accept) && (strpos($accept, '*/*') === false) &&
54 !in_array($accept, $resource->get_formats())) ||
55 ((!$isget && !$isdelete) && !in_array($response->get_content_type(), $resource->get_formats()))) {
56 continue;
58 $template = $resource->get_template();
59 $template = preg_replace('/\{[a-zA-Z_]+\}/', '[^/]+', $template);
60 $template = preg_replace('/\{\?[0-9a-zA-Z_\-,]+\}$/', '', $template);
61 $template = str_replace('/', '\/', $template);
62 if (preg_match("/{$template}/", $path) === 1) {
63 $ok = true;
64 break 2;
68 if (!$ok) {
69 $response->set_code(400);
70 } else {
71 $body = file_get_contents('php://input');
72 $response->set_request_data($body);
73 if (in_array($response->get_request_method(), $resource->get_methods())) {
74 $resource->execute($response);
75 } else {
76 $response->set_code(405);
79 $response->send();