weekly release 2.2dev
[moodle.git] / blocks / html / block_html.php
CommitLineData
4ca6cfbf 1<?php
86db09ef 2
64f93798
PS
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Form for editing HTML block instances.
20 *
21 * @package block_html
22 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
86db09ef 26class block_html extends block_base {
27
28 function init() {
8c5bba16 29 $this->title = get_string('pluginname', 'block_html');
86db09ef 30 }
31
b959599b 32 function applicable_formats() {
33 return array('all' => true);
34 }
35
86db09ef 36 function specialization() {
f36acb35 37 $this->title = isset($this->config->title) ? format_string($this->config->title) : format_string(get_string('newhtmlblock', 'block_html'));
86db09ef 38 }
39
40 function instance_allow_multiple() {
41 return true;
42 }
43
44 function get_content() {
99d19c13
PS
45 global $CFG;
46
47 require_once($CFG->libdir . '/filelib.php');
48
86db09ef 49 if ($this->content !== NULL) {
50 return $this->content;
51 }
52
367a75fa
SH
53 $filteropt = new stdClass;
54 $filteropt->overflowdiv = true;
3179b000 55 if ($this->content_is_trusted()) {
e92c286c 56 // fancy html allowed only on course, category and system blocks.
e8a7edd7 57 $filteropt->noclean = true;
e8a7edd7 58 }
e345909c 59
86db09ef 60 $this->content = new stdClass;
86db09ef 61 $this->content->footer = '';
94ee9ae0 62 if (isset($this->config->text)) {
41976db3 63 // rewrite url
ce162914
DC
64 $this->config->text = file_rewrite_pluginfile_urls($this->config->text, 'pluginfile.php', $this->context->id, 'block_html', 'content', NULL);
65 $this->content->text = format_text($this->config->text, $this->config->format, $filteropt);
94ee9ae0
MD
66 } else {
67 $this->content->text = '';
68 }
86db09ef 69
e345909c 70 unset($filteropt); // memory footprint
71
86db09ef 72 return $this->content;
73 }
43457dc8 74
41976db3
DC
75
76 /**
77 * Serialize and store config data
78 */
79 function instance_config_save($data, $nolongerused = false) {
80 global $DB;
81
ce162914 82 $config = clone($data);
41976db3 83 // Move embedded files into a proper filearea and adjust HTML links to match
ce162914
DC
84 $config->text = file_save_draft_area_files($data->text['itemid'], $this->context->id, 'block_html', 'content', 0, array('subdirs'=>true), $data->text['text']);
85 $config->format = $data->text['format'];
41976db3 86
ce162914 87 parent::instance_config_save($config, $nolongerused);
41976db3
DC
88 }
89
af140288
DC
90 function instance_delete() {
91 global $DB;
92 $fs = get_file_storage();
64f93798 93 $fs->delete_area_files($this->context->id, 'block_html');
af140288
DC
94 return true;
95 }
41976db3 96
3179b000 97 function content_is_trusted() {
d449b246
PS
98 global $SCRIPT;
99
758bfdf6
PS
100 if (!$context = get_context_instance_by_id($this->instance->parentcontextid)) {
101 return false;
102 }
d449b246
PS
103 //find out if this block is on the profile page
104 if ($context->contextlevel == CONTEXT_USER) {
105 if ($SCRIPT === '/my/index.php') {
106 // this is exception - page is completely private, nobody else may see content there
107 // that is why we allow JS here
108 return true;
109 } else {
110 // no JS on public personal pages, it would be a big security issue
111 return false;
112 }
113 }
114
115 return true;
3179b000 116 }
86db09ef 117}