<?php
/*
* Copyright 2012 Douglas Robbins <hide@address.com>
*
* This file is part of Blite, a blogging application, available at
* <http://blite.ca/>.
*
* Blite 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
require('configure.php');
if (!empty($_GET['t']) && $cfg['rsscomments'] == '0') {
exit;
}
elseif (!$cfg['rssposts']) {
exit;
}
$url = $cfg['home'];
$sitename = strip_tags(stripslashes($cfg['sitename']));
$limit = $cfg['rssnumposts'];
if (!empty($_GET['t']) && is_numeric($_GET['t']) && $_GET['t'] > 0) {
$t = $_GET['t'];
}
if (empty($t) && $limit < 1) {
$limit = 10;
}
if (!empty($t)) {
$query_params = array( 't' => 'int' );
$results = db_query("SELECT title,status FROM posts WHERE id='$t'");
$row = db_getdata($results);
if ($row['status'] == 1) {
$articletitle = strip_tags(stripslashes($row['title']));
$chantitle = "$sitename Comments : $articletitle";
$chandesc = "Comments on: $articletitle";
$rssurl = $url . "rss.php?t=$t";
$query_params = array( 't' => 'int' );
$results = db_query("SELECT id, stamp, name, comment FROM comments WHERE postid=? ORDER BY stamp DESC");
}
else {
// May land here if someone is subscribed to comments on a post that has been retracted.
// Need to produce a "This feed is no longer available message?"
exit;
}
}
else {
$query_params = '';
$results = db_query("SELECT id, stamp, title, body FROM posts WHERE status='1' ORDER BY stamp DESC LIMIT $limit");
$chantitle = $sitename . ': Articles';
$chandesc = "Latest Articles from $sitename";
$rssurl = $url . "rss.php";
}
$rss = '';
//while ($row = $results->fetchArray()) {
while ($row = db_getdata($results)) {
$date = gmdate("D, d M Y H:i:s T", $row['stamp']);
$maxdesclength = 250;
// Set the channel lastBuildDate to the date of the most recent item.
if (empty($chandate)) {
$chandate = $date;
}
if (!empty($t)) {
$itemtitle = 'Comment by: ' . strip_tags(stripslashes($row['name']));
$body = stripslashes($row['comment']);
$commentid = $row['id'];
$itemurl = "$url?t=${t}#comment-$commentid";
$maxdesclength = $cfg['rsscomdesclength'];
}
else {
$itemtitle = strip_tags(stripslashes($row['title']));
$body = stripslashes($row['body']);
$id = $row['id'];
$itemurl = "$url?t=$id";
$maxdesclength = $cfg['rssdesclength'];
}
// For blog posts, use the excerpt as description. If an excerpt is not available,
// fall back to first $maxdesclength characters.
if (empty($t)) {
$excerpt = '';
if ( $cfg['excerpts'] == '1' && file_exists($cfg['datadir'] . '/cache/excerpt-' . $id) ) {
$excerpt = file_get_contents($cfg['datadir'] . '/cache/excerpt-' . $id);
}
elseif ($cfg['excerpts'] == '1') {
if ( preg_match ("/\n<!-- -mark- -->\n/", $body, $matches) ) {
$excerpt_mark = $matches[0];
list($excerpt) = explode($excerpt_mark, $body, 2);
}
}
if (empty($excerpt)) {
$excerpt = substr ($body, 0, $maxdesclength);
if (!empty($excerpt)) {
$excerpt .= '...';
}
}
$excerpt = strip_tags($excerpt);
$excerpt = trim(htmlspecialchars($excerpt));
$excerpt = str_replace("\n", " ", $excerpt);
}
if (!empty($excerpt)) {
$itemdesc = $excerpt;
}
// For comments...
else {
$itemdesc = $body;
$itemdesc = str_replace("\n", " ", $itemdesc);
if (!empty($t) && stristr($itemdesc, '<blockquote>') ) {
$itemdesc = preg_replace("/\<blockquote\>(.*)\<\/blockquote\>/", '', $itemdesc);
}
$itemdesc = strip_tags($itemdesc);
if (strlen($itemdesc) > $maxdesclength) {
$itemdesc = substr ($itemdesc, 0, $maxdesclength);
$itemdesc .= '...';
}
$itemdesc = htmlspecialchars($itemdesc);
}
$itemtitle = htmlspecialchars($itemtitle);
if ( (!empty($t) && $cfg['rsscomfullcontent']) || (empty($t) && $cfg['rssfullcontent']) ) {
// Replace any relative links in body text with absolute ones.
if ( stristr($body, 'href=') || stristr($body, 'src=') ) {
$body = absolute_url($body, $url);
}
$itemencoded = "\n<content:encoded><![CDATA[${body}]]></content:encoded>";
}
$rss .= "<item>\n<title>$itemtitle</title>\n<link>$itemurl</link>
<guid isPermaLink=\"true\">$itemurl</guid>\n<pubDate>$date</pubDate>
<description>$itemdesc</description>${itemencoded}\n</item>\n";
}
$rss = str_replace('&', '&', $rss);
$rss = trim(str_replace('"', '"', $rss));
$rss = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\">
<channel>
<title>$chantitle</title>
<link>$url</link>
<description>$chandesc</description>
<lastBuildDate>$chandate</lastBuildDate>
<language>en-us</language>
<atom:link href=\"${rssurl}\" rel=\"self\" type=\"application/rss+xml\" />
$rss
</channel>
</rss>";
header ("Content-Type: text/xml");
echo $rss;
function absolute_url($txt, $base_url) {
// Based on: http://www.howtoforge.com/forums/showthread.php?t=4
$needles = array("href='", "src='", 'href="', 'src="');
$new_txt = '';
if(substr($base_url,-1) != '/') {
$base_url .= '/';
}
$new_base_url = $base_url;
$base_url_parts = parse_url($base_url);
foreach($needles as $needle) {
while($pos = strpos($txt, $needle)) {
$pos += strlen($needle);
if (substr($txt,$pos,7) !== 'http://' && substr($txt,$pos,8) !== 'https://' && substr($txt,$pos,6) !== 'ftp://' && substr($txt,$pos,9) !== 'mailto://') {
if (substr($txt,$pos,1) == '/') {
$new_base_url = $base_url_parts['scheme'].'://'.$base_url_parts['host'];
}
$new_txt .= substr($txt,0,$pos).$new_base_url;
}
else {
$new_txt .= substr($txt,0,$pos);
}
$txt = substr($txt,$pos);
}
$txt = $new_txt.$txt;
$new_txt = '';
}
return $txt;
}
?>