1 (edited by rADo 2004-11-15 23:28)

Topic: RSS 2.0 feed of latest posts

I found extern.php not very useful for people, as most do not want just topics, but last X posts..

Just save this as rss.php:

<?php
/**
  * BLOG:CMS: PHP/MySQL Personal Content Management System 
  * http://blogcms.com/
  * http://forum.blogcms.com/
  *
  * 2003-2004, (c) Radek HULAN 
  * http://hulan.info/
  *
  * This program 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 2
  * of the License, or (at your option) any later version.
**/

$pun_root = './';
@include $pun_root.'config.php';

// If PUN isn't defined, config.php is missing or corrupt
if (!defined('PUN'))
    exit('The file \'config.php\' doesn\'t exist or is corrupt. Please run install.php to install PunBB first.');

// Disable error reporting for uninitialized variables
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Turn off magic_quotes_runtime
set_magic_quotes_runtime(0);


// Load the functions script
require $pun_root.'include/functions.php';
require $pun_root.'include/parser.php';

// Load DB abstraction layer and try to connect
require $pun_root.'include/dblayer/common_db.php';

// Get the forum config
$result = $db->query('SELECT * FROM '.$db->prefix.'config') or error('Unable to fetch forum config', __FILE__, __LINE__, $db->error());
while ($cur_config_item = $db->fetch_row($result))
    $pun_config[$cur_config_item[0]] = $cur_config_item[1];

// Make sure we (guests) have permission to read the forums
$result = $db->query('SELECT g_read_board FROM '.$db->prefix.'groups WHERE g_id=3') or error('Unable to fetch group info', __FILE__, __LINE__, $db->error());
if ($db->result($result) == '0')
    exit('No permission');


// Attempt to load the common language file
@include $pun_root.'lang/'.$pun_config['o_default_lang'].'/common.php';
if (!isset($lang_common)) exit('There is no valid language pack \''.$pun_config['o_default_lang'].'\' installed. Please reinstall a language of that name.');

// parse RSS
ob_start();

// make feed
putHeader();
$result = $db->query(
    "select p.id as id, p.message as message, p.posted as postposted, t.subject as subject ".
    "from ".$db->prefix."posts as p, ".$db->prefix."topics as t ".
    "where p.topic_id=t.id ".
    "order by postposted desc ".
    "limit 0,15") 
    or error('Unable to fetch forum posts', __FILE__, __LINE__, $db->error());;
while ($cur = $db->fetch_assoc($result)) putPost($cur);
putEnd();

// get feed into $feed
$feed = ob_get_contents();
ob_end_clean();

// create ETAG (hash of feed)
$eTag = '"'.md5($feed).'"';
header('Etag: '.$eTag);

// compare Etag to what we got
if ($eTag == $_SERVER['HTTP_IF_NONE_MATCH']) {    
    header("HTTP/1.0 304 Not Modified");
    header('Content-Length: 0');
} else {
    // dump feed
    header ("Content-type: text/xml");
    echo $feed;
}

