]> git.wh0rd.org Git - tt-rss.git/blob - plugins/af_redditimgur/init.php
71e6e3b0fa02e6189cd8f168578ae6edc4de89ee
[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                                                                 $video = $doc->createElement('video');
35                                                                 $video->setAttribute("autoplay", "1");
36                                                                 $video->setAttribute("loop", "1");
37
38                                                                 $source = $doc->createElement('source');
39                                                                 $source->setAttribute("src", str_replace(".gifv", ".mp4", $entry->getAttribute("href")));
40                                                                 $source->setAttribute("type", "video/mp4");
41
42                                                                 $video->appendChild($source);
43
44                                                                 $br = $doc->createElement('br');
45                                                                 $entry->parentNode->insertBefore($video, $entry);
46                                                                 $entry->parentNode->insertBefore($br, $entry);
47
48                                                                 $img = $doc->createElement('img');
49                                                                 $img->setAttribute("src",
50                                                                         "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
51
52                                                                 $entry->parentNode->insertBefore($img, $entry);
53
54                                                                 $found = true;
55                                                         }
56
57                                                         if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9])?$/i", $entry->getAttribute("href"))) {
58
59                                                                 $img = $doc->createElement('img');
60                                                                 $img->setAttribute("src", $entry->getAttribute("href"));
61
62                                                                 $br = $doc->createElement('br');
63                                                                 $entry->parentNode->insertBefore($img, $entry);
64                                                                 $entry->parentNode->insertBefore($br, $entry);
65
66                                                                 $found = true;
67                                                         }
68
69                                                         // links to imgur pages
70                                                         $matches = array();
71                                                         if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
72
73                                                                 $token = $matches[2];
74
75                                                                 $album_content = fetch_file_contents($entry->getAttribute("href"),
76                                                                         false, false, false, false, 10);
77
78                                                                 if ($album_content && $token) {
79                                                                         $adoc = new DOMDocument();
80                                                                         @$adoc->loadHTML($album_content);
81
82                                                                         if ($adoc) {
83                                                                                 $axpath = new DOMXPath($adoc);
84                                                                                 $aentries = $axpath->query('(//img[@src])');
85
86                                                                                 foreach ($aentries as $aentry) {
87                                                                                         if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
88                                                                                                 $img = $doc->createElement('img');
89                                                                                                 $img->setAttribute("src", $aentry->getAttribute("src"));
90
91                                                                                                 $br = $doc->createElement('br');
92
93                                                                                                 $entry->parentNode->insertBefore($img, $entry);
94                                                                                                 $entry->parentNode->insertBefore($br, $entry);
95
96                                                                                                 $found = true;
97
98                                                                                                 break;
99                                                                                         }
100                                                                                 }
101                                                                         }
102                                                                 }
103                                                         }
104
105                                                         // linked albums, ffs
106                                                         if (preg_match("/^https?:\/\/imgur.com\/(a|album)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
107
108                                                                 $album_content = fetch_file_contents($entry->getAttribute("href"),
109                                                                         false, false, false, false, 10);
110
111                                                                 if ($album_content) {
112                                                                         $adoc = new DOMDocument();
113                                                                         @$adoc->loadHTML($album_content);
114
115                                                                         if ($adoc) {
116                                                                                 $axpath = new DOMXPath($adoc);
117                                                                                 $aentries = $axpath->query("//meta[@property='og:image']");
118                                                                                 $urls = array();
119
120                                                                                 foreach ($aentries as $aentry) {
121
122                                                                                         if (!in_array($aentry->getAttribute("content"), $urls)) {
123                                                                                                 $img = $doc->createElement('img');
124                                                                                                 $img->setAttribute("src", $aentry->getAttribute("content"));
125                                                                                                 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
126
127                                                                                                 $br = $doc->createElement('br');
128
129                                                                                                 $entry->parentNode->insertBefore($img, $entry);
130                                                                                                 $entry->parentNode->insertBefore($br, $entry);
131
132                                                                                                 array_push($urls, $aentry->getAttribute("content"));
133
134                                                                                                 $found = true;
135                                                                                         }
136                                                                                 }
137                                                                         }
138                                                                 }
139                                                         }
140                                                 }
141
142                                                 // remove tiny thumbnails
143                                                 if ($entry->hasAttribute("src")) {
144                                                         if ($entry->parentNode && $entry->parentNode->parentNode) {
145                                                                 $entry->parentNode->parentNode->removeChild($entry->parentNode);
146                                                         }
147                                                 }
148                                         }
149
150                                         $node = $doc->getElementsByTagName('body')->item(0);
151
152                                         if ($node && $found) {
153                                                 $article["content"] = $doc->saveXML($node);
154                                         }
155                                 }
156                 }
157
158                 return $article;
159         }
160
161         function api_version() {
162                 return 2;
163         }
164
165 }
166 ?>