ID: 3abb4f6c7e5a6c71b18a5d48261127c87be0d6e0
74 lines
—
2K —
View raw
| <?php
require_once 'database.php';
$db = new Database ();
$db->connect ();
// How should the feeds be sorted?
$rss_sort = isset ($_GET['sort']) ? strtoupper ($_GET['sort']) : NULL;
// Retrieve the posts
switch ($rss_sort)
{
case 'NEW': $posts = $db->get_new_posts (); break;
default: $posts = $db->get_hot_posts ();
}
/***** Create the XML (RSS) feed *****/
$rss = new SimpleXMLElement ('<rss/>');
$rss->addAttribute ("version", "2.0");
// <channel> info
$channel = $rss->addChild ('channel');
$channel->addChild ('title', 'freepost');
$channel->addChild ('description', '');
$channel->addChild ('link', 'https://freepo.st');
$channel->addChild ('lastBuildDate', date ('r'));
// Add our posts to the feed
foreach ($posts as $post)
{
$item = $channel->addChild ('item');
// The link of the the freepost submission
$freepost_link = 'https://freepo.st/post/' . $post['hashId'];
/* Link submitted by the user.
* If no URL was posted (only title/text), link to freepo.st
*/
$link = strlen ($post['link']) > 0 ? $post['link'] : $freepost_link;
// Short description with username and comments count
$description = 'by ' . $post['username'] . ' — ' . $post['vote'] . ' votes, <a href="' . $freepost_link . '">' . ($post['commentsCount'] > 0 ? $post['commentsCount'] . ' comments' : 'discuss') . '</a>';
// Add post text if any
if (strlen ($post['text']) > 0)
{
// Cut text at 1024 chars
$description .= '<p>' . substr ($post['text'], 0, 1024);
// Add a [Read More] link if some text has been cut
if (strlen ($post['text']) > 1024)
$description .= '... [<a href="' . $freepost_link . '">Read More</a>]';
$description .= '</p>';
}
// 'r' » RFC 2822 formatted date (Example: Thu, 21 Dec 2000 16:01:07 +0200)
$date = date ('r', strtotime ($post['created']));
$item->addChild ('title', htmlspecialchars ($post['title']));
$item->addChild ('description', htmlspecialchars ($description));
$item->addChild ('link', $link);
$item->addChild ('freepostLink', $freepost_link);
$item->addChild ('pubDate', $date);
$item->addChild ('author', htmlspecialchars ($post['username']));
}
// Output RSS
header ('Content-Type: application/rss+xml; charset=UTF-8');
echo $rss->asXML ();
|