rss MDL-24509 added support for 1.9 rss feed requests
[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
98 //calling isguestuser() just to make sure that $CFG->siteguest is set
99 isguestuser($token);
100 $guesttoken = rss_get_token($CFG->siteguest);
101
102 //change forum to mod_forum (for example)
103 $componentname = 'mod_'.$componentname;
104
105 $url = $PAGE->url;
106 $url->set_slashargument("/{$context->id}/$guesttoken/$componentname/$instanceid/rss.xml");
107
108 //redirect to the 2.0 rss URL
109 redirect($url);
110} else {
111 // Authenticate the user from the token
112 $userid = rss_get_userid_from_token($token);
113 if (!$userid) {
114 rss_error('rsserrorauth');
115 }
fcce139a 116}
b111858d 117
fcce139a 118$user = get_complete_user_data('id', $userid);
690aa229 119session_set_user($user); //for login and capability checks
fcce139a 120
b111858d 121// Check the context actually exists
52cede73
AD
122list($context, $course, $cm) = get_context_info_array($contextid);
123
fcce139a 124if (!$context) {
b111858d 125 rss_error();
fcce139a
AD
126}
127$PAGE->set_context($context);
128
52cede73
AD
129try {
130 $autologinguest = true;
131 $setwantsurltome = true;
132 $preventredirect = true;
133 require_login($course, $autologinguest, $cm, $setwantsurltome, $preventredirect);
134} catch (Exception $e) {
c61daed0
AD
135 if (isguestuser()) {
136 rss_error('rsserrorguest');
137 } else {
138 rss_error('rsserrorauth');
139 }
52cede73 140}
b111858d
MD
141
142// Work out which component in Moodle we want (from the frankenstyle name)
690aa229
AD
143$componentdir = get_component_directory($componentname);
144list($type, $plugin) = normalize_component($componentname);
fcce139a 145
b111858d
MD
146
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)) {
b111858d
MD
155 // $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
274f9840 157 $pathname = $functionname($context, $args);
aa60291e 158 }
fcce139a 159}
6f5e0852 160
b111858d
MD
161
162// Check that file exists
fcce139a 163if (empty($pathname) || !file_exists($pathname)) {
b111858d 164 rss_error();
fcce139a 165}
8adcb49f 166
b111858d
MD
167// Send the RSS file to the user!
168send_file($pathname, 'rss.xml', 3600); // Cached by browsers for 1 hour
8adcb49f 169
e7f927a0 170
b111858d
MD
171/*
172 * Sends an error formatted as an rss file and then dies
173 */
174function rss_error($error='rsserror', $filename='rss.xml', $lifetime=0) {
175 send_file(rss_geterrorxmlfile($error), $filename, $lifetime, false, true);
176 exit;
fcce139a 177}