]> git.wh0rd.org - tt-rss.git/blame - plugins/af_redditimgur/init.php
update .gitignore
[tt-rss.git] / plugins / af_redditimgur / init.php
CommitLineData
cc85704f 1<?php
0862a602 2class Af_RedditImgur extends Plugin {
19c73507
AD
3 private $host;
4
d2a421e3 5 function about() {
7a866114
AD
6 return array(1.0,
7 "Inline image links in Reddit RSS feeds",
8 "fox");
9 }
10
d2a421e3 11 function init($host) {
19c73507
AD
12 $this->host = $host;
13
14 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
15 }
16
17 function hook_article_filter($article) {
0b5ef30d 18
cc85704f 19 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
a16d7a5d
AD
20 $doc = new DOMDocument();
21 @$doc->loadHTML($article["content"]);
cc85704f 22
a16d7a5d
AD
23 if ($doc) {
24 $xpath = new DOMXPath($doc);
25 $entries = $xpath->query('(//a[@href]|//img[@src])');
cc85704f 26
ce7d5e87
AD
27 $found = false;
28
a16d7a5d
AD
29 foreach ($entries as $entry) {
30 if ($entry->hasAttribute("href")) {
299aeb30 31
5d429910
AD
32 $matches = array();
33
34 if (preg_match("/https?:\/\/gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
35
36 $tmp = fetch_file_contents($entry->getAttribute("href"));
37
38 if ($tmp) {
39 $tmpdoc = new DOMDocument();
40 @$tmpdoc->loadHTML($tmp);
41
42 if ($tmpdoc) {
43 $tmpxpath = new DOMXPath($tmpdoc);
44 $source_meta = $tmpxpath->query("//meta[@property='og:video']")->item(0);
45
46 if ($source_meta) {
47 $source_stream = $source_meta->getAttribute("content");
48
49 if ($source_stream) {
50 $this->handle_as_video($doc, $entry, $source_stream);
51 $found = 1;
52 }
53 }
54 }
55 }
56
57 }
58
9875d717 59 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
60
5d429910 61 /*$video = $doc->createElement('video');
1ae46c50
AD
62 $video->setAttribute("autoplay", "1");
63 $video->setAttribute("loop", "1");
299aeb30 64
1ae46c50
AD
65 $source = $doc->createElement('source');
66 $source->setAttribute("src", str_replace(".gifv", ".mp4", $entry->getAttribute("href")));
67 $source->setAttribute("type", "video/mp4");
299aeb30 68
1ae46c50
AD
69 $video->appendChild($source);
70
71 $br = $doc->createElement('br');
72 $entry->parentNode->insertBefore($video, $entry);
73 $entry->parentNode->insertBefore($br, $entry);
74
98e20510
AD
75 $img = $doc->createElement('img');
76 $img->setAttribute("src",
77 "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
78
5d429910
AD
79 $entry->parentNode->insertBefore($img, $entry);*/
80
81 $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
82 $this->handle_as_video($doc, $entry, $source_stream);
98e20510 83
1ae46c50 84 $found = true;
299aeb30 85 }
9875d717 86
7adf9556 87 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9])?$/i", $entry->getAttribute("href"))) {
cc85704f 88
a16d7a5d
AD
89 $img = $doc->createElement('img');
90 $img->setAttribute("src", $entry->getAttribute("href"));
cc85704f 91
7ff4d1fa
AD
92 $br = $doc->createElement('br');
93 $entry->parentNode->insertBefore($img, $entry);
94 $entry->parentNode->insertBefore($br, $entry);
ce7d5e87
AD
95
96 $found = true;
97 }
98
99 // links to imgur pages
100 $matches = array();
248c5a6a 101 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
ce7d5e87 102
1af9f54f 103 $token = $matches[2];
ce7d5e87
AD
104
105 $album_content = fetch_file_contents($entry->getAttribute("href"),
106 false, false, false, false, 10);
107
108 if ($album_content && $token) {
109 $adoc = new DOMDocument();
110 @$adoc->loadHTML($album_content);
111
112 if ($adoc) {
113 $axpath = new DOMXPath($adoc);
114 $aentries = $axpath->query('(//img[@src])');
115
116 foreach ($aentries as $aentry) {
e44ea762 117 if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
ce7d5e87
AD
118 $img = $doc->createElement('img');
119 $img->setAttribute("src", $aentry->getAttribute("src"));
7ff4d1fa
AD
120
121 $br = $doc->createElement('br');
122
ce7d5e87 123 $entry->parentNode->insertBefore($img, $entry);
7ff4d1fa
AD
124 $entry->parentNode->insertBefore($br, $entry);
125
ce7d5e87 126 $found = true;
35055d05
AD
127
128 break;
ce7d5e87
AD
129 }
130 }
131 }
132 }
a16d7a5d 133 }
35055d05
AD
134
135 // linked albums, ffs
47cdc58c 136 if (preg_match("/^https?:\/\/imgur.com\/(a|album)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
35055d05
AD
137
138 $album_content = fetch_file_contents($entry->getAttribute("href"),
139 false, false, false, false, 10);
140
141 if ($album_content) {
142 $adoc = new DOMDocument();
143 @$adoc->loadHTML($album_content);
144
145 if ($adoc) {
146 $axpath = new DOMXPath($adoc);
47cdc58c 147 $aentries = $axpath->query("//meta[@property='og:image']");
98e20510 148 $urls = array();
35055d05
AD
149
150 foreach ($aentries as $aentry) {
7ff4d1fa 151
98e20510
AD
152 if (!in_array($aentry->getAttribute("content"), $urls)) {
153 $img = $doc->createElement('img');
154 $img->setAttribute("src", $aentry->getAttribute("content"));
155 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
7ff4d1fa 156
98e20510 157 $br = $doc->createElement('br');
7ff4d1fa 158
98e20510
AD
159 $entry->parentNode->insertBefore($img, $entry);
160 $entry->parentNode->insertBefore($br, $entry);
161
162 array_push($urls, $aentry->getAttribute("content"));
163
164 $found = true;
165 }
35055d05
AD
166 }
167 }
168 }
169 }
cc85704f
AD
170 }
171
a16d7a5d
AD
172 // remove tiny thumbnails
173 if ($entry->hasAttribute("src")) {
174 if ($entry->parentNode && $entry->parentNode->parentNode) {
175 $entry->parentNode->parentNode->removeChild($entry->parentNode);
176 }
cc85704f
AD
177 }
178 }
179
a16d7a5d 180 $node = $doc->getElementsByTagName('body')->item(0);
cc85704f 181
ce7d5e87 182 if ($node && $found) {
cc38c8e5 183 $article["content"] = $doc->saveXML($node);
a16d7a5d 184 }
cc85704f 185 }
cc85704f
AD
186 }
187
188 return $article;
189 }
106a3de9
AD
190
191 function api_version() {
192 return 2;
193 }
194
5d429910
AD
195 private function handle_as_video($doc, $entry, $source_stream) {
196
197 $video = $doc->createElement('video');
198 $video->setAttribute("autoplay", "1");
199 $video->setAttribute("loop", "1");
200
201 $source = $doc->createElement('source');
202 $source->setAttribute("src", $source_stream);
203 $source->setAttribute("type", "video/mp4");
204
205 $video->appendChild($source);
206
207 $br = $doc->createElement('br');
208 $entry->parentNode->insertBefore($video, $entry);
209 $entry->parentNode->insertBefore($br, $entry);
210
211 $img = $doc->createElement('img');
212 $img->setAttribute("src",
213 "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
214
215 $entry->parentNode->insertBefore($img, $entry);
216 }
cc85704f
AD
217}
218?>