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.
/admin/ -> lib.php (source)

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

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * This file contains functions used by the admin pages
  20   *
  21   * @since Moodle 2.1
  22   * @package admin
  23   * @copyright 2011 Andrew Davis
  24   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Return a list of page types
  31   * @param string $pagetype current page type
  32   * @param stdClass $parentcontext Block's parent context
  33   * @param stdClass $currentcontext Current context of block
  34   */
  35  function admin_page_type_list($pagetype, $parentcontext, $currentcontext) {
  36      $array = array(
  37          'admin-*' => get_string('page-admin-x', 'pagetype'),
  38          $pagetype => get_string('page-admin-current', 'pagetype')
  39      );
  40      return $array;
  41  }
  42  
  43  /**
  44   * File serving.
  45   *
  46   * @param stdClass $course The course object.
  47   * @param stdClass $cm The cm object.
  48   * @param context $context The context object.
  49   * @param string $filearea The file area.
  50   * @param array $args List of arguments.
  51   * @param bool $forcedownload Whether or not to force the download of the file.
  52   * @param array $options Array of options.
  53   * @return void|false
  54   */
  55  function core_admin_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
  56      global $CFG;
  57  
  58      if (in_array($filearea, ['logo', 'logocompact'])) {
  59          $size = array_shift($args); // The path hides the size.
  60          $itemid = clean_param(array_shift($args), PARAM_INT);
  61          $filename = clean_param(array_shift($args), PARAM_FILE);
  62          $themerev = theme_get_revision();
  63          if ($themerev <= 0) {
  64              // Normalise to 0 as -1 doesn't place well with paths.
  65               $themerev = 0;
  66          }
  67  
  68          // Extract the requested width and height.
  69          $maxwidth = 0;
  70          $maxheight = 0;
  71          if (preg_match('/^\d+x\d+$/', $size)) {
  72              list($maxwidth, $maxheight) = explode('x', $size);
  73              $maxwidth = clean_param($maxwidth, PARAM_INT);
  74              $maxheight = clean_param($maxheight, PARAM_INT);
  75          }
  76  
  77          $lifetime = 0;
  78          if ($itemid > 0 && $themerev == $itemid) {
  79              // The itemid is $CFG->themerev, when 0 or less no caching. Also no caching when they don't match.
  80              $lifetime = DAYSECS * 60;
  81          }
  82  
  83          // Anyone, including guests and non-logged in users, can view the logos.
  84          $options = ['cacheability' => 'public'];
  85  
  86          // Check if we've got a cached file to return. When lifetime is 0 then we don't want to cached one.
  87          $candidate = $CFG->localcachedir . "/core_admin/$themerev/$filearea/{$maxwidth}x{$maxheight}/$filename";
  88          if (file_exists($candidate) && $lifetime > 0) {
  89              send_file($candidate, $filename, $lifetime, 0, false, false, '', false, $options);
  90          }
  91  
  92          // Find the original file.
  93          $fs = get_file_storage();
  94          $filepath = "/{$context->id}/core_admin/{$filearea}/0/{$filename}";
  95          if (!$file = $fs->get_file_by_hash(sha1($filepath))) {
  96              send_file_not_found();
  97          }
  98  
  99          // No need for resizing, but if the file should be cached we save it so we can serve it fast next time.
 100          if (empty($maxwidth) && empty($maxheight)) {
 101              if ($lifetime) {
 102                  file_safe_save_content($file->get_content(), $candidate);
 103              }
 104              send_stored_file($file, $lifetime, 0, false, $options);
 105          }
 106  
 107          // Proceed with the resizing.
 108          $filedata = $file->resize_image($maxwidth, $maxheight);
 109          if (!$filedata) {
 110              send_file_not_found();
 111          }
 112  
 113          // If we don't want to cached the file, serve now and quit.
 114          if (!$lifetime) {
 115              send_content_uncached($filedata, $filename);
 116          }
 117  
 118          // Save, serve and quit.
 119          file_safe_save_content($filedata, $candidate);
 120          send_file($candidate, $filename, $lifetime, 0, false, false, '', false, $options);
 121      }
 122  
 123      send_file_not_found();
 124  }