Just pushing the RSS a bit further, it's not tested properly yet
[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
7fe0ce4e 34
66c52fdf 35
a848c48c 36 if (count($args) < 5) {
66c52fdf 37 rss_not_found();
8adcb49f 38 }
39
e7f927a0 40 $courseid = (int)$args[0];
41 $userid = (int)$args[1];
42 $modulename = clean_param($args[2], PARAM_FILE);
a848c48c 43 $instance = $args[3];
c5152930 44 $filename = 'rss.xml';
7fe0ce4e 45
a848c48c 46 if ($isblog = $modulename == 'blog') {
47 $blogid = (int)$args[4]; // could be groupid / courseid / userid depending on $instance
48 if ($args[5] != 'rss.xml') {
49 $tag = clean_param($args[5], PARAM_FILE); // could be groupid / courseid / userid depending on $instance
50 }
51 } else {
52 $instance = (int)$instance; // we know it's an id number
53 }
54
55
56 if (!$course = get_record('course', 'id', $courseid)) {
66c52fdf 57 rss_not_found();
8adcb49f 58 }
7fe0ce4e 59
c062fee6 60 //Check name of module
a848c48c 61 if (!$isblog) {
62 $mods = get_list_of_plugins("mod");
63 if (!in_array(strtolower($modulename), $mods)) {
64 rss_not_found();
65 }
8adcb49f 66 }
67
c0f778a2 68 //Get course_module to check it's visible
a848c48c 69 if (!$isblog && (!$cm = get_coursemodule_from_instance($modulename,$instance,$courseid)) ) {
66c52fdf 70 rss_not_found();
c0f778a2 71 }
c0f778a2 72
73 $isstudent = isstudent($courseid,$userid);
74 $isteacher = isteacher($courseid,$userid);
75
8adcb49f 76 //Check for "security" if !course->guest or course->password
56e8032e 77 if ($course->id != SITEID) {
a848c48c 78 if ((!$course->guest || $course->password) && (!($isstudent || $isteacher))) {
66c52fdf 79 rss_not_found();
56e8032e 80 }
c0f778a2 81 }
82
7fe0ce4e 83 //Check for "security" if the course is hidden or the activity is hidden
a848c48c 84 if ((!$course->visible || !$cm->visible) && (!$isteacher)) {
66c52fdf 85 rss_not_found();
8adcb49f 86 }
87
7fe0ce4e 88 if ($isblog) {
a848c48c 89 if (empty($tag)) {
90 $pathname = $CFG->dataroot.'/rss/blog/'.$instance.'/'.$blogid.'.xml';
91 } else {
92 $pathname = $CFG->dataroot.'/rss/blog/'.$instance.'/'.$blogid.'/'.$tag.'.xml';
93 }
7fe0ce4e 94 } else {
95 $pathname = $CFG->dataroot.'/rss/'.$modulename.'/'.$instance.'.xml';
96 }
e7f927a0 97 //Check that file exists
98 if (!file_exists($pathname)) {
a848c48c 99 if ($isblog) {
100 if (filemtime($pathname) + 3600 < time()) {
101 require_once($CFG->dirroot.'/blog/rsslib.php');
102 if (!blog_generate_rss_feed($instance, $blogid)) {
103 rss_not_found();
104 }
105 }
106 } else {
107 rss_not_found();
108 }
8adcb49f 109 }
110
e7f927a0 111 //Send it to user!
112 send_file($pathname, $filename, $lifetime);
113
66c52fdf 114 function rss_not_found() {
e7f927a0 115 /// error, send some XML with error message
be88433c 116 global $lifetime, $filename;
117 send_file(rss_geterrorxmlfile(), $filename, $lifetime, false, true);
e7f927a0 118 }
8adcb49f 119?>