MDL-32471 preview thumbnails support for blocks
[moodle.git] / blocks / html / lib.php
CommitLineData
64f93798
PS
1<?php
2
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 *
64f93798
PS
21 * @copyright 2010 Petr Skoda (http://skodak.org)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d2b7803e
DC
23 * @package block_html
24 * @category files
25 * @param stdClass $course course object
26 * @param stdClass $birecord_or_cm block instance record
27 * @param stdClass $context context object
28 * @param string $filearea file area
29 * @param array $args extra arguments
30 * @param bool $forcedownload whether or not force download
957fc845 31 * @param array $options additional options affecting the file serving
d2b7803e 32 * @return bool
64f93798 33 */
957fc845 34function block_html_pluginfile($course, $birecord_or_cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
d449b246 35 global $SCRIPT;
64f93798
PS
36
37 if ($context->contextlevel != CONTEXT_BLOCK) {
38 send_file_not_found();
39 }
40
41 require_course_login($course);
42
43 if ($filearea !== 'content') {
44 send_file_not_found();
45 }
46
47 $fs = get_file_storage();
48
49 $filename = array_pop($args);
d85ab705 50 $filepath = $args ? '/'.implode('/', $args).'/' : '/';
64f93798
PS
51
52 if (!$file = $fs->get_file($context->id, 'block_html', 'content', 0, $filepath, $filename) or $file->is_directory()) {
53 send_file_not_found();
54 }
55
d449b246
PS
56 if ($parentcontext = get_context_instance_by_id($birecord_or_cm->parentcontextid)) {
57 if ($parentcontext->contextlevel == CONTEXT_USER) {
58 // force download on all personal pages including /my/
59 //because we do not have reliable way to find out from where this is used
60 $forcedownload = true;
61 }
62 } else {
63 // weird, there should be parent context, better force dowload then
64 $forcedownload = true;
65 }
66
64f93798 67 session_get_instance()->write_close();
957fc845 68 send_stored_file($file, 60*60, 0, $forcedownload, $options);
64f93798 69}
c3b5e829
PS
70
71/**
72 * Perform global search replace such as when migrating site to new URL.
73 * @param $search
74 * @param $replace
75 * @return void
76 */
77function block_html_global_db_replace($search, $replace) {
78 global $DB;
79
80 $instances = $DB->get_recordset('block_instances', array('blockname' => 'html'));
81 foreach ($instances as $instance) {
82 // TODO: intentionally hardcoded until MDL-26800 is fixed
83 $config = unserialize(base64_decode($instance->configdata));
84 if (isset($config->text) and is_string($config->text)) {
85 $config->text = str_replace($search, $replace, $config->text);
86 $DB->set_field('block_instances', 'configdata', base64_encode(serialize($config)), array('id' => $instance->id));
87 }
88 }
89 $instances->close();
d2b7803e 90}