weekly release 3.0dev
[moodle.git] / rss / file.php
CommitLineData
b111858d
MD
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * rss/file.php - entry point to serve rss streams
e922fe23 19 *
b111858d 20 * This script simply checks the parameters to construct a $USER
e922fe23 21 * then finds and calls a function in the relevant component to
b111858d
MD
22 * actually check security and create the RSS stream
23 *
13d1c9ed
JF
24 * @package core_rss
25 * @category rss
26 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
b111858d
MD
28 */
29
1502bf77 30/** NO_DEBUG_DISPLAY - bool, Disable moodle debug and error messages. Set to false to see any errors during RSS generation */
13d1c9ed 31define('NO_DEBUG_DISPLAY', true);
fcce139a 32
13d1c9ed 33/** NO_MOODLE_COOKIES - bool, Disable the use of sessions/cookies - we recreate $USER for every call. */
30d24538 34define('NO_MOODLE_COOKIES', true);
fcce139a
AD
35
36require_once('../config.php');
37require_once($CFG->libdir.'/filelib.php');
38require_once($CFG->libdir.'/rsslib.php');
39
1502bf77 40// RSS feeds must be enabled site-wide.
fcce139a
AD
41if (empty($CFG->enablerssfeeds)) {
42 debugging('DISABLED (admin variables)');
b111858d 43 rss_error();
fcce139a
AD
44}
45
1502bf77 46// All the arguments are in the path.
fcce139a
AD
47$relativepath = get_file_argument();
48if (!$relativepath) {
b111858d 49 rss_error();
fcce139a
AD
50}
51
1502bf77 52// Extract relative path components into variables.
fcce139a
AD
53$args = explode('/', trim($relativepath, '/'));
54if (count($args) < 5) {
b111858d 55 rss_error();
fcce139a
AD
56}
57
58$contextid = (int)$args[0];
83b912e0 59$token = clean_param($args[1], PARAM_ALPHANUM);
aa60291e 60$componentname = clean_param($args[2], PARAM_FILE);
fcce139a 61
1502bf77
AD
62// Check if they have requested a 1.9 RSS feed.
63// If token is an int it is a user id (1.9 request).
64// If token contains any letters it is a token (2.0 request).
c61daed0 65$inttoken = intval($token);
1502bf77
AD
66if ($token === "$inttoken") {
67 // They have requested a feed using a 1.9 url. redirect them to the 2.0 url using the guest account.
c61daed0
AD
68
69 $instanceid = clean_param($args[3], PARAM_INT);
70
1502bf77 71 // 1.9 URL puts course id where the context id is in 2.0 URLs.
c61daed0
AD
72 $courseid = $contextid;
73 unset($contextid);
74
1502bf77 75 // Find the context id.
c61daed0 76 if ($course = $DB->get_record('course', array('id' => $courseid))) {
f20edd52 77 $modinfo = get_fast_modinfo($course);
c61daed0 78
1502bf77
AD
79 foreach ($modinfo->get_instances_of($componentname) as $modinstanceid => $cm) {
80 if ($modinstanceid == $instanceid) {
43731030 81 $context = context_module::instance($cm->id, IGNORE_MISSING);
c61daed0
AD
82 break;
83 }
84 }
85 }
86
87 if (empty($context)) {
1502bf77 88 // This shouldnt happen. something bad is going on.
c61daed0
AD
89 rss_error('rsserror');
90 }
91
1502bf77 92 // Make sure that $CFG->siteguest is set.
c05ccf3f 93 if (empty($CFG->siteguest)) {
1502bf77
AD
94 if (!$guestid = $DB->get_field('user', 'id', array('username' => 'guest', 'mnethostid' => $CFG->mnet_localhost_id))) {
95 // Guest does not exist yet, weird.
c05ccf3f
AD
96 rss_error('rsserror');
97 }
98 set_config('siteguest', $guestid);
99 }
c61daed0
AD
100 $guesttoken = rss_get_token($CFG->siteguest);
101
1502bf77 102 // Change forum to mod_forum (for example).
c61daed0
AD
103 $componentname = 'mod_'.$componentname;
104
105 $url = $PAGE->url;
106 $url->set_slashargument("/{$context->id}/$guesttoken/$componentname/$instanceid/rss.xml");
107
1502bf77 108 // Redirect to the 2.0 rss URL.
c61daed0
AD
109 redirect($url);
110} else {
1502bf77 111 // Authenticate the user from the token.
c61daed0
AD
112 $userid = rss_get_userid_from_token($token);
113 if (!$userid) {
114 rss_error('rsserrorauth');
115 }
fcce139a 116}
b111858d 117
1502bf77 118// Check the context actually exists.
e73cb1ac
JF
119list($context, $course, $cm) = get_context_info_array($contextid);
120
121$PAGE->set_context($context);
122
fcce139a 123$user = get_complete_user_data('id', $userid);
e922fe23 124
1502bf77 125// Let enrol plugins deal with new enrolments if necessary.
e922fe23
PS
126enrol_check_plugins($user);
127
1502bf77 128\core\session\manager::set_user($user); // For login and capability checks.
fcce139a 129
52cede73
AD
130try {
131 $autologinguest = true;
132 $setwantsurltome = true;
133 $preventredirect = true;
134 require_login($course, $autologinguest, $cm, $setwantsurltome, $preventredirect);
135} catch (Exception $e) {
c61daed0
AD
136 if (isguestuser()) {
137 rss_error('rsserrorguest');
138 } else {
139 rss_error('rsserrorauth');
140 }
52cede73 141}
b111858d 142
1502bf77 143// Work out which component in Moodle we want (from the frankenstyle name).
b0d1d941 144$componentdir = core_component::get_component_directory($componentname);
56da374e 145list($type, $plugin) = core_component::normalize_component($componentname);
fcce139a 146
1502bf77 147// Call the component to check/update the feed and tell us the path to the cached file.
fcce139a 148$pathname = null;
e417be4c 149
aa60291e
AD
150if (file_exists($componentdir)) {
151 require_once("$componentdir/rsslib.php");
152 $functionname = $plugin.'_rss_get_feed';
153
154 if (function_exists($functionname)) {
1502bf77
AD
155 // The $pathname will be null if there was a problem (eg user doesn't have the necessary capabilities).
156 // NOTE:the component providing the feed must do its own capability checks and security.
e73cb1ac
JF
157 try {
158 $pathname = $functionname($context, $args);
159 } catch (Exception $e) {
160 rss_error('rsserror');
161 }
aa60291e 162 }
fcce139a 163}
6f5e0852 164
1502bf77 165// Check that file exists.
fcce139a 166if (empty($pathname) || !file_exists($pathname)) {
b111858d 167 rss_error();
fcce139a 168}
8adcb49f 169
b111858d 170// Send the RSS file to the user!
1502bf77 171send_file($pathname, 'rss.xml', 3600); // Cached by browsers for 1 hour.
e7f927a0 172
13d1c9ed
JF
173/**
174 * Sends an error formatted as an rss file and then exits
175 *
176 * @package core_rss
177 * @category rss
178 *
179 * @param string $error the error type, default is rsserror
180 * @param string $filename the name of the file to create (NOT USED)
181 * @param int $lifetime UNSURE (NOT USED)
182 * @uses exit
b111858d
MD
183 */
184function rss_error($error='rsserror', $filename='rss.xml', $lifetime=0) {
185 send_file(rss_geterrorxmlfile($error), $filename, $lifetime, false, true);
186 exit;
fcce139a 187}