MDL-18243 data module filter - make it cross-db by using sql_compare_text() ; merged...
[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
11e7b506 9 $relativepath = get_file_argument();
172dd12c 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) {
9e5fa330 46 send_file_not_found();
172dd12c 47 }
48 }
49 }
50 $entryid = (int)array_shift($args);
51 if (!$entry = $DB->get_record('post', array('module'=>'blog', 'id'=>$entryid))) {
9e5fa330 52 send_file_not_found();
172dd12c 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) {
9e5fa330 65 send_file_not_found();
172dd12c 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()) {
9e5fa330 75 send_file_not_found();
172dd12c 76 }
77
6c0d09ee 78 send_stored_file($file, 10*60, 0, true); // download MUST be forced - security!
172dd12c 79
80 } else {
9e5fa330 81 send_file_not_found();
172dd12c 82 }
83
84
85 } else if ($context->contextlevel == CONTEXT_USER) {
9e5fa330 86 send_file_not_found();
172dd12c 87
88
89 } else if ($context->contextlevel == CONTEXT_COURSECAT) {
106f3b67 90 if ($filearea !== 'coursecat_intro') {
9e5fa330 91 send_file_not_found();
172dd12c 92 }
93
94 if ($CFG->forcelogin) {
95 // no login necessary - unless login forced everywhere
96 require_login();
97 }
98
99 $relativepath = '/'.implode('/', $args);
106f3b67 100 $fullpath = $context->id.'coursecat_intro0'.$relativepath;
172dd12c 101
102 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->get_filename() == '.') {
9e5fa330 103 send_file_not_found();
172dd12c 104 }
105
56949c17 106 session_get_instance()->write_close(); // unlock session during fileserving
172dd12c 107 send_stored_file($file, 60*60, 0, $forcedownload);
108
109
110 } else if ($context->contextlevel == CONTEXT_COURSE) {
172dd12c 111 if (!$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
112 print_error('invalidcourseid');
113 }
114
106f3b67 115 if ($filearea === 'course_backup') {
172dd12c 116 require_login($course);
117 require_capability('moodle/site:backupdownload', $context);
106f3b67 118
119 $relativepath = '/'.implode('/', $args);
120 $fullpath = $context->id.'course_backup0'.$relativepath;
121
122 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
123 send_file_not_found();
124 }
125
56949c17 126 session_get_instance()->write_close(); // unlock session during fileserving
106f3b67 127 send_stored_file($file, 0, 0, true);
128
129 } else if ($filearea === 'course_intro') {
172dd12c 130 if ($CFG->forcelogin) {
131 require_login();
132 }
172dd12c 133
106f3b67 134 $relativepath = '/'.implode('/', $args);
135 $fullpath = $context->id.'course_intro0'.$relativepath;
172dd12c 136
106f3b67 137 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
138 send_file_not_found();
139 }
172dd12c 140
56949c17 141 session_get_instance()->write_close(); // unlock session during fileserving
106f3b67 142 send_stored_file($file, 60*60, 0, false); // TODO: change timeout?
172dd12c 143
106f3b67 144 } else if ($filearea === 'user_profile') {
145 $userid = (int)array_shift($args);
146 $usercontext = get_context_instance(CONTEXT_USER, $userid);
147
148 if (!empty($CFG->forceloginforprofiles)) {
149 require_login();
150 if (isguestuser()) {
151 print_error('noguest');
152 }
153
154 if (!isteacherinanycourse()
155 and !isteacherinanycourse($userid)
156 and !has_capability('moodle/user:viewdetails', $usercontext)) {
157 print_error('usernotavailable');
158 }
159 if (!has_capability('moodle/user:viewdetails', $context) &&
160 !has_capability('moodle/user:viewdetails', $usercontext)) {
161 print_error('cannotviewprofile');
162 }
163 if (!has_capability('moodle/course:view', $context, $userid, false)) {
164 print_error('notenrolledprofile');
165 }
166 if (groups_get_course_groupmode($course) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
167 print_error('groupnotamember');
168 }
169 }
170
171 $relativepath = '/'.implode('/', $args);
172 $fullpath = $usercontext->id.'user_profile0'.$relativepath;
173
174 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
175 send_file_not_found();
176 }
177
56949c17 178 session_get_instance()->write_close(); // unlock session during fileserving
106f3b67 179 send_stored_file($file, 0, 0, true); // must force download - security!
180
181 } else {
182 send_file_not_found();
183 }
172dd12c 184
185 } else if ($context->contextlevel == CONTEXT_MODULE) {
106f3b67 186
172dd12c 187 if (!$coursecontext = get_context_instance_by_id(get_parent_contextid($context))) {
9e5fa330 188 send_file_not_found();
172dd12c 189 }
190
191 if (!$course = $DB->get_record('course', array('id'=>$coursecontext->instanceid))) {
9e5fa330 192 send_file_not_found();
172dd12c 193 }
194 $modinfo = get_fast_modinfo($course);
195 if (empty($modinfo->cms[$context->instanceid])) {
9e5fa330 196 send_file_not_found();
172dd12c 197 }
198
199 $cminfo = $modinfo->cms[$context->instanceid];
200 $modname = $cminfo->modname;
201 $libfile = "$CFG->dirroot/mod/$modname/lib.php";
202 if (file_exists($libfile)) {
203 require_once($libfile);
204 $filefunction = $modname.'_pluginfile';
205 if (function_exists($filefunction)) {
206 if ($filefunction($course, $cminfo, $context, $filearea, $args) !== false) {
207 die;
208 }
209 }
210 }
9e5fa330 211 send_file_not_found();
172dd12c 212
213 } else if ($context->contextlevel == CONTEXT_BLOCK) {
214 //not supported yet
9e5fa330 215 send_file_not_found();
172dd12c 216
217
218 } else {
9e5fa330 219 send_file_not_found();
172dd12c 220 }