Merged blog RSS fix MDL-7251
[moodle.git] / rss / file.php
CommitLineData
e7f927a0 1<?PHP //$Id$
8adcb49f 2 //This file returns the required rss feeds
3 //The URL format MUST include:
4 // course: the course id
e7f927a0 5 // user: the user id
6 // name: the name of the module (forum...)
7 // id: the id (instance) of the module (forumid...)
8adcb49f 8 //If the course has a password or it doesn't
7fe0ce4e 9 //allow guest access then the user field is
8adcb49f 10 //required to see that the user is enrolled
11 //in the course, else no check is performed.
12 //This allows to limit a bit the rss access
13 //to correct users. It isn't unbreakable,
14 //obviously, but its the best I've thought!!
15
e7f927a0 16 $nomoodlecookie = true; // Because it interferes with caching
7fe0ce4e 17
e7f927a0 18 require_once('../config.php');
7ade05c5 19 require_once($CFG->libdir.'/filelib.php');
48b29ba4 20 require_once($CFG->libdir.'/rsslib.php');
8adcb49f 21
e7f927a0 22 $lifetime = 3600; // Seconds for files to remain in caches - 1 hour
8adcb49f 23
7eb0b60a 24 // disable moodle specific debug messages
25 disable_debugging();
26
e7f927a0 27 $relativepath = get_file_argument('file.php');
8adcb49f 28
7fe0ce4e 29
e7f927a0 30 if (!$relativepath) {
66c52fdf 31 rss_not_found();
8adcb49f 32 }
33
e7f927a0 34 // extract relative path components
35 $args = explode('/', trim($relativepath, '/'));
7fe0ce4e 36
a848c48c 37 if (count($args) < 5) {
66c52fdf 38 rss_not_found();
8adcb49f 39 }
40
e7f927a0 41 $courseid = (int)$args[0];
42 $userid = (int)$args[1];
43 $modulename = clean_param($args[2], PARAM_FILE);
a848c48c 44 $instance = $args[3];
c5152930 45 $filename = 'rss.xml';
7fe0ce4e 46
a848c48c 47 if ($isblog = $modulename == 'blog') {
48 $blogid = (int)$args[4]; // could be groupid / courseid / userid depending on $instance
49 if ($args[5] != 'rss.xml') {
6619a7f4 50 $tagid = (int)$args[5];
51 } else {
52 $tagid = 0;
a848c48c 53 }
54 } else {
55 $instance = (int)$instance; // we know it's an id number
56 }
57
58
59 if (!$course = get_record('course', 'id', $courseid)) {
66c52fdf 60 rss_not_found();
8adcb49f 61 }
7fe0ce4e 62
c062fee6 63 //Check name of module
a848c48c 64 if (!$isblog) {
72f383b1 65 $mods = get_list_of_plugins('mod');
a848c48c 66 if (!in_array(strtolower($modulename), $mods)) {
67 rss_not_found();
68 }
72f383b1 69 //Get course_module to check it's visible
70 if (!$cm = get_coursemodule_from_instance($modulename,$instance,$courseid)) {
71 rss_not_found();
72 }
73 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
74 $isuser = has_capability('moodle/course:view', $context, $userid); // Not ideal, this should be module-specific, but deferring until RSS gets a revamp with codes in the URLs
75 } else {
76 $context = get_context_instance(CONTEXT_COURSE, $course->id);
77 $isuser = has_capability('moodle/course:view', $context, $userid);
8adcb49f 78 }
d02eeded 79
8adcb49f 80 //Check for "security" if !course->guest or course->password
56e8032e 81 if ($course->id != SITEID) {
d02eeded 82 if ((!$course->guest || $course->password) && (!$isuser)) {
66c52fdf 83 rss_not_found();
56e8032e 84 }
c0f778a2 85 }
86
7fe0ce4e 87 //Check for "security" if the course is hidden or the activity is hidden
d02eeded 88 if (!$isblog and (!$course->visible || !$cm->visible) && (!has_capability('moodle/course:viewhiddenactivities', $context))) {
66c52fdf 89 rss_not_found();
8adcb49f 90 }
91
6619a7f4 92 //Work out the filename of the RSS file
7fe0ce4e 93 if ($isblog) {
6619a7f4 94 require_once($CFG->dirroot.'/blog/rsslib.php');
95 $pathname = blog_generate_rss_feed($instance, $blogid, $tagid);
7fe0ce4e 96 } else {
97 $pathname = $CFG->dataroot.'/rss/'.$modulename.'/'.$instance.'.xml';
98 }
6619a7f4 99
e7f927a0 100 //Check that file exists
101 if (!file_exists($pathname)) {
6619a7f4 102 rss_not_found();
8adcb49f 103 }
104
e7f927a0 105 //Send it to user!
106 send_file($pathname, $filename, $lifetime);
107
66c52fdf 108 function rss_not_found() {
e7f927a0 109 /// error, send some XML with error message
be88433c 110 global $lifetime, $filename;
111 send_file(rss_geterrorxmlfile(), $filename, $lifetime, false, true);
e7f927a0 112 }
8adcb49f 113?>