From c61daed045db6e49de7aa160f8e446fef9a0e1b8 Mon Sep 17 00:00:00 2001
From: Andrew Davis <andrew@affinitysoftware.net>
Date: Tue, 5 Oct 2010 03:30:01 +0000
Subject: [PATCH] rss MDL-24509 added support for 1.9 rss feed requests

---
 rss/file.php | 61 +++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 56 insertions(+), 5 deletions(-)

diff --git a/rss/file.php b/rss/file.php
index bb4dcfa64cfdf..20ed87c6e5849 100644
--- a/rss/file.php
+++ b/rss/file.php
@@ -61,11 +61,58 @@
 $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)
+$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))) {
+        $modinfo =& get_fast_modinfo($course);
+
+        if (!isset($modinfo->instances[$componentname])) {
+            $modinfo->instances[$componentname] = array();
+        }
+        
+        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');
+    }
+
+    //calling isguestuser() just to make sure that $CFG->siteguest is set
+    isguestuser($token);
+    $guesttoken = rss_get_token($CFG->siteguest);
 
-// Authenticate the user from the token
-$userid = rss_get_userid_from_token($token);
-if (!$userid) {
-    rss_error('rsserrorauth');
+    //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');
+    }
 }
 
 $user = get_complete_user_data('id', $userid);
@@ -85,7 +132,11 @@
     $preventredirect = true;
     require_login($course, $autologinguest, $cm, $setwantsurltome, $preventredirect);
 } catch (Exception $e) {
-    rss_error('rsserrorauth');
+    if (isguestuser()) {
+        rss_error('rsserrorguest');
+    } else {
+        rss_error('rsserrorauth');
+    }
 }
 
 // Work out which component in Moodle we want (from the frankenstyle name)