<?php
require_once "../config.php";
require_once "rss.conf.php";
mysql_connect($db_host,$db_username,$db_password) or
die("Could not connect to DB server");
mysql_select_db($db_name) or
die("Could not select db");
mysql_query("SET NAMES '".str_replace('-','',$channelCharset)."'");
$data = <<<IDSTR
<?xml version="1.0" encoding="{$channelCharset}" ?>
<rss version="2.0">
<channel>
<title>{$channelTitle}</title>
<link>{$baseUrl}/</link>
<description>{$channelDescription}</description>
<language>{$channelLanguage}</language>
<copyright>{$channelCopyright}</copyright>
<pubDate>{$channelPubDate}</pubDate>
<generator>{$channelGenerator}</generator>
<ttl>{$channelTtl}</ttl>
#items#
</channel>
</rss>
IDSTR;
$item = '
<item>
<title>[#post_author] #forum_name#: #topic_name# </title>
<link>#base_url#/viewtopic.php?pid=#post_number##p#post_number#</link>
<description>#post_content#</description>
<comments>#base_url#/post.php?tid=#topic_number#&qid=#post_number#</comments>
<guid isPermaLink="true">#base_url#/viewtopic.php?pid=#post_number##p#post_number#</guid>
<pubDate>#post_time#</pubDate>
</item>
';
function do_query($condition_sql, $limit, $order) {
global $db_prefix;
$result = mysql_query("
SELECT
p.id AS post_number,
p.message AS post_content,
p.posted AS post_time,
p.poster AS post_author,
t.subject AS topic_name,
t.id AS topic_number,
f.forum_name AS forum_name
FROM {$db_prefix}posts p
LEFT JOIN {$db_prefix}topics t ON t.id = p.topic_id
LEFT JOIN {$db_prefix}forums f ON f.id = t.forum_id
LEFT JOIN {$db_prefix}forum_perms fp ON fp.forum_id = f.id AND fp.group_id = 3
WHERE
( fp.read_forum = 1 OR fp.read_forum IS NULL ) AND t.moved_to IS NULL {$condition_sql}
ORDER BY p.posted {$order}
LIMIT {$limit}
") or die("Mysql error: ".mysql_error());
$posts = array();
while($row = mysql_fetch_assoc($result)) {
$posts[] = $row;
}
return $posts;
}
if ( isset($_GET['fids']) and strlen($_GET['fids']) ) {
$fids = explode(",",@$_GET['fids']);
$fids = array_map("intval",$fids);
}
$posts = array();
$sql = "";
if ( sizeof($fids) ) {
$sql = " AND f.id IN (".implode(",",$fids).")";
}
if ( ! isset($_GET['onlytopics']) ) {
$posts = do_query($sql, $channelSize, 'DESC');
} else {
$result = mysql_query("
SELECT t.id
FROM {$db_prefix}topics t
LEFT JOIN {$db_prefix}forums f ON f.id = t.forum_id
LEFT JOIN {$db_prefix}forum_perms fp ON fp.forum_id = f.id AND fp.group_id = 3
WHERE
( fp.read_forum = 1 OR fp.read_forum IS NULL ) AND t.moved_to IS NULL {$sql}
ORDER BY t.posted DESC
LIMIT {$channelSize}
") or die("mysql error: ".mysql_error());
$tids = array();
while($row = mysql_fetch_assoc($result)) {
$tids[] = $row['id'];
}
foreach($tids as $_ => $tid) {
$sql = " AND t.id = ".$tid;
$local_posts = do_query($sql, 1, 'ASC');
$post = $local_posts[0];
$posts[] = $post;
}
}
$item_data = "";
foreach( $posts as $_ => $iter ) {
$tail = ( $itemDescriptionLength < strlen($iter['post_content']) ) ? "..." : "";
if ('utf-8' == strtolower($channelCharset)) {
mb_internal_encoding('utf-8');
$c = mb_substr($iter['post_content'],0,$itemDescriptionLength).$tail;
} else {
$c = substr($iter['post_content'],0,$itemDescriptionLength).$tail;
}
$c = preg_replace('{(\[quote[^\[]*\])}si',"\n".'\1',$c);
$c = preg_replace('{\[/quote\]}si',"[/quote]\n",$c);
$c = nl2br($c);
$iter['post_content'] = $c;
$iter = array_map("htmlspecialchars",$iter);
$iter['post_content'] = "[ ".$iter['post_author']." ] ".$iter['post_content'];
$iter['post_time'] = gmdate('r',$iter['post_time']);
$item_data = $item_data.
str_replace(
array(
'#post_author',
'#post_time#','#topic_number#','#post_content#',
'#post_number#','#forum_name#','#topic_name#',
'#base_url#'),
array(
$iter['post_author'],
$iter['post_time'],$iter['topic_number'],$iter['post_content'],
$iter['post_number'],$iter['forum_name'],$iter['topic_name'],
$baseUrl),
$item
);
}
$data = str_replace('#items#',$item_data,$data);
header("Content-Type: application/rss+xml; charset={$channelCharset}");
print $data;
?>