Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * This file contains a controller for receiving LTI service requests
  19   *
  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
  24   */
  25  
  26  define('NO_DEBUG_DISPLAY', true);
  27  define('NO_MOODLE_COOKIES', true);
  28  
  29  require_once(__DIR__ . '/../../config.php');
  30  require_once($CFG->dirroot . '/mod/lti/locallib.php');
  31  
  32  
  33  $response = new \mod_lti\local\ltiservice\response();
  34  
  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;
  37  
  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] : '');
  42  }
  43  
  44  $ok = false;
  45  $path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
  46  
  47  $accept = $response->get_accept();
  48  $contenttype = $response->get_content_type();
  49  
  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;
  58          }
  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;
  67          }
  68      }
  69  }
  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);
  80      }
  81  }
  82  $response->send();