/* entity to unicode decimal value */
function entity_to_decimal_value($string){
    static $entities_dec = false;
    if (!is_array($entities_dec)) {
        $entities_named       = array(" ","¡","¢","£","¤","¥","¦","§","¨","©","ª","«","¬","­","®","¯","°","±","²","³","´","µ","¶","·","¸","¹","º","»","¼","½","¾","¿","À","Á","Â","Ã","Ä","Å","Æ","Ç","È","É","Ê","Ë","Ì","Í","Î","Ï","Ð","Ñ","Ò","Ó","Ô","Õ","Ö","×","Ø","Ù","Ú","Û","Ü","Ý","Þ","ß","à","á","â","ã","ä","å","æ","ç","è","é","ê","ë","ì","í","î","ï","ð","ñ","ò","ó","ô","õ","ö","÷","ø","ù","ú","û","ü","ý","þ","ÿ","ƒ","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","•","…","?","?","?","?","?","?","?","™","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?",""","&","<",">","Œ","œ","Š","š","Ÿ","ˆ","˜","?","?","?","?","?","?","?","–","—","‘","’","‚","“","”","„","†","‡","‰","‹","›","€","&apos;");
        $entities_decimal     = array(" ","¡","¢","£","¤","¥","¦","§","¨","©","ª","«","¬","­","®","¯","°","±","²","³","´","µ","¶","·","¸","¹","º","»","¼","½","¾","¿","À","Á","Â","Ã","Ä","Å","Æ","Ç","È","É","Ê","Ë","Ì","Í","Î","Ï","Ð","Ñ","Ò","Ó","Ô","Õ","Ö","×","Ø","Ù","Ú","Û","Ü","Ý","Þ","ß","à","á","â","ã","ä","å","æ","ç","è","é","ê","ë","ì","í","î","ï","ð","ñ","ò","ó","ô","õ","ö","÷","ø","ù","ú","û","ü","ý","þ","ÿ","ƒ","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","•","…","?","?","?","?","?","?","?","™","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?",""","&","<",">","Œ","œ","Š","š","Ÿ","ˆ","˜","?","?","?","?","?","?","?","–","—","‘","’","‚","“","”","„","†","‡","‰","‹","›","€","'");
        if (function_exists('array_combine')) 
            $entities_dec=array_combine($entities_named,$entities_decimal);
        else {
            $i=0;
            foreach ($entities_named as $_entities_named) $entities_dec[$_entities_named]=$entities_decimal[$i++];
        }
    }
    return preg_replace( "/&[A-Za-z]+;/", " ", strtr($string,$entities_dec) );
}

function encode_xml($data){
    // line breaks
    $data=str_replace('<br />',"\n",$data);
    // ending html tags into line breaks
    $data=preg_replace("/<\/(pre|ul|li|p|table|tr)>/","\n",$data);
    // remove other html tags
    $data=preg_replace("/<(.*?)>/","",$data);
    // remove multiple newlines
    $data=preg_replace("/\n\n+/","\n\n",$data);
    return entity_to_decimal_value($data);
}

function putHeader() {   
    global $lang_common,$pun_config;
    echo '<'.'?xml version="1.0" encoding="'.$lang_common['lang_encoding'].'"?'.'>'."\n";
    echo "<rss version=\"2.0\">\n";
    echo "<channel>\n";
    echo "<title>".entity_to_decimal_value(htmlspecialchars($pun_config['o_board_title']))."</title>\n";
    echo "<link>".$pun_config['o_base_url']."</link>\n";
    echo "<description>".entity_to_decimal_value(htmlspecialchars($rss_description.' '.$pun_config['o_board_title']))."</description>\n";
    echo "<language>en</language>\n";
    echo "<docs>http://backend.userland.com/rss</docs>\n";
} 

function putPost($cur) {
    global $pun_config;
    echo "<item>\n";
    echo "<title>".entity_to_decimal_value(htmlspecialchars($cur['subject']))."</title>\n";
    $link = $pun_config['o_base_url'].'/viewtopic.php?pid='.strval($cur['id']).'#'.strval($cur['id']);
    echo "<link>".entity_to_decimal_value(htmlspecialchars($link))."</link>\n";
    $data = "Topic: ".parse_message($cur['subject'],0)."\n\nMessage: ".parse_message($cur['message'],0);
    echo "<description>".encode_xml($data)."</description>\n";
    echo "<pubDate>".strval(date("r",$cur['postposted']))."</pubDate>\n";
    echo "</item>\n";
}


function putEnd() {
    echo "</channel>\n";
    echo "</rss>\n";
} 

?>

2 (edited by rADo 2004-11-16 11:26)

Re: RSS 2.0 feed of latest posts

even better, download it here, as this forum will convert unicode decimals characters into their utf-8 equivalents..

http://cvs.sourceforge.net/viewcvs.py/* … hp?rev=1.5

PS: tested on 1.2dev *only*

3

Re: RSS 2.0 feed of latest posts

I have never understood how to use RSS feed ... but one day, maybe ...

Re: RSS 2.0 feed of latest posts

