2010-07-20 09:06:35 +00:00
< ? php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* rss / file . php - entry point to serve rss streams
2011-10-14 12:48:00 +02:00
*
2010-07-20 09:06:35 +00:00
* This script simply checks the parameters to construct a $USER
2011-10-14 12:48:00 +02:00
* then finds and calls a function in the relevant component to
2010-07-20 09:06:35 +00:00
* actually check security and create the RSS stream
*
2012-01-11 14:02:53 +08:00
* @ package core_rss
* @ category rss
* @ copyright 1999 onwards Martin Dougiamas { @ link http :// moodle . com }
* @ license http :// www . gnu . org / copyleft / gpl . html GNU GPL v3 or later
2010-07-20 09:06:35 +00:00
*/
2012-01-11 14:02:53 +08:00
/** 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 */
define ( 'NO_DEBUG_DISPLAY' , true );
2010-05-02 11:43:57 +00:00
2012-01-11 14:02:53 +08:00
/** NO_MOODLE_COOKIES - bool, Disable the use of sessions/cookies - we recreate $USER for every call. */
2010-05-02 16:48:41 +00:00
define ( 'NO_MOODLE_COOKIES' , true );
2010-05-02 11:43:57 +00:00
require_once ( '../config.php' );
require_once ( $CFG -> libdir . '/filelib.php' );
require_once ( $CFG -> libdir . '/rsslib.php' );
2010-07-20 09:06:35 +00:00
// RSS feeds must be enabled site-wide
2010-05-02 11:43:57 +00:00
if ( empty ( $CFG -> enablerssfeeds )) {
debugging ( 'DISABLED (admin variables)' );
2010-07-20 09:06:35 +00:00
rss_error ();
2010-05-02 11:43:57 +00:00
}
2010-07-20 09:06:35 +00:00
// All the arguments are in the path
2010-05-02 11:43:57 +00:00
$relativepath = get_file_argument ();
if ( ! $relativepath ) {
2010-07-20 09:06:35 +00:00
rss_error ();
2010-05-02 11:43:57 +00:00
}
2010-07-20 09:06:35 +00:00
// Extract relative path components into variables
2010-05-02 11:43:57 +00:00
$args = explode ( '/' , trim ( $relativepath , '/' ));
if ( count ( $args ) < 5 ) {
2010-07-20 09:06:35 +00:00
rss_error ();
2010-05-02 11:43:57 +00:00
}
$contextid = ( int ) $args [ 0 ];
2010-10-04 06:56:51 +00:00
$token = clean_param ( $args [ 1 ], PARAM_ALPHANUM );
2010-07-19 10:57:52 +00:00
$componentname = clean_param ( $args [ 2 ], PARAM_FILE );
2010-05-02 11:43:57 +00:00
2010-10-05 03:30:01 +00:00
//check if they have requested a 1.9 RSS feed
//if token is an int its a user id (1.9 request)
//if token contains any letters its a token (2.0 request)
$inttoken = intval ( $token );
if ( $token === " $inttoken " ) {
//they've requested a feed using a 1.9 url. redirect them to the 2.0 url using the guest account
$instanceid = clean_param ( $args [ 3 ], PARAM_INT );
//1.9 URL puts course id where the context id is in 2.0 URLs
$courseid = $contextid ;
unset ( $contextid );
//find the context id
if ( $course = $DB -> get_record ( 'course' , array ( 'id' => $courseid ))) {
2012-03-18 18:37:24 +01:00
$modinfo = get_fast_modinfo ( $course );
2010-10-05 03:30:01 +00:00
if ( ! isset ( $modinfo -> instances [ $componentname ])) {
$modinfo -> instances [ $componentname ] = array ();
}
2011-10-14 12:48:00 +02:00
2010-10-05 03:30:01 +00:00
foreach ( $modinfo -> instances [ $componentname ] as $modinstanceid => $cm ) {
if ( $modinstanceid == $instanceid ) {
$context = get_context_instance ( CONTEXT_MODULE , $cm -> id );
break ;
}
}
}
if ( empty ( $context )) {
//this shouldnt happen. something bad is going on.
rss_error ( 'rsserror' );
}
2010-11-19 06:39:00 +00:00
//make sure that $CFG->siteguest is set
if ( empty ( $CFG -> siteguest )) {
if ( ! $guestid = $DB -> get_field ( 'user' , 'id' , array ( 'username' => 'guest' , 'mnethostid' => $CFG -> mnet_localhost_id ))) {
// guest does not exist yet, weird
rss_error ( 'rsserror' );
}
set_config ( 'siteguest' , $guestid );
}
2010-10-05 03:30:01 +00:00
$guesttoken = rss_get_token ( $CFG -> siteguest );
//change forum to mod_forum (for example)
$componentname = 'mod_' . $componentname ;
$url = $PAGE -> url ;
$url -> set_slashargument ( " / { $context -> id } / $guesttoken / $componentname / $instanceid /rss.xml " );
//redirect to the 2.0 rss URL
redirect ( $url );
} else {
// Authenticate the user from the token
$userid = rss_get_userid_from_token ( $token );
if ( ! $userid ) {
rss_error ( 'rsserrorauth' );
}
2010-05-02 11:43:57 +00:00
}
2010-07-20 09:06:35 +00:00
2010-05-02 11:43:57 +00:00
$user = get_complete_user_data ( 'id' , $userid );
2011-10-14 12:48:00 +02:00
// let enrol plugins deal with new enrolments if necessary
enrol_check_plugins ( $user );
2010-07-20 03:47:20 +00:00
session_set_user ( $user ); //for login and capability checks
2010-05-02 11:43:57 +00:00
2010-07-20 09:06:35 +00:00
// Check the context actually exists
2010-10-04 14:42:23 +00:00
list ( $context , $course , $cm ) = get_context_info_array ( $contextid );
2010-05-02 11:43:57 +00:00
if ( ! $context ) {
2010-07-20 09:06:35 +00:00
rss_error ();
2010-05-02 11:43:57 +00:00
}
$PAGE -> set_context ( $context );
2010-10-04 14:42:23 +00:00
try {
$autologinguest = true ;
$setwantsurltome = true ;
$preventredirect = true ;
require_login ( $course , $autologinguest , $cm , $setwantsurltome , $preventredirect );
} catch ( Exception $e ) {
2010-10-05 03:30:01 +00:00
if ( isguestuser ()) {
rss_error ( 'rsserrorguest' );
} else {
rss_error ( 'rsserrorauth' );
}
2010-10-04 14:42:23 +00:00
}
2010-07-20 09:06:35 +00:00
// Work out which component in Moodle we want (from the frankenstyle name)
2010-07-20 03:47:20 +00:00
$componentdir = get_component_directory ( $componentname );
list ( $type , $plugin ) = normalize_component ( $componentname );
2010-05-02 11:43:57 +00:00
2010-07-20 09:06:35 +00:00
// Call the component to check/update the feed and tell us the path to the cached file
2010-05-02 11:43:57 +00:00
$pathname = null ;
2010-07-19 06:59:02 +00:00
2010-07-19 10:57:52 +00:00
if ( file_exists ( $componentdir )) {
require_once ( " $componentdir /rsslib.php " );
$functionname = $plugin . '_rss_get_feed' ;
if ( function_exists ( $functionname )) {
2010-07-20 09:06:35 +00:00
// $pathname will be null if there was a problem (eg user doesn't have the necessary capabilities)
// NOTE:the component providing the feed must do its own capability checks and security
2010-07-20 07:49:34 +00:00
$pathname = $functionname ( $context , $args );
2010-07-19 10:57:52 +00:00
}
2010-05-02 11:43:57 +00:00
}
2009-11-01 16:48:45 +00:00
2010-07-20 09:06:35 +00:00
// Check that file exists
2010-05-02 11:43:57 +00:00
if ( empty ( $pathname ) || ! file_exists ( $pathname )) {
2010-07-20 09:06:35 +00:00
rss_error ();
2010-05-02 11:43:57 +00:00
}
2004-05-02 23:08:19 +00:00
2010-07-20 09:06:35 +00:00
// Send the RSS file to the user!
send_file ( $pathname , 'rss.xml' , 3600 ); // Cached by browsers for 1 hour
2004-05-02 23:08:19 +00:00
2004-12-14 18:57:51 +00:00
2012-01-11 14:02:53 +08:00
/**
* Sends an error formatted as an rss file and then exits
*
* @ package core_rss
* @ category rss
*
* @ param string $error the error type , default is rsserror
* @ param string $filename the name of the file to create ( NOT USED )
* @ param int $lifetime UNSURE ( NOT USED )
* @ uses exit
2010-07-20 09:06:35 +00:00
*/
function rss_error ( $error = 'rsserror' , $filename = 'rss.xml' , $lifetime = 0 ) {
send_file ( rss_geterrorxmlfile ( $error ), $filename , $lifetime , false , true );
exit ;
2010-05-02 11:43:57 +00:00
}