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