Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * @copyright 2010 Petr Skoda (http://skodak.org)
21
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @package   block_html
23
 * @category  files
24
 * @param stdClass $course course object
25
 * @param stdClass $birecord_or_cm block instance record
26
 * @param context $context context object
27
 * @param string $filearea file area
28
 * @param array $args extra arguments
29
 * @param bool $forcedownload whether or not force download
30
 * @param array $options additional options affecting the file serving
31
 * @return bool
32
 * @todo MDL-36050 improve capability check on stick blocks, so we can check user capability before sending images.
33
 */
34
function block_html_pluginfile($course, $birecord_or_cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
1441 ariadna 35
    global $CFG;
1 efrain 36
 
1441 ariadna 37
    require_once("{$CFG->dirroot}/user/lib.php");
38
 
1 efrain 39
    if ($context->contextlevel != CONTEXT_BLOCK) {
40
        send_file_not_found();
41
    }
42
 
43
    // If block is in course context, then check if user has capability to access course.
44
    if ($context->get_course_context(false)) {
45
        require_course_login($course);
46
    } else if ($CFG->forcelogin) {
47
        require_login();
48
    } else {
49
        // Get parent context and see if user have proper permission.
50
        $parentcontext = $context->get_parent_context();
51
        if ($parentcontext->contextlevel === CONTEXT_COURSECAT) {
52
            // Check if category is visible and user can view this category.
53
            if (!core_course_category::get($parentcontext->instanceid, IGNORE_MISSING)) {
54
                send_file_not_found();
55
            }
1441 ariadna 56
        } else if ($parentcontext->contextlevel === CONTEXT_USER) {
57
            $user = core_user::get_user($parentcontext->instanceid, '*', MUST_EXIST);
58
            $extracaps = block_method_result('html', 'get_extra_capabilities');
59
            if (!user_can_view_profile($user, null, $parentcontext) || !has_any_capability($extracaps, $context)) {
60
                send_file_not_found();
61
            }
1 efrain 62
        }
63
        // At this point there is no way to check SYSTEM context, so ignoring it.
64
    }
65
 
66
    if ($filearea !== 'content') {
67
        send_file_not_found();
68
    }
69
 
70
    $fs = get_file_storage();
71
 
72
    $filename = array_pop($args);
73
    $filepath = $args ? '/'.implode('/', $args).'/' : '/';
74
 
75
    if (!$file = $fs->get_file($context->id, 'block_html', 'content', 0, $filepath, $filename) or $file->is_directory()) {
76
        send_file_not_found();
77
    }
78
 
79
    if ($parentcontext = context::instance_by_id($birecord_or_cm->parentcontextid, IGNORE_MISSING)) {
80
        if ($parentcontext->contextlevel == CONTEXT_USER) {
81
            // force download on all personal pages including /my/
82
            //because we do not have reliable way to find out from where this is used
83
            $forcedownload = true;
84
        }
85
    } else {
86
        // weird, there should be parent context, better force dowload then
87
        $forcedownload = true;
88
    }
89
 
90
    // NOTE: it woudl be nice to have file revisions here, for now rely on standard file lifetime,
91
    //       do not lower it because the files are dispalyed very often.
92
    \core\session\manager::write_close();
93
    send_stored_file($file, null, 0, $forcedownload, $options);
94
}
95
 
96
/**
97
 * Perform global search replace such as when migrating site to new URL.
98
 * @param  $search
99
 * @param  $replace
100
 * @return void
101
 */
102
function block_html_global_db_replace($search, $replace) {
103
    global $DB;
104
 
105
    $instances = $DB->get_recordset('block_instances', array('blockname' => 'html'));
106
    foreach ($instances as $instance) {
107
        // TODO: intentionally hardcoded until MDL-26800 is fixed
108
        $config = unserialize_object(base64_decode($instance->configdata));
109
        if (isset($config->text) and is_string($config->text)) {
110
            $config->text = str_replace($search, $replace, $config->text);
111
            $DB->update_record('block_instances', ['id' => $instance->id,
112
                    'configdata' => base64_encode(serialize($config)), 'timemodified' => time()]);
113
        }
114
    }
115
    $instances->close();
116
}
117
 
118
/**
119
 * Given an array with a file path, it returns the itemid and the filepath for the defined filearea.
120
 *
121
 * @param  string $filearea The filearea.
122
 * @param  array  $args The path (the part after the filearea and before the filename).
123
 * @return array The itemid and the filepath inside the $args path, for the defined filearea.
124
 */
125
function block_html_get_path_from_pluginfile(string $filearea, array $args): array {
126
    // This block never has an itemid (the number represents the revision but it's not stored in database).
127
    array_shift($args);
128
 
129
    // Get the filepath.
130
    if (empty($args)) {
131
        $filepath = '/';
132
    } else {
133
        $filepath = '/' . implode('/', $args) . '/';
134
    }
135
 
136
    return [
137
        'itemid' => 0,
138
        'filepath' => $filepath,
139
    ];
140
}