weekly release 2.2dev
[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
19 *
20 * This script simply checks the parameters to construct a $USER
21 * then finds and calls a function in the relevant component to
22 * actually check security and create the RSS stream
23 *
24 * @package moodlecore
25 * @copyright 1999 onwards Martin Dougiamas http://moodle.com
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29
30// Disable moodle specific debug messages and any errors in output
fcce139a
AD
31define('NO_DEBUG_DISPLAY', true);//comment this out to see any error messages during RSS generation
32
b111858d 33// Sessions not used here, we recreate $USER every time we are called
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
b111858d 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
fcce139a 46
b111858d 47// All the arguments are in the path
fcce139a
AD
48$relativepath = get_file_argument();
49if (!$relativepath) {
b111858d 50 rss_error();
fcce139a
AD
51}
52
b111858d
MD
53
54// Extract relative path components into variables
fcce139a
AD
55$args = explode('/', trim($relativepath, '/'));
56if (count($args) < 5) {
b111858d 57 rss_error();
fcce139a
AD
58}
59
60$contextid = (int)$args[0];
83b912e0 61$token = clean_param($args[1], PARAM_ALPHANUM);
aa60291e 62$componentname = clean_param($args[2], PARAM_FILE);
fcce139a 63
c61daed0
AD
64//check if they have requested a 1.9 RSS feed
65//if token is an int its a user id (1.9 request)
66//if token contains any letters its a token (2.0 request)
67$inttoken = intval($token);
68if ($token==="$inttoken") {
69 //they've requested a feed using a 1.9 url. redirect them to the 2.0 url using the guest account
70
71 $instanceid = clean_param($args[3], PARAM_INT);
72
73 //1.9 URL puts course id where the context id is in 2.0 URLs
74 $courseid = $contextid;
75 unset($contextid);
76
77 //find the context id
78 if ($course = $DB->get_record('course', array('id' => $courseid))) {
79 $modinfo =& get_fast_modinfo($course);
80
81 if (!isset($modinfo->instances[$componentname])) {
82 $modinfo->instances[$componentname] = array();
83 }
84
85 foreach ($modinfo->instances[$componentname] as $modinstanceid=>$cm) {
86 if ($modinstanceid==$instanceid) {
87 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
88 break;
89 }
90 }
91 }
92
93 if (empty($context)) {
94 //this shouldnt happen. something bad is going on.
95 rss_error('rsserror');
96 }
97
c05ccf3f
AD
98 //make sure that $CFG->siteguest is set
99 if (empty($CFG->siteguest)) {
100 if (!$guestid = $DB->get_field('user', 'id', array('username'=>'guest', 'mnethostid'=>$CFG->mnet_localhost_id))) {
101 // guest does not exist yet, weird
102 rss_error('rsserror');
103 }
104 set_config('siteguest', $guestid);
105 }
c61daed0
AD
106 $guesttoken = rss_get_token($CFG->siteguest);
107
108 //change forum to mod_forum (for example)
109 $componentname = 'mod_'.$componentname;
110
111 $url = $PAGE->url;
112 $url->set_slashargument("/{$context->id}/$guesttoken/$componentname/$instanceid/rss.xml");
113
114 //redirect to the 2.0 rss URL
115 redirect($url);
116} else {
117 // Authenticate the user from the token
118 $userid = rss_get_userid_from_token($token);
119 if (!$userid) {
120 rss_error('rsserrorauth');
121 }
fcce139a 122}
b111858d 123
fcce139a 124$user = get_complete_user_data('id', $userid);
690aa229 125session_set_user($user); //for login and capability checks
fcce139a 126
b111858d 127// Check the context actually exists
52cede73
AD
128list($context, $course, $cm) = get_context_info_array($contextid);
129
fcce139a 130if (!$context) {
b111858d 131 rss_error();
fcce139a
AD
132}
133$PAGE->set_context($context);
134
52cede73
AD
135try {
136 $autologinguest = true;
137 $setwantsurltome = true;
138 $preventredirect = true;
139 require_login($course, $autologinguest, $cm, $setwantsurltome, $preventredirect);
140} catch (Exception $e) {
c61daed0
AD
141 if (isguestuser()) {
142 rss_error('rsserrorguest');
143 } else {
144 rss_error('rsserrorauth');
145 }
52cede73 146}
b111858d
MD
147
148// Work out which component in Moodle we want (from the frankenstyle name)
690aa229
AD
149$componentdir = get_component_directory($componentname);
150list($type, $plugin) = normalize_component($componentname);
fcce139a 151
b111858d
MD
152
153// Call the component to check/update the feed and tell us the path to the cached file
fcce139a 154$pathname = null;
e417be4c 155
aa60291e
AD
156if (file_exists($componentdir)) {
157 require_once("$componentdir/rsslib.php");
158 $functionname = $plugin.'_rss_get_feed';
159
160 if (function_exists($functionname)) {
b111858d
MD
161 // $pathname will be null if there was a problem (eg user doesn't have the necessary capabilities)
162 // NOTE:the component providing the feed must do its own capability checks and security
274f9840 163 $pathname = $functionname($context, $args);
aa60291e 164 }
fcce139a 165}
6f5e0852 166
b111858d
MD
167
168// Check that file exists
fcce139a 169if (empty($pathname) || !file_exists($pathname)) {
b111858d 170 rss_error();
fcce139a 171}
8adcb49f 172
b111858d
MD
173// Send the RSS file to the user!
174send_file($pathname, 'rss.xml', 3600); // Cached by browsers for 1 hour
8adcb49f 175
e7f927a0 176
b111858d
MD
177/*
178 * Sends an error formatted as an rss file and then dies
179 */
180function rss_error($error='rsserror', $filename='rss.xml', $lifetime=0) {
181 send_file(rss_geterrorxmlfile($error), $filename, $lifetime, false, true);
182 exit;
fcce139a 183}