lib.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. * This file contains functions used by the admin pages
  18. *
  19. * @since Moodle 2.1
  20. * @package admin
  21. * @copyright 2011 Andrew Davis
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. /**
  26. * Return a list of page types
  27. * @param string $pagetype current page type
  28. * @param stdClass $parentcontext Block's parent context
  29. * @param stdClass $currentcontext Current context of block
  30. */
  31. function admin_page_type_list($pagetype, $parentcontext, $currentcontext) {
  32. $array = array(
  33. 'admin-*' => get_string('page-admin-x', 'pagetype'),
  34. $pagetype => get_string('page-admin-current', 'pagetype')
  35. );
  36. return $array;
  37. }
  38. /**
  39. * File serving.
  40. *
  41. * @param stdClass $course The course object.
  42. * @param stdClass $cm The cm object.
  43. * @param context $context The context object.
  44. * @param string $filearea The file area.
  45. * @param array $args List of arguments.
  46. * @param bool $forcedownload Whether or not to force the download of the file.
  47. * @param array $options Array of options.
  48. * @return void|false
  49. */
  50. function core_admin_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
  51. global $CFG;
  52. if (in_array($filearea, ['logo', 'logocompact'])) {
  53. $size = array_shift($args); // The path hides the size.
  54. $itemid = clean_param(array_shift($args), PARAM_INT);
  55. $filename = clean_param(array_shift($args), PARAM_FILE);
  56. $themerev = theme_get_revision();
  57. if ($themerev <= 0) {
  58. // Normalise to 0 as -1 doesn't place well with paths.
  59. $themerev = 0;
  60. }
  61. // Extract the requested width and height.
  62. $maxwidth = 0;
  63. $maxheight = 0;
  64. if (preg_match('/^\d+x\d+$/', $size)) {
  65. list($maxwidth, $maxheight) = explode('x', $size);
  66. $maxwidth = clean_param($maxwidth, PARAM_INT);
  67. $maxheight = clean_param($maxheight, PARAM_INT);
  68. }
  69. $lifetime = 0;
  70. if ($itemid > 0 && $themerev == $itemid) {
  71. // The itemid is $CFG->themerev, when 0 or less no caching. Also no caching when they don't match.
  72. $lifetime = DAYSECS * 60;
  73. }
  74. // Anyone, including guests and non-logged in users, can view the logos.
  75. $options = ['cacheability' => 'public'];
  76. // Check if we've got a cached file to return. When lifetime is 0 then we don't want to cached one.
  77. $candidate = $CFG->localcachedir . "/core_admin/$themerev/$filearea/{$maxwidth}x{$maxheight}/$filename";
  78. if (file_exists($candidate) && $lifetime > 0) {
  79. send_file($candidate, $filename, $lifetime, 0, false, false, '', false, $options);
  80. }
  81. // Find the original file.
  82. $fs = get_file_storage();
  83. $filepath = "/{$context->id}/core_admin/{$filearea}/0/{$filename}";
  84. if (!$file = $fs->get_file_by_hash(sha1($filepath))) {
  85. send_file_not_found();
  86. }
  87. // No need for resizing, but if the file should be cached we save it so we can serve it fast next time.
  88. if (empty($maxwidth) && empty($maxheight)) {
  89. if ($lifetime) {
  90. file_safe_save_content($file->get_content(), $candidate);
  91. }
  92. send_stored_file($file, $lifetime, 0, false, $options);
  93. }
  94. // Proceed with the resizing.
  95. $filedata = $file->resize_image($maxwidth, $maxheight);
  96. if (!$filedata) {
  97. send_file_not_found();
  98. }
  99. // If we don't want to cached the file, serve now and quit.
  100. if (!$lifetime) {
  101. send_content_uncached($filedata, $filename);
  102. }
  103. // Save, serve and quit.
  104. file_safe_save_content($filedata, $candidate);
  105. send_file($candidate, $filename, $lifetime, 0, false, false, '', false, $options);
  106. }
  107. send_file_not_found();
  108. }