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