Merge branch 'MDL-57812-master' of git://github.com/jleyva/moodle
[moodle.git] / blocks / html / block_html.php
blob2183370417285b1cbc74d8d21b9ddd64f0e5bd3e
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.
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/>.
17 /**
18 * Form for editing HTML block instances.
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
25 class block_html extends block_base {
27 function init() {
28 $this->title = get_string('pluginname', 'block_html');
31 function has_config() {
32 return true;
35 function applicable_formats() {
36 return array('all' => true);
39 function specialization() {
40 $this->title = isset($this->config->title) ? format_string($this->config->title) : format_string(get_string('newhtmlblock', 'block_html'));
43 function instance_allow_multiple() {
44 return true;
47 function get_content() {
48 global $CFG;
50 require_once($CFG->libdir . '/filelib.php');
52 if ($this->content !== NULL) {
53 return $this->content;
56 $filteropt = new stdClass;
57 $filteropt->overflowdiv = true;
58 if ($this->content_is_trusted()) {
59 // fancy html allowed only on course, category and system blocks.
60 $filteropt->noclean = true;
63 $this->content = new stdClass;
64 $this->content->footer = '';
65 if (isset($this->config->text)) {
66 // rewrite url
67 $this->config->text = file_rewrite_pluginfile_urls($this->config->text, 'pluginfile.php', $this->context->id, 'block_html', 'content', NULL);
68 // Default to FORMAT_HTML which is what will have been used before the
69 // editor was properly implemented for the block.
70 $format = FORMAT_HTML;
71 // Check to see if the format has been properly set on the config
72 if (isset($this->config->format)) {
73 $format = $this->config->format;
75 $this->content->text = format_text($this->config->text, $format, $filteropt);
76 } else {
77 $this->content->text = '';
80 unset($filteropt); // memory footprint
82 return $this->content;
86 /**
87 * Serialize and store config data
89 function instance_config_save($data, $nolongerused = false) {
90 global $DB;
92 $config = clone($data);
93 // Move embedded files into a proper filearea and adjust HTML links to match
94 $config->text = file_save_draft_area_files($data->text['itemid'], $this->context->id, 'block_html', 'content', 0, array('subdirs'=>true), $data->text['text']);
95 $config->format = $data->text['format'];
97 parent::instance_config_save($config, $nolongerused);
100 function instance_delete() {
101 global $DB;
102 $fs = get_file_storage();
103 $fs->delete_area_files($this->context->id, 'block_html');
104 return true;
108 * Copy any block-specific data when copying to a new block instance.
109 * @param int $fromid the id number of the block instance to copy from
110 * @return boolean
112 public function instance_copy($fromid) {
113 $fromcontext = context_block::instance($fromid);
114 $fs = get_file_storage();
115 // This extra check if file area is empty adds one query if it is not empty but saves several if it is.
116 if (!$fs->is_area_empty($fromcontext->id, 'block_html', 'content', 0, false)) {
117 $draftitemid = 0;
118 file_prepare_draft_area($draftitemid, $fromcontext->id, 'block_html', 'content', 0, array('subdirs' => true));
119 file_save_draft_area_files($draftitemid, $this->context->id, 'block_html', 'content', 0, array('subdirs' => true));
121 return true;
124 function content_is_trusted() {
125 global $SCRIPT;
127 if (!$context = context::instance_by_id($this->instance->parentcontextid, IGNORE_MISSING)) {
128 return false;
130 //find out if this block is on the profile page
131 if ($context->contextlevel == CONTEXT_USER) {
132 if ($SCRIPT === '/my/index.php') {
133 // this is exception - page is completely private, nobody else may see content there
134 // that is why we allow JS here
135 return true;
136 } else {
137 // no JS on public personal pages, it would be a big security issue
138 return false;
142 return true;
146 * The block should only be dockable when the title of the block is not empty
147 * and when parent allows docking.
149 * @return bool
151 public function instance_can_be_docked() {
152 return (!empty($this->config->title) && parent::instance_can_be_docked());
156 * Add custom html attributes to aid with theming and styling
158 * @return array
160 function html_attributes() {
161 global $CFG;
163 $attributes = parent::html_attributes();
165 if (!empty($CFG->block_html_allowcssclasses)) {
166 if (!empty($this->config->classes)) {
167 $attributes['class'] .= ' '.$this->config->classes;
171 return $attributes;