Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/h5p/ -> lib.php (source)

Differences Between: [Versions 311 and 402] [Versions 311 and 403]

   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   * Callbacks.
  19   *
  20   * @package    core_h5p
  21   * @copyright  2019 Bas Brands <bas@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  use core_h5p\local\library\autoloader;
  27  
  28  /**
  29   * Serve the files from the core_h5p file areas.
  30   *
  31   * @package core_h5p
  32   * @category files
  33   *
  34   * @param  stdClass $course the course object
  35   * @param  stdClass $cm the course module object
  36   * @param  stdClass $context the newmodule's context
  37   * @param  string $filearea the name of the file area
  38   * @param  array $args extra arguments (itemid, path)
  39   * @param  bool $forcedownload whether or not force download
  40   * @param  array $options additional options affecting the file serving
  41   *
  42   * @return bool Returns false if we don't find a file.
  43   */
  44  function core_h5p_pluginfile($course, $cm, $context, string $filearea, array $args, bool $forcedownload,
  45      array $options = []) : bool {
  46      global $DB;
  47  
  48      // Require classes from H5P third party library
  49      autoloader::register();
  50  
  51      $filesettingsset = false;
  52  
  53      switch ($filearea) {
  54          default:
  55              return false; // Invalid file area.
  56  
  57          case \core_h5p\file_storage::LIBRARY_FILEAREA:
  58              if ($context->contextlevel != CONTEXT_SYSTEM) {
  59                   return false; // Invalid context because the libraries are loaded always in the context system.
  60              }
  61  
  62              $itemid = null;
  63  
  64              // The files that can be delivered to this function are unfortunately out of our control. Some of the
  65              // references are embedded into the JavaScript of the files and we have no ability to inject an item id.
  66              // We also don't know the location of the item id when we do include it, so we look for the first numeric
  67              // value and try to serve that file.
  68              foreach ($args as $key => $value) {
  69                  if (is_numeric($value)) {
  70                      $itemid = $value;
  71                      unset($args[$key]);
  72                      break;
  73                  }
  74              }
  75  
  76              if (!isset($itemid)) {
  77                  // We didn't find an item id to use, so we fall back to retrieving the record using all the other
  78                  // fields. The combination of component, filearea, filepath, and filename is enough for a unique
  79                  // record.
  80                  $filename = array_pop($args);
  81                  $filepath = '/' . implode('/', $args) . '/';
  82                  $itemid = $DB->get_field('files', 'itemid', [
  83                      'component' => \core_h5p\file_storage::COMPONENT,
  84                      'filearea' => \core_h5p\file_storage::LIBRARY_FILEAREA,
  85                      'filepath' => $filepath,
  86                      'filename' => $filename
  87                  ]);
  88                  $filesettingsset = true;
  89              }
  90              break;
  91          case \core_h5p\file_storage::CONTENT_FILEAREA:
  92              if ($context->contextlevel != CONTEXT_SYSTEM) {
  93                  return false; // Invalid context because the content files are loaded always in the context system.
  94              }
  95              $itemid = array_shift($args);
  96              break;
  97          case \core_h5p\file_storage::CACHED_ASSETS_FILEAREA:
  98          case \core_h5p\file_storage::EXPORT_FILEAREA:
  99              $itemid = 0;
 100              break;
 101      }
 102  
 103      if (!$filesettingsset) {
 104          $filename = array_pop($args);
 105          $filepath = (!$args ? '/' : '/' . implode('/', $args) . '/');
 106      }
 107  
 108      $fs = get_file_storage();
 109      $file = $fs->get_file($context->id, \core_h5p\file_storage::COMPONENT, $filearea, $itemid, $filepath, $filename);
 110      if (!$file) {
 111          return false; // No such file.
 112      }
 113  
 114      send_stored_file($file, null, 0, $forcedownload, $options);
 115  
 116      return true;
 117  }