source: moodle/trunk/fuentes/blocks/html/block_html.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 5.7 KB
Line 
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.
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
25class block_html extends block_base {
26
27    function init() {
28        $this->title = get_string('pluginname', 'block_html');
29    }
30
31    function has_config() {
32        return true;
33    }
34
35    function applicable_formats() {
36        return array('all' => true);
37    }
38
39    function specialization() {
40        $this->title = isset($this->config->title) ? format_string($this->config->title) : format_string(get_string('newhtmlblock', 'block_html'));
41    }
42
43    function instance_allow_multiple() {
44        return true;
45    }
46
47    function get_content() {
48        global $CFG;
49
50        require_once($CFG->libdir . '/filelib.php');
51
52        if ($this->content !== NULL) {
53            return $this->content;
54        }
55
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;
61        }
62
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;
74            }
75            $this->content->text = format_text($this->config->text, $format, $filteropt);
76        } else {
77            $this->content->text = '';
78        }
79
80        unset($filteropt); // memory footprint
81
82        return $this->content;
83    }
84
85
86    /**
87     * Serialize and store config data
88     */
89    function instance_config_save($data, $nolongerused = false) {
90        global $DB;
91
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'];
96
97        parent::instance_config_save($config, $nolongerused);
98    }
99
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;
105    }
106
107    /**
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
111     */
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));
120        }
121        return true;
122    }
123
124    function content_is_trusted() {
125        global $SCRIPT;
126
127        if (!$context = context::instance_by_id($this->instance->parentcontextid, IGNORE_MISSING)) {
128            return false;
129        }
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;
139            }
140        }
141
142        return true;
143    }
144
145    /**
146     * The block should only be dockable when the title of the block is not empty
147     * and when parent allows docking.
148     *
149     * @return bool
150     */
151    public function instance_can_be_docked() {
152        return (!empty($this->config->title) && parent::instance_can_be_docked());
153    }
154
155    /*
156     * Add custom html attributes to aid with theming and styling
157     *
158     * @return array
159     */
160    function html_attributes() {
161        global $CFG;
162
163        $attributes = parent::html_attributes();
164
165        if (!empty($CFG->block_html_allowcssclasses)) {
166            if (!empty($this->config->classes)) {
167                $attributes['class'] .= ' '.$this->config->classes;
168            }
169        }
170
171        return $attributes;
172    }
173}
Note: See TracBrowser for help on using the repository browser.