Commit | Line | Data |
---|---|---|
2e9b772f | 1 | <?PHP |
8adcb49f | 2 | //This file returns the required rss feeds |
3 | //The URL format MUST include: | |
fcce139a | 4 | // context: the context 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 | ||
2e9b772f | 16 | // disable moodle specific debug messages and any errors in output |
fcce139a AD |
17 | define('NO_DEBUG_DISPLAY', true);//comment this out to see any error messages during RSS generation |
18 | ||
19 | // session not used here | |
30d24538 | 20 | define('NO_MOODLE_COOKIES', true); |
fcce139a AD |
21 | |
22 | require_once('../config.php'); | |
23 | require_once($CFG->libdir.'/filelib.php'); | |
24 | require_once($CFG->libdir.'/rsslib.php'); | |
25 | ||
26 | //Check RSS feeds are enabled | |
27 | if (empty($CFG->enablerssfeeds)) { | |
28 | debugging('DISABLED (admin variables)'); | |
29 | rss_not_found(); | |
30 | } | |
31 | ||
690aa229 | 32 | $lifetime = 3600; // Seconds for files to remain in browser caches - 1 hour |
fcce139a AD |
33 | $filename = 'rss.xml'; |
34 | ||
fcce139a AD |
35 | $relativepath = get_file_argument(); |
36 | if (!$relativepath) { | |
37 | rss_not_found(); | |
38 | } | |
39 | ||
40 | // extract relative path components | |
41 | $args = explode('/', trim($relativepath, '/')); | |
42 | if (count($args) < 5) { | |
43 | rss_not_found(); | |
44 | } | |
45 | ||
46 | $contextid = (int)$args[0]; | |
47 | $token = $args[1]; | |
aa60291e | 48 | $componentname = clean_param($args[2], PARAM_FILE); |
274f9840 | 49 | //$instance = $args[3]; |
fcce139a AD |
50 | |
51 | $userid = rss_get_userid_from_token($token); | |
52 | if (!$userid) { | |
53 | rss_not_authenticated(); | |
54 | } | |
55 | $user = get_complete_user_data('id', $userid); | |
690aa229 | 56 | session_set_user($user); //for login and capability checks |
fcce139a AD |
57 | |
58 | $context = get_context_instance_by_id($contextid); | |
59 | if (!$context) { | |
60 | rss_not_found(); | |
61 | } | |
62 | $PAGE->set_context($context); | |
63 | ||
690aa229 AD |
64 | $componentdir = get_component_directory($componentname); |
65 | list($type, $plugin) = normalize_component($componentname); | |
fcce139a | 66 | |
aa60291e | 67 | //this will store the path to the cached rss feed contents |
fcce139a | 68 | $pathname = null; |
e417be4c | 69 | |
aa60291e AD |
70 | if (file_exists($componentdir)) { |
71 | require_once("$componentdir/rsslib.php"); | |
72 | $functionname = $plugin.'_rss_get_feed'; | |
73 | ||
74 | if (function_exists($functionname)) { | |
690aa229 | 75 | //$pathname will be null if there was a problem or the user doesn't have the necessary capabilities |
274f9840 AD |
76 | //NOTE the component providing the feed should do its own capability checks |
77 | $pathname = $functionname($context, $args); | |
aa60291e | 78 | } |
fcce139a | 79 | } |
6f5e0852 | 80 | |
fcce139a AD |
81 | //Check that file exists |
82 | if (empty($pathname) || !file_exists($pathname)) { | |
83 | rss_not_found(); | |
84 | } | |
8adcb49f | 85 | |
fcce139a AD |
86 | //Send it to user! |
87 | send_file($pathname, $filename, $lifetime); | |
8adcb49f | 88 | |
fcce139a AD |
89 | function rss_not_found() { |
90 | /// error, send some XML with error message | |
91 | global $lifetime, $filename; | |
92 | send_file(rss_geterrorxmlfile(), $filename, $lifetime, false, true); | |
690aa229 | 93 | die(); |
fcce139a | 94 | } |
e7f927a0 | 95 | |
fcce139a AD |
96 | function rss_not_authenticated() { |
97 | global $lifetime, $filename; | |
98 | send_file(rss_geterrorxmlfile('rsserrorauth'), $filename, $lifetime, false, true); | |
690aa229 | 99 | die(); |
fcce139a | 100 | } |
2e9b772f | 101 |