well other websites can parse it to add news to their site or browsers like FF can do stuff with it, go to www.punbb.org and click on the RSS button in the bottom right hand corner in FF to see

5

Re: RSS 2.0 feed of latest posts

i switched to FF 99% now big_smile

If your people come crazy, you will not need to your mind any more.

6

Re: RSS 2.0 feed of latest posts

zaher wrote:

i switched to FF 99% now :D

So you are suffering only 1% of the time.

Bert Garcia - When all you have is a keyboard

Re: RSS 2.0 feed of latest posts

help!! there are not comments!! I don't know what to do with it o.o

8 (edited by cuteseal 2004-11-17 07:16)

Re: RSS 2.0 feed of latest posts

Oh cool! smile

I'm getting this error though:

An error was encountered
Error: Unable to fetch group info.

I don't think there's a table called "groups" in the standard install...

Digital photography news, reviews, discussions and more!
http://www.shuttertalk.com

The online bible for all
http://www.publicbible.com

9

Re: RSS 2.0 feed of latest posts

i've tested this script also with 1.2-dev.

In the rss summary are also the content of posts which are only for special members of my board. i prefer a solution with a http authentication. For public users, the rss feeds should only summarise guest's posts and for members etc which are authenticated with http://user:password@pathtoboard/rss.php all of their permissions.

10

Re: RSS 2.0 feed of latest posts

I tried this with 1.2 beta, but it will only load a blank  page.
An additional feature I'd love to see, btw, is (if possible) an option to get feeds for specific threads.

11

Re: RSS 2.0 feed of latest posts

Made some mods so it works with PunBB 1.2.1

<?php
/**
  * BLOG:CMS: PHP/MySQL Personal Content Management System 
  * http://blogcms.com/
  * http://forum.blogcms.com/
  *
  * 2003-2004, (c) Radek HULAN 
  * http://hulan.cz/
  *
  * Mod by Bert Garcia for PunBB 1.2.1
  * http://nupusi.com
  *
  * This program 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 2
  * of the License, or (at your option) any later version.
**/

define('PUN_ROOT', './');
@include PUN_ROOT.'config.php';

// If PUN isn't defined, config.php is missing or corrupt
if (!defined('PUN'))
    exit('The file \'config.php\' doesn\'t exist or is corrupt. Please run install.php to install PunBB first.');

// Disable error reporting for uninitialized variables
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Turn off magic_quotes_runtime
set_magic_quotes_runtime(0);


// Load the functions script
require PUN_ROOT.'include/functions.php';
require PUN_ROOT.'include/parser.php';

// Load DB abstraction layer and try to connect
require PUN_ROOT.'include/dblayer/common_db.php';

// Get the forum config
$result = $db->query('SELECT * FROM '.$db->prefix.'config') or error('Unable to fetch forum config', __FILE__, __LINE__, $db->error());
while ($cur_config_item = $db->fetch_row($result))
    $pun_config[$cur_config_item[0]] = $cur_config_item[1];

// Make sure we (guests) have permission to read the forums
$result = $db->query('SELECT g_read_board FROM '.$db->prefix.'groups WHERE g_id=3') or error('Unable to fetch group info', __FILE__, __LINE__, $db->error());
if ($db->result($result) == '0')
    exit('No permission');


// Attempt to load the common language file
@include PUN_ROOT.'lang/'.$pun_config['o_default_lang'].'/common.php';
if (!isset($lang_common)) exit('There is no valid language pack \''.$pun_config['o_default_lang'].'\' installed. Please reinstall a language of that name.');

// parse RSS
ob_start();

// make feed
putHeader();
$result = $db->query(
    "select p.id as id, p.message as message, p.posted as postposted, t.subject as subject ".
    "from ".$db->prefix."posts as p, ".$db->prefix."topics as t ".
    "where p.topic_id=t.id ".
    "order by postposted desc ".
    "limit 0,15") 
    or error('Unable to fetch forum posts', __FILE__, __LINE__, $db->error());;
while ($cur = $db->fetch_assoc($result)) putPost($cur);
putEnd();

