lib.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * Form for editing HTML block instances.
  18. *
  19. * @copyright 2010 Petr Skoda (http://skodak.org)
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package block_html
  22. * @category files
  23. * @param stdClass $course course object
  24. * @param stdClass $birecord_or_cm block instance record
  25. * @param stdClass $context context object
  26. * @param string $filearea file area
  27. * @param array $args extra arguments
  28. * @param bool $forcedownload whether or not force download
  29. * @param array $options additional options affecting the file serving
  30. * @return bool
  31. * @todo MDL-36050 improve capability check on stick blocks, so we can check user capability before sending images.
  32. */
  33. function block_html_pluginfile($course, $birecord_or_cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
  34. global $DB, $CFG, $USER;
  35. if ($context->contextlevel != CONTEXT_BLOCK) {
  36. send_file_not_found();
  37. }
  38. // If block is in course context, then check if user has capability to access course.
  39. if ($context->get_course_context(false)) {
  40. require_course_login($course);
  41. } else if ($CFG->forcelogin) {
  42. require_login();
  43. } else {
  44. // Get parent context and see if user have proper permission.
  45. $parentcontext = $context->get_parent_context();
  46. if ($parentcontext->contextlevel === CONTEXT_COURSECAT) {
  47. // Check if category is visible and user can view this category.
  48. $category = $DB->get_record('course_categories', array('id' => $parentcontext->instanceid), '*', MUST_EXIST);
  49. if (!$category->visible) {
  50. require_capability('moodle/category:viewhiddencategories', $parentcontext);
  51. }
  52. } else if ($parentcontext->contextlevel === CONTEXT_USER && $parentcontext->instanceid != $USER->id) {
  53. // The block is in the context of a user, it is only visible to the user who it belongs to.
  54. send_file_not_found();
  55. }
  56. // At this point there is no way to check SYSTEM context, so ignoring it.
  57. }
  58. if ($filearea !== 'content') {
  59. send_file_not_found();
  60. }
  61. $fs = get_file_storage();
  62. $filename = array_pop($args);
  63. $filepath = $args ? '/'.implode('/', $args).'/' : '/';
  64. if (!$file = $fs->get_file($context->id, 'block_html', 'content', 0, $filepath, $filename) or $file->is_directory()) {
  65. send_file_not_found();
  66. }
  67. if ($parentcontext = context::instance_by_id($birecord_or_cm->parentcontextid, IGNORE_MISSING)) {
  68. if ($parentcontext->contextlevel == CONTEXT_USER) {
  69. // force download on all personal pages including /my/
  70. //because we do not have reliable way to find out from where this is used
  71. $forcedownload = true;
  72. }
  73. } else {
  74. // weird, there should be parent context, better force dowload then
  75. $forcedownload = true;
  76. }
  77. // NOTE: it woudl be nice to have file revisions here, for now rely on standard file lifetime,
  78. // do not lower it because the files are dispalyed very often.
  79. \core\session\manager::write_close();
  80. send_stored_file($file, null, 0, $forcedownload, $options);
  81. }
  82. /**
  83. * Perform global search replace such as when migrating site to new URL.
  84. * @param $search
  85. * @param $replace
  86. * @return void
  87. */
  88. function block_html_global_db_replace($search, $replace) {
  89. global $DB;
  90. $instances = $DB->get_recordset('block_instances', array('blockname' => 'html'));
  91. foreach ($instances as $instance) {
  92. // TODO: intentionally hardcoded until MDL-26800 is fixed
  93. $config = unserialize(base64_decode($instance->configdata));
  94. if (isset($config->text) and is_string($config->text)) {
  95. $config->text = str_replace($search, $replace, $config->text);
  96. $DB->set_field('block_instances', 'configdata', base64_encode(serialize($config)), array('id' => $instance->id));
  97. }
  98. }
  99. $instances->close();
  100. }