Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/rss/file.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00030 // Disable moodle specific debug messages and any errors in output
00031 define('NO_DEBUG_DISPLAY', true);//comment this out to see any error messages during RSS generation
00032 
00033 // Sessions not used here, we recreate $USER every time we are called
00034 define('NO_MOODLE_COOKIES', true);
00035 
00036 require_once('../config.php');
00037 require_once($CFG->libdir.'/filelib.php');
00038 require_once($CFG->libdir.'/rsslib.php');
00039 
00040 // RSS feeds must be enabled site-wide
00041 if (empty($CFG->enablerssfeeds)) {
00042     debugging('DISABLED (admin variables)');
00043     rss_error();
00044 }
00045 
00046 
00047 // All the arguments are in the path
00048 $relativepath = get_file_argument();
00049 if (!$relativepath) {
00050     rss_error();
00051 }
00052 
00053 
00054 // Extract relative path components into variables
00055 $args = explode('/', trim($relativepath, '/'));
00056 if (count($args) < 5) {
00057     rss_error();
00058 }
00059 
00060 $contextid   = (int)$args[0];
00061 $token  = clean_param($args[1], PARAM_ALPHANUM);
00062 $componentname = clean_param($args[2], PARAM_FILE);
00063 
00064 //check if they have requested a 1.9 RSS feed
00065 //if token is an int its a user id (1.9 request)
00066 //if token contains any letters its a token (2.0 request)
00067 $inttoken = intval($token);
00068 if ($token==="$inttoken") {
00069     //they've requested a feed using a 1.9 url. redirect them to the 2.0 url using the guest account
00070 
00071     $instanceid  = clean_param($args[3], PARAM_INT);
00072 
00073     //1.9 URL puts course id where the context id is in 2.0 URLs
00074     $courseid = $contextid;
00075     unset($contextid);
00076 
00077     //find the context id
00078     if ($course = $DB->get_record('course', array('id' => $courseid))) {
00079         $modinfo =& get_fast_modinfo($course);
00080 
00081         if (!isset($modinfo->instances[$componentname])) {
00082             $modinfo->instances[$componentname] = array();
00083         }
00084 
00085         foreach ($modinfo->instances[$componentname] as $modinstanceid=>$cm) {
00086             if ($modinstanceid==$instanceid) {
00087                 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00088                 break;
00089             }
00090         }
00091     }
00092 
00093     if (empty($context)) {
00094         //this shouldnt happen. something bad is going on.
00095         rss_error('rsserror');
00096     }
00097 
00098     //make sure that $CFG->siteguest is set
00099     if (empty($CFG->siteguest)) {
00100         if (!$guestid = $DB->get_field('user', 'id', array('username'=>'guest', 'mnethostid'=>$CFG->mnet_localhost_id))) {
00101             // guest does not exist yet, weird
00102             rss_error('rsserror');
00103         }
00104         set_config('siteguest', $guestid);
00105     }
00106     $guesttoken = rss_get_token($CFG->siteguest);
00107 
00108     //change forum to mod_forum (for example)
00109     $componentname = 'mod_'.$componentname;
00110 
00111     $url = $PAGE->url;
00112     $url->set_slashargument("/{$context->id}/$guesttoken/$componentname/$instanceid/rss.xml");
00113 
00114     //redirect to the 2.0 rss URL
00115     redirect($url);
00116 } else {
00117     // Authenticate the user from the token
00118     $userid = rss_get_userid_from_token($token);
00119     if (!$userid) {
00120         rss_error('rsserrorauth');
00121     }
00122 }
00123 
00124 $user = get_complete_user_data('id', $userid);
00125 
00126 // let enrol plugins deal with new enrolments if necessary
00127 enrol_check_plugins($user);
00128 
00129 session_set_user($user); //for login and capability checks
00130 
00131 // Check the context actually exists
00132 list($context, $course, $cm) = get_context_info_array($contextid);
00133 
00134 if (!$context) {
00135     rss_error();
00136 }
00137 $PAGE->set_context($context);
00138 
00139 try {
00140     $autologinguest = true;
00141     $setwantsurltome = true;
00142     $preventredirect = true;
00143     require_login($course, $autologinguest, $cm, $setwantsurltome, $preventredirect);
00144 } catch (Exception $e) {
00145     if (isguestuser()) {
00146         rss_error('rsserrorguest');
00147     } else {
00148         rss_error('rsserrorauth');
00149     }
00150 }
00151 
00152 // Work out which component in Moodle we want (from the frankenstyle name)
00153 $componentdir = get_component_directory($componentname);
00154 list($type, $plugin) = normalize_component($componentname);
00155 
00156 
00157 // Call the component to check/update the feed and tell us the path to the cached file
00158 $pathname = null;
00159 
00160 if (file_exists($componentdir)) {
00161     require_once("$componentdir/rsslib.php");
00162     $functionname = $plugin.'_rss_get_feed';
00163 
00164     if (function_exists($functionname)) {
00165         // $pathname will be null if there was a problem (eg user doesn't have the necessary capabilities)
00166         // NOTE:the component providing the feed must do its own capability checks and security
00167         $pathname = $functionname($context, $args);
00168     }
00169 }
00170 
00171 
00172 // Check that file exists
00173 if (empty($pathname) || !file_exists($pathname)) {
00174     rss_error();
00175 }
00176 
00177 // Send the RSS file to the user!
00178 send_file($pathname, 'rss.xml', 3600);   // Cached by browsers for 1 hour
00179 
00180 
00181 /*
00182  * Sends an error formatted as an rss file and then dies
00183  */
00184 function rss_error($error='rsserror', $filename='rss.xml', $lifetime=0) {
00185     send_file(rss_geterrorxmlfile($error), $filename, $lifetime, false, true);
00186     exit;
00187 }
 All Data Structures Namespaces Files Functions Variables Enumerations