MDL-65788 tool_policy: Prevent two modals rendering for guests.
[moodle.git] / blocks / html / lib.php
CommitLineData
64f93798 1<?php
64f93798
PS
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/**
18 * Form for editing HTML block instances.
19 *
64f93798
PS
20 * @copyright 2010 Petr Skoda (http://skodak.org)
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d2b7803e
DC
22 * @package block_html
23 * @category files
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
957fc845 30 * @param array $options additional options affecting the file serving
d2b7803e 31 * @return bool
5ad8935f 32 * @todo MDL-36050 improve capability check on stick blocks, so we can check user capability before sending images.
64f93798 33 */
957fc845 34function block_html_pluginfile($course, $birecord_or_cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
b04bf988 35 global $DB, $CFG, $USER;
64f93798
PS
36
37 if ($context->contextlevel != CONTEXT_BLOCK) {
38 send_file_not_found();
39 }
40
5ad8935f
RT
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) {
45 require_login();
46 } else {
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.
beff3806
MG
51 if (!core_course_category::get($parentcontext->instanceid, IGNORE_MISSING)) {
52 send_file_not_found();
5ad8935f 53 }
b04bf988
FM
54 } else if ($parentcontext->contextlevel === CONTEXT_USER && $parentcontext->instanceid != $USER->id) {
55 // The block is in the context of a user, it is only visible to the user who it belongs to.
56 send_file_not_found();
5ad8935f 57 }
b04bf988 58 // At this point there is no way to check SYSTEM context, so ignoring it.
5ad8935f 59 }
64f93798
PS
60
61 if ($filearea !== 'content') {
62 send_file_not_found();
63 }
64
65 $fs = get_file_storage();
66
67 $filename = array_pop($args);
d85ab705 68 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798
PS
69
70 if (!$file = $fs->get_file($context->id, 'block_html', 'content', 0, $filepath, $filename) or $file->is_directory()) {
71 send_file_not_found();
72 }
73
5fbe2118 74 if ($parentcontext = context::instance_by_id($birecord_or_cm->parentcontextid, IGNORE_MISSING)) {
d449b246
PS
75 if ($parentcontext->contextlevel == CONTEXT_USER) {
76 // force download on all personal pages including /my/
77 //because we do not have reliable way to find out from where this is used
78 $forcedownload = true;
79 }
80 } else {
81 // weird, there should be parent context, better force dowload then
82 $forcedownload = true;
83 }
84
0c431257
PS
85 // NOTE: it woudl be nice to have file revisions here, for now rely on standard file lifetime,
86 // do not lower it because the files are dispalyed very often.
d79d5ac2 87 \core\session\manager::write_close();
0c431257 88 send_stored_file($file, null, 0, $forcedownload, $options);
64f93798 89}
c3b5e829
PS
90
91/**
92 * Perform global search replace such as when migrating site to new URL.
93 * @param $search
94 * @param $replace
95 * @return void
96 */
97function block_html_global_db_replace($search, $replace) {
98 global $DB;
99
100 $instances = $DB->get_recordset('block_instances', array('blockname' => 'html'));
101 foreach ($instances as $instance) {
102 // TODO: intentionally hardcoded until MDL-26800 is fixed
103 $config = unserialize(base64_decode($instance->configdata));
104 if (isset($config->text) and is_string($config->text)) {
105 $config->text = str_replace($search, $replace, $config->text);
557554f9 106 $DB->update_record('block_instances', ['id' => $instance->id,
107 'configdata' => base64_encode(serialize($config)), 'timemodified' => time()]);
c3b5e829
PS
108 }
109 }
110 $instances->close();
d2b7803e 111}