block_html.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * Form for editing HTML block instances.
  18. *
  19. * @package block_html
  20. * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. class block_html extends block_base {
  24. function init() {
  25. $this->title = get_string('pluginname', 'block_html');
  26. }
  27. function has_config() {
  28. return true;
  29. }
  30. function applicable_formats() {
  31. return array('all' => true);
  32. }
  33. function specialization() {
  34. $this->title = isset($this->config->title) ? format_string($this->config->title) : format_string(get_string('newhtmlblock', 'block_html'));
  35. }
  36. function instance_allow_multiple() {
  37. return true;
  38. }
  39. function get_content() {
  40. global $CFG;
  41. require_once($CFG->libdir . '/filelib.php');
  42. if ($this->content !== NULL) {
  43. return $this->content;
  44. }
  45. $filteropt = new stdClass;
  46. $filteropt->overflowdiv = true;
  47. if ($this->content_is_trusted()) {
  48. // fancy html allowed only on course, category and system blocks.
  49. $filteropt->noclean = true;
  50. }
  51. $this->content = new stdClass;
  52. $this->content->footer = '';
  53. if (isset($this->config->text)) {
  54. // rewrite url
  55. $this->config->text = file_rewrite_pluginfile_urls($this->config->text, 'pluginfile.php', $this->context->id, 'block_html', 'content', NULL);
  56. // Default to FORMAT_HTML which is what will have been used before the
  57. // editor was properly implemented for the block.
  58. $format = FORMAT_HTML;
  59. // Check to see if the format has been properly set on the config
  60. if (isset($this->config->format)) {
  61. $format = $this->config->format;
  62. }
  63. $this->content->text = format_text($this->config->text, $format, $filteropt);
  64. } else {
  65. $this->content->text = '';
  66. }
  67. unset($filteropt); // memory footprint
  68. return $this->content;
  69. }
  70. /**
  71. * Serialize and store config data
  72. */
  73. function instance_config_save($data, $nolongerused = false) {
  74. global $DB;
  75. $config = clone($data);
  76. // Move embedded files into a proper filearea and adjust HTML links to match
  77. $config->text = file_save_draft_area_files($data->text['itemid'], $this->context->id, 'block_html', 'content', 0, array('subdirs'=>true), $data->text['text']);
  78. $config->format = $data->text['format'];
  79. parent::instance_config_save($config, $nolongerused);
  80. }
  81. function instance_delete() {
  82. global $DB;
  83. $fs = get_file_storage();
  84. $fs->delete_area_files($this->context->id, 'block_html');
  85. return true;
  86. }
  87. /**
  88. * Copy any block-specific data when copying to a new block instance.
  89. * @param int $fromid the id number of the block instance to copy from
  90. * @return boolean
  91. */
  92. public function instance_copy($fromid) {
  93. $fromcontext = context_block::instance($fromid);
  94. $fs = get_file_storage();
  95. // This extra check if file area is empty adds one query if it is not empty but saves several if it is.
  96. if (!$fs->is_area_empty($fromcontext->id, 'block_html', 'content', 0, false)) {
  97. $draftitemid = 0;
  98. file_prepare_draft_area($draftitemid, $fromcontext->id, 'block_html', 'content', 0, array('subdirs' => true));
  99. file_save_draft_area_files($draftitemid, $this->context->id, 'block_html', 'content', 0, array('subdirs' => true));
  100. }
  101. return true;
  102. }
  103. function content_is_trusted() {
  104. global $SCRIPT;
  105. if (!$context = context::instance_by_id($this->instance->parentcontextid, IGNORE_MISSING)) {
  106. return false;
  107. }
  108. //find out if this block is on the profile page
  109. if ($context->contextlevel == CONTEXT_USER) {
  110. if ($SCRIPT === '/my/index.php') {
  111. // this is exception - page is completely private, nobody else may see content there
  112. // that is why we allow JS here
  113. return true;
  114. } else {
  115. // no JS on public personal pages, it would be a big security issue
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. /**
  122. * The block should only be dockable when the title of the block is not empty
  123. * and when parent allows docking.
  124. *
  125. * @return bool
  126. */
  127. public function instance_can_be_docked() {
  128. return (!empty($this->config->title) && parent::instance_can_be_docked());
  129. }
  130. /*
  131. * Add custom html attributes to aid with theming and styling
  132. *
  133. * @return array
  134. */
  135. function html_attributes() {
  136. global $CFG;
  137. $attributes = parent::html_attributes();
  138. if (!empty($CFG->block_html_allowcssclasses)) {
  139. if (!empty($this->config->classes)) {
  140. $attributes['class'] .= ' '.$this->config->classes;
  141. }
  142. }
  143. return $attributes;
  144. }
  145. }