rss.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. require_once 'database.php';
  3. $db = new Database ();
  4. $db->connect ();
  5. // How should the feeds be sorted?
  6. $rss_sort = isset ($_GET['sort']) ? strtoupper ($_GET['sort']) : NULL;
  7. // Retrieve the posts
  8. switch ($rss_sort)
  9. {
  10. case 'NEW': $posts = $db->get_new_posts (); break;
  11. default: $posts = $db->get_hot_posts ();
  12. }
  13. /***** Create the XML (RSS) feed *****/
  14. $rss = new SimpleXMLElement ('<rss/>');
  15. $rss->addAttribute ("version", "2.0");
  16. // <channel> info
  17. $channel = $rss->addChild ('channel');
  18. $channel->addChild ('title', 'freepost');
  19. $channel->addChild ('description', '');
  20. $channel->addChild ('link', 'https://freepo.st');
  21. $channel->addChild ('lastBuildDate', date ('r'));
  22. // Add our posts to the feed
  23. foreach ($posts as $post)
  24. {
  25. $item = $channel->addChild ('item');
  26. // The link of the the freepost submission
  27. $freepost_link = 'https://freepo.st/post/' . $post['hashId'];
  28. /* Link submitted by the user.
  29. * If no URL was posted (only title/text), link to freepo.st
  30. */
  31. $link = strlen ($post['link']) > 0 ? $post['link'] : $freepost_link;
  32. // Short description with username and comments count
  33. $description = 'by ' . $post['username'] . ' — ' . $post['vote'] . ' votes, <a href="' . $freepost_link . '">' . ($post['commentsCount'] > 0 ? $post['commentsCount'] . ' comments' : 'discuss') . '</a>';
  34. // Add post text if any
  35. if (strlen ($post['text']) > 0)
  36. {
  37. // Cut text at 1024 chars
  38. $description .= '<p>' . substr ($post['text'], 0, 1024);
  39. // Add a [Read More] link if some text has been cut
  40. if (strlen ($post['text']) > 1024)
  41. $description .= '... [<a href="' . $freepost_link . '">Read More</a>]';
  42. $description .= '</p>';
  43. }
  44. // 'r' » RFC 2822 formatted date (Example: Thu, 21 Dec 2000 16:01:07 +0200)
  45. $date = date ('r', strtotime ($post['created']));
  46. /**
  47. * It's recommended that you provide the guid, and if possible make it a
  48. * permalink. This enables aggregators to not repeat items, even if there
  49. * have been editing changes.
  50. */
  51. $item->addChild ('guid', $post['hashId']);
  52. /**
  53. * Optional. If set to true, the reader may assume that it is a permalink
  54. * to the item (a url that points to the full item described by the <item>
  55. * element). The default value is true. If set to false, the guid may not
  56. * be assumed to be a url.
  57. */
  58. $item->addChild ('isPermaLink', false);
  59. $item->addChild ('title', htmlspecialchars ($post['title']));
  60. $item->addChild ('description', htmlspecialchars ($description));
  61. $item->addChild ('link', $link);
  62. $item->addChild ('freepostLink', $freepost_link);
  63. $item->addChild ('pubDate', $date);
  64. $item->addChild ('author', htmlspecialchars ($post['username']));
  65. }
  66. // Output RSS
  67. header ('Content-Type: application/rss+xml; charset=UTF-8');
  68. echo $rss->asXML ();