MDL-41219 course: Make properties of course_modinfo read-only
[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
30
13d1c9ed
JF
31/** NO_DEBUG_DISPLAY - bool, Disable moodle specific debug messages and any errors in output. Set to false to see any error messages during RSS generation */
32define('NO_DEBUG_DISPLAY', true);
fcce139a 33
13d1c9ed 34/** NO_MOODLE_COOKIES - bool, Disable the use of sessions/cookies - we recreate $USER for every call. */
30d24538 35define('NO_MOODLE_COOKIES', true);
fcce139a
AD
36
37require_once('../config.php');
38require_once($CFG->libdir.'/filelib.php');
39require_once($CFG->libdir.'/rsslib.php');
40
b111858d 41// RSS feeds must be enabled site-wide
fcce139a
AD
42if (empty($CFG->enablerssfeeds)) {
43 debugging('DISABLED (admin variables)');
b111858d 44 rss_error();
fcce139a
AD
45}
46
fcce139a 47
b111858d 48// All the arguments are in the path
fcce139a
AD
49$relativepath = get_file_argument();
50if (!$relativepath) {
b111858d 51 rss_error();
fcce139a
AD
52}
53
b111858d
MD
54
55// Extract relative path components into variables
fcce139a
AD
56$args = explode('/', trim($relativepath, '/'));
57if (count($args) < 5) {
b111858d 58 rss_error();
fcce139a
AD
59}
60
61$contextid = (int)$args[0];
83b912e0 62$token = clean_param($args[1], PARAM_ALPHANUM);
aa60291e 63$componentname = clean_param($args[2], PARAM_FILE);
fcce139a 64
c61daed0
AD
65//check if they have requested a 1.9 RSS feed
66//if token is an int its a user id (1.9 request)
67//if token contains any letters its a token (2.0 request)
68$inttoken = intval($token);
69if ($token==="$inttoken") {
70 //they've requested a feed using a 1.9 url. redirect them to the 2.0 url using the guest account
71
72 $instanceid = clean_param($args[3], PARAM_INT);
73
74 //1.9 URL puts course id where the context id is in 2.0 URLs
75 $courseid = $contextid;
76 unset($contextid);
77
78 //find the context id
79 if ($course = $DB->get_record('course', array('id' => $courseid))) {
f20edd52 80 $modinfo = get_fast_modinfo($course);
c61daed0 81
44ddd2a1 82 foreach ($modinfo->get_instances_of($componentname) as $modinstanceid=>$cm) {
c61daed0 83 if ($modinstanceid==$instanceid) {
43731030 84 $context = context_module::instance($cm->id, IGNORE_MISSING);
c61daed0
AD
85 break;
86 }
87 }
88 }
89
90 if (empty($context)) {
91 //this shouldnt happen. something bad is going on.
92 rss_error('rsserror');
93 }
94
c05ccf3f
AD
95 //make sure that $CFG->siteguest is set
96 if (empty($CFG->siteguest)) {
97 if (!$guestid = $DB->get_field('user', 'id', array('username'=>'guest', 'mnethostid'=>$CFG->mnet_localhost_id))) {
98 // guest does not exist yet, weird
99 rss_error('rsserror');
100 }
101 set_config('siteguest', $guestid);
102 }
c61daed0
AD
103 $guesttoken = rss_get_token($CFG->siteguest);
104
105 //change forum to mod_forum (for example)
106 $componentname = 'mod_'.$componentname;
107
108 $url = $PAGE->url;
109 $url->set_slashargument("/{$context->id}/$guesttoken/$componentname/$instanceid/rss.xml");
110
111 //redirect to the 2.0 rss URL
112 redirect($url);
113} else {
114 // Authenticate the user from the token
115 $userid = rss_get_userid_from_token($token);
116 if (!$userid) {
117 rss_error('rsserrorauth');
118 }
fcce139a 119}
b111858d 120
e73cb1ac
JF
121// Check the context actually exists
122list($context, $course, $cm) = get_context_info_array($contextid);
123
124$PAGE->set_context($context);
125
fcce139a 126$user = get_complete_user_data('id', $userid);
e922fe23
PS
127
128// let enrol plugins deal with new enrolments if necessary
129enrol_check_plugins($user);
130
690aa229 131session_set_user($user); //for login and capability checks
fcce139a 132
52cede73
AD
133try {
134 $autologinguest = true;
135 $setwantsurltome = true;
136 $preventredirect = true;
137 require_login($course, $autologinguest, $cm, $setwantsurltome, $preventredirect);
138} catch (Exception $e) {
c61daed0
AD
139 if (isguestuser()) {
140 rss_error('rsserrorguest');
141 } else {
142 rss_error('rsserrorauth');
143 }
52cede73 144}
b111858d
MD
145
146// Work out which component in Moodle we want (from the frankenstyle name)
b0d1d941 147$componentdir = core_component::get_component_directory($componentname);
56da374e 148list($type, $plugin) = core_component::normalize_component($componentname);
fcce139a 149
b111858d
MD
150
151// Call the component to check/update the feed and tell us the path to the cached file
fcce139a 152$pathname = null;
e417be4c 153
aa60291e
AD
154if (file_exists($componentdir)) {
155 require_once("$componentdir/rsslib.php");
156 $functionname = $plugin.'_rss_get_feed';
157
158 if (function_exists($functionname)) {
b111858d
MD
159 // $pathname will be null if there was a problem (eg user doesn't have the necessary capabilities)
160 // NOTE:the component providing the feed must do its own capability checks and security
e73cb1ac
JF
161 try {
162 $pathname = $functionname($context, $args);
163 } catch (Exception $e) {
164 rss_error('rsserror');
165 }
aa60291e 166 }
fcce139a 167}
6f5e0852 168
b111858d
MD
169
170// Check that file exists
fcce139a 171if (empty($pathname) || !file_exists($pathname)) {
b111858d 172 rss_error();
fcce139a 173}
8adcb49f 174
b111858d
MD
175// Send the RSS file to the user!
176send_file($pathname, 'rss.xml', 3600); // Cached by browsers for 1 hour
8adcb49f 177
e7f927a0 178
13d1c9ed
JF
179/**
180 * Sends an error formatted as an rss file and then exits
181 *
182 * @package core_rss
183 * @category rss
184 *
185 * @param string $error the error type, default is rsserror
186 * @param string $filename the name of the file to create (NOT USED)
187 * @param int $lifetime UNSURE (NOT USED)
188 * @uses exit
b111858d
MD
189 */
190function rss_error($error='rsserror', $filename='rss.xml', $lifetime=0) {
191 send_file(rss_geterrorxmlfile($error), $filename, $lifetime, false, true);
192 exit;
fcce139a 193}