[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/rss/ -> file.php (source)

   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    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
  28   */
  29  
  30  /** NO_DEBUG_DISPLAY - bool, Disable moodle debug and error messages. Set to false to see any errors during RSS generation */
  31  define('NO_DEBUG_DISPLAY', true);
  32  
  33  /** NO_MOODLE_COOKIES - bool, Disable the use of sessions/cookies - we recreate $USER for every call. */
  34  define('NO_MOODLE_COOKIES', true);
  35  
  36  require_once('../config.php');
  37  require_once($CFG->libdir.'/filelib.php');
  38  require_once($CFG->libdir.'/rsslib.php');
  39  
  40  // RSS feeds must be enabled site-wide.
  41  if (empty($CFG->enablerssfeeds)) {
  42      debugging('DISABLED (admin variables)');
  43      rss_error();
  44  }
  45  
  46  // All the arguments are in the path.
  47  $relativepath = get_file_argument();
  48  if (!$relativepath) {
  49      rss_error();
  50  }
  51  
  52  // Extract relative path components into variables.
  53  $args = explode('/', trim($relativepath, '/'));
  54  if (count($args) < 5) {
  55      rss_error();
  56  }
  57  
  58  $contextid   = (int)$args[0];
  59  $token  = clean_param($args[1], PARAM_ALPHANUM);
  60  $componentname = clean_param($args[2], PARAM_FILE);
  61  
  62  // Check if they have requested a 1.9 RSS feed.
  63  // If token is an int it is a user id (1.9 request).
  64  // If token contains any letters it is a token (2.0 request).
  65  $inttoken = intval($token);
  66  if ($token === "$inttoken") {
  67      // They have requested a feed using a 1.9 url. redirect them to the 2.0 url using the guest account.
  68  
  69      $instanceid  = clean_param($args[3], PARAM_INT);
  70  
  71      // 1.9 URL puts course id where the context id is in 2.0 URLs.
  72      $courseid = $contextid;
  73      unset($contextid);
  74  
  75      // Find the context id.
  76      if ($course = $DB->get_record('course', array('id' => $courseid))) {
  77          $modinfo = get_fast_modinfo($course);
  78  
  79          foreach ($modinfo->get_instances_of($componentname) as $modinstanceid => $cm) {
  80              if ($modinstanceid == $instanceid) {
  81                  $context = context_module::instance($cm->id, IGNORE_MISSING);
  82                  break;
  83              }
  84          }
  85      }
  86  
  87      if (empty($context)) {
  88          // This shouldnt happen. something bad is going on.
  89          rss_error('rsserror');
  90      }
  91  
  92      // Make sure that $CFG->siteguest is set.
  93      if (empty($CFG->siteguest)) {
  94          if (!$guestid = $DB->get_field('user', 'id', array('username' => 'guest', 'mnethostid' => $CFG->mnet_localhost_id))) {
  95              // Guest does not exist yet, weird.
  96              rss_error('rsserror');
  97          }
  98          set_config('siteguest', $guestid);
  99      }
 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      }
 116  }
 117  
 118  // Check the context actually exists.
 119  list($context, $course, $cm) = get_context_info_array($contextid);
 120  
 121  $PAGE->set_context($context);
 122  
 123  $user = get_complete_user_data('id', $userid);
 124  
 125  // Let enrol plugins deal with new enrolments if necessary.
 126  enrol_check_plugins($user);
 127  
 128  \core\session\manager::set_user($user); // For login and capability checks.
 129  
 130  try {
 131      $autologinguest = true;
 132      $setwantsurltome = true;
 133      $preventredirect = true;
 134      require_login($course, $autologinguest, $cm, $setwantsurltome, $preventredirect);
 135  } catch (Exception $e) {
 136      if (isguestuser()) {
 137          rss_error('rsserrorguest');
 138      } else {
 139          rss_error('rsserrorauth');
 140      }
 141  }
 142  
 143  // Work out which component in Moodle we want (from the frankenstyle name).
 144  $componentdir = core_component::get_component_directory($componentname);
 145  list($type, $plugin) = core_component::normalize_component($componentname);
 146  
 147  // Call the component to check/update the feed and tell us the path to the cached file.
 148  $pathname = null;
 149  
 150  if (file_exists($componentdir)) {
 151      require_once("$componentdir/rsslib.php");
 152      $functionname = $plugin.'_rss_get_feed';
 153  
 154      if (function_exists($functionname)) {
 155          // The $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.
 157          try {
 158              $pathname = $functionname($context, $args);
 159          } catch (Exception $e) {
 160              rss_error('rsserror');
 161          }
 162      }
 163  }
 164  
 165  // Check that file exists.
 166  if (empty($pathname) || !file_exists($pathname)) {
 167      rss_error();
 168  }
 169  
 170  // Send the RSS file to the user!
 171  send_file($pathname, 'rss.xml', 3600);   // Cached by browsers for 1 hour.
 172  
 173  /**
 174   * Sends an error formatted as an rss file and then exits
 175   *
 176   * @package core_rss
 177   * @category rss
 178   *
 179   * @param string $error the error type, default is rsserror
 180   * @param string $filename the name of the file to create (NOT USED)
 181   * @param int $lifetime UNSURE (NOT USED)
 182   * @uses exit
 183   */
 184  function rss_error($error='rsserror', $filename='rss.xml', $lifetime=0) {
 185      send_file(rss_geterrorxmlfile($error), $filename, $lifetime, false, true);
 186      exit;
 187  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1