Skip to content

Commit

Permalink
MDL-46463 core_rss: coding style cleanup in /rss
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjdavis committed Jul 23, 2014
1 parent ec36faf commit 1502bf7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 58 deletions.
64 changes: 29 additions & 35 deletions rss/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/


/** 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 */
/** NO_DEBUG_DISPLAY - bool, Disable moodle debug and error messages. Set to false to see any errors during RSS generation */
define('NO_DEBUG_DISPLAY', true);

/** NO_MOODLE_COOKIES - bool, Disable the use of sessions/cookies - we recreate $USER for every call. */
Expand All @@ -38,21 +37,19 @@
require_once($CFG->libdir.'/filelib.php');
require_once($CFG->libdir.'/rsslib.php');

// RSS feeds must be enabled site-wide
// RSS feeds must be enabled site-wide.
if (empty($CFG->enablerssfeeds)) {
debugging('DISABLED (admin variables)');
rss_error();
}


// All the arguments are in the path
// All the arguments are in the path.
$relativepath = get_file_argument();
if (!$relativepath) {
rss_error();
}


// Extract relative path components into variables
// Extract relative path components into variables.
$args = explode('/', trim($relativepath, '/'));
if (count($args) < 5) {
rss_error();
Expand All @@ -62,73 +59,73 @@
$token = clean_param($args[1], PARAM_ALPHANUM);
$componentname = clean_param($args[2], PARAM_FILE);

//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)
// Check if they have requested a 1.9 RSS feed.
// If token is an int it is a user id (1.9 request).
// If token contains any letters it is 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
if ($token === "$inttoken") {
// They have 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
// 1.9 URL puts course id where the context id is in 2.0 URLs.
$courseid = $contextid;
unset($contextid);

//find the context id
// Find the context id.
if ($course = $DB->get_record('course', array('id' => $courseid))) {
$modinfo = get_fast_modinfo($course);

foreach ($modinfo->get_instances_of($componentname) as $modinstanceid=>$cm) {
if ($modinstanceid==$instanceid) {
foreach ($modinfo->get_instances_of($componentname) as $modinstanceid => $cm) {
if ($modinstanceid == $instanceid) {
$context = context_module::instance($cm->id, IGNORE_MISSING);
break;
}
}
}

if (empty($context)) {
//this shouldnt happen. something bad is going on.
// This shouldnt happen. something bad is going on.
rss_error('rsserror');
}

//make sure that $CFG->siteguest is set
// 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
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);
}
$guesttoken = rss_get_token($CFG->siteguest);

//change forum to mod_forum (for example)
// 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 to the 2.0 rss URL.
redirect($url);
} else {
// Authenticate the user from the token
// Authenticate the user from the token.
$userid = rss_get_userid_from_token($token);
if (!$userid) {
rss_error('rsserrorauth');
}
}

// Check the context actually exists
// Check the context actually exists.
list($context, $course, $cm) = get_context_info_array($contextid);

$PAGE->set_context($context);

$user = get_complete_user_data('id', $userid);

// let enrol plugins deal with new enrolments if necessary
// Let enrol plugins deal with new enrolments if necessary.
enrol_check_plugins($user);

\core\session\manager::set_user($user); //for login and capability checks
\core\session\manager::set_user($user); // For login and capability checks.

try {
$autologinguest = true;
Expand All @@ -143,21 +140,20 @@
}
}

// Work out which component in Moodle we want (from the frankenstyle name)
// Work out which component in Moodle we want (from the frankenstyle name).
$componentdir = core_component::get_component_directory($componentname);
list($type, $plugin) = core_component::normalize_component($componentname);


// Call the component to check/update the feed and tell us the path to the cached file
// Call the component to check/update the feed and tell us the path to the cached file.
$pathname = null;

if (file_exists($componentdir)) {
require_once("$componentdir/rsslib.php");
$functionname = $plugin.'_rss_get_feed';

if (function_exists($functionname)) {
// $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
// The $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.
try {
$pathname = $functionname($context, $args);
} catch (Exception $e) {
Expand All @@ -166,15 +162,13 @@
}
}


// Check that file exists
// Check that file exists.
if (empty($pathname) || !file_exists($pathname)) {
rss_error();
}

// Send the RSS file to the user!
send_file($pathname, 'rss.xml', 3600); // Cached by browsers for 1 hour

send_file($pathname, 'rss.xml', 3600); // Cached by browsers for 1 hour.

/**
* Sends an error formatted as an rss file and then exits
Expand Down
1 change: 0 additions & 1 deletion rss/index.html

This file was deleted.

39 changes: 17 additions & 22 deletions rss/renderer.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
<?php
///////////////////////////////////////////////////////////////////////////
// //
// This file is part of Moodle - http://moodle.org/ //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// //
// 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/>. //
// //
///////////////////////////////////////////////////////////////////////////
// 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/>.

/**
* Web service documentation renderer.
Expand All @@ -42,8 +37,8 @@ class core_rss_renderer extends plugin_renderer_base {
public function user_reset_rss_token_confirmation() {
global $OUTPUT, $CFG;
$managetokenurl = $CFG->wwwroot."/user/managetoken.php?sesskey=" . sesskey();
$optionsyes = array('action'=>'resetrsstoken', 'confirm'=>1, 'sesskey'=>sesskey());
$optionsno = array('section'=>'webservicetokens', 'sesskey'=>sesskey());
$optionsyes = array('action' => 'resetrsstoken', 'confirm' => 1, 'sesskey' => sesskey());
$optionsno = array('section' => 'webservicetokens', 'sesskey' => sesskey());
$formcontinue = new single_button(new moodle_url($managetokenurl, $optionsyes), get_string('reset'));
$formcancel = new single_button(new moodle_url($managetokenurl, $optionsno), get_string('cancel'), 'get');
$html = $OUTPUT->confirm(get_string('resettokenconfirmsimple', 'webservice'), $formcontinue, $formcancel);
Expand All @@ -58,7 +53,7 @@ public function user_reset_rss_token_confirmation() {
public function user_rss_token_box($token) {
global $OUTPUT, $CFG;

// display strings
// Display strings.
$stroperation = get_string('operation', 'webservice');
$strtoken = get_string('key', 'webservice');

Expand Down

0 comments on commit 1502bf7

Please sign in to comment.