Skip to content
Snippets Groups Projects
blogfeed.php 5.91 KiB
Newer Older
<?php
/*
 * feed script - outputs an feed. Currently supports both RSS 2.0 and ATOM
 */

// We're outputting raw XML, so want to turn the templating off
$no_template = TRUE;

//include the blog lib, and thus everything else we need
include("../lib/blog/blog.lib.php");

//setup our path and from that get the feed type and usernae
$request = explode('/', substr($_SERVER['PATH_INFO'], 1));
array_shift($request);
array_shift($request);
//default to rss (so old links and existing feed readers still work, would be nice to ditch this at some point)
$feedtype = 'rss';
if ($request[0] == 'rss' or $request[0] == 'atom'){
	$feedtype = array_shift($request);
}
//grab the username
$user = array('username'=> array_shift($request));
//check the user is sane
if(!safeuname($user['username'])){
	error(1,"Invalid username");
}
//check the user exists
$row = $BlogDB->GetRow("SELECT username, id, name, title, description FROM users WHERE username='".$user['username']."' AND enabled=true;");
if(!$row){
	error(1, "No such user");
}
else {
	//fetch the users info from the db
	$user = $row;
	if(substr(dirname($_SERVER['SCRIPT_NAME']), -1)=="/"){
		$user['link'] = "http://".$_SERVER['HTTP_HOST']."/blogs/".$user['username']."/";
		$user['feed'] = "http://".$_SERVER['HTTP_HOST']."/blog/feed/".$feedtype."/".$user['username']."";
	}
	else {
		$user['link'] = "http://".$_SERVER['HTTP_HOST']."/blogs/".$user['username']."/";
		$user['feed'] = "http://".$_SERVER['HTTP_HOST']."/blog/feed/".$feedtype."/".$user['username']."";
	}
}

//check to see if we are only interested in one category
switch(array_shift($request)){
	case "category":
		$extra = " AND category=".(int)array_shift($request);
		break;
	default:
		$extra = "";
}

//grab the first 15 entires
$query = "SELECT subject, body, timestamp, shortsubject FROM entries WHERE user_id=".$user['id'].$extra." ORDER BY timestamp DESC LIMIT 15;";
$result = $BlogDB->GetAll($query);

//if we have relivent entrys fetch them
if (count($result) > 0) {
	$row = array_shift($result);
	//set the publish dates in the required format
	if($feedtype=='rss') {
		$pubdate = date("r",strtotime($row['timestamp']));
	}
	if($feedtype=='atom') {
		//this should be a date type c when we are on php5, untill then, this nasty hack should work.
		$pubdate = preg_replace('/ /', 'T', $row['timestamp']).":00";
	}
	$entries = "";
	//output this entry
	if ($feedtype=='rss') {
		do {
			$entrydate = date("r",strtotime($row['timestamp']));
			$entries .= "\t\t<item>\n";
			$entries .= "\t\t\t<guid>".$user['link']."entry/".$row['shortsubject']."</guid>\n";
			$entries .= "\t\t\t<title>" . $row['subject'] . "</title>\n";
			$entries .= "\t\t\t<description><![CDATA[" . substr(strip_tags($row['body']),0,150) . " [...]]]></description>\n";
			$entries .= "\t\t\t<link>".$user['link']."entry/".$row['shortsubject']."</link>\n";
			$entries .= "\t\t\t<pubDate>" . $entrydate . "</pubDate>\n";
			$entries .= "\t\t\t<dc:creator>".$user['name']."</dc:creator>\n";
			$entries .= "\t\t\t<content:encoded><![CDATA[" . str_replace("'", "&#8217;", $row['body']) . "]]></content:encoded>\n";
			$entries .= "\t\t</item>\n";
			//and ever other 
		} while ($row = array_shift($result));
	}
	if($feedtype=='atom') {
		do {
			$entrydate = preg_replace('/ /', 'T', $row['timestamp']).":00";
			$entries .= "\t<entry>\n";
			$entries .= "\t\t<title>".htmlentities($row['subject'])."</title>\n";
			$entries .= "\t\t<link rel=\"alternate\" type=\"text/html\" href=\"".$user['link']."entry/".$row['shortsubject']."\"/>\n";
			$entries .= "\t\t<id>".$user['link']."entry/".$row['shortsubject']."</id>\n";
			$entries .= "\t\t<updated>".$entrydate."</updated>\n";
			$entries .= "\t\t<content type=\"xhtml\" xml:lang=\"en\" xml:base=\"".$user['link']."entry/"."\">\n";
			$entries .= "\t\t<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
			//just passing though the raw body will cause the feed to fail if the code isnt valid, perhaps we should chuck everything though tidy on its way in (or even out) once we're on php5
			$config = array('output-xhtml' => true, 'show-body-only' => true, 'wrap' => false);
			$tidy = new tidy;
			$tidy->parseString($row['body'], $config, 'utf8');
			$tidy->cleanRepair();
			$entries .= $tidy;
			$entries .= "\t\t</div>\n";
			$entries .= "\t\t</content>\n";
			$entries .= "\t</entry>\n";
		} while ($row = array_shift($result));
	}
}

if($feedtype=='rss') {
	//tell the client its xml and utf8 encoded (which it should be)
	header("Content-type: application/rss+xml; charset=utf-8");
	echo "<?xml version=\"1.0\" ?>\n";
?>
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
>
	<channel>
		<title><?php print $user['title']?></title>
		<description><?php print $user['description']?></description>
		<link><?php print $user['link']?></link>
		<pubDate><?php echo $pubdate; ?></pubDate>
		<generator>SUCS Blogs - http://sucs.org/blogs/</generator>
		<language>en</language>
		<?php 
		// add our entrys here
		echo $entries; ?>
	</channel>
</rss>
<?
}
if($feedtype=='atom') {
	//tell the client its xml and utf8 encoded (which it should be)
	header("Content-type: application/atom+xml; charset=utf-8");
	echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
	?>
	<feed xmlns="http://www.w3.org/2005/Atom">
		<title type="text"><?php print $user['title']?></title>
		<subtitle type="text"><?php print $user['description']?></subtitle>
		<updated><?php print $pubdate;?></updated>
		<id><?php print $user['link']?></id>
		<link rel="alternate" type="text/html" hreflang="en" href="<?php print $user['link']?>"/>
		<link rel="self" type="application/atom+xml" href="<?php print $user['feed']?>"/>
		<rights>Copyright (c) <? echo date("Y",strtotime($row['timestamp'])).", ".$user['name'] ?></rights>
		<author>
			<name><? echo $user['name'] ?></name>
		</author>
		<generator uri="http://sucs.org/blogs/" version="1.0">SUCS Blogs</generator>
<?
		echo $entries; 
?>
	</feed>
<?
}
?>