3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * This file contains functions used by the admin pages
23 * @copyright 2011 Andrew Davis
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
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
35 function admin_page_type_list($pagetype, $parentcontext, $currentcontext) {
37 'admin-*' => get_string('page-admin-x', 'pagetype'),
38 $pagetype => get_string('page-admin-current', 'pagetype')
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.
55 function core_admin_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
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();
64 // Normalise to 0 as -1 doesn't place well with paths.
68 // Extract the requested width and height.
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
);
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;
83 // Anyone, including guests and non-logged in users, can view the logos.
84 $options = ['cacheability' => 'public'];
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);
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();
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)) {
104 file_safe_save_content($file->get_content(), $candidate);
106 send_stored_file($file, $lifetime, 0, false, $options);
109 // If we don't want to cached the file, serve now and quit.
111 send_content_uncached($filedata, $filename);
114 // Save, serve and quit.
115 file_safe_save_content($filedata, $candidate);
116 send_file($candidate, $filename, $lifetime, 0, false, false, '', false, $options);
119 send_file_not_found();