MDL-15919 fixed scriptname param
[moodle.git] / draftfile.php
CommitLineData
7070268e 1<?php // $Id$
2
3 require_once('config.php');
4 require_once('lib/filelib.php');
5
6 require_login();
7 if (isguestuser()) {
8 print_error('noguest');
9 }
10
11 // disable moodle specific debug messages
12 disable_debugging();
13
d8c1ec43 14 $relativepath = get_file_argument('draftfile.php');
7070268e 15 $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
16
17 // relative path must start with '/'
18 if (!$relativepath) {
19 print_error('invalidargorconf');
20 } else if ($relativepath{0} != '/') {
21 print_error('pathdoesnotstartslash');
22 }
23
24 // extract relative path components
25 $args = explode('/', ltrim($relativepath, '/'));
26
27 if (count($args) == 0) { // always at least user id
28 print_error('invalidarguments');
29 }
30
31 $contextid = (int)array_shift($args);
d8c1ec43 32 $itemid = (int)array_shift($args);
7070268e 33
34 $context = get_context_instance_by_id($contextid);
35 if ($context->contextlevel != CONTEXT_USER) {
36 print_error('invalidarguments');
37 }
38
39 $userid = $context->instanceid;
40 if ($USER->id != $userid) {
41 print_error('invaliduserid');
42 }
43
7070268e 44 $relativepath = '/'.implode('/', $args);
45
7070268e 46 $fs = get_file_storage();
47
d8c1ec43 48 $fullpath = $context->id.'user_draft'.$itemid.$relativepath;
7070268e 49
50 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->get_filename() == '.') {
2aea0c5e 51 send_file_not_found();
7070268e 52 }
53
54 // ========================================
55 // finally send the file
56 // ========================================
57 session_write_close(); // unlock session during fileserving
58 send_stored_file($file, 0, false, $forcedownload);