MDL-65788 tool_policy: Prevent two modals rendering for guests.
[moodle.git] / blocks / html / block_html.php
CommitLineData
4ca6cfbf 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 *
20 * @package block_html
21 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
86db09ef 25class block_html extends block_base {
26
27 function init() {
8c5bba16 28 $this->title = get_string('pluginname', 'block_html');
86db09ef 29 }
30
055cc835
AB
31 function has_config() {
32 return true;
33 }
34
b959599b 35 function applicable_formats() {
36 return array('all' => true);
37 }
38
86db09ef 39 function specialization() {
6586118e
LB
40 if (isset($this->config->title)) {
41 $this->title = $this->title = format_string($this->config->title, true, ['context' => $this->context]);
42 } else {
43 $this->title = get_string('newhtmlblock', 'block_html');
44 }
86db09ef 45 }
46
47 function instance_allow_multiple() {
48 return true;
49 }
50
51 function get_content() {
99d19c13
PS
52 global $CFG;
53
54 require_once($CFG->libdir . '/filelib.php');
55
86db09ef 56 if ($this->content !== NULL) {
57 return $this->content;
58 }
59
367a75fa
SH
60 $filteropt = new stdClass;
61 $filteropt->overflowdiv = true;
3179b000 62 if ($this->content_is_trusted()) {
e92c286c 63 // fancy html allowed only on course, category and system blocks.
e8a7edd7 64 $filteropt->noclean = true;
e8a7edd7 65 }
e345909c 66
86db09ef 67 $this->content = new stdClass;
86db09ef 68 $this->content->footer = '';
94ee9ae0 69 if (isset($this->config->text)) {
41976db3 70 // rewrite url
ce162914 71 $this->config->text = file_rewrite_pluginfile_urls($this->config->text, 'pluginfile.php', $this->context->id, 'block_html', 'content', NULL);
bd462476
SH
72 // Default to FORMAT_HTML which is what will have been used before the
73 // editor was properly implemented for the block.
74 $format = FORMAT_HTML;
75 // Check to see if the format has been properly set on the config
76 if (isset($this->config->format)) {
77 $format = $this->config->format;
78 }
79 $this->content->text = format_text($this->config->text, $format, $filteropt);
94ee9ae0
MD
80 } else {
81 $this->content->text = '';
82 }
86db09ef 83
e345909c 84 unset($filteropt); // memory footprint
85
86db09ef 86 return $this->content;
87 }
43457dc8 88
a9732b89
JL
89 public function get_content_for_external($output) {
90 global $CFG;
91 require_once($CFG->libdir . '/externallib.php');
92
93 $bc = new stdClass;
94 $bc->title = null;
95 $bc->content = '';
96 $bc->contenformat = FORMAT_MOODLE;
97 $bc->footer = '';
98 $bc->files = [];
99
100 if (!$this->hide_header()) {
101 $bc->title = $this->title;
102 }
103
104 if (isset($this->config->text)) {
105 $filteropt = new stdClass;
106 if ($this->content_is_trusted()) {
107 // Fancy html allowed only on course, category and system blocks.
108 $filteropt->noclean = true;
109 }
110
111 $format = FORMAT_HTML;
112 // Check to see if the format has been properly set on the config.
113 if (isset($this->config->format)) {
114 $format = $this->config->format;
115 }
116 list($bc->content, $bc->contentformat) =
117 external_format_text($this->config->text, $format, $this->context, 'block_html', 'content', null, $filteropt);
118 $bc->files = external_util::get_area_files($this->context->id, 'block_html', 'content', false, false);
119
120 }
121 return $bc;
122 }
123
41976db3
DC
124
125 /**
126 * Serialize and store config data
127 */
128 function instance_config_save($data, $nolongerused = false) {
129 global $DB;
130
ce162914 131 $config = clone($data);
41976db3 132 // Move embedded files into a proper filearea and adjust HTML links to match
ce162914
DC
133 $config->text = file_save_draft_area_files($data->text['itemid'], $this->context->id, 'block_html', 'content', 0, array('subdirs'=>true), $data->text['text']);
134 $config->format = $data->text['format'];
41976db3 135
ce162914 136 parent::instance_config_save($config, $nolongerused);
41976db3
DC
137 }
138
af140288
DC
139 function instance_delete() {
140 global $DB;
141 $fs = get_file_storage();
64f93798 142 $fs->delete_area_files($this->context->id, 'block_html');
af140288
DC
143 return true;
144 }
41976db3 145
f5b93846
MG
146 /**
147 * Copy any block-specific data when copying to a new block instance.
148 * @param int $fromid the id number of the block instance to copy from
149 * @return boolean
150 */
151 public function instance_copy($fromid) {
152 $fromcontext = context_block::instance($fromid);
153 $fs = get_file_storage();
154 // This extra check if file area is empty adds one query if it is not empty but saves several if it is.
155 if (!$fs->is_area_empty($fromcontext->id, 'block_html', 'content', 0, false)) {
156 $draftitemid = 0;
157 file_prepare_draft_area($draftitemid, $fromcontext->id, 'block_html', 'content', 0, array('subdirs' => true));
158 file_save_draft_area_files($draftitemid, $this->context->id, 'block_html', 'content', 0, array('subdirs' => true));
159 }
160 return true;
161 }
162
3179b000 163 function content_is_trusted() {
d449b246
PS
164 global $SCRIPT;
165
5fbe2118 166 if (!$context = context::instance_by_id($this->instance->parentcontextid, IGNORE_MISSING)) {
758bfdf6
PS
167 return false;
168 }
d449b246
PS
169 //find out if this block is on the profile page
170 if ($context->contextlevel == CONTEXT_USER) {
171 if ($SCRIPT === '/my/index.php') {
172 // this is exception - page is completely private, nobody else may see content there
173 // that is why we allow JS here
174 return true;
175 } else {
176 // no JS on public personal pages, it would be a big security issue
177 return false;
178 }
179 }
180
181 return true;
3179b000 182 }
efe41e35 183
baf9c832
JF
184 /**
185 * The block should only be dockable when the title of the block is not empty
186 * and when parent allows docking.
187 *
188 * @return bool
189 */
190 public function instance_can_be_docked() {
191 return (!empty($this->config->title) && parent::instance_can_be_docked());
192 }
14bcde67
AB
193
194 /*
195 * Add custom html attributes to aid with theming and styling
196 *
197 * @return array
198 */
199 function html_attributes() {
055cc835
AB
200 global $CFG;
201
14bcde67
AB
202 $attributes = parent::html_attributes();
203
055cc835
AB
204 if (!empty($CFG->block_html_allowcssclasses)) {
205 if (!empty($this->config->classes)) {
206 $attributes['class'] .= ' '.$this->config->classes;
207 }
14bcde67
AB
208 }
209
210 return $attributes;
211 }
86db09ef 212}