forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_html.php
173 lines (145 loc) · 5.68 KB
/
block_html.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Form for editing HTML block instances.
*
* @package block_html
* @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_html extends block_base {
function init() {
$this->title = get_string('pluginname', 'block_html');
}
function has_config() {
return true;
}
function applicable_formats() {
return array('all' => true);
}
function specialization() {
$this->title = isset($this->config->title) ? format_string($this->config->title) : format_string(get_string('newhtmlblock', 'block_html'));
}
function instance_allow_multiple() {
return true;
}
function get_content() {
global $CFG;
require_once($CFG->libdir . '/filelib.php');
if ($this->content !== NULL) {
return $this->content;
}
$filteropt = new stdClass;
$filteropt->overflowdiv = true;
if ($this->content_is_trusted()) {
// fancy html allowed only on course, category and system blocks.
$filteropt->noclean = true;
}
$this->content = new stdClass;
$this->content->footer = '';
if (isset($this->config->text)) {
// rewrite url
$this->config->text = file_rewrite_pluginfile_urls($this->config->text, 'pluginfile.php', $this->context->id, 'block_html', 'content', NULL);
// Default to FORMAT_HTML which is what will have been used before the
// editor was properly implemented for the block.
$format = FORMAT_HTML;
// Check to see if the format has been properly set on the config
if (isset($this->config->format)) {
$format = $this->config->format;
}
$this->content->text = format_text($this->config->text, $format, $filteropt);
} else {
$this->content->text = '';
}
unset($filteropt); // memory footprint
return $this->content;
}
/**
* Serialize and store config data
*/
function instance_config_save($data, $nolongerused = false) {
global $DB;
$config = clone($data);
// Move embedded files into a proper filearea and adjust HTML links to match
$config->text = file_save_draft_area_files($data->text['itemid'], $this->context->id, 'block_html', 'content', 0, array('subdirs'=>true), $data->text['text']);
$config->format = $data->text['format'];
parent::instance_config_save($config, $nolongerused);
}
function instance_delete() {
global $DB;
$fs = get_file_storage();
$fs->delete_area_files($this->context->id, 'block_html');
return true;
}
/**
* Copy any block-specific data when copying to a new block instance.
* @param int $fromid the id number of the block instance to copy from
* @return boolean
*/
public function instance_copy($fromid) {
$fromcontext = context_block::instance($fromid);
$fs = get_file_storage();
// This extra check if file area is empty adds one query if it is not empty but saves several if it is.
if (!$fs->is_area_empty($fromcontext->id, 'block_html', 'content', 0, false)) {
$draftitemid = 0;
file_prepare_draft_area($draftitemid, $fromcontext->id, 'block_html', 'content', 0, array('subdirs' => true));
file_save_draft_area_files($draftitemid, $this->context->id, 'block_html', 'content', 0, array('subdirs' => true));
}
return true;
}
function content_is_trusted() {
global $SCRIPT;
if (!$context = context::instance_by_id($this->instance->parentcontextid, IGNORE_MISSING)) {
return false;
}
//find out if this block is on the profile page
if ($context->contextlevel == CONTEXT_USER) {
if ($SCRIPT === '/my/index.php') {
// this is exception - page is completely private, nobody else may see content there
// that is why we allow JS here
return true;
} else {
// no JS on public personal pages, it would be a big security issue
return false;
}
}
return true;
}
/**
* The block should only be dockable when the title of the block is not empty
* and when parent allows docking.
*
* @return bool
*/
public function instance_can_be_docked() {
return (!empty($this->config->title) && parent::instance_can_be_docked());
}
/*
* Add custom html attributes to aid with theming and styling
*
* @return array
*/
function html_attributes() {
global $CFG;
$attributes = parent::html_attributes();
if (!empty($CFG->block_html_allowcssclasses)) {
if (!empty($this->config->classes)) {
$attributes['class'] .= ' '.$this->config->classes;
}
}
return $attributes;
}
}