diff --git a/rss/file.php b/rss/file.php index 2eab98423d2..4f59eb0d238 100644 --- a/rss/file.php +++ b/rss/file.php @@ -1,70 +1,91 @@ -. -// disable moodle specific debug messages and any errors in output +/** + * rss/file.php - entry point to serve rss streams + * + * This script simply checks the parameters to construct a $USER + * then finds and calls a function in the relevant component to + * actually check security and create the RSS stream + * + * @package moodlecore + * @copyright 1999 onwards Martin Dougiamas http://moodle.com + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + + +// Disable moodle specific debug messages and any errors in output define('NO_DEBUG_DISPLAY', true);//comment this out to see any error messages during RSS generation -// session not used here +// Sessions not used here, we recreate $USER every time we are called define('NO_MOODLE_COOKIES', true); require_once('../config.php'); require_once($CFG->libdir.'/filelib.php'); require_once($CFG->libdir.'/rsslib.php'); -//Check RSS feeds are enabled +// RSS feeds must be enabled site-wide if (empty($CFG->enablerssfeeds)) { debugging('DISABLED (admin variables)'); - rss_not_found(); + rss_error(); } -$lifetime = 3600; // Seconds for files to remain in browser caches - 1 hour -$filename = 'rss.xml'; +// All the arguments are in the path $relativepath = get_file_argument(); if (!$relativepath) { - rss_not_found(); + rss_error(); } -// extract relative path components + +// Extract relative path components into variables $args = explode('/', trim($relativepath, '/')); if (count($args) < 5) { - rss_not_found(); + rss_error(); } $contextid = (int)$args[0]; $token = $args[1]; $componentname = clean_param($args[2], PARAM_FILE); -//$instance = $args[3]; + +// Authenticate the user from the token $userid = rss_get_userid_from_token($token); if (!$userid) { - rss_not_authenticated(); + rss_error('rsserrorauth'); } + $user = get_complete_user_data('id', $userid); session_set_user($user); //for login and capability checks + +// Check the context actually exists $context = get_context_instance_by_id($contextid); if (!$context) { - rss_not_found(); + rss_error(); } $PAGE->set_context($context); + +// Work out which component in Moodle we want (from the frankenstyle name) $componentdir = get_component_directory($componentname); list($type, $plugin) = normalize_component($componentname); -//this will store the path to the cached rss feed contents + +// Call the component to check/update the feed and tell us the path to the cached file $pathname = null; if (file_exists($componentdir)) { @@ -72,30 +93,26 @@ if (file_exists($componentdir)) { $functionname = $plugin.'_rss_get_feed'; if (function_exists($functionname)) { - //$pathname will be null if there was a problem or the user doesn't have the necessary capabilities - //NOTE the component providing the feed should do its own capability checks + // $pathname will be null if there was a problem (eg user doesn't have the necessary capabilities) + // NOTE:the component providing the feed must do its own capability checks and security $pathname = $functionname($context, $args); } } -//Check that file exists + +// Check that file exists if (empty($pathname) || !file_exists($pathname)) { - rss_not_found(); + rss_error(); } -//Send it to user! -send_file($pathname, $filename, $lifetime); +// Send the RSS file to the user! +send_file($pathname, 'rss.xml', 3600); // Cached by browsers for 1 hour -function rss_not_found() { - /// error, send some XML with error message - global $lifetime, $filename; - send_file(rss_geterrorxmlfile(), $filename, $lifetime, false, true); - die(); + +/* + * Sends an error formatted as an rss file and then dies + */ +function rss_error($error='rsserror', $filename='rss.xml', $lifetime=0) { + send_file(rss_geterrorxmlfile($error), $filename, $lifetime, false, true); + exit; } - -function rss_not_authenticated() { - global $lifetime, $filename; - send_file(rss_geterrorxmlfile('rsserrorauth'), $filename, $lifetime, false, true); - die(); -} -