phpBB 3.x RSS Hack

Where I work we use phpBB and I was surprised to see version 3 didn’t have built in RSS support. I found a mod, but it contained several files and required modifying several more files. I put together a hack that will pull the latest posts that are approved and non-reported; However, it will keep them unique to a topic. So if the last 5 posts are for a topic called “That is awesome”, it will only show a link to the last post on that topic and will look for the other latest posts from other topics.

It’s configurable with a item_limit (number of posts to pull) and word_limit (how long the description is). However the description is just the title for the last post, so it will most often be “RE: original post title” unless the user changes it when posting. I could of done another query in a loop to the table phpbb_posts and pulled out the corresponding post_text, but didn’t want to have the overhead. Although you could easily modify that in and cache the file for say 5 or 15 minutes. Think of this as a head start to get the feeds from your forum for whatever you plan to do with them. 😉

Create a php file at the root of your forums with this code.

$item_limit = 20;//total rss items to pull
$word_limit = 25;//word limit
$db_connect = mysql_connect($dbhost, $dbuser, $dbpasswd) or die('Unable to connect to database.');
$db_select = mysql_select_db($dbname, $db_connect) or die('Could not set the database.');
header("content-type: text/xml");
$db_record_site_data = mysql_query('SELECT config_name, config_value FROM phpbb_config WHERE config_name = \'script_path\' OR config_name = \'server_name\' OR config_name = \'sitename\' OR config_name = \'site_desc\' ORDER BY config_name');
$counter = 0;
//will get the items in this order: script_path, server_name, site_desc, sitename
while ($row = mysql_fetch_array($db_record_site_data))
if ($counter == 0)
$script_path = $row['config_value'];
else if ($counter == 1)
$site_url = 'http://'.$row['config_value'];
else if ($counter == 2)
$site_description = $row['config_value'];
else if ($counter == 3)
$site_title = $row['config_value'];
//GET THE RECENT TOPICS, show only approved and non-reported topics
$db_record_topic_data = mysql_query('SELECT forum_id, topic_id, topic_last_post_time, topic_title, topic_last_post_subject, topic_last_poster_name, topic_first_post_id, topic_last_post_id FROM phpbb_topics WHERE topic_approved=1 AND topic_reported=0 ORDER BY topic_last_post_time DESC LIMIT ' . $item_limit);
echo '<' . '?xml version="1.0"?' . '>' . "\n";
echo '<rss version="2.0">' . "\n";
echo '<channel>' . "\n";
echo '<title>' . $site_title . '</title>' . "\n";
echo '<link>' . $site_url . $script_path . '</link>' . "\n";
echo '<description>' . $site_description . '</description>' . "\n";
while ($row = mysql_fetch_array($db_record_topic_data))
echo '<item>' . "\n";
echo '<title>' . htmlspecialchars(trim($row['topic_title']), ENT_QUOTES) . '</title>' . "\n";
echo '<description>';
//filter description
$post_text = htmlspecialchars(trim($row['topic_last_post_subject']), ENT_QUOTES);
//word limit
$post_text = str_replace(' ', ' ', $post_text);//get rid of multispaces
$post_word_array = explode(' ', $post_text);
$total_words = count($post_word_array);
$total_words = min($word_limit, $total_words);
for ($x = 0; $x < $total_words; $x++)
echo $post_word_array[$x] . ' ';
if ($total_words > $word_limit)
echo '...';
echo '</description>' . "\n";
//echo '<description>' . substr(, 0, 128) . '</description>';//character limit
echo '<link>' . $site_url . $script_path . 'viewtopic.php?f=' . $row['forum_id'] . '&amp;t=' . $row['topic_id'] . '&amp;p=' . $row['topic_first_post_id'] . '#' . $row['topic_last_post_id'] . '</link>' . "\n";
echo '<pubdate>' . date('r', $row['topic_last_post_time']) . '</pubdate>' . "\n";//RFC822
echo '<author>' . htmlspecialchars(trim($row['topic_last_poster_name']), ENT_QUOTES) . '</author>';
echo '</item>' . "\n";
echo '</channel>' . "\n";
echo '</rss>' . "\n";

Tags: , ,

One Comment to “phpBB 3.x RSS Hack”

  1. -49 · Unknown says:

    Unknown message

Leave a Comment

Comments are reviewed before publishing to prevent spam.