2009-05-21 09:50:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
/**
|
2009-05-21 09:52:00 +00:00
|
|
|
* This script serves draft files of current user
|
2009-05-21 09:50:04 +00:00
|
|
|
*
|
2010-07-03 13:37:13 +00:00
|
|
|
* @package core
|
2009-05-21 09:50:04 +00:00
|
|
|
* @subpackage file
|
|
|
|
* @copyright 2008 Petr Skoda (http://skodak.org)
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2009-10-31 22:02:05 +00:00
|
|
|
// disable moodle specific debug messages and any errors in output
|
|
|
|
define('NO_DEBUG_DISPLAY', true);
|
|
|
|
|
2009-05-21 09:50:04 +00:00
|
|
|
require_once('config.php');
|
|
|
|
require_once('lib/filelib.php');
|
|
|
|
|
|
|
|
require_login();
|
|
|
|
if (isguestuser()) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('noguest');
|
2009-05-21 09:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$relativepath = get_file_argument();
|
2012-05-12 16:20:40 +02:00
|
|
|
$preview = optional_param('preview', null, PARAM_ALPHANUM);
|
2009-05-21 09:50:04 +00:00
|
|
|
|
|
|
|
// relative path must start with '/'
|
|
|
|
if (!$relativepath) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invalidargorconf');
|
2019-10-19 20:52:24 +02:00
|
|
|
} else if ($relativepath[0] != '/') {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('pathdoesnotstartslash');
|
2009-05-21 09:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// extract relative path components
|
|
|
|
$args = explode('/', ltrim($relativepath, '/'));
|
|
|
|
|
|
|
|
if (count($args) == 0) { // always at least user id
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invalidarguments');
|
2009-05-21 09:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$contextid = (int)array_shift($args);
|
2010-07-03 13:37:13 +00:00
|
|
|
$component = array_shift($args);
|
|
|
|
$filearea = array_shift($args);
|
|
|
|
$draftid = (int)array_shift($args);
|
|
|
|
|
|
|
|
if ($component !== 'user' or $filearea !== 'draft') {
|
|
|
|
send_file_not_found();
|
|
|
|
}
|
2009-05-21 09:50:04 +00:00
|
|
|
|
2012-08-21 14:20:30 +08:00
|
|
|
$context = context::instance_by_id($contextid);
|
2009-05-21 09:50:04 +00:00
|
|
|
if ($context->contextlevel != CONTEXT_USER) {
|
2010-07-03 13:37:13 +00:00
|
|
|
send_file_not_found();
|
2009-05-21 09:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$userid = $context->instanceid;
|
|
|
|
if ($USER->id != $userid) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invaliduserid');
|
2009-05-21 09:50:04 +00:00
|
|
|
}
|
|
|
|
|
2008-09-06 21:16:59 +00:00
|
|
|
|
2009-05-21 09:50:04 +00:00
|
|
|
$fs = get_file_storage();
|
2008-08-29 06:03:52 +00:00
|
|
|
|
2010-07-03 13:37:13 +00:00
|
|
|
$relativepath = implode('/', $args);
|
|
|
|
$fullpath = "/$context->id/user/draft/$draftid/$relativepath";
|
2008-08-29 06:03:52 +00:00
|
|
|
|
2009-05-21 09:50:04 +00:00
|
|
|
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->get_filename() == '.') {
|
|
|
|
send_file_not_found();
|
|
|
|
}
|
2008-08-29 06:03:52 +00:00
|
|
|
|
2009-05-21 09:50:04 +00:00
|
|
|
// ========================================
|
|
|
|
// finally send the file
|
|
|
|
// ========================================
|
2013-09-08 08:38:52 +02:00
|
|
|
\core\session\manager::write_close(); // Unlock session during file serving.
|
2012-05-12 16:20:40 +02:00
|
|
|
send_stored_file($file, 0, false, true, array('preview' => $preview)); // force download - security first!
|