Merge branch 'MDL-60174-master' of https://github.com/sammarshallou/moodle
[moodle.git] / admin / lib.php
blob39550844fc2d426528bca8044adf1a02664b4ee3
1 <?php
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.
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/>.
18 /**
19 * This file contains functions used by the admin pages
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
27 defined('MOODLE_INTERNAL') || die();
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
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')
40 return $array;
43 /**
44 * File serving.
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
55 function core_admin_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
56 global $CFG;
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;
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);
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;
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 // 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);
104 send_stored_file($file, $lifetime, 0, false, $options);
107 // Proceed with the resizing.
108 $filedata = $file->resize_image($maxwidth, $maxheight);
109 if (!$filedata) {
110 send_file_not_found();
113 // If we don't want to cached the file, serve now and quit.
114 if (!$lifetime) {
115 send_content_uncached($filedata, $filename);
118 // Save, serve and quit.
119 file_safe_save_content($filedata, $candidate);
120 send_file($candidate, $filename, $lifetime, 0, false, false, '', false, $options);
123 send_file_not_found();