// get feed into $feed
$feed = ob_get_contents();
ob_end_clean();

// create ETAG (hash of feed)
$eTag = '"'.md5($feed).'"';
header('Etag: '.$eTag);

// compare Etag to what we got
if ($eTag == $_SERVER['HTTP_IF_NONE_MATCH']) {    
    header("HTTP/1.0 304 Not Modified");
    header('Content-Length: 0');
} else {
    // dump feed
    header ("Content-type: text/xml");
    echo $feed;
}

/* entity to unicode decimal value */
function entity_to_decimal_value($string){
    static $entities_dec = false;
    if (!is_array($entities_dec)) {
        $entities_named       = array(" ","¡","¢","£","¤","¥","¦","§","¨","©","ª","«","¬","­","®","¯","°","±","²","³","´","µ","¶","·","¸","¹","º","»","¼","½","¾","¿","À","Á","Â","Ã","Ä","Å","Æ","Ç","È","É","Ê","Ë","Ì","Í","Î","Ï","Ð","Ñ","Ò","Ó","Ô","Õ","Ö","×","Ø","Ù","Ú","Û","Ü","Ý","Þ","ß","à","á","â","ã","ä","å","æ","ç","è","é","ê","ë","ì","í","î","ï","ð","ñ","ò","ó","ô","õ","ö","÷","ø","ù","ú","û","ü","ý","þ","ÿ","ƒ","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","•","…","?","?","?","?","?","?","?","™","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?",""","&","<",">","Œ","œ","Š","š","Ÿ","ˆ","˜","?","?","?","?","?","?","?","–","—","‘","’","‚","“","”","„","†","‡","‰","‹","›","€","&apos;");
        $entities_decimal     = array(" ","¡","¢","£","¤","¥","¦","§","¨","©","ª","«","¬","­","®","¯","°","±","²","³","´","µ","¶","·","¸","¹","º","»","¼","½","¾","¿","À","Á","Â","Ã","Ä","Å","Æ","Ç","È","É"," ","Ë","Ì","Í","Î","Ï","Ð","Ñ","Ò","Ó","Ô","Õ","Ö","×","Ø","Ù","Ú","Û","Ü","Ý","Þ","ß","à","á","â","ã","ä","å","æ","ç","è","é","ê","ë","ì","í","î","ï","ð","ñ","ò","ó","ô","õ","ö","÷","ø","ù","ú","û","ü","ý","þ","ÿ","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?",""","&","<",">","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","'");
        if (function_exists('array_combine')) 
            $entities_dec=array_combine($entities_named,$entities_decimal);
        else {
            $i=0;
            foreach ($entities_named as $_entities_named) $entities_dec[$_entities_named]=$entities_decimal[$i++];
        }
    }
    return preg_replace( "/&[A-Za-z]+;/", " ", strtr($string,$entities_dec) );
}

function encode_xml($data){
    // line breaks
    $data=str_replace('<br(.*?)>',"\n",$data);
    // ending html tags into line breaks
    $data=preg_replace("/<\/(pre|ul|li|p|table|tr)>/","\n",$data);
    // remove other html tags
    $data=preg_replace("/<(.*?)>/","",$data);
    // remove multiple newlines
    $data=preg_replace("/\n\n+/","\n\n",$data);
    return entity_to_decimal_value($data);
}

function putHeader() {   
    global $lang_common,$pun_config;
    echo '<'.'?xml version="1.0" encoding="'.$lang_common['lang_encoding'].'"?'.'>'."\n";
    echo "<rss version=\"2.0\">\n";
    echo "<channel>\n";
    echo "<title>".entity_to_decimal_value(htmlspecialchars($pun_config['o_board_title']))."</title>\n";
    echo "<link>".$pun_config['o_base_url']."</link>\n";
    echo "<description>".entity_to_decimal_value(htmlspecialchars($rss_description.' '.$pun_config['o_board_title']))."</description>\n";
    echo "<language>en</language>\n";
    echo "<docs>http://backend.userland.com/rss</docs>\n";
} 

