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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   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', 'favicon'])) {
  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          // Check whether width/height are specified, and we can resize the image (some types such as ICO cannot be resized).
 100          if (($maxwidth === 0 && $maxheight === 0) ||
 101                  !$filedata = $file->resize_image($maxwidth, $maxheight)) {
 102  
 103              if ($lifetime) {
 104                  file_safe_save_content($file->get_content(), $candidate);
 105              }
 106              send_stored_file($file, $lifetime, 0, false, $options);
 107          }
 108  
 109          // If we don't want to cached the file, serve now and quit.
 110          if (!$lifetime) {
 111              send_content_uncached($filedata, $filename);
 112          }
 113  
 114          // Save, serve and quit.
 115          file_safe_save_content($filedata, $candidate);
 116          send_file($candidate, $filename, $lifetime, 0, false, false, '', false, $options);
 117      }
 118  
 119      send_file_not_found();
 120  }