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