init.php 3.8 KB

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