/** * Get the RSS feed * * This method will output the RSS feed with the last 50 posts to the * browser. */ function rss() { HTTP::set_cache_age(3600); // cache for one hour $threadID = null; $forumID = null; // optionally allow filtering of the forum posts by the url in the format // rss/thread/$ID or rss/forum/$ID if (isset($this->urlParams['ID']) && ($action = $this->urlParams['ID'])) { if (isset($this->urlParams['OtherID']) && ($id = $this->urlParams['OtherID'])) { switch ($action) { case 'forum': $forumID = (int) $id; break; case 'thread': $threadID = (int) $id; } } else { // fallback is that it is the ID of a forum like it was in // previous versions $forumID = (int) $action; } } $data = array('last_created' => null, 'last_id' => null); if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && !isset($_SERVER['HTTP_IF_NONE_MATCH'])) { // just to get the version data.. $available = ForumHolder::new_posts_available($this->ID, $data, null, null, $forumID, $threadID); // No information provided by the client, just return the last posts $rss = new RSSFeed($this->getRecentPosts(50, $forumID, $threadID), $this->Link() . 'rss', sprintf(_t('Forum.RSSFORUMPOSTSTO'), $this->Title), "", "Title", "RSSContent", "RSSAuthor", $data['last_created'], $data['last_id']); return $rss->outputToBrowser(); } else { // Return only new posts, check the request headers! $since = null; $etag = null; if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { // Split the If-Modified-Since (Netscape < v6 gets this wrong) $since = explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE']); // Turn the client request If-Modified-Since into a timestamp $since = @strtotime($since[0]); if (!$since) { $since = null; } } if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && is_numeric($_SERVER['HTTP_IF_NONE_MATCH'])) { $etag = (int) $_SERVER['HTTP_IF_NONE_MATCH']; } if ($available = ForumHolder::new_posts_available($this->ID, $data, $since, $etag, $forumID, $threadID)) { HTTP::register_modification_timestamp($data['last_created']); $rss = new RSSFeed($this->getRecentPosts(50, $forumID, $threadID, $etag), $this->Link() . 'rss', sprintf(_t('Forum.RSSFORUMPOSTSTO'), $this->Title), "", "Title", "RSSContent", "RSSAuthor", $data['last_created'], $data['last_id']); return $rss->outputToBrowser(); } else { if ($data['last_created']) { HTTP::register_modification_timestamp($data['last_created']); } if ($data['last_id']) { HTTP::register_etag($data['last_id']); } // There are no new posts, just output an "304 Not Modified" message HTTP::add_cache_headers(); header('HTTP/1.1 304 Not Modified'); } } exit; }
function testGetNewPostsAvailable() { $fh = $this->objFromFixture("ForumHolder", "fh"); // test last visit. we can assume that these tests have been reloaded in the past 24 hours $data = array(); $this->assertTrue(ForumHolder::new_posts_available($fh->ID, $data, date('Y-m-d H:i:s', mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'))))); // set the last post ID (test the first post - so there should be a post, last post (false)) $fixtureIDs = $this->allFixtureIDs('Post'); $lastPostID = end($fixtureIDs); $this->assertTrue(ForumHolder::new_posts_available($fh->ID, $data, null, 1)); $this->assertFalse(ForumHolder::new_posts_available($fh->ID, $data, null, $lastPostID)); // limit to a specific forum $forum = $this->objFromFixture("Forum", "general"); $this->assertTrue(ForumHolder::new_posts_available($fh->ID, $data, null, null, $forum->ID)); $this->assertFalse(ForumHolder::new_posts_available($fh->ID, $data, null, $lastPostID, $forum->ID)); // limit to a specific thread $thread = $this->objFromFixture("ForumThread", "Thread1"); $this->assertTrue(ForumHolder::new_posts_available($fh->ID, $data, null, null, null, $thread->ID)); $this->assertFalse(ForumHolder::new_posts_available($fh->ID, $data, null, $lastPostID, null, $thread->ID)); }