Merge branch 'MDL-80896-main' of https://github.com/andrewnicols/moodle
[moodle.git] / enrol / lti / cartridge.php
blobc3df932607be94a2f27875b87b16a008607f44c2
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 * Generates an XML IMS Cartridge with the details for the given tool
20 * @package enrol_lti
21 * @copyright 2016 John Okely <john@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(dirname(__FILE__) . '/../../config.php');
26 require_once($CFG->dirroot . '/lib/weblib.php');
28 $toolid = null;
29 $token = null;
31 $filearguments = get_file_argument();
32 $arguments = explode('/', trim($filearguments, '/'));
33 if (count($arguments) >= 2) { // Can put cartridge.xml at the end, or anything really.
34 list($toolid, $token) = $arguments;
37 $toolid = optional_param('id', $toolid, PARAM_INT);
38 $token = optional_param('token', $token, PARAM_ALPHANUM);
40 // Only show the cartridge if the token parameter is correct.
41 // If we do not compare with a shared secret, someone could very easily
42 // guess an id for the enrolment.
43 if (!\enrol_lti\helper::verify_cartridge_token($toolid, $token)) {
44 throw new \moodle_exception('incorrecttoken', 'enrol_lti');
47 $tool = \enrol_lti\helper::get_lti_tool($toolid);
49 if (!is_enabled_auth('lti')) {
50 throw new \moodle_exception('pluginnotenabled', 'auth', '', get_string('pluginname', 'auth_lti'));
52 } else if (!enrol_is_enabled('lti')) {
53 throw new \moodle_exception('enrolisdisabled', 'enrol_lti');
55 } else if ($tool->status != ENROL_INSTANCE_ENABLED) {
56 throw new \moodle_exception('enrolisdisabled', 'enrol_lti');
58 } else {
59 header('Content-Type: text/xml; charset=utf-8');
60 echo \enrol_lti\helper::create_cartridge($toolid);