-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.php
More file actions
124 lines (108 loc) · 4.65 KB
/
Copy pathlib.php
File metadata and controls
124 lines (108 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
// This file is part of ProFormA Question Type for Moodle
//
// ProFormA Question Type for Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProFormA Question Type for Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Plugin library
*
* @package qtype
* @subpackage proforma
* @copyright 2019 Ostfalia Hochschule fuer angewandte Wissenschaften
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author K.Borm <k.borm[at]ostfalia.de>
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/question/type/proforma/questiontype.php');
require_once($CFG->dirroot . '/question/type/proforma/classes/filearea.php');
/**
* Checks file access for proforma questions.
*
* @package qtype_proforma
* @category files
* @param stdClass $course course object
* @param stdClass $cm course module object
* @param stdClass $context context object
* @param string $filearea file area
* @param array $args extra arguments
* @param bool $forcedownload whether or not force download
* @param array $options additional options affecting the file serving
* @return bool
*/
function qtype_proforma_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
// Check the contextlevel is as expected - if your plugin is a block, this becomes CONTEXT_BLOCK, etc.
switch ($context->contextlevel) {
case CONTEXT_COURSE:
case CONTEXT_COURSECAT:
case CONTEXT_MODULE:
break;
default:
return false;
}
// Make sure the user is logged in and has access to the module (plugins that are not course modules should leave out the 'cm' part).
require_login($course, false, $cm);
// Make sure the filearea is one of those used by the plugin and:
// Check the relevant capabilities - these vary depending on the filearea being accessed.
switch ($filearea) {
case qtype_proforma::FILEAREA_MODELSOL:
case qtype_proforma::FILEAREA_TASK:
if (!has_capability('moodle/question:editmine', $context)) {
return false;
}
break;
default:
$pass = false;
foreach (qtype_proforma::fileareas_for_studentfiles() as $proformafilearea => $value) {
if ($proformafilearea === $filearea) {
if (!has_capability('moodle/question:flag', $context)) {
return false;
} else {
$pass = true;
}
}
}
if (!$pass) {
return false;
}
}
// Leave this line out if you set the itemid to null in make_pluginfile_url (set $itemid to 0 instead).
$itemid = array_shift($args); // The first item in the $args array.
// Use the itemid to retrieve any relevant data records and perform any security checks to see if the
// user really does have access to the file in question.
// Extract the filename / filepath from the $args array.
// $filename = array_pop($args); // The last item in the $args array.
if (!$args) {
// echo 'no file given';
return false;
}
$filename = implode('/', $args); // $args contains elements of the filepath
// $filepath = '/';
// Retrieve the file from the Files API.
$fs = get_file_storage();
// for historical reasons we use two approaches for retrieving the file:
// (I want to avoid updating database values to fit the new model)
// New approach:
// properly split filename into path and basename
list($filepath, $basename) = qtype_proforma_filearea::split_filename($filename);
$file = $fs->get_file($context->id, 'qtype_proforma', $filearea, $itemid, $filepath , $basename);
if (!$file) {
// Old approach:
// filename also contains path part
$file = $fs->get_file($context->id, 'qtype_proforma', $filearea, $itemid, '/', $filename);
}
if (!$file) {
return false; // The file does not exist.
}
// We can now send the file back to the browser
send_stored_file($file, 0, 0, true, $options); // download MUST be forced - security!
}