]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_redditimgur/init.php
2 class Af_RedditImgur
extends Plugin
{
9 "Inline image links in Reddit RSS feeds",
13 function init($host) {
14 $this->link
= $host->get_link();
17 $host->add_hook($host::HOOK_ARTICLE_FILTER
, $this);
20 function hook_article_filter($article) {
21 $owner_uid = $article["owner_uid"];
25 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
26 if (strpos($article["plugin_data"], "redditimgur,$owner_uid:") === FALSE ||
$force) {
27 $doc = new DOMDocument();
28 @$doc->loadHTML($article["content"]);
31 $xpath = new DOMXPath($doc);
32 $entries = $xpath->query('(//a[@href]|//img[@src])');
36 foreach ($entries as $entry) {
37 if ($entry->hasAttribute("href")) {
38 if (preg_match("/\.(jpg|jpeg|gif|png)$/i", $entry->getAttribute("href"))) {
40 $img = $doc->createElement('img');
41 $img->setAttribute("src", $entry->getAttribute("href"));
43 $entry->parentNode
->replaceChild($img, $entry);
48 // links to imgur pages
50 if (preg_match("/^http:\/\/imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
54 $album_content = fetch_file_contents($entry->getAttribute("href"),
55 false, false, false, false, 10);
57 if ($album_content && $token) {
58 $adoc = new DOMDocument();
59 @$adoc->loadHTML($album_content);
62 $axpath = new DOMXPath($adoc);
63 $aentries = $axpath->query('(//img[@src])');
65 foreach ($aentries as $aentry) {
66 if (preg_match("/^http:\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
67 $img = $doc->createElement('img');
68 $img->setAttribute("src", $aentry->getAttribute("src"));
69 $entry->parentNode
->insertBefore($img, $entry);
80 if (preg_match("/^http:\/\/imgur.com\/a\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
82 $album_content = fetch_file_contents($entry->getAttribute("href"),
83 false, false, false, false, 10);
86 $adoc = new DOMDocument();
87 @$adoc->loadHTML($album_content);
90 $axpath = new DOMXPath($adoc);
91 $aentries = $axpath->query("//div[@class='image']//a[@href and @class='zoom']");
93 foreach ($aentries as $aentry) {
94 $img = $doc->createElement('img');
95 $img->setAttribute("src", $aentry->getAttribute("href"));
96 $entry->parentNode
->insertBefore($doc->createElement('br'), $entry);
97 $entry->parentNode
->insertBefore($img, $entry);
105 // remove tiny thumbnails
106 if ($entry->hasAttribute("src")) {
107 if ($entry->parentNode
&& $entry->parentNode
->parentNode
) {
108 $entry->parentNode
->parentNode
->removeChild($entry->parentNode
);
113 $node = $doc->getElementsByTagName('body')->item(0);
115 if ($node && $found) {
116 $article["content"] = $doc->saveXML($node, LIBXML_NOEMPTYTAG
);
117 if (!$force) $article["plugin_data"] = "redditimgur,$owner_uid:" . $article["plugin_data"];
120 } else if (isset($article["stored"]["content"])) {
121 $article["content"] = $article["stored"]["content"];