2008-07-31 22:15:30 +00:00
|
|
|
<?php // $Id$
|
|
|
|
|
|
|
|
require_once('config.php');
|
|
|
|
require_once('lib/filelib.php');
|
|
|
|
|
|
|
|
// disable moodle specific debug messages
|
|
|
|
disable_debugging();
|
|
|
|
|
2009-01-05 21:37:20 +00:00
|
|
|
$relativepath = get_file_argument();
|
2008-07-31 22:15:30 +00:00
|
|
|
$forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
|
|
|
|
|
|
|
|
// relative path must start with '/'
|
|
|
|
if (!$relativepath) {
|
|
|
|
print_error('invalidargorconf');
|
|
|
|
} else if ($relativepath{0} != '/') {
|
|
|
|
print_error('pathdoesnotstartslash');
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract relative path components
|
|
|
|
$args = explode('/', ltrim($relativepath, '/'));
|
|
|
|
|
|
|
|
if (count($args) == 0) { // always at least user id
|
|
|
|
print_error('invalidarguments');
|
|
|
|
}
|
|
|
|
|
|
|
|
$contextid = (int)array_shift($args);
|
|
|
|
$filearea = array_shift($args);
|
|
|
|
|
|
|
|
$context = get_context_instance_by_id($contextid);
|
|
|
|
if ($context->contextlevel != CONTEXT_USER) {
|
|
|
|
print_error('invalidarguments');
|
|
|
|
}
|
|
|
|
|
2008-09-05 09:48:51 +00:00
|
|
|
$userid = $context->instanceid;
|
|
|
|
|
2008-07-31 22:15:30 +00:00
|
|
|
switch ($filearea) {
|
2008-09-05 09:48:51 +00:00
|
|
|
case 'user_profile':
|
2008-09-08 23:16:48 +00:00
|
|
|
require_login();
|
|
|
|
if (isguestuser()) {
|
|
|
|
print_error('noguest');
|
|
|
|
}
|
|
|
|
|
|
|
|
// access controll here must match user edit forms
|
|
|
|
if ($userid == $USER->id) {
|
|
|
|
if (!has_capability('moodle/user:editownprofile', get_context_instance(CONTEXT_SYSTEM))) {
|
|
|
|
send_file_not_found();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!has_capability('moodle/user:editprofile', $context) and !has_capability('moodle/user:update', $context)) {
|
|
|
|
send_file_not_found();
|
2008-09-05 09:48:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$itemid = 0;
|
|
|
|
$forcedownload = true;
|
|
|
|
break;
|
2008-09-08 23:16:48 +00:00
|
|
|
|
2008-09-04 07:20:24 +00:00
|
|
|
case 'user_private':
|
2008-09-05 09:48:51 +00:00
|
|
|
require_login();
|
|
|
|
if (isguestuser()) {
|
2008-09-08 23:16:48 +00:00
|
|
|
send_file_not_found();
|
|
|
|
}
|
|
|
|
if ($USER->id != $userid) {
|
|
|
|
send_file_not_found();
|
2008-09-04 07:20:24 +00:00
|
|
|
}
|
|
|
|
$itemid = 0;
|
|
|
|
$forcedownload = true;
|
|
|
|
break;
|
2008-09-08 23:16:48 +00:00
|
|
|
|
2008-09-04 07:20:24 +00:00
|
|
|
default:
|
|
|
|
send_file_not_found();
|
2008-07-31 22:15:30 +00:00
|
|
|
}
|
2008-09-05 09:48:51 +00:00
|
|
|
|
2008-07-31 22:15:30 +00:00
|
|
|
$relativepath = '/'.implode('/', $args);
|
|
|
|
|
|
|
|
$fs = get_file_storage();
|
|
|
|
|
|
|
|
$fullpath = $context->id.$filearea.$itemid.$relativepath;
|
|
|
|
|
|
|
|
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->get_filename() == '.') {
|
2008-09-03 08:47:36 +00:00
|
|
|
send_file_not_found();
|
2008-07-31 22:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ========================================
|
|
|
|
// finally send the file
|
|
|
|
// ========================================
|
2009-01-17 15:25:08 +00:00
|
|
|
session_get_instance()->write_close(); // unlock session during fileserving
|
2008-07-31 22:15:30 +00:00
|
|
|
send_stored_file($file, 0, false, $forcedownload);
|