diff --git a/blocks/rss_client/block_rss_client.php b/blocks/rss_client/block_rss_client.php
index 0b3b9f7..a433c96 100644
--- a/blocks/rss_client/block_rss_client.php
+++ b/blocks/rss_client/block_rss_client.php
@@ -1,25 +1,21 @@
 <?php //$Id$
 
-/*******************************************************************
-* This file contains one class which defines a block for display on
-* any Moodle page. This block can be configured to display the contents
-* of a remote RSS news feed in your web site.
-*
-* @author Daryl Hawes
-* @version  $Id$
-* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
-* @package base
-******************************************************************/
-
 /**
- * This class is for a block which defines a block for display on
- * any Moodle page.
+ * A block which displays Remote feeds
+ *
+ * @package    rss_client
+ * @version    $Id$
+ * @author     Daryl Hawes, Dan Poltawski <talktodan@gmail.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
  */
- class block_rss_client extends block_base {
+
+require_once($CFG->libdir.'/simplepie/moodle_simplepie.php');
+
+class block_rss_client extends block_base {
 
     function init() {
         $this->title = get_string('feedstitle', 'block_rss_client');
-        $this->version = 2007101511;
+        $this->version = 2009041200;
         $this->cron = 300; /// Set min time between cron executions to 300 secs (5 mins)
     }
 
@@ -31,6 +27,18 @@
         return array('all' => true, 'tag' => false);   // Needs work to make it work on tags MDL-11960
     }
 
+    function instance_allow_multiple() {
+        return true;
+    }
+
+    function has_config() {
+        return true;
+    }
+
+    function instance_allow_config() {
+        return true;
+    }
+
     function specialization() {
         // After the block has been loaded we customize the block's title display
         if (!empty($this->config) && !empty($this->config->title)) {
@@ -43,259 +51,220 @@
     }
 
     function get_content() {
-        global $CFG, $editing, $USER;
-
-        if (!empty($this->page->course)) {
-            $this->courseid = $this->page->course->id;
-        }
-
-    /// When displaying feeds in block, we double $CFG->block_rss_client_timeout
-    /// so those feeds retrieved and cached by the cron() process will have a
-    /// better chance to be used
-        if (!empty($CFG->block_rss_client_timeout)) {
-            $CFG->block_rss_client_timeout *= 2;
-        }
-
-        require_once($CFG->libdir .'/rsslib.php');
+        global $CFG, $DB;
 
-        if($this->content !== NULL) {
+        if ($this->content !== NULL) {
             return $this->content;
         }
 
+        // initalise block content object
         $this->content = new stdClass;
         $this->content->text   = '';
         $this->content->footer = '';
 
         if (empty($this->instance)) {
-            // We're being asked for content without an associated instance
             return $this->content;
         }
 
-        $output = '';
-        $rssid = -1;
-        $display_description = false;
-        if (isset($CFG->block_rss_client_num_entries) && is_numeric($CFG->block_rss_client_num_entries) ) {
-            $shownumentries = intval($CFG->block_rss_client_num_entries);
+        if (empty($this->instance->pinned)) {
+            $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
         } else {
-            $shownumentries = 5; //default to 5 entries is not specified in admin section or instance
+            $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
         }
 
-        if (!empty($this->config)) {
-            if (!empty($this->config->rssid)) {
-                if (is_array($this->config->rssid)) {
-                    $rssidarray = $this->config->rssid;
-                } else {     // Make an array of the single value
-                    $rssidarray = array($this->config->rssid);
-                }
-            }
-            if (!empty($this->config->display_description)) {
-                $display_description = intval($this->config->display_description);
-            }
-            if (!empty($this->config->shownumentries)) {
-                $shownumentries = intval($this->config->shownumentries);
+        if (!isset($this->config)) {
+            // The block has yet to be configured - just display configure link in
+            // the block if user has permission to configure it
+
+            if (has_capability('block/rss_client:manageanyfeeds', $context)) {
+                $this->content->text = $this->get_configure_link_html();
             }
+
+            return $this->content;
         }
 
-        // TODO - temporary hack to get the block context only if it already exists.
-        global $DB;
-        if ($DB->record_exists('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $this->instance->id))) {
-            $context = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
-        } else {
-            $context = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
+        // How many feed items should we display?
+        $maxentries = 5;
+        if ( !empty($this->config->shownumentries) ) {
+            $maxentries = intval($this->config->shownumentries);
+        }elseif( isset($CFG->block_rss_client_num_entries) ) {
+            $maxentries = intval($CFG->block_rss_client_num_entries);
         }
 
-        if (has_capability('block/rss_client:createsharedfeeds', $context)
-                || has_capability('block/rss_client:createprivatefeeds', $context)) {
-
-            $page = $this->page;
-            //if ($page->user_allowed_editing()) { // for SUBMITTERS_ALL_ACCOUNT_HOLDERS we're going to run into trouble later if we show it and then they don't have write access to the page.
-            if (isset($this->config)) {
-                // This instance is configured - show Add/Edit feeds link.
-                $script = $page->url->out(
-                                    array('instanceid' => $this->instance->id,
-                                          'sesskey' => sesskey(),
-                                          'blockaction' => 'config',
-                                          'currentaction' => 'managefeeds',
-                                          'id' => $this->courseid,
-                                          'section' => 'rss'
-                                          ));
-                $output .= '<div class="info"><a title="'. get_string('feedsaddedit', 'block_rss_client') .'" href="'. $script .'">'. get_string('feedsaddedit', 'block_rss_client') .'</a></div>';
-            } else {
-                // This instance has not been configured yet - show configure link?
-                if (has_capability('block/rss_client:manageanyfeeds', $context)) {
-                    $script = $page->url->out(
-                                    array('instanceid' => $this->instance->id,
-                                          'sesskey' => sesskey(),
-                                          'blockaction' => 'config',
-                                          'currentaction' => 'configblock',
-                                          'id' => $this->page->course->id,
-                                          'section' => 'rss'
-                                          ));
-                    $output .= '<div class="info"><a title="'. get_string('feedsconfigurenewinstance', 'block_rss_client') .'" href="'. $script.'">'. get_string('feedsconfigurenewinstance', 'block_rss_client') .'</a></div>';
-                }
-            }
-            //}
+
+        /* ---------------------------------
+         * Begin Normal Display of Block Content
+         * --------------------------------- */
+
+        $output = '';
+
+        // display add/edit feed link at top for 'teachers'
+        if (has_any_capability(array('block/rss_client:createsharedfeeds', 'block/rss_client:createprivatefeeds'), $context)) {
+                $output .= $this->get_addfeed_link_html();
         }
 
-        // Daryl Hawes note: if count of rssidarray is greater than 1 
-        // we should possibly display a drop down menu of selected feed titles
-        // so user can select a single feed to view (similar to RSSFeed)
-        if (!empty($rssidarray)) {
-            $numids = count($rssidarray);
-            $count = 0;
-            foreach ($rssidarray as $rssid) {
-                $output .=  $this->get_rss_by_id($rssid, $display_description, $shownumentries, ($numids > 1) ? true : false);
-                if ($numids > 1 && $count != $numids -1 && !empty($rssfeedstring)) {
-                    $output .= '<hr style="width=:80%" />';
-                }
-                $count ++;
+        if (!empty($this->config->rssid)) {
+            list($rss_ids_sql, $params) = $DB->get_in_or_equal($this->config->rssid);
+
+            $rss_feeds = $DB->get_records_select('block_rss_client', "id $rss_ids_sql", $params);
+
+            $showtitle = false;
+            if (count($rss_feeds) > 1) {
+                // when many feeds show the title for each feed
+                $showtitle = true;
+            }
+
+            foreach($rss_feeds as $feed){
+                $output.= $this->get_feed_html($feed, $maxentries, $showtitle);
             }
         }
 
         $this->content->text = $output;
+
         return $this->content;
     }
 
-    function instance_allow_multiple() {
-        return true;
-    }
+    /**
+     * Returns the html of a feed to be displaed in the block
+     *
+     * @param mixed feedrecord The feed record from the database
+     * @param int maxentries The maximum number of entries to be displayed
+     * @param boolean showtitle Should the feed title be displayed in html
+     * @return string html representing the rss feed content
+     */
+    function get_feed_html($feedrecord, $maxentries, $showtitle){
+        global $CFG;
 
-    function has_config() {
-        return true;
-    }
+        $feed = new moodle_simplepie($feedrecord->url);
 
-    function instance_allow_config() {
-        return true;
-    }
+        // TODO: Test this works as planned..
+        if(isset($CFG->block_rss_client_timeout)){
+            $feed->set_cache_duration($CFG->block_rss_client_timeout*60);
+        }
 
-    /**
-     * @param int $rssid The feed to be displayed
-     * @param bool $display_description Should the description information from the feed be displayed or simply the title?
-     * @param int $shownumentries The maximum number of feed entries to be displayed.
-     * @param bool $showtitle True if the feed title should be displayed above the feed entries.
-     * @return string|NULL
-     */
-    function get_rss_by_id($rssid, $display_description, $shownumentries, $showtitle=false) {
-        global $CFG, $DB;
-        $returnstring = '';
-        $now = time();
-        require_once($CFG->libdir .'/rsslib.php');
-        require_once(MAGPIE_DIR .'rss_fetch.inc');
-        if (!defined('MAGPIE_OUTPUT_ENCODING')) {
-            define('MAGPIE_OUTPUT_ENCODING', 'utf-8');  // see bug 3107
+        if(debugging() && $feed->error()){
+            return '<a href="'. $CFG->wwwroot .'/blocks/rss_client/block_rss_client_error.php?error='.urlencode($feedrecord->url .' Failed with code: '.$feed->error()).'">'.get_string('errorloadingfeed', 'block_rss_client').'</a><br />';
         }
+        //TODO: test behaviour if failed and not debugging
 
-        $rss_record = $DB->get_record('block_rss_client', array('id'=>$rssid));
-        if (isset($rss_record) && isset($rss_record->id)) {
-            // By capturing the output from fetch_rss this way
-            // error messages do not display and clutter up the moodle interface
-            // however, we do lose out on seeing helpful messages like "cache hit", etc.
-            ob_start();
-            $rss = fetch_rss($rss_record->url);
-            $rsserror = ob_get_contents();
-            ob_end_clean();
-
-            if ($rss === false) {
-                if (debugging() && !empty($rsserror)) {
-                    // There was a failure in loading the rss feed, print link to full error text
-                    return '<a href="'. $CFG->wwwroot .'/blocks/rss_client/block_rss_client_error.php?error='. urlencode($rsserror) .'">Error loading a feed.</a><br />'; //Daryl Hawes note: localize this line
+        $r = ''; // return string
+
+        if($this->config->block_rss_client_show_channel_image){
+            if($image = $feed->get_image_url()){
+                $imagetitle = s($feed->get_image_title());
+                $imagelink  = $feed->get_image_link();
+
+                $r.='<div class="image" title="'.$imagetitle.'">'."\n";
+                if($imagelink){
+                    $r.='<a href="'.$imagelink.'">';
                 }
+                $r.='<img src="'.$image.'" alt="'.$imagetitle.'" />'."\n";
+                if($imagelink){
+                    $r.='</a>';
+                }
+                $r.= '</div>';
             }
+        }
 
-            // first we must verify that the rss feed is loaded
-            // by checking $rss and $rss->items exist before using them
-            if (empty($rss) || empty($rss->items)) {
-                return '';
-            }
+        if(empty($feedrecord->preferredtitle)){
+            $feedtitle = $this->format_title($feed->get_title());
+        }else{
+            $feedtitle = $this->format_title($feedrecord->preferredtitle);
+        }
 
-            if ($shownumentries > 0 && $shownumentries < count($rss->items) ) {
-                $rss->items = array_slice($rss->items, 0, $shownumentries);
-            }
+        if($showtitle){
+            $r.='<div class="title">'.$feedtitle.'</div>';
+        }
 
-            if (empty($rss_record->preferredtitle)) {
-                if (isset($rss->channel['title'])) {  // Just in case feed is dead
-                    $feedtitle = $this->format_title($rss->channel['title']);
-                } else {
-                    $feedtitle = '';
-                }
-            } else {
-                $feedtitle = $this->format_title($rss_record->preferredtitle);
-            }
 
-            if (isset($this->config) &&
-                    isset($this->config->block_rss_client_show_channel_image) &&
-                        $this->config->block_rss_client_show_channel_image &&
-                            isset($rss->image) && isset($rss->image['link']) && isset($rss->image['title']) && isset($rss->image['url']) ) {
+        $r.='<ul class="list">'."\n";
 
-                    $rss->image['title'] = s($rss->image['title']);
-                    $returnstring .= "\n".'<div class="image" title="'. $rss->image['title'] .'"><a href="'. $rss->image['link'] .'"><img src="'. $rss->image['url'] .'" alt="'. $rss->image['title'] .'" /></a></div>';
+        $feeditems = $feed->get_items(0, $maxentries);
+        foreach($feeditems as $item){
+            $r.= $this->get_item_html($item);
+        }
 
-            }
+        $r.='</ul>';
 
-            if ($showtitle) {
-                $returnstring .= '<div class="title">'. $feedtitle .'</div>';
-            }
 
-            $formatoptions->para = false;
+        if ($this->config->block_rss_client_show_channel_link) {
 
+            $channellink = str_replace('&', '&amp;', $feed->get_link());
 
-            /// Accessibility: markup as a list.
-            $returnstring .= '<ul class="list">'."\n";
+            if (!empty($channellink)){
+                //FIXME: this means the 'last feed' display wins the block - but
+                //this is exiting behaviour..
+                $this->content->footer = '<a href="'. $channellink.'">'. get_string('clientchannellink', 'block_rss_client') .'</a>';
+            }
+        }
 
-            foreach ($rss->items as $item) {
-                if ($item['title'] == '') {
-                    // no title present, use portion of description
-                    $item['title'] = substr(strip_tags($item['description']), 0, 20) . '...';
-                } else {
-                    $item['title'] = break_up_long_words($item['title'], 30);
-                }
+        if (empty($this->config->title)){
+            //FIXME: this means the 'last feed' display wins the block - but
+            //this is exiting behaviour..
+            $this->title = strip_tags($feedtitle);
+        }
 
-                if ($item['link'] == '') {
-                    $item['link'] = $item['guid'];
-                }
-                $item['title'] = s($item['title']);
+        return $r;
+    }
 
-                $item['link'] = str_replace('&', '&amp;', $item['link']);
+    /**
+     * Returns the html list item of a feed item
+     *
+     * @param mixed item simplepie_item representing the feed item
+     * @return string html li representing the rss feed item
+     */
+    function get_item_html($item){
+        global $COURSE;
 
-                $returnstring .= '<li><div class="link"><a href="'. $item['link'] .'" onclick="this.target=\'_blank\'" >'. $item['title'] . "</a></div>\n";
+        $link        = $item->get_link();
+        $title       = $item->get_title();
+        $description = $item->get_description();
 
-                if ($display_description && !empty($item['description'])) {
-                    $item['description'] = break_up_long_words($item['description'], 30);
-                    $returnstring .= '<div class="description">'.
-                                     format_text($item['description'], FORMAT_MOODLE, $formatoptions, $this->courseid) .
-                                     '</div>';
-                }
-                $returnstring .= "</li>\n";
-            }
-            $returnstring .= "</ul>\n";
 
-            if (!empty($rss->channel['link'])) {
-                $rss->channel['link'] = str_replace('&', '&amp;', $rss->channel['link']);
+        if(empty($title)){
+            // no title present, use portion of description
+            $title = substr(strip_tags($description), 0, 20) . '...';
+        }else{
+            $title = break_up_long_words($title, 30);
+        }
 
-                if (!empty($this->config) && isset($this->config->block_rss_client_show_channel_link) && $this->config->block_rss_client_show_channel_link) {
-                    $this->content->footer =  '<a href="'. $rss->channel['link'] .'">'. get_string('clientchannellink', 'block_rss_client') .'</a>';
-                } 
-                if (!empty($feedtitle) ) {
-                    $feedtitle = '<a href="'. $rss->channel['link'] .'">'. $feedtitle .'</a>';
-                }
-            }
+        if(empty($link)){
+            $link = $item->get_id();
         }
 
-        // if block has no custom title
-        if (empty($this->config) || (!empty($this->config) && empty($this->config->title))) {
-            // if the feed has a title
-            if (!empty($feedtitle) and ($feedtitle != '<a href="'. $rss->channel['link'] .'"></a>')) {
-                // set the block's title to the feed's title
-                $this->title = strip_tags($feedtitle);
-            }
+
+        $r = "<li>\n";
+        $r.= '<div class="link"><a href="'.$link.'" onclick="this.target=\'_blank\'" >'."\n";
+        $r.= s($title);
+        $r.= "</a></div>\n";
+
+        if($this->config->display_description && !empty($description)){
+
+            $description = break_up_long_words($description, 30);
+
+            $formatoptions = new object;
+            $formatoptions->para = false;
+
+            $r.= '<div class="description">';
+            $r.= format_text($description, FORMAT_MOODLE, $formatoptions, $COURSE->id);
+            $r.= '</div>';
         }
-        return $returnstring;
+
+        $r.= '</li>';
+
+        return $r;
     }
 
-    // just strips the title down and adds ... for excessively long titles.
+    /**
+     * Strips a large title to size and adds ... if title too long
+     *
+     * @param string title to shorten
+     * @param int max character length of title
+     * @return string title s() quoted and shortened if necessary
+     */
     function format_title($title,$max=64) {
 
-    /// Loading the textlib singleton instance. We are going to need it.
+        /// Loading the textlib singleton instance. We are going to need it.
         $textlib = textlib_get_instance();
 
         if ($textlib->strlen($title) <= $max) {
@@ -305,49 +274,94 @@
         }
     }
 
-    // cron function, used to refresh all the RSS feeds from Moodle cron
+    /**
+     * Get link html for confifurin the instance of this block
+     *
+     * @return string html of link
+     */
+    function get_configure_link_html(){
+
+        $script = $this->page->url->out(false,
+            array('instanceid' => $this->instance->id,
+            'sesskey' => sesskey(),
+            'blockaction' => 'config',
+            'currentaction' => 'configblock',
+            'id' => $this->page->course->id
+        ));
+
+        $r = '<div class="info"><a title="'. get_string('feedsconfigurenewinstance', 'block_rss_client');
+        $r .= '" href="'. $script.'">'. get_string('feedsconfigurenewinstance', 'block_rss_client') .'</a></div>';
+
+        return $r;
+    }
+
+    /**
+     * Get link html for adding a feed page for the current block instance
+     *
+     * @return string html of link
+     */
+    function get_addfeed_link_html(){
+
+        // This instance is configured - show Add/Edit feeds link.
+        $script = $this->page->url->out(false,
+            array('instanceid' => $this->instance->id,
+            'sesskey' => sesskey(),
+            'blockaction' => 'config',
+            'currentaction' => 'managefeeds',
+            'id' => $this->page->course->id
+        ));
+        $r = '<div class="info"><a title="'. get_string('feedsaddedit', 'block_rss_client') .'" href="'. $script .'">';
+        $r.= get_string('feedsaddedit', 'block_rss_client') .'</a></div>';
+
+        return $r;
+    }
+
+    /**
+     * cron - goes through all feeds and retrieves them with 0 cache duration
+     * set in order to trigger a cache refresh
+     *
+     * @return boolean true if all feeds were retrieved succesfully
+     */
     function cron() {
         global $CFG, $DB;
 
-    /// We are going to measure execution times
+        /// We are going to measure execution times
         $starttime =  microtime();
 
-    /// And we have one initial $status
+        /// And we have one initial $status
         $status = true;
 
-    /// We require some stuff
-        require_once($CFG->libdir .'/rsslib.php');
-        require_once(MAGPIE_DIR .'rss_fetch.inc');
-
-        if (!defined('MAGPIE_OUTPUT_ENCODING')) {
-            define('MAGPIE_OUTPUT_ENCODING', 'utf-8');  // see bug 3107
-        }
-
-    /// Fetch all site feeds.
+        /// Fetch all site feeds.
         $rs = $DB->get_recordset('block_rss_client');
         $counter = 0;
         mtrace('');
         foreach ($rs as $rec) {
             mtrace('    ' . $rec->url . ' ', '');
-        /// Fetch the rss feed, using standard magpie caching
-        /// so feeds will be renewed only if cache has expired
-            // sometimes the cron times out on moodle.org during fetching,
-            // there is a 5s limit in magpie which should work, but does not sometimes :-(
+            /// Fetch the rss feed, using standard simplepie caching
+            /// so feeds will be renewed only if cache has expired
             @set_time_limit(60);
-            if ($rss = fetch_rss($rec->url)) {
-                mtrace ('ok');
-            } else {
+
+            $feed =  new moodle_simplepie();
+            //TODO: test this cache ensures feeds are retrieved every cron
+            $feed->set_cache_duration(0);
+            $feed->set_feed_url($rec->url);
+            $feed->init();
+
+            if ( $feed->error() ){
                 mtrace ('error');
+                mtrace ('SimplePie says:'.$feed->error());
                 $status = false;
+            } else {
+                mtrace ('ok');
             }
             $counter ++;
         }
         $rs->close();
 
-    /// Show times
+        /// Show times
         mtrace($counter . ' feeds refreshed (took ' . microtime_diff($starttime, microtime()) . ' seconds)');
 
-    /// And return $status
+        /// And return $status
         return $status;
     }
 }
diff --git a/blocks/rss_client/block_rss_client_action.php b/blocks/rss_client/block_rss_client_action.php
index 3f727dc..8547422 100644
--- a/blocks/rss_client/block_rss_client_action.php
+++ b/blocks/rss_client/block_rss_client_action.php
@@ -1,76 +1,46 @@
 <?php //$Id$
 
-/*******************************************************************
-* This file contains no classes. It will display a list of existing feeds
-* defined for the site and allow add/edit/delete of site feeds.
-*
-* @author Daryl Hawes
-* @version  $Id$
-* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
-* @package base
-******************************************************************/
-
-require_once('../../config.php');
-require_once($CFG->libdir .'/rsslib.php');
-require_once(MAGPIE_DIR .'rss_fetch.inc');
-
-require_login();
-
-
-if (isset($_SERVER['HTTP_REFERER'])) {
-    $referrer = $_SERVER['HTTP_REFERER'];
-} else {
-    $referrer = $CFG->wwwroot.'/';
+/**
+ * Displays a list of existing feeds defined for and allow
+ * add/edit/delete of feeds.
+ *
+ * @package    rss_client
+ * @version    $Id$
+ * @author     Daryl Hawes, Dan Poltawski <talktodan@gmail.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
+ */
+
+require_once(dirname(__FILE__).'/../../config.php');
+require_once(dirname(__FILE__).'/locallib.php');
+require_once($CFG->libdir.'/simplepie/moodle_simplepie.php');
+
+$action   = optional_param('act', false, PARAM_ALPHA);
+$rssid    = optional_param('rssid', 0, PARAM_INT);
+$courseid = optional_param('courseid', 0, PARAM_INT);
+
+require_login($courseid, false);
+
+if(!confirm_sesskey()){
+    print_error('invalidsesskey');
 }
 
-
-// Ensure that the logged in user is not using the guest account
-if (isguest()) {
-    print_error('noguestpost', 'forum', $referrer);
-}
-
-
-$url = optional_param('url','',PARAM_URL);
-
-if (!empty($url)) {
-    // attempting to replace feed and rss url types with http
-    // it appears that the rss feed validator will validate these url types but magpie will not load them    $url = str_replace ("feed://", "http://", "$url");
-    // Shifting this forward since PARAM_URL rejects these feed types as invalid entries!
-    $url = str_replace ("feed://", "http://", "$url");
-    $url = str_replace ("FEED://", "http://", "$url");
-    $url = str_replace ("rss://", "http://", "$url");
-    $url = str_replace ("RSS://", "http://", "$url");
-}
-
-$act            = optional_param('act', NULL, PARAM_ALPHA);
-$rssid          = optional_param('rssid', NULL, PARAM_INT);
-$id             = optional_param('id', SITEID, PARAM_INT);
-//$url            = clean_param($url, PARAM_URL);
-$preferredtitle = optional_param('preferredtitle', '', PARAM_TEXT);
-$shared         = optional_param('shared', 0, PARAM_INT);
-
-
-if (!defined('MAGPIE_OUTPUT_ENCODING')) {
-    define('MAGPIE_OUTPUT_ENCODING', 'utf-8');  // see bug 3107
+if (!empty($courseid) && $DB->record_exists('course', array('id'=>$courseid))) {
+    $context = get_context_instance(CONTEXT_COURSE, $courseid);
+}else{
+    $context = get_context_instance(CONTEXT_SYSTEM); // default to system context
 }
 
+if(! has_any_capability(array('block/rss_client:manageownfeeds',
+    'block/rss_client:manageanyfeeds',
+    'block/rss_client:createsharedfeeds',
+    'block/rss_client:createprivatefeeds'), $context) ){
 
-if (!empty($id)) {
-    // we get the complete $course object here because print_header assumes this is
-    // a complete object (needed for proper course theme settings)
-    if ($course = $DB->get_record('course', array('id'=>$id))) {
-        $context = get_context_instance(CONTEXT_COURSE, $id);
-    }
-} else {
-    $context = get_context_instance(CONTEXT_SYSTEM);
+    //TODO:port this to 1.9!!
+    print_error('nopermissions', 'error', get_referrer());
 }
 
-
 $straddedit = get_string('feedsaddedit', 'block_rss_client');
-$link = $CFG->wwwroot.'/course/view.php?id='.$id;
-if (empty($course)) {
-    $link = '';
-}
+
 $navlinks = array();
 $navlinks = array(array('name' => get_string('administration'), 'link' => "$CFG->wwwroot/$CFG->admin/index.php", 'type' => 'misc'));
 $navlinks[] = array('name' => get_string('managemodules'), 'link' => null, 'type' => 'misc');
@@ -80,74 +50,101 @@ $navlinks[] = array('name' => get_string('addnew', 'block_rss_client'), 'link' =
 $navigation = build_navigation($navlinks);
 print_header($straddedit, $straddedit, $navigation);
 
-
-if ( !isset($act) ) {
-    rss_display_feeds($id, $USER->id, '', $context);
-    rss_print_form($act, $url, $rssid, $preferredtitle, $shared, $id, $context);
-    print_footer();
-    die();
+$managefeeds = false;
+$rss_record  = false;
+if ($rssid && ($rss_record = $DB->get_record('block_rss_client', array('id'=>$rssid)) )){
+    $managefeeds = ($rss_record->userid == $USER->id && has_capability('block/rss_client:manageownfeeds', $context))
+                || ($rss_record->userid != $USER->id && has_capability('block/rss_client:manageanyfeeds', $context));
 }
 
-if ( isset($rssid) ) {
-    $rss_record = $DB->get_record('block_rss_client', array('id'=>$rssid));
+
+// initial permission/sanity check
+switch($action){
+    case 'delfeed':
+    case 'updfeed':
+    case 'rssedit':
+        if(!$managefeeds){
+            print_error('cannotmakemodification', 'rss_client', get_referrer());
+        }
+        break;
+    case 'view':
+        if(!$rss_record){
+            print_error('couldnotfindfeed', 'block_rss_client', get_referrer(), $rssid);
+        }
+        break;
 }
 
 
-if (isset($rss_record)) {
-    $managefeeds = ($rss_record->userid == $USER->id && has_capability('block/rss_client:manageownfeeds', $context))
-                || ($rss_record->userid != $USER->id && has_capability('block/rss_client:manageanyfeeds', $context));
+// peform our actions
+switch ($action){
+    case 'addfeed':
+        // add a feed
+        block_rss_client_update_feed($context);
+        break;
+
+    case 'updfeed':
+        // edit a feed
+        block_rss_client_update_feed($context, $rss_record);
+        break;
+
+    case 'rssedit':
+        // display edit feed form
+        rss_display_feeds($courseid, $USER->id, $rss_record->id, $context);
+        rss_print_form('rssedit', $rss_record->url, $rss_record->id, $rss_record->preferredtitle, $rss_record->shared, $courseid, $context);
+        break;
+
+    case 'view':
+        // view a feed listing
+        block_rss_client_view_feed($rss_record);
+        break;
+
+    case 'delfeed':
+        // delete a feed
+        block_rss_client_delete_feed($rss_record->id);
+        break;
+
+    default:
+        rss_display_feeds($courseid, $USER->id, '', $context);
+        rss_print_form('', '', '', '', 0, $courseid, $context);
 }
+print_footer();
 
 
-if ($act == 'updfeed') {
 
-    if (!$managefeeds) {
-        print_error('cannotmakemodification', 'rss_client', $referrer);
-    }
+function block_rss_client_view_feed($rss_record){
 
+    $feed = new moodle_simplepie($rss_record->url);
 
-    if (empty($url)) {
-        print_error('urlnotdefinerss');
+    if (!empty($rss_record->preferredtitle)) {
+        $feedtitle = $rss_record->preferredtitle;
+    } else {
+        $feedtitle = $feed->get_title();
     }
 
-    // By capturing the output from fetch_rss this way
-    // error messages do not display and clutter up the moodle interface
-    // however, we do lose out on seeing helpful messages like "cache hit", etc.
-    $message = '';
-    ob_start();
-    $rss = fetch_rss($url);
-    if (debugging()) {
-        $message .= ob_get_contents();
+    print '<table align="center" width="50%" cellspacing="1">'."\n";
+    print '<tr><td colspan="2"><strong>'. $feedtitle .'</strong></td></tr>'."\n";
+    foreach($feed->get_items() as $item){
+        print '<tr><td valign="middle">'."\n";
+        print '<a href="'. $item->get_permalink().'" target="_blank"><strong>'. $item->get_title();
+        print '</strong></a>'."\n";
+        print '</td>'."\n";
+        print '<td>&nbsp;';
+        print '</td></tr>'."\n";
+        print '<tr><td colspan=2><small>';
+        print $item->get_title() .'</small></td></tr>'."\n";
     }
-    ob_end_clean();
-
-    $canaddsharedfeeds = has_capability('block/rss_client:createsharedfeeds', $context);
-
-    $dataobject->id = $rssid;
-    if ($rss === false) {
-        $dataobject->description = '';
-        $dataobject->title = '';
-        $dataobject->preferredtitle = '';
-        $dataobject->shared = 0;
-    } else {
-        $dataobject->description = $rss->channel['description'];
-        $dataobject->title = $rss->channel['title'];
-        $dataobject->preferredtitle = $preferredtitle;
-        if ($shared == 1 && $canaddsharedfeeds) {
-            $dataobject->shared = 1;
-        } else {
-            $dataobject->shared = 0;
-        }
-    }
-    $dataobject->url = $url;
+    print '</table>'."\n";
+}
 
-    $DB->update_record('block_rss_client', $dataobject);
 
-    $message .= '<br />'. get_string('feedupdated', 'block_rss_client');
-    redirect($referrer, $message);
+function block_rss_client_update_feed($context, $existing_record = false){
+    global $USER, $DB;
 
-} else if ($act == 'addfeed' ) {
+    $url            = required_param('url', PARAM_URL);
+    $shared         = optional_param('shared', 0, PARAM_INT);
+    $preferredtitle = optional_param('preferredtitle', '', PARAM_TEXT);
 
+    //TODO: see what these permission checks were like before for updates..
     $canaddprivfeeds = has_capability('block/rss_client:createprivatefeeds', $context);
     $canaddsharedfeeds = has_capability('block/rss_client:createsharedfeeds', $context);
 
@@ -155,15 +152,28 @@ if ($act == 'updfeed') {
         print_error('cannotaddrss', 'error');
     }
 
-    if (empty($url)) {
-        print_error('urlnotdefinerss', 'error');
+    $feed = new moodle_simplepie($url);
+    $message = '';
+
+    if($feed->error()){
+        $message.= '<br />There was an error loading this rss feed. You may want to verify the url you have specified before using it.';
+        $message.= '<br />Error code: '.$feed->error();
+        redirect(get_referrer(), $message);
+    }
+
+    $subscribeurl = $feed->subscribe_url();
 
+    if($subscribeurl !== $url){
+        //FIXME: pretty me up!
+        print_simple_box("Moodle has autodiscovered a feed to subscribe to at: $subscribeurl - please snure this is what you want");
     }
-    $dataobject->userid = $USER->id;
-    $dataobject->description = '';
-    $dataobject->title = '';
-    $dataobject->url = $url;
+
+
+    $dataobject =  new object;
+    $dataobject->url = $subscribeurl;
     $dataobject->preferredtitle = $preferredtitle;
+    $dataobject->description =  $feed->get_description();
+    $dataobject->title =  $feed->get_title();
 
     if ($shared == 1 && $canaddsharedfeeds) {
         $dataobject->shared = 1;
@@ -171,119 +181,40 @@ if ($act == 'updfeed') {
         $dataobject->shared = 0;
     }
 
-    $rssid = $DB->insert_record('block_rss_client', $dataobject);
-
-    // By capturing the output from fetch_rss this way
-    // error messages do not display and clutter up the moodle interface
-    // however, we do lose out on seeing helpful messages like "cache hit", etc.
-    $message = '';
-    ob_start();
-    $rss = fetch_rss($url);
-    if (debugging()) {
-        $message .= ob_get_contents();
-    }
-    ob_end_clean();
-
-    if ($rss === false) {
-        $message .= '<br /><br />There was an error loading this rss feed. You may want to verify the url you have specified before using it.'; //Daryl Hawes note: localize this line
-    } else {
-
-        $dataobject->id = $rssid;
-        if (!empty($rss->channel['description'])) {
-            $dataobject->description = $rss->channel['description'];
+    if($existing_record){
+        $dataobject->id = $existing_record->id;
+        if (!$DB->update_record('block_rss_client', $dataobject)) {
+            print_error('updatersserror', 'error', '', $existing_record->id);
         }
-        if (!empty($rss->channel['title'])) {
-            $dataobject->title = $rss->channel['title'];
+        $message .= '<br />'. get_string('feedupdated', 'block_rss_client');
+    }else{
+        $dataobject->userid = $USER->id;
+        if (!$rssid = $DB->insert_record('block_rss_client', $dataobject) ){
+            print_error('updatersserror', 'error', '', $url);
         }
-        $DB->update_record('block_rss_client', $dataobject);
         $message .= '<br />'. get_string('feedadded', 'block_rss_client');
     }
-    redirect($referrer, $message);
-/*
-        rss_display_feeds($id, $USER->id, '', $context);
-        rss_print_form($act, $dataobject->url, $dataobject->id, $dataobject->preferredtitle, $shared, $id, $context);
-*/
-} else if ( isset($rss_record) && $act == 'rssedit' ) {
-
-    $preferredtitle = $rss_record->preferredtitle;
-    if (empty($preferredtitle)) {
-        $preferredtitle = $rss_record->title;
-    }
-    $url = $rss_record->url;
-    $shared = $rss_record->shared;
-    rss_display_feeds($id, $USER->id, $rssid, $context);
-    rss_print_form($act, $url, $rssid, $preferredtitle, $shared, $id, $context);
 
-} else if ($act == 'delfeed') {
 
-    if (!$managefeeds) {
-        print_error('cannotmakemodification', 'rss_client', $referrer);
-    }
-
-    $file = $CFG->dataroot .'/cache/rsscache/'. $rssid .'.xml';
-    if (file_exists($file)) {
-        unlink($file);
-    }
+    redirect(get_referrer(), $message);
+}
 
-    // echo "DEBUG: act = delfeed"; //debug
+function block_rss_client_delete_feed($rssid){
+    global $DB;
     $DB->delete_records('block_rss_client', array('id'=>$rssid));
 
-    redirect($referrer, get_string('feeddeleted', 'block_rss_client') );
+    redirect(get_referrer(), get_string('feeddeleted', 'block_rss_client') );
+}
 
-} else if ( isset($rss_record) && $act == 'view' ) {
-    //              echo $sql; //debug
-    //              print_object($res); //debug
-    if (!$rss_record->id) {
-        print '<strong>'. get_string('couldnotfindfeed', 'block_rss_client') .': '. $rssid .'</strong>';
+//TODO: get rid of this yukyness  (navigation 2.0 blocks?)
+function get_referrer(){
+    global $CFG;
+    if (isset($_SERVER['HTTP_REFERER'])) {
+        $referrer = $_SERVER['HTTP_REFERER'];
     } else {
-        // By capturing the output from fetch_rss this way
-        // error messages do not display and clutter up the moodle interface
-        // however, we do lose out on seeing helpful messages like "cache hit", etc.
-        ob_start();
-        $rss = fetch_rss($rss_record->url);
-        ob_end_clean();
-
-        if (empty($rss_record->preferredtitle)) {
-            $feedtitle = $rss_record->preferredtitle;
-        } else {
-            $feedtitle =  $rss->channel['title'];
-        }
-        print '<table align="center" width="50%" cellspacing="1">'."\n";
-        print '<tr><td colspan="2"><strong>'. $feedtitle .'</strong></td></tr>'."\n";
-        for($y=0; $y < count($rss->items); $y++) {
-            if ($rss->items[$y]['link'] == '') {
-                $rss->items[$y]['link'] = $rss->items[$y]['guid'];
-            }
-
-            if ($rss->items[$y]['title'] == '') {
-                $rss->items[$y]['title'] = '&gt;&gt;';
-            }
-
-            print '<tr><td valign="middle">'."\n";
-            print '<a href="'. $rss->items[$y]['link'] .'" target="_blank"><strong>'. $rss->items[$y]['title'];
-            print '</strong></a>'."\n";
-            print '</td>'."\n";
-            if (file_exists($CFG->dirroot .'/blog/lib.php')) {
-                //Blog module is installed - provide "blog this" link
-                print '<td align="right">'."\n";
-
-                /// MDL-9291, blog this feature needs further discussion/implementation
-                /// temporarily disabling for now.
-
-                // print '<img src="'. $CFG->pixpath .'/blog/blog.gif" alt="'. get_string('blogthis', 'blog').'" title="'. get_string('blogthis', 'blog') .'" border="0" align="middle" />'."\n";
-                //print '<a href="'. $CFG->wwwroot .'/blog/blogthis.php?userid='. $USER->id .'&act=use&item='. $y .'&rssid='. $rssid .'"><small><strong>'. get_string('blogthis', 'blog') .'</strong></small></a>'."\n";
-            } else {
-                print '<td>&nbsp;';
-            }
-            print '</td></tr>'."\n";
-            print '<tr><td colspan=2><small>';
-            print $rss->items[$y]['description'] .'</small></td></tr>'."\n";
-        }
-        print '</table>'."\n";
+        $referrer = $CFG->wwwroot.'/';
     }
-} else {
-    rss_display_feeds($id, $USER->id, '', $context);
-    rss_print_form($act, $url, $rssid, $preferredtitle, $shared, $id, $context);
+    return $referrer;
 }
-print_footer();
+
 ?>
diff --git a/blocks/rss_client/block_rss_client_error.php b/blocks/rss_client/block_rss_client_error.php
index 14ab2aa..b3a9dc3 100644
--- a/blocks/rss_client/block_rss_client_error.php
+++ b/blocks/rss_client/block_rss_client_error.php
@@ -1,14 +1,14 @@
 <?php //$Id$
 // Print an error page condition
-require_once('../../config.php');
+require_once(dirname(__FILE__).'/../../config.php');
 
 $error = required_param('error', PARAM_CLEAN);
 
-print_header(get_string('error'),
-              get_string('error'),
-              get_string('error') );
+print_header(get_string('error'), get_string('error'), build_navigation(array( 'name'=>get_string('error')) ) );
 
-print clean_text(urldecode($error));
+
+print_heading(get_string('error'));
+print print_simple_box(clean_text(urldecode($error)));
 
 print_footer();
 ?>
diff --git a/blocks/rss_client/config_instance.html b/blocks/rss_client/config_instance.html
index 2fc69e1..42a0858 100644
--- a/blocks/rss_client/config_instance.html
+++ b/blocks/rss_client/config_instance.html
@@ -1,12 +1,9 @@
 <?php // $Id$ //
-require_once($CFG->libdir .'/rsslib.php');
+require_once(dirname(__FILE__).'/locallib.php');
+global $USER;
 
 $id = optional_param('id', SITEID, PARAM_INT);
 
-//create a page object for url_get_full()
-$page = $this->page;
-
-
 /// Print tabs at top
 $currentaction = optional_param('currentaction', '', PARAM_ALPHA);
 if (empty($currentaction) || $currentaction == 'configblock') {
@@ -76,7 +73,6 @@ print_box_start();
                     $selectedarray = array($this->config->rssid);
                 }
             }
-            global $USER;
 
             if ($rssfeeds = $DB->get_records_select('block_rss_client', 'userid = ? OR shared = 1', array($USER->id))) {
                 foreach($rssfeeds as $rssfeed) {
@@ -105,7 +101,7 @@ print_box_start();
                 if (has_capability('block/rss_client:createprivatefeeds', $context)
                         || has_capability('block/rss_client:createsharedfeeds', $context)) {
 
-                    $addrsspage = $page->url->out(array('instanceid' => $this->instance->id,
+                        $addrsspage = $this->page->url->out(false, array('instanceid' => $this->instance->id,
                                                             'sesskey' => sesskey(),
                                                             'blockaction' => 'config',
                                                             'currentaction' => 'managefeeds',
@@ -132,7 +128,7 @@ print_box_start();
 <tr valign="top">
     <td class="label"><?php print_string('clientshowchannellinklabel', 'block_rss_client'); ?></td>
     <td class="value">
-        <?php 
+        <?php
                 if (isset($this->config) && isset($this->config->block_rss_client_show_channel_link)) {
                         $selected = $this->config->block_rss_client_show_channel_link;
                 } else {
@@ -149,7 +145,7 @@ print_box_start();
 <tr valign="top">
     <td class="label"><?php print_string('clientshowimagelabel', 'block_rss_client'); ?></td>
     <td class="value">
-        <?php 
+        <?php
                 if (isset($this->config) && isset($this->config->block_rss_client_show_channel_image)) {
                         $selected = $this->config->block_rss_client_show_channel_image;
                 } else {
diff --git a/blocks/rss_client/config_instance_tabs.php b/blocks/rss_client/config_instance_tabs.php
index a577497..3134327 100644
--- a/blocks/rss_client/config_instance_tabs.php
+++ b/blocks/rss_client/config_instance_tabs.php
@@ -2,7 +2,6 @@
 /// This file to be included so we can assume config.php has already been included.
 /// We also assume that $inactive, $activetab and $currentaction have been set
 
-global $USER;
 $tabs = $row = array();
 
     // TODO - temporary hack to get the block context only if it already exists.
@@ -14,12 +13,12 @@ $tabs = $row = array();
     }
 
 if (has_capability('moodle/site:manageblocks', $context)) {
-    $script = $page->url->out(array('instanceid' => $this->instance->id, 'sesskey' => sesskey(), 'blockaction' => 'config', 'currentaction' => 'configblock', 'id' => $id, 'section' => 'rss'));
+    $script = $this->page->url->out(false, array('instanceid' => $this->instance->id, 'sesskey' => sesskey(), 'blockaction' => 'config', 'currentaction' => 'configblock', 'id' => $id));
     $row[] = new tabobject('configblock', $script,
                 get_string('configblock', 'block_rss_client'));
 }
 
-$script = $page->url->out(array('instanceid' => $this->instance->id, 'sesskey' => sesskey(), 'blockaction' => 'config', 'currentaction' => 'managefeeds', 'id' => $id, 'section' => 'rss'));
+$script = $this->page->url->out(false, array('instanceid' => $this->instance->id, 'sesskey' => sesskey(), 'blockaction' => 'config', 'currentaction' => 'managefeeds', 'id' => $id));
 $row[] = new tabobject('managefeeds', $script,
             get_string('managefeeds', 'block_rss_client'));
 
diff --git a/blocks/rss_client/db/upgrade.php b/blocks/rss_client/db/upgrade.php
index 76eb452..5741ef0 100644
--- a/blocks/rss_client/db/upgrade.php
+++ b/blocks/rss_client/db/upgrade.php
@@ -1,6 +1,6 @@
 <?php  //$Id$
 
-// This file keeps track of upgrades to 
+// This file keeps track of upgrades to
 // the rss_client block
 //
 // Sometimes, changes between versions involve
@@ -26,8 +26,8 @@ function xmldb_block_rss_client_upgrade($oldversion) {
     $dbman = $DB->get_manager();
     $result = true;
 
-/// And upgrade begins here. For each one, you'll need one 
-/// block of code similar to the next one. Please, delete 
+/// And upgrade begins here. For each one, you'll need one
+/// block of code similar to the next one. Please, delete
 /// this comment lines once this file start handling proper
 /// upgrade code.
 
@@ -39,6 +39,13 @@ function xmldb_block_rss_client_upgrade($oldversion) {
         upgrade_block_savepoint($result, 2007080100, 'rss_client');
     }
 
+    if ($result && $oldversion < 2009041200) {
+        // Config variable which is no longer used..
+
+        $result = $result && $DB->delete_records('config', array('name' =>'block_rss_client_submitters'));
+        upgrade_block_savepoint($result, 2009041200, 'rss_client');
+    }
+
     return $result;
 }
 
diff --git a/blocks/rss_client/locallib.php b/blocks/rss_client/locallib.php
new file mode 100644
index 0000000..1ea91d0
--- /dev/null
+++ b/blocks/rss_client/locallib.php
@@ -0,0 +1,177 @@
+<?php //$id$
+/**
+ * Library of shared functions used by the rss_client block only
+ *
+ * @package    rss_client
+ * @version    $Id$
+ * @author     Daryl Hawes, Dan Poltawski <talktodan@gmail.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
+ */
+
+if (!defined('MOODLE_INTERNAL')) {
+    die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page.
+}
+
+/**
+ * @param int $courseid The id of the course the user is currently viewing
+ * @param int $userid We need this to know which feeds the user is allowed to manage
+ * @param int $rssid If present the rss entry matching this id alone will be displayed
+ *            as long as the user is allowed to manage this feed
+ * @param object $context we need the context object to check what the user is allowed to do.
+ */
+function rss_display_feeds($courseid, $userid, $rssid='', $context) {
+    global $USER, $CFG, $DB;
+
+    require_once($CFG->libdir.'/tablelib.php');
+
+    $select = '';
+    $managesharedfeeds = has_capability('block/rss_client:manageanyfeeds', $context);
+    $manageownfeeds = has_capability('block/rss_client:manageownfeeds', $context);
+
+    if ($rssid != '') {
+        $select = 'id = '.$rssid.' AND ';
+    }
+    if ($managesharedfeeds) {
+        $select .= '(userid = '.$userid.' OR shared = 1)';
+    } else if ($manageownfeeds) {
+        $select .= 'userid = '.$userid;
+    }
+
+    $table = new flexible_table('rss-display-feeds');
+
+    $table->define_columns(array('feed', 'actions'));
+    $table->define_headers(array(get_string('feed', 'block_rss_client'), get_string('actions', 'moodle')));
+
+    $table->set_attribute('cellspacing', '0');
+    $table->set_attribute('id', 'rssfeeds');
+    $table->set_attribute('class', 'generaltable generalbox');
+    $table->column_class('feed', 'feed');
+    $table->column_class('actions', 'actions');
+
+    $table->setup();
+
+    $feeds = $DB->get_records_select('block_rss_client', $select, null, $DB->sql_order_by_text('title'));
+
+    if(!empty($feeds)) {
+        foreach($feeds as $feed) {
+
+            if (!empty($feed->preferredtitle)) {
+                $feedtitle = $feed->preferredtitle;
+            } else {
+                $feedtitle =  $feed->title;
+            }
+
+            if ( ($feed->userid == $USER->id && $manageownfeeds)
+                    || ($feed->shared && $managesharedfeeds) ) {
+
+                $feedicons = '<a href="'. $CFG->wwwroot .'/blocks/rss_client/block_rss_client_action.php?courseid='. $courseid .'&amp;act=rssedit&amp;rssid='. $feed->id .'&amp;shared='.$feed->shared.'&amp;sesskey='.sesskey().'">'.
+                             '<img src="'. $CFG->pixpath .'/t/edit.gif" alt="'. get_string('edit').'" title="'. get_string('edit') .'" /></a>&nbsp;'.
+
+                             '<a href="'. $CFG->wwwroot .'/blocks/rss_client/block_rss_client_action.php?courseid='. $courseid .'&amp;act=delfeed&amp;rssid='. $feed->id.'&amp;shared='.$feed->shared.'&amp;sesskey='.sesskey().'"
+                onclick="return confirm(\''. get_string('deletefeedconfirm', 'block_rss_client') .'\');">'.
+                             '<img src="'. $CFG->pixpath .'/t/delete.gif" alt="'. get_string('delete').'" title="'. get_string('delete') .'" /></a>';
+            }
+            else {
+                $feedicons = '';
+            }
+
+            $feedinfo = '
+    <div class="title">
+        <a href="'. $CFG->wwwroot .'/blocks/rss_client/block_rss_client_action.php?courseid='. $courseid .'&amp;act=view&amp;rssid='.$feed->id.'&amp;sesskey='.sesskey() .'">
+        '. $feedtitle .'</a>
+    </div>
+    <div class="url">
+        <a href="'. $feed->url .'">'. $feed->url .'</a>
+    </div>
+    <div class="description">'.$feed->description.'</div>';
+
+            $table->add_data(array($feedinfo, $feedicons));
+        }
+    }
+
+    $table->print_html();
+}
+
+/**
+ * Prints or returns a form for managing rss feed entries.
+ * @param string $act The current action. If "rssedit" then and "update" button is used, otherwise "add" is used.
+ * @param string $url The url of the feed that is being updated or NULL
+ * @param int $rssid The dataabse id of the feed that is being updated or NULL
+ * @param string $preferredtitle The preferred title to display for this feed
+ * @param int $shared Whether this feed is to be shared or not
+ * @param int $courseid The id of the course that is currently being viewed if applicable
+ * @param object $context The context that we will use to check for permissions
+ * @return string Either the form is printed directly and nothing is returned or the form is returned as a string
+ */
+function rss_print_form($act='none', $url='', $rssid='', $preferredtitle='', $shared=0, $courseid='', $context) {
+    global $USER, $CFG, $blockid, $blockaction;
+
+    $stredit = get_string('edit');
+    $stradd = get_string('add');
+    $strupdatefeed = get_string('updatefeed', 'block_rss_client');
+    $straddfeed = get_string('addfeed', 'block_rss_client');
+
+    $returnstring = '';
+
+    $returnstring .= '<form action="'. $CFG->wwwroot .'/blocks/rss_client/block_rss_client_action.php" method="post" id="block_rss">'."\n";
+    print_location_comment(__FILE__,__LINE__);
+    $returnstring .= '<div id="rss_table">'."\n";
+    if ($act == 'rssedit') {
+        $returnstring .= $strupdatefeed;
+    } else {
+        $returnstring .= $straddfeed;
+    }
+
+    $returnstring .= "\n".'<br /><input type="text" size="60" maxlength="256" name="url" value="';
+    if ($act == 'rssedit') {
+        $returnstring .= $url;
+    }
+
+    $returnstring .= '" />'."\n";
+    $returnstring .= '<br />'. get_string('customtitlelabel', 'block_rss_client');
+    $returnstring .= '<br /><input type="text" size="60" maxlength="128" name="preferredtitle" value="';
+
+    if ($act == 'rssedit') {
+        $returnstring .= $preferredtitle;
+    }
+
+    $returnstring .= '" />'."\n";
+
+    if (has_capability('block/rss_client:createsharedfeeds', $context)) {
+        $returnstring .= '<br /><input type="checkbox" name="shared" value="1" ';
+        if ($shared) {
+            $returnstring .= 'checked="checked" ';
+        }
+        $returnstring .= '/> ';
+        $returnstring .= get_string('sharedfeed', 'block_rss_client');
+        $returnstring .= '<br />'."\n";
+    }
+
+    $returnstring .= '<input type="hidden" name="act" value="';
+
+    if ($act == 'rssedit') {
+        $returnstring .= 'updfeed';
+    } else {
+        $returnstring .= 'addfeed';
+    }
+
+    $returnstring .= '" />'."\n";
+    if ($act == 'rssedit') {
+        $returnstring .= '<input type="hidden" name="rssid" value="'. $rssid .'" />'. "\n";
+    }
+
+    $returnstring .= '<input type="hidden" name="id" value="'. $courseid .'" />'."\n";
+    $returnstring .= '<input type="hidden" name="user" value="'. $USER->id .'" />'."\n";
+    $returnstring .= '<input type="hidden" name="sesskey" value="'.sesskey().'" />'."\n";
+    $returnstring .= '<br /><input type="submit" value="';
+    $validatestring = "<a href=\"#\" onclick=\"window.open('http://feedvalidator.org/check.cgi?url='+getElementById('block_rss').elements['url'].value,'validate','width=640,height=480,scrollbars=yes,status=yes,resizable=yes');return true;\">". get_string('validatefeed', 'block_rss_client')."</a>";
+
+    if ($act == 'rssedit') {
+        $returnstring .= $stredit;
+    } else {
+        $returnstring .= $stradd;
+    }
+
+    $returnstring .= '" />&nbsp;'. $validatestring ."\n";
+    print $returnstring;
+}
diff --git a/blocks/rss_client/settings.php b/blocks/rss_client/settings.php
index f18c8c2..3e3c6a3 100644
--- a/blocks/rss_client/settings.php
+++ b/blocks/rss_client/settings.php
@@ -1,21 +1,12 @@
 <?php  //$Id$
 
-require_once($CFG->libdir.'/rsslib.php');
-
 $settings->add(new admin_setting_configtext('block_rss_client_num_entries', get_string('numentries', 'block_rss_client'),
                    get_string('clientnumentries', 'block_rss_client'), 5, PARAM_INT));
 
 $settings->add(new admin_setting_configtext('block_rss_client_timeout', get_string('timeout2', 'block_rss_client'),
                    get_string('timeout', 'block_rss_client'), 30, PARAM_INT));
 
-$options = array (SUBMITTERS_ALL_ACCOUNT_HOLDERS => get_string('everybody'),
-                  SUBMITTERS_ADMIN_ONLY => get_string('administrators'),
-                  SUBMITTERS_ADMIN_AND_TEACHER => get_string('administratorsandteachers'));
-$settings->add(new admin_setting_configselect('block_rss_client_submitters', get_string('submitters2', 'block_rss_client'),
-                   get_string('submitters', 'block_rss_client'), SUBMITTERS_ADMIN_ONLY, $options));
-
-$link ='<a href="'.$CFG->wwwroot.'/blocks/rss_client/block_rss_client_action.php">'.get_string('feedsaddedit', 'block_rss_client').'</a>';
+$link ='<a href="'.$CFG->wwwroot.'/blocks/rss_client/block_rss_client_action.php?sesskey='.sesskey().'">'.get_string('feedsaddedit', 'block_rss_client').'</a>';
 $settings->add(new admin_setting_heading('block_rss_addheading', '', $link));
 
-
 ?>
diff --git a/lang/en_utf8/block_rss_client.php b/lang/en_utf8/block_rss_client.php
index 280fd94..1e55676 100644
--- a/lang/en_utf8/block_rss_client.php
+++ b/lang/en_utf8/block_rss_client.php
@@ -20,6 +20,7 @@ $string['displaydescriptionlabel'] = 'Display each link\'s description?';
 $string['editfeeds'] = 'Edit, subscribe or unsubsribe from RSS/Atom news feeds';
 $string['editnewsfeeds'] = 'Edit news feeds';
 $string['editrssblock'] = 'Edit RSS Headline Block';
+$string['errorloadingfeed'] = 'Error loading feed.';
 $string['feed'] = 'Feed';
 $string['feedadded'] = 'News feed added';
 $string['feeddeleted'] = 'News feed deleted';
diff --git a/lib/blocklib.php b/lib/blocklib.php
index 6df93bb..f9cace2 100644
--- a/lib/blocklib.php
+++ b/lib/blocklib.php
@@ -753,7 +753,7 @@ function block_load_class($blockname) {
     }
 
     require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
-    @include_once($CFG->dirroot.'/blocks/'.$blockname.'/block_'.$blockname.'.php'); // do not throw errors if block code not present
+    include_once($CFG->dirroot.'/blocks/'.$blockname.'/block_'.$blockname.'.php'); // do not throw errors if block code not present
 
     return class_exists($classname);
 }
diff --git a/lib/rsslib.php b/lib/rsslib.php
index ca49068..26fefd3 100644
--- a/lib/rsslib.php
+++ b/lib/rsslib.php
@@ -269,7 +269,7 @@ function rss_add_items($items) {
             $result .= rss_full_tag('link',3,false,$item->link);
             $result .= rss_add_enclosures($item);
             $result .= rss_full_tag('pubDate',3,false,gmdate('D, d M Y H:i:s',$item->pubdate).' GMT');  # MDL-12563
-            //Include the author if exists 
+            //Include the author if exists
             if (isset($item->author)) {
                 //$result .= rss_full_tag('author',3,false,$item->author);
                 //We put it in the description instead because it's more important
@@ -381,19 +381,6 @@ function rss_full_tag($tag,$level=0,$endline=true,$content,$attributes=null) {
     return $st.$co.$et;
 }
 
-//////////////////// LIBRARY FUNCTIONS FOR RSS_CLIENT BLOCK ////////////////
-
-//initialize default config vars for rss_client block if needed
-if (!isset($CFG->block_rss_client_submitters) ) {
-    $CFG->block_rss_client_submitters = 1; //default to admin only
-}
-if (empty($CFG->block_rss_client_num_entries) ) {
-    $CFG->block_rss_client_num_entries = 5; //default to 5 entries per block
-}
-if (!isset($CFG->block_rss_client_timeout) ) {
-    $CFG->block_rss_client_timeout = 30; //default to 30 mins
-}
-
 // Defines for moodle's use of magpierss classes
 define('MAGPIE_DIR', $CFG->libdir.'/magpie/');
 define('MAGPIE_CACHE_DIR', $CFG->dataroot .'/cache/rsscache');
@@ -402,193 +389,6 @@ define('MAGPIE_CACHE_FRESH_ONLY', false); //should be exposed as an admin config
 define('MAGPIE_CACHE_AGE', $CFG->block_rss_client_timeout * 60);
 define('MAGPIE_DEBUG', $CFG->debug); // magpie, like moodle, takes an integer debug
 
-// defines for config var block_rss_client_submitters
-define('SUBMITTERS_ALL_ACCOUNT_HOLDERS', 0);
-define('SUBMITTERS_ADMIN_ONLY', 1);
-define('SUBMITTERS_ADMIN_AND_TEACHER', 2);
-
-/**
- * @param int $courseid The id of the course the user is currently viewing
- * @param int $userid We need this to know which feeds the user is allowed to manage
- * @param int $rssid If present the rss entry matching this id alone will be displayed
- *            as long as the user is allowed to manage this feed
- * @param object $context we need the context object to check what the user is allowed to do.
- */
-function rss_display_feeds($courseid, $userid, $rssid='', $context) {
-    global $USER, $CFG, $DB;
-    global $blogid; //hackish, but if there is a blogid it would be good to preserve it
-
-    require_once($CFG->libdir.'/tablelib.php');
-
-    $select = '';
-    $managesharedfeeds = has_capability('block/rss_client:manageanyfeeds', $context);
-    $manageownfeeds = has_capability('block/rss_client:manageownfeeds', $context);
-
-    if ($rssid != '') {
-        $select = 'id = '.$rssid.' AND ';
-    }
-    if ($managesharedfeeds) {
-        $select .= '(userid = '.$userid.' OR shared = 1)';
-    } else if ($manageownfeeds) {
-        $select .= 'userid = '.$userid;
-    }
-
-    $table = new flexible_table('rss-display-feeds');
-
-    $table->define_columns(array('feed', 'actions'));
-    $table->define_headers(array(get_string('feed', 'block_rss_client'), get_string('actions', 'moodle')));
-
-    $table->set_attribute('cellspacing', '0');
-    $table->set_attribute('id', 'rssfeeds');
-    $table->set_attribute('class', 'generaltable generalbox');
-    $table->column_class('feed', 'feed');
-    $table->column_class('actions', 'actions');
-
-    $table->setup();
-
-    $feeds = $DB->get_records_select('block_rss_client', $select, null, $DB->sql_order_by_text('title'));
-
-    if(!empty($feeds)) {
-        foreach($feeds as $feed) {
-
-            if (!empty($feed->preferredtitle)) {
-                $feedtitle = $feed->preferredtitle;
-            } else {
-                $feedtitle =  $feed->title;
-            }
-
-            if ( ($feed->userid == $USER->id && $manageownfeeds)
-                    || ($feed->shared && $managesharedfeeds) ) {
-
-                $feedicons = '<a href="'. $CFG->wwwroot .'/blocks/rss_client/block_rss_client_action.php?id='. $courseid .'&amp;act=rssedit&amp;rssid='. $feed->id .'&amp;shared='.$feed->shared.'&amp;blogid='. $blogid .'">'.
-                             '<img src="'. $CFG->pixpath .'/t/edit.gif" alt="'. get_string('edit').'" title="'. get_string('edit') .'" /></a>&nbsp;'.
-
-                             '<a href="'. $CFG->wwwroot .'/blocks/rss_client/block_rss_client_action.php?id='. $courseid .'&amp;act=delfeed&amp;rssid='. $feed->id.'&amp;shared='.$feed->shared.'blogid='. $blogid .'"
-                onclick="return confirm(\''. get_string('deletefeedconfirm', 'block_rss_client') .'\');">'.
-                             '<img src="'. $CFG->pixpath .'/t/delete.gif" alt="'. get_string('delete').'" title="'. get_string('delete') .'" /></a>';
-            }
-            else {
-                $feedicons = '';
-            }
-
-            $feedinfo = '
-    <div class="title">
-        <a href="'. $CFG->wwwroot .'/blocks/rss_client/block_rss_client_action.php?id='. $courseid .'&amp;act=view&amp;rssid='.$feed->id .'&amp;blogid='. $blogid .'">
-        '. $feedtitle .'</a>
-    </div>
-    <div class="url">
-        <a href="'. $feed->url .'">'. $feed->url .'</a>
-    </div>
-    <div class="description">'.$feed->description.'</div>';
-
-            $table->add_data(array($feedinfo, $feedicons));
-        }
-    }
-
-    $table->print_html();
-}
-
-
-/**
- * Wrapper function for rss_get_form
- */
-function rss_print_form($act='none', $url='', $rssid='', $preferredtitle='', $shared=0, $courseid='', $context) {
-    print rss_get_form($act, $url, $rssid, $preferredtitle, $shared, $courseid, $context);
-}
-
-
-/**
- * Prints or returns a form for managing rss feed entries.
- *
- * @global object
- * @global object
- * @global int
- * @global string
- * @param string $act The current action. If "rssedit" then and "update" button is used, otherwise "add" is used.
- * @param string $url The url of the feed that is being updated or NULL
- * @param int $rssid The dataabse id of the feed that is being updated or NULL
- * @param string $preferredtitle The preferred title to display for this feed
- * @param int $shared Whether this feed is to be shared or not
- * @param int $courseid The id of the course that is currently being viewed if applicable
- * @param object $context The context that we will use to check for permissions
- * @return string Either the form is printed directly and nothing is returned or the form is returned as a string
- */
-function rss_get_form($act='none', $url='', $rssid='', $preferredtitle='', $shared=0, $courseid='', $context) {
-    global $USER, $CFG, $blockid, $blockaction;
-    global $blogid; //hackish, but if there is a blogid it would be good to preserve it
-
-    $stredit = get_string('edit');
-    $stradd = get_string('add');
-    $strupdatefeed = get_string('updatefeed', 'block_rss_client');
-    $straddfeed = get_string('addfeed', 'block_rss_client');
-
-    $returnstring = '';
-
-    $returnstring .= '<form action="'. $CFG->wwwroot .'/blocks/rss_client/block_rss_client_action.php" method="post" id="block_rss">'."\n";
-    print_location_comment(__FILE__,__LINE__);
-    $returnstring .= '<div id="rss_table">'."\n";
-    if ($act == 'rssedit') {
-        $returnstring .= $strupdatefeed;
-    } else {
-        $returnstring .= $straddfeed;
-    }
-
-    $returnstring .= "\n".'<br /><input type="text" size="60" maxlength="256" name="url" value="';
-    if ($act == 'rssedit') {
-        $returnstring .= $url;
-    }
-
-    $returnstring .= '" />'."\n";
-    $returnstring .= '<br />'. get_string('customtitlelabel', 'block_rss_client');
-    $returnstring .= '<br /><input type="text" size="60" maxlength="128" name="preferredtitle" value="';
-
-    if ($act == 'rssedit') {
-        $returnstring .= $preferredtitle;
-    }
-
-    $returnstring .= '" />'."\n";
-
-    if (has_capability('block/rss_client:createsharedfeeds', $context)) {
-        $returnstring .= '<br /><input type="checkbox" name="shared" value="1" ';
-        if ($shared) {
-            $returnstring .= 'checked="checked" ';
-        }
-        $returnstring .= '/> ';
-        $returnstring .= get_string('sharedfeed', 'block_rss_client');
-        $returnstring .= '<br />'."\n";
-    }
-
-    $returnstring .= '<input type="hidden" name="act" value="';
-
-    if ($act == 'rssedit') {
-        $returnstring .= 'updfeed';
-    } else {
-        $returnstring .= 'addfeed';
-    }
-
-    $returnstring .= '" />'."\n";
-    if ($act == 'rssedit') {
-        $returnstring .= '<input type="hidden" name="rssid" value="'. $rssid .'" />'. "\n";
-    }
-
-    $returnstring .= '<input type="hidden" name="id" value="'. $courseid .'" />'."\n";
-    $returnstring .= '<input type="hidden" name="blogid" value="'. $blogid .'" />'."\n";
-    $returnstring .= '<input type="hidden" name="user" value="'. $USER->id .'" />'."\n";
-    $returnstring .= '<br /><input type="submit" value="';
-    $validatestring = "<a href=\"#\" onclick=\"window.open('http://feedvalidator.org/check.cgi?url='+getElementById('block_rss').elements['url'].value,'validate','width=640,height=480,scrollbars=yes,status=yes,resizable=yes');return true;\">". get_string('validatefeed', 'block_rss_client')."</a>";
-
-    if ($act == 'rssedit') {
-        $returnstring .= $stredit;
-    } else {
-        $returnstring .= $stradd;
-    }
-
-    $returnstring .= '" />&nbsp;'. $validatestring ."\n";
-    // $returnstring .= '</div></form>'."\n"; // Avoiding nested forms... Ugly temporary hack #8922
-    return $returnstring . print_location_comment(__FILE__, __LINE__, true);
-}
-
-
 /**
  * Adds RSS Media Enclosures for "podcasting" by examining links to media files,
  * and attachments which are media files. Please note that the RSS that is
