MDL-20691 "Fatal error: Exception thrown without a stack frame in Unknown on line...
[moodle.git] / draftfile.php
CommitLineData
d92e7c4d 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/**
19a2b80f 19 * This script serves draft files of current user
d92e7c4d 20 *
21 * @package moodlecore
22 * @subpackage file
23 * @copyright 2008 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27require_once('config.php');
28require_once('lib/filelib.php');
29
30require_login();
31if (isguestuser()) {
32 print_error('noguest');
33}
34
35// disable moodle specific debug messages
36disable_debugging();
37
38$relativepath = get_file_argument();
39
40// relative path must start with '/'
41if (!$relativepath) {
42 print_error('invalidargorconf');
43} else if ($relativepath{0} != '/') {
44 print_error('pathdoesnotstartslash');
45}
46
47// extract relative path components
48$args = explode('/', ltrim($relativepath, '/'));
49
50if (count($args) == 0) { // always at least user id
51 print_error('invalidarguments');
52}
53
54$contextid = (int)array_shift($args);
55$filearea = array_shift($args);
56
57$context = get_context_instance_by_id($contextid);
58if ($context->contextlevel != CONTEXT_USER) {
59 print_error('invalidarguments');
60}
61
62$userid = $context->instanceid;
63if ($USER->id != $userid) {
64 print_error('invaliduserid');
65}
66
67switch ($filearea) {
68 case 'user_draft':
69 $itemid = (int)array_shift($args);
70 break;
71 default:
72 send_file_not_found();
73}
4149edbd 74
d92e7c4d 75$relativepath = '/'.implode('/', $args);
7070268e 76
4149edbd 77
d92e7c4d 78$fs = get_file_storage();
7070268e 79
d92e7c4d 80$fullpath = $context->id.$filearea.$itemid.$relativepath;
7070268e 81
d92e7c4d 82if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->get_filename() == '.') {
83 send_file_not_found();
84}
7070268e 85
d92e7c4d 86// ========================================
87// finally send the file
88// ========================================
89session_get_instance()->write_close(); // unlock session during fileserving
90send_stored_file($file, 0, false, true); // force download - security first!