MDL-14589 initial file storage implementation, temporary file manager, migration...
[moodle.git] / pluginfile.php
CommitLineData
172dd12c 1<?php // $Id$
2
3 require_once('config.php');
4 require_once('lib/filelib.php');
5
6 // disable moodle specific debug messages
7 disable_debugging();
8
9 $relativepath = get_file_argument('file.php');
10 $forcedownload = optional_param('forcedownload', 0, PARAM_BOOL);
11
12 // relative path must start with '/'
13 if (!$relativepath) {
14 print_error('invalidargorconf');
15 } else if ($relativepath{0} != '/') {
16 print_error('pathdoesnotstartslash');
17 }
18
19 // extract relative path components
20 $args = explode('/', ltrim($relativepath, '/'));
21
22 if (count($args) == 0) { // always at least user id
23 print_error('invalidarguments');
24 }
25
26 $contextid = (int)array_shift($args);
27 $filearea = array_shift($args);
28
29 $context = get_context_instance_by_id($contextid);
30 $fs = get_file_storage();
31
32
33 if ($context->contextlevel == CONTEXT_SYSTEM) {
34 if ($filearea === 'blog') {
35
36 if (empty($CFG->bloglevel)) {
37 print_error('siteblogdisable', 'blog');
38 }
39 if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL) {
40 require_login();
41 if (isguestuser()) {
42 print_error('noguest');
43 }
44 if ($CFG->bloglevel == BLOG_USER_LEVEL) {
45 if ($USER->id != $entry->userid) {
46 not_found();
47 }
48 }
49 }
50 $entryid = (int)array_shift($args);
51 if (!$entry = $DB->get_record('post', array('module'=>'blog', 'id'=>$entryid))) {
52 not_found();
53 }
54 if ('publishstate' === 'public') {
55 if ($CFG->forcelogin) {
56 require_login();
57 }
58
59 } else if ('publishstate' === 'site') {
60 require_login();
61 //ok
62 } else if ('publishstate' === 'draft') {
63 require_login();
64 if ($USER->id != $entry->userid) {
65 not_found();
66 }
67 }
68
69 //TODO: implement shared course and shared group access
70
71 $relativepath = '/'.implode('/', $args);
72 $fullpath = $context->id.'blog'.$entryid.$relativepath;
73
74 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
75 not_found();
76 }
77
78 send_stored_file($file, 10*60, 0, true); // downlaod MUST be forced - security!
79
80 } else {
81 not_found();
82 }
83
84
85 } else if ($context->contextlevel == CONTEXT_USER) {
86 not_found();
87
88
89 } else if ($context->contextlevel == CONTEXT_COURSECAT) {
90 if ($filearea !== 'intro') {
91 not_found();
92 }
93
94 if ($CFG->forcelogin) {
95 // no login necessary - unless login forced everywhere
96 require_login();
97 }
98
99 $relativepath = '/'.implode('/', $args);
100 $fullpath = $context->id.'intro0'.$relativepath;
101
102 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->get_filename() == '.') {
103 not_found();
104 }
105
106 session_write_close(); // unlock session during fileserving
107 send_stored_file($file, 60*60, 0, $forcedownload);
108
109
110 } else if ($context->contextlevel == CONTEXT_COURSE) {
111 if ($filearea !== 'intro' and $filearea !== 'backup') {
112 not_found();
113 }
114
115 if (!$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
116 print_error('invalidcourseid');
117 }
118
119 if ($filearea === 'backup') {
120 require_login($course);
121 require_capability('moodle/site:backupdownload', $context);
122 } else {
123 if ($CFG->forcelogin) {
124 require_login();
125 }
126 }
127
128 $relativepath = '/'.implode('/', $args);
129 $fullpath = $context->id.'intro0'.$relativepath;
130
131 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
132 not_found();
133 }
134
135 session_write_close(); // unlock session during fileserving
136 send_stored_file($file, 60*60, 0, $forcedownload);
137
138
139 } else if ($context->contextlevel == CONTEXT_MODULE) {
140
141 if (!$coursecontext = get_context_instance_by_id(get_parent_contextid($context))) {
142 not_found();
143 }
144
145 if (!$course = $DB->get_record('course', array('id'=>$coursecontext->instanceid))) {
146 not_found();
147 }
148 $modinfo = get_fast_modinfo($course);
149 if (empty($modinfo->cms[$context->instanceid])) {
150 not_found();
151 }
152
153 $cminfo = $modinfo->cms[$context->instanceid];
154 $modname = $cminfo->modname;
155 $libfile = "$CFG->dirroot/mod/$modname/lib.php";
156 if (file_exists($libfile)) {
157 require_once($libfile);
158 $filefunction = $modname.'_pluginfile';
159 if (function_exists($filefunction)) {
160 if ($filefunction($course, $cminfo, $context, $filearea, $args) !== false) {
161 die;
162 }
163 }
164 }
165 not_found();
166
167 } else if ($context->contextlevel == CONTEXT_BLOCK) {
168 //not supported yet
169 not_found();
170
171
172 } else {
173 not_found();
174 }
175
176
177 function not_found() {
178 global $CFG;
179 header('HTTP/1.0 404 not found');
180 print_error('filenotfound', 'error', $CFG->wwwroot.'/'); //this is not displayed on IIS??
181 }