]> git.wh0rd.org - tt-rss.git/blob - plugins/af_redditimgur/init.php
Sometimes imgur links has ?1 in the url
[tt-rss.git] / plugins / af_redditimgur / init.php
1 <?php
2 class Af_RedditImgur extends Plugin {
3 private $host;
4
5 function about() {
6 return array(1.0,
7 "Inline image links in Reddit RSS feeds",
8 "fox");
9 }
10
11 function init($host) {
12 $this->host = $host;
13
14 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
15 }
16
17 function hook_article_filter($article) {
18
19 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
20 $doc = new DOMDocument();
21 @$doc->loadHTML($article["content"]);
22
23 if ($doc) {
24 $xpath = new DOMXPath($doc);
25 $entries = $xpath->query('(//a[@href]|//img[@src])');
26
27 $found = false;
28
29 foreach ($entries as $entry) {
30 if ($entry->hasAttribute("href")) {
31
32 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
33
34 $gifv_meta = fetch_file_contents($entry->getAttribute("href"),
35 false, false, false, false, 10);
36
37 if ($gifv_meta) {
38 $adoc = new DOMDocument();
39 @$adoc->loadHTML($gifv_meta);
40
41 if ($adoc) {
42 $axpath = new DOMXPath($adoc);
43 $aentries = $axpath->query('(//meta)');
44
45 $width = false;
46 $height = false;
47
48 foreach ($aentries as $aentry) {
49 if (strpos($aentry->getAttribute("property"), "og:image:width") !== FALSE) {
50 $width = $aentry->getAttribute("content");
51 }
52 if (strpos($aentry->getAttribute("property"), "og:image:height") !== FALSE) {
53 $height = $aentry->getAttribute("content");
54 }
55 }
56 }
57 }
58
59 if ($width && $height) {
60
61 $iframe = $doc->createElement('iframe');
62 $iframe->setAttribute("src", str_replace("http:", "", $entry->getAttribute("href")));
63 $iframe->setAttribute("frameborder", "0");
64 $iframe->setAttribute("width", $width);
65 $iframe->setAttribute("height", $height);
66
67 $br = $doc->createElement('br');
68 $entry->parentNode->insertBefore($iframe, $entry);
69 $entry->parentNode->insertBefore($br, $entry);
70
71 // add empty img tag to disable display of attachment
72 $img = $doc->createElement('img');
73 $img->setAttribute("src", "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
74 $img->setAttribute("width", "0");
75 $img->setAttribute("height", "0");
76 $entry->parentNode->insertBefore($img, $entry);
77 $found = true;
78 }
79 }
80
81 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9])?$/i", $entry->getAttribute("href"))) {
82
83 $img = $doc->createElement('img');
84 $img->setAttribute("src", $entry->getAttribute("href"));
85
86 $br = $doc->createElement('br');
87 $entry->parentNode->insertBefore($img, $entry);
88 $entry->parentNode->insertBefore($br, $entry);
89
90 $found = true;
91 }
92
93 // links to imgur pages
94 $matches = array();
95 if (preg_match("/^https?:\/\/imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
96
97 $token = $matches[1];
98
99 $album_content = fetch_file_contents($entry->getAttribute("href"),
100 false, false, false, false, 10);
101
102 if ($album_content && $token) {
103 $adoc = new DOMDocument();
104 @$adoc->loadHTML($album_content);
105
106 if ($adoc) {
107 $axpath = new DOMXPath($adoc);
108 $aentries = $axpath->query('(//img[@src])');
109
110 foreach ($aentries as $aentry) {
111 if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
112 $img = $doc->createElement('img');
113 $img->setAttribute("src", $aentry->getAttribute("src"));
114
115 $br = $doc->createElement('br');
116
117 $entry->parentNode->insertBefore($img, $entry);
118 $entry->parentNode->insertBefore($br, $entry);
119
120 $found = true;
121
122 break;
123 }
124 }
125 }
126 }
127 }
128
129 // linked albums, ffs
130 if (preg_match("/^https?:\/\/imgur.com\/(a|album)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
131
132 $album_content = fetch_file_contents($entry->getAttribute("href"),
133 false, false, false, false, 10);
134
135 if ($album_content) {
136 $adoc = new DOMDocument();
137 @$adoc->loadHTML($album_content);
138
139 if ($adoc) {
140 $axpath = new DOMXPath($adoc);
141 $aentries = $axpath->query("//meta[@property='og:image']");
142
143 foreach ($aentries as $aentry) {
144 $img = $doc->createElement('img');
145 $img->setAttribute("src", $aentry->getAttribute("content"));
146 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
147
148 $br = $doc->createElement('br');
149
150 $entry->parentNode->insertBefore($img, $entry);
151 $entry->parentNode->insertBefore($br, $entry);
152
153 $found = true;
154 }
155 }
156 }
157 }
158 }
159
160 // remove tiny thumbnails
161 if ($entry->hasAttribute("src")) {
162 if ($entry->parentNode && $entry->parentNode->parentNode) {
163 $entry->parentNode->parentNode->removeChild($entry->parentNode);
164 }
165 }
166 }
167
168 $node = $doc->getElementsByTagName('body')->item(0);
169
170 if ($node && $found) {
171 $article["content"] = $doc->saveXML($node);
172 }
173 }
174 }
175
176 return $article;
177 }
178
179 function api_version() {
180 return 2;
181 }
182
183 }
184 ?>