function putPost($cur) {
    global $pun_config;
    echo "<item>\n";
    echo "<title>".entity_to_decimal_value(htmlspecialchars($cur['subject']))."</title>\n";
    $link = $pun_config['o_base_url'].'/viewtopic.php?pid='.strval($cur['id']).'#'.strval($cur['id']);
    echo "<link>".entity_to_decimal_value(htmlspecialchars($link))."</link>\n";
    $data = "Topic: ".parse_message($cur['subject'],0)."\n\nMessage: ".parse_message($cur['message'],0);
    echo "<description>".encode_xml($data)."</description>\n";
    echo "<pubDate>".strval(date("r",$cur['postposted']))."</pubDate>\n";
    echo "</item>\n";
}


function putEnd() {
    echo "</channel>\n";
    echo "</rss>\n";
} 

?>
Bert Garcia - When all you have is a keyboard

12

Re: RSS 2.0 feed of latest posts

Is there a version for punbb 1.1.5 ?

13

Re: RSS 2.0 feed of latest posts

lebel wrote:

Is there a version for punbb 1.1.5 ?

The above code should work with 1.1.5, don't see why not.

Bert Garcia - When all you have is a keyboard

Re: RSS 2.0 feed of latest posts

i've put this rss.php file in the root forum folder. now what? do i follow the instructions as extern.php? thanks!

MentalSpew.com - Better living through fluid thinking (or something like that) http://www.mentalspew.com

15

Re: RSS 2.0 feed of latest posts

There's an enhanced version of this at this thread:

http://punbb.org/forums/viewtopic.php?id=6586

Bert Garcia - When all you have is a keyboard

Re: RSS 2.0 feed of latest posts

hcgtv wrote:

There's an enhanced version of this at this thread:

http://punbb.org/forums/viewtopic.php?id=6586

thanks hgctv - aren't you also on nucleus?

MentalSpew.com - Better living through fluid thinking (or something like that) http://www.mentalspew.com

Re: RSS 2.0 feed of latest posts

fyi-

installed fine, followed the readme and refreshed the forum and voila - rss! has someone also made a ping plugin to notify sites of new entries?

also, when there are no entries in a forum, you get this error:

The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

End tag was not expected at this location. Error processing resource 'http://www.superx3.com/forums/rss.php?fid=6'. Line 1...

</channel>
--^


here's a live example.
http://www.superx3.com/forums/rss.php?fid=6

MentalSpew.com - Better living through fluid thinking (or something like that) http://www.mentalspew.com

18

Re: RSS 2.0 feed of latest posts

mentalspew wrote:

thanks hgctv - aren't you also on nucleus?

Yeah, I help out on Nucleus and a few other projects.

Bert Garcia - When all you have is a keyboard

19

Re: RSS 2.0 feed of latest posts

Thanks for this script!
A few modifications to make output validate:
line 145: header ("Content-type: text/xml; charset=ISO-8859-1"); // Change accordingly
line 227: commented out the whole line, apparently the <content> tag is no longer used(?)

Re: RSS 2.0 feed of latest posts

For some reason it is not working for me This is the error I get

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in ******\****\rss.php on line 93

Whats the problem I have tried using both code given above. Thanks...

Re: RSS 2.0 feed of latest posts

great mod, thanks alot! I edited out line 93, you had an extra " and something else is in it that i couldnt be bothered to find so i just edited the whole line smile

!

22 (edited by cal 2007-02-28 02:11)

Re: RSS 2.0 feed of latest posts

How do I make this mod only show the first message in every thread when using this call;
http://www.example.com/forum/rss.php?fid=4

I want people to be able to comment the threads, without showing the comments in the RSS (other posts than the first one in every thread).

Could anyone help please?

This is what it looks like right now. RSS-feed at the right of the page: http://www.nolltid.se/
This is the feed:  http://www.nolltid.se/forum/rss.php?fid=4

This is my rss.php file: http://www.nolltid.se/rss.zip

23

Re: RSS 2.0 feed of latest posts

There is an alternative enhanced RSS-feed generator for PunBB:
http://letmeseetheart.tripod.com/files/ … 16.php.txt