Just tweaking
[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
e7f927a0 24 $relativepath = get_file_argument('file.php');
8adcb49f 25
7fe0ce4e 26
e7f927a0 27 if (!$relativepath) {
66c52fdf 28 rss_not_found();
8adcb49f 29 }
30
e7f927a0 31 // extract relative path components
32 $args = explode('/', trim($relativepath, '/'));
7fe0ce4e 33
ab7d76bb 34 $isblog = ($args[0] == 'blog');
7fe0ce4e 35
66c52fdf 36 $needcourse = !$isblog;
37
7fe0ce4e 38 if (count($args) < 5 && !$isblog) {
66c52fdf 39 rss_not_found();
8adcb49f 40 }
41
e7f927a0 42 $courseid = (int)$args[0];
43 $userid = (int)$args[1];
44 $modulename = clean_param($args[2], PARAM_FILE);
45 $instance = (int)$args[3];
c5152930 46 $filename = 'rss.xml';
7fe0ce4e 47
66c52fdf 48 if ($needcourse and (!$course = get_record('course', 'id', $courseid))) {
49 rss_not_found();
8adcb49f 50 }
7fe0ce4e 51
c062fee6 52 //Check name of module
53 $mods = get_list_of_plugins("mod");
66c52fdf 54 if ($needcourse and !in_array(strtolower($modulename), $mods)) {
55 rss_not_found();
8adcb49f 56 }
57
c0f778a2 58 //Get course_module to check it's visible
66c52fdf 59 if ($needcourse && (!$cm = get_coursemodule_from_instance($modulename,$instance,$courseid)) ) {
60 rss_not_found();
c0f778a2 61 }
c0f778a2 62
63 $isstudent = isstudent($courseid,$userid);
64 $isteacher = isteacher($courseid,$userid);
65
8adcb49f 66 //Check for "security" if !course->guest or course->password
56e8032e 67 if ($course->id != SITEID) {
66c52fdf 68 if ($needcourse and ((!$course->guest || $course->password) && (!($isstudent || $isteacher)))) {
69 rss_not_found();
56e8032e 70 }
c0f778a2 71 }
72
7fe0ce4e 73 //Check for "security" if the course is hidden or the activity is hidden
66c52fdf 74 if ($needcourse and ((!$course->visible || !$cm->visible) && (!$isteacher))) {
75 rss_not_found();
8adcb49f 76 }
77
7fe0ce4e 78 if ($isblog) {
79 $pathname = $CFG->dataroot.'/rss/'.$relativepath;
80 } else {
81 $pathname = $CFG->dataroot.'/rss/'.$modulename.'/'.$instance.'.xml';
82 }
e7f927a0 83 //Check that file exists
84 if (!file_exists($pathname)) {
66c52fdf 85 rss_not_found();
8adcb49f 86 }
87
e7f927a0 88 //Send it to user!
89 send_file($pathname, $filename, $lifetime);
90
66c52fdf 91 function rss_not_found() {
e7f927a0 92 /// error, send some XML with error message
be88433c 93 global $lifetime, $filename;
94 send_file(rss_geterrorxmlfile(), $filename, $lifetime, false, true);
e7f927a0 95 }
8adcb49f 96?>