redditimgur.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. class RedditImgur extends Plugin {
  3. private $link;
  4. private $host;
  5. function about() {
  6. return array(1.0,
  7. "Inline image links in Reddit RSS feeds",
  8. "fox");
  9. }
  10. function init($host) {
  11. $this->link = $host->get_link();
  12. $this->host = $host;
  13. $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
  14. }
  15. function hook_article_filter($article) {
  16. if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
  17. $doc = new DOMDocument();
  18. @$doc->loadHTML($article["content"]);
  19. if ($doc) {
  20. $xpath = new DOMXPath($doc);
  21. $entries = $xpath->query('(//a[@href]|//img[@src])');
  22. foreach ($entries as $entry) {
  23. if ($entry->hasAttribute("href")) {
  24. if (preg_match("/\.(jpg|jpeg|gif|png)$/i", $entry->getAttribute("href"))) {
  25. $img = $doc->createElement('img');
  26. $img->setAttribute("src", $entry->getAttribute("href"));
  27. $entry->parentNode->replaceChild($img, $entry);
  28. }
  29. }
  30. // remove tiny thumbnails
  31. if ($entry->hasAttribute("src")) {
  32. if ($entry->parentNode && $entry->parentNode->parentNode) {
  33. $entry->parentNode->parentNode->removeChild($entry->parentNode);
  34. }
  35. }
  36. }
  37. $node = $doc->getElementsByTagName('body')->item(0);
  38. if ($node) {
  39. $article["content"] = $doc->saveXML($node, LIBXML_NOEMPTYTAG);
  40. }
  41. }
  42. }
  43. return $article;
  44. }
  45. }
  46. ?>