PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/blocks/html/lib.php

https://github.com/danmarsden/moodle
PHP | 111 lines | 52 code | 11 blank | 48 comment | 19 complexity | 0b536c26236698be887689bc86283ed3 MD5 | raw file
  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. if (!core_course_category::get($parentcontext->instanceid, IGNORE_MISSING)) {
  49. send_file_not_found();
  50. }
  51. } else if ($parentcontext->contextlevel === CONTEXT_USER && $parentcontext->instanceid != $USER->id) {
  52. // The block is in the context of a user, it is only visible to the user who it belongs to.
  53. send_file_not_found();
  54. }
  55. // At this point there is no way to check SYSTEM context, so ignoring it.
  56. }
  57. if ($filearea !== 'content') {
  58. send_file_not_found();
  59. }
  60. $fs = get_file_storage();
  61. $filename = array_pop($args);
  62. $filepath = $args ? '/'.implode('/', $args).'/' : '/';
  63. if (!$file = $fs->get_file($context->id, 'block_html', 'content', 0, $filepath, $filename) or $file->is_directory()) {
  64. send_file_not_found();
  65. }
  66. if ($parentcontext = context::instance_by_id($birecord_or_cm->parentcontextid, IGNORE_MISSING)) {
  67. if ($parentcontext->contextlevel == CONTEXT_USER) {
  68. // force download on all personal pages including /my/
  69. //because we do not have reliable way to find out from where this is used
  70. $forcedownload = true;
  71. }
  72. } else {
  73. // weird, there should be parent context, better force dowload then
  74. $forcedownload = true;
  75. }
  76. // NOTE: it woudl be nice to have file revisions here, for now rely on standard file lifetime,
  77. // do not lower it because the files are dispalyed very often.
  78. \core\session\manager::write_close();
  79. send_stored_file($file, null, 0, $forcedownload, $options);
  80. }
  81. /**
  82. * Perform global search replace such as when migrating site to new URL.
  83. * @param $search
  84. * @param $replace
  85. * @return void
  86. */
  87. function block_html_global_db_replace($search, $replace) {
  88. global $DB;
  89. $instances = $DB->get_recordset('block_instances', array('blockname' => 'html'));
  90. foreach ($instances as $instance) {
  91. // TODO: intentionally hardcoded until MDL-26800 is fixed
  92. $config = unserialize(base64_decode($instance->configdata));
  93. if (isset($config->text) and is_string($config->text)) {
  94. $config->text = str_replace($search, $replace, $config->text);
  95. $DB->update_record('block_instances', ['id' => $instance->id,
  96. 'configdata' => base64_encode(serialize($config)), 'timemodified' => time()]);
  97. }
  98. }
  99. $instances->close();
  100. }