init.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
  15. $doc = new DOMDocument();
  16. @$doc->loadHTML($article["content"]);
  17. if ($doc) {
  18. $xpath = new DOMXPath($doc);
  19. $entries = $xpath->query('(//a[@href]|//img[@src])');
  20. $found = false;
  21. foreach ($entries as $entry) {
  22. if ($entry->hasAttribute("href")) {
  23. if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
  24. $video = $doc->createElement('video');
  25. $video->setAttribute("autoplay", "1");
  26. $video->setAttribute("loop", "1");
  27. $source = $doc->createElement('source');
  28. $source->setAttribute("src", str_replace(".gifv", ".mp4", $entry->getAttribute("href")));
  29. $source->setAttribute("type", "video/mp4");
  30. $video->appendChild($source);
  31. $br = $doc->createElement('br');
  32. $entry->parentNode->insertBefore($video, $entry);
  33. $entry->parentNode->insertBefore($br, $entry);
  34. $img = $doc->createElement('img');
  35. $img->setAttribute("src",
  36. "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
  37. $entry->parentNode->insertBefore($img, $entry);
  38. $found = true;
  39. }
  40. if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9])?$/i", $entry->getAttribute("href"))) {
  41. $img = $doc->createElement('img');
  42. $img->setAttribute("src", $entry->getAttribute("href"));
  43. $br = $doc->createElement('br');
  44. $entry->parentNode->insertBefore($img, $entry);
  45. $entry->parentNode->insertBefore($br, $entry);
  46. $found = true;
  47. }
  48. // links to imgur pages
  49. $matches = array();
  50. if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
  51. $token = $matches[2];
  52. $album_content = fetch_file_contents($entry->getAttribute("href"),
  53. false, false, false, false, 10);
  54. if ($album_content && $token) {
  55. $adoc = new DOMDocument();
  56. @$adoc->loadHTML($album_content);
  57. if ($adoc) {
  58. $axpath = new DOMXPath($adoc);
  59. $aentries = $axpath->query('(//img[@src])');
  60. foreach ($aentries as $aentry) {
  61. if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
  62. $img = $doc->createElement('img');
  63. $img->setAttribute("src", $aentry->getAttribute("src"));
  64. $br = $doc->createElement('br');
  65. $entry->parentNode->insertBefore($img, $entry);
  66. $entry->parentNode->insertBefore($br, $entry);
  67. $found = true;
  68. break;
  69. }
  70. }
  71. }
  72. }
  73. }
  74. // linked albums, ffs
  75. if (preg_match("/^https?:\/\/imgur.com\/(a|album)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
  76. $album_content = fetch_file_contents($entry->getAttribute("href"),
  77. false, false, false, false, 10);
  78. if ($album_content) {
  79. $adoc = new DOMDocument();
  80. @$adoc->loadHTML($album_content);
  81. if ($adoc) {
  82. $axpath = new DOMXPath($adoc);
  83. $aentries = $axpath->query("//meta[@property='og:image']");
  84. $urls = array();
  85. foreach ($aentries as $aentry) {
  86. if (!in_array($aentry->getAttribute("content"), $urls)) {
  87. $img = $doc->createElement('img');
  88. $img->setAttribute("src", $aentry->getAttribute("content"));
  89. $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
  90. $br = $doc->createElement('br');
  91. $entry->parentNode->insertBefore($img, $entry);
  92. $entry->parentNode->insertBefore($br, $entry);
  93. array_push($urls, $aentry->getAttribute("content"));
  94. $found = true;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. // remove tiny thumbnails
  102. if ($entry->hasAttribute("src")) {
  103. if ($entry->parentNode && $entry->parentNode->parentNode) {
  104. $entry->parentNode->parentNode->removeChild($entry->parentNode);
  105. }
  106. }
  107. }
  108. $node = $doc->getElementsByTagName('body')->item(0);
  109. if ($node && $found) {
  110. $article["content"] = $doc->saveXML($node);
  111. }
  112. }
  113. }
  114. return $article;
  115. }
  116. function api_version() {
  117. return 2;
  118. }
  119. }
  120. ?>