2 //This file returns the required rss feeds
3 //The URL format MUST include:
4 // context: the context id
6 // name: the name of the module (forum...)
7 // id: the id (instance) of the module (forumid...)
8 //If the course has a password or it doesn't
9 //allow guest access then the user field is
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!!
16 // disable moodle specific debug messages and any errors in output
17 define('NO_DEBUG_DISPLAY', true);//comment this out to see any error messages during RSS generation
19 // session not used here
20 define('NO_MOODLE_COOKIES', true);
22 require_once('../config.php');
23 require_once($CFG->libdir.'/filelib.php');
24 require_once($CFG->libdir.'/rsslib.php');
26 //Check RSS feeds are enabled
27 if (empty($CFG->enablerssfeeds)) {
28 debugging('DISABLED (admin variables)');
32 $lifetime = 3600; // Seconds for files to remain in browser caches - 1 hour
33 $filename = 'rss.xml';
35 // this is a big one big hack - NO_MOODLE_COOKIES is not compatible with capabilities MDL-7243
36 // it should be replaced once we get to codes in urls
38 $relativepath = get_file_argument();
43 // extract relative path components
44 $args = explode('/', trim($relativepath, '/'));
45 if (count($args) < 5) {
49 $contextid = (int)$args[0];
51 $componentname = clean_param($args[2], PARAM_FILE);
54 $userid = rss_get_userid_from_token($token);
56 rss_not_authenticated();
58 $user = get_complete_user_data('id', $userid);
59 session_set_user($user); //for login and capability checks
61 $context = get_context_instance_by_id($contextid);
65 $PAGE->set_context($context);
67 $componentdir = get_component_directory($componentname);
68 list($type, $plugin) = normalize_component($componentname);
70 //this will store the path to the cached rss feed contents
73 //check user's psuedo login created by session_set_user()
74 //NOTE the component providing the feed should do its own capability checks
77 if (!empty($plugin) && !empty($instance)) {
78 $cm = get_coursemodule_from_instance($plugin, $instance, 0, false, MUST_EXIST);
81 //Get course from context
82 //TODO: note that in the case of the hub rss feed, the feed is not related to a course context,
83 //it is more a "site" context. The Hub RSS bypass the following line using context id = 2
84 $coursecontext = get_course_context($context);
88 $course = $DB->get_record('course', array('id' => $coursecontext->instanceid), '*', MUST_EXIST);
91 require_login($course, false, $cm, false, true);
92 } catch (Exception $e) {
96 if (file_exists($componentdir)) {
97 require_once("$componentdir/rsslib.php");
98 $functionname = $plugin.'_rss_get_feed';
100 if (function_exists($functionname)) {
101 //$pathname will be null if there was a problem or the user doesn't have the necessary capabilities
102 $pathname = $functionname($context, $cm, $instance, $args);
106 //Check that file exists
107 if (empty($pathname) || !file_exists($pathname)) {
112 send_file($pathname, $filename, $lifetime);
114 function rss_not_found() {
115 /// error, send some XML with error message
116 global $lifetime, $filename;
117 send_file(rss_geterrorxmlfile(), $filename, $lifetime, false, true);
121 function rss_not_authenticated() {
122 global $lifetime, $filename;
123 send_file(rss_geterrorxmlfile('rsserrorauth'), $filename, $lifetime, false, true);