init.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. class Af_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. $owner_uid = $article["owner_uid"];
  17. $force = false;
  18. if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
  19. if (strpos($article["plugin_data"], "redditimgur,$owner_uid:") === FALSE || $force) {
  20. $doc = new DOMDocument();
  21. @$doc->loadHTML($article["content"]);
  22. if ($doc) {
  23. $xpath = new DOMXPath($doc);
  24. $entries = $xpath->query('(//a[@href]|//img[@src])');
  25. $found = false;
  26. foreach ($entries as $entry) {
  27. if ($entry->hasAttribute("href")) {
  28. if (preg_match("/\.(jpg|jpeg|gif|png)$/i", $entry->getAttribute("href"))) {
  29. $img = $doc->createElement('img');
  30. $img->setAttribute("src", $entry->getAttribute("href"));
  31. $br = $doc->createElement('br');
  32. $entry->parentNode->insertBefore($img, $entry);
  33. $entry->parentNode->insertBefore($br, $entry);
  34. $found = true;
  35. }
  36. // links to imgur pages
  37. $matches = array();
  38. if (preg_match("/^http:\/\/imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
  39. $token = $matches[1];
  40. $album_content = fetch_file_contents($entry->getAttribute("href"),
  41. false, false, false, false, 10);
  42. if ($album_content && $token) {
  43. $adoc = new DOMDocument();
  44. @$adoc->loadHTML($album_content);
  45. if ($adoc) {
  46. $axpath = new DOMXPath($adoc);
  47. $aentries = $axpath->query('(//img[@src])');
  48. foreach ($aentries as $aentry) {
  49. if (preg_match("/^http:\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
  50. $img = $doc->createElement('img');
  51. $img->setAttribute("src", $aentry->getAttribute("src"));
  52. $br = $doc->createElement('br');
  53. $entry->parentNode->insertBefore($img, $entry);
  54. $entry->parentNode->insertBefore($br, $entry);
  55. $found = true;
  56. break;
  57. }
  58. }
  59. }
  60. }
  61. }
  62. // linked albums, ffs
  63. if (preg_match("/^http:\/\/imgur.com\/a\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
  64. $album_content = fetch_file_contents($entry->getAttribute("href"),
  65. false, false, false, false, 10);
  66. if ($album_content) {
  67. $adoc = new DOMDocument();
  68. @$adoc->loadHTML($album_content);
  69. if ($adoc) {
  70. $axpath = new DOMXPath($adoc);
  71. $aentries = $axpath->query("//div[@class='image']//a[@href and @class='zoom']");
  72. foreach ($aentries as $aentry) {
  73. $img = $doc->createElement('img');
  74. $img->setAttribute("src", $aentry->getAttribute("href"));
  75. $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
  76. $br = $doc->createElement('br');
  77. $entry->parentNode->insertBefore($img, $entry);
  78. $entry->parentNode->insertBefore($br, $entry);
  79. $found = true;
  80. }
  81. }
  82. }
  83. }
  84. }
  85. // remove tiny thumbnails
  86. if ($entry->hasAttribute("src")) {
  87. if ($entry->parentNode && $entry->parentNode->parentNode) {
  88. $entry->parentNode->parentNode->removeChild($entry->parentNode);
  89. }
  90. }
  91. }
  92. $node = $doc->getElementsByTagName('body')->item(0);
  93. if ($node && $found) {
  94. $article["content"] = $doc->saveXML($node);
  95. if (!$force) $article["plugin_data"] = "redditimgur,$owner_uid:" . $article["plugin_data"];
  96. }
  97. }
  98. } else if (isset($article["stored"]["content"])) {
  99. $article["content"] = $article["stored"]["content"];
  100. }
  101. }
  102. return $article;
  103. }
  104. }
  105. ?>