Automatically generated installer lang files
[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
AD
81
82 if (!isset($modinfo->instances[$componentname])) {
83 $modinfo->instances[$componentname] = array();
84 }
e922fe23 85
c61daed0
AD
86 foreach ($modinfo->instances[$componentname] as $modinstanceid=>$cm) {
87 if ($modinstanceid==$instanceid) {
88 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
89 break;
90 }
91 }
92 }
93
94 if (empty($context)) {
95 //this shouldnt happen. something bad is going on.
96 rss_error('rsserror');
97 }
98
c05ccf3f
AD
99 //make sure that $CFG->siteguest is set
100 if (empty($CFG->siteguest)) {
101 if (!$guestid = $DB->get_field('user', 'id', array('username'=>'guest', 'mnethostid'=>$CFG->mnet_localhost_id))) {
102 // guest does not exist yet, weird
103 rss_error('rsserror');
104 }
105 set_config('siteguest', $guestid);
106 }
c61daed0
AD
107 $guesttoken = rss_get_token($CFG->siteguest);
108
109 //change forum to mod_forum (for example)
110 $componentname = 'mod_'.$componentname;
111
112 $url = $PAGE->url;
113 $url->set_slashargument("/{$context->id}/$guesttoken/$componentname/$instanceid/rss.xml");
114
115 //redirect to the 2.0 rss URL
116 redirect($url);
117} else {
118 // Authenticate the user from the token
119 $userid = rss_get_userid_from_token($token);
120 if (!$userid) {
121 rss_error('rsserrorauth');
122 }
fcce139a 123}
b111858d 124
1adad7f7
JF
125// Check the context actually exists
126list($context, $course, $cm) = get_context_info_array($contextid);
127
128$PAGE->set_context($context);
129
fcce139a 130$user = get_complete_user_data('id', $userid);
e922fe23
PS
131
132// let enrol plugins deal with new enrolments if necessary
133enrol_check_plugins($user);
134
690aa229 135session_set_user($user); //for login and capability checks
fcce139a 136
52cede73
AD
137try {
138 $autologinguest = true;
139 $setwantsurltome = true;
140 $preventredirect = true;
141 require_login($course, $autologinguest, $cm, $setwantsurltome, $preventredirect);
142} catch (Exception $e) {
c61daed0
AD
143 if (isguestuser()) {
144 rss_error('rsserrorguest');
145 } else {
146 rss_error('rsserrorauth');
147 }
52cede73 148}
b111858d
MD
149
150// Work out which component in Moodle we want (from the frankenstyle name)
690aa229
AD
151$componentdir = get_component_directory($componentname);
152list($type, $plugin) = normalize_component($componentname);
fcce139a 153
b111858d
MD
154
155// Call the component to check/update the feed and tell us the path to the cached file
fcce139a 156$pathname = null;
e417be4c 157
aa60291e
AD
158if (file_exists($componentdir)) {
159 require_once("$componentdir/rsslib.php");
160 $functionname = $plugin.'_rss_get_feed';
161
162 if (function_exists($functionname)) {
b111858d
MD
163 // $pathname will be null if there was a problem (eg user doesn't have the necessary capabilities)
164 // NOTE:the component providing the feed must do its own capability checks and security
1adad7f7
JF
165 try {
166 $pathname = $functionname($context, $args);
167 } catch (Exception $e) {
168 rss_error('rsserror');
169 }
aa60291e 170 }
fcce139a 171}
6f5e0852 172
b111858d
MD
173
174// Check that file exists
fcce139a 175if (empty($pathname) || !file_exists($pathname)) {
b111858d 176 rss_error();
fcce139a 177}
8adcb49f 178
b111858d
MD
179// Send the RSS file to the user!
180send_file($pathname, 'rss.xml', 3600); // Cached by browsers for 1 hour
8adcb49f 181
e7f927a0 182
13d1c9ed
JF
183/**
184 * Sends an error formatted as an rss file and then exits
185 *
186 * @package core_rss
187 * @category rss
188 *
189 * @param string $error the error type, default is rsserror
190 * @param string $filename the name of the file to create (NOT USED)
191 * @param int $lifetime UNSURE (NOT USED)
192 * @uses exit
b111858d
MD
193 */
194function rss_error($error='rsserror', $filename='rss.xml', $lifetime=0) {
195 send_file(rss_geterrorxmlfile($error), $filename, $lifetime, false, true);
196 exit;
fcce139a 197}