]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_redditimgur/init.php
f2d5c7b674906712e634e7f6d1ad8f72b1abfda4
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 $br = $doc->createElement('br');
44 $entry->parentNode
->insertBefore($img, $entry);
45 $entry->parentNode
->insertBefore($br, $entry);
50 // links to imgur pages
52 if (preg_match("/^http:\/\/imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
56 $album_content = fetch_file_contents($entry->getAttribute("href"),
57 false, false, false, false, 10);
59 if ($album_content && $token) {
60 $adoc = new DOMDocument();
61 @$adoc->loadHTML($album_content);
64 $axpath = new DOMXPath($adoc);
65 $aentries = $axpath->query('(//img[@src])');
67 foreach ($aentries as $aentry) {
68 if (preg_match("/^http:\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
69 $img = $doc->createElement('img');
70 $img->setAttribute("src", $aentry->getAttribute("src"));
72 $br = $doc->createElement('br');
74 $entry->parentNode
->insertBefore($img, $entry);
75 $entry->parentNode
->insertBefore($br, $entry);
87 if (preg_match("/^http:\/\/imgur.com\/a\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
89 $album_content = fetch_file_contents($entry->getAttribute("href"),
90 false, false, false, false, 10);
93 $adoc = new DOMDocument();
94 @$adoc->loadHTML($album_content);
97 $axpath = new DOMXPath($adoc);
98 $aentries = $axpath->query("//div[@class='image']//a[@href and @class='zoom']");
100 foreach ($aentries as $aentry) {
101 $img = $doc->createElement('img');
102 $img->setAttribute("src", $aentry->getAttribute("href"));
103 $entry->parentNode
->insertBefore($doc->createElement('br'), $entry);
105 $br = $doc->createElement('br');
107 $entry->parentNode
->insertBefore($img, $entry);
108 $entry->parentNode
->insertBefore($br, $entry);
117 // remove tiny thumbnails
118 if ($entry->hasAttribute("src")) {
119 if ($entry->parentNode
&& $entry->parentNode
->parentNode
) {
120 $entry->parentNode
->parentNode
->removeChild($entry->parentNode
);
125 $node = $doc->getElementsByTagName('body')->item(0);
127 if ($node && $found) {
128 $article["content"] = $doc->saveXML($node);
129 if (!$force) $article["plugin_data"] = "redditimgur,$owner_uid:" . $article["plugin_data"];
132 } else if (isset($article["stored"]["content"])) {
133 $article["content"] = $article["stored"]["content"];