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.
21 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 class block_html
extends block_base
{
28 $this->title
= get_string('pluginname', 'block_html');
31 function has_config() {
35 function applicable_formats() {
36 return array('all' => true);
39 function specialization() {
40 if (isset($this->config
->title
)) {
41 $this->title
= $this->title
= format_string($this->config
->title
, true, ['context' => $this->context
]);
43 $this->title
= get_string('newhtmlblock', 'block_html');
47 function instance_allow_multiple() {
51 function get_content() {
54 require_once($CFG->libdir
. '/filelib.php');
56 if ($this->content
!== NULL) {
57 return $this->content
;
60 $filteropt = new stdClass
;
61 $filteropt->overflowdiv
= true;
62 if ($this->content_is_trusted()) {
63 // fancy html allowed only on course, category and system blocks.
64 $filteropt->noclean
= true;
67 $this->content
= new stdClass
;
68 $this->content
->footer
= '';
69 if (isset($this->config
->text
)) {
71 $this->config
->text
= file_rewrite_pluginfile_urls($this->config
->text
, 'pluginfile.php', $this->context
->id
, 'block_html', 'content', NULL);
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
;
79 $this->content
->text
= format_text($this->config
->text
, $format, $filteropt);
81 $this->content
->text
= '';
84 unset($filteropt); // memory footprint
86 return $this->content
;
89 public function get_content_for_external($output) {
91 require_once($CFG->libdir
. '/externallib.php');
96 $bc->contenformat
= FORMAT_MOODLE
;
100 if (!$this->hide_header()) {
101 $bc->title
= $this->title
;
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;
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
;
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);
126 * Serialize and store config data
128 function instance_config_save($data, $nolongerused = false) {
131 $config = clone($data);
132 // Move embedded files into a proper filearea and adjust HTML links to match
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'];
136 parent
::instance_config_save($config, $nolongerused);
139 function instance_delete() {
141 $fs = get_file_storage();
142 $fs->delete_area_files($this->context
->id
, 'block_html');
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
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)) {
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));
163 function content_is_trusted() {
166 if (!$context = context
::instance_by_id($this->instance
->parentcontextid
, IGNORE_MISSING
)) {
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
176 // no JS on public personal pages, it would be a big security issue
185 * The block should only be dockable when the title of the block is not empty
186 * and when parent allows docking.
190 public function instance_can_be_docked() {
191 return (!empty($this->config
->title
) && parent
::instance_can_be_docked());
195 * Add custom html attributes to aid with theming and styling
199 function html_attributes() {
202 $attributes = parent
::html_attributes();
204 if (!empty($CFG->block_html_allowcssclasses
)) {
205 if (!empty($this->config
->classes
)) {
206 $attributes['class'] .= ' '.$this->config
->classes
;