]> git.wh0rd.org Git - tt-rss.git/blob - plugins/af_redditimgur/init.php
af_redditimgur: gifv: fix iframe not being wide enough, fix indenting
[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
53                                                                                         if (strpos($aentry->getAttribute("property"), "og:image:height") !== FALSE) {
54                                                                                                 $height = $aentry->getAttribute("content");
55                                                                                         }
56                                                                                 }
57                                                                         }
58                                                                 }
59
60                                                                 if ($width && $height) {
61
62                                                                         $iframe = $doc->createElement('iframe');
63                                                                         $iframe->setAttribute("src", str_replace("http:", "", $entry->getAttribute("href")));
64                                                                         $iframe->setAttribute("frameborder", "0");
65                                                                         $iframe->setAttribute("width", $width + 64);
66                                                                         $iframe->setAttribute("height", $height + 64);
67
68                                                                         $br = $doc->createElement('br');
69                                                                         $entry->parentNode->insertBefore($iframe, $entry);
70                                                                         $entry->parentNode->insertBefore($br, $entry);
71
72                                                                         // add empty img tag to disable display of attachment
73                                                                         $img = $doc->createElement('img');
74                                                                         $img->setAttribute("src", "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
75                                                                         $img->setAttribute("width", "0");
76                                                                         $img->setAttribute("height", "0");
77                                                                         $entry->parentNode->insertBefore($img, $entry);
78                                                                         $found = true;
79                                                                 }
80                                                         }
81
82                                                         if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9])?$/i", $entry->getAttribute("href"))) {
83
84                                                                 $img = $doc->createElement('img');
85                                                                 $img->setAttribute("src", $entry->getAttribute("href"));
86
87                                                                 $br = $doc->createElement('br');
88                                                                 $entry->parentNode->insertBefore($img, $entry);
89                                                                 $entry->parentNode->insertBefore($br, $entry);
90
91                                                                 $found = true;
92                                                         }
93
94                                                         // links to imgur pages
95                                                         $matches = array();
96                                                         if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
97
98                                                                 $token = $matches[2];
99
100                                                                 $album_content = fetch_file_contents($entry->getAttribute("href"),
101                                                                         false, false, false, false, 10);
102
103                                                                 if ($album_content && $token) {
104                                                                         $adoc = new DOMDocument();
105                                                                         @$adoc->loadHTML($album_content);
106
107                                                                         if ($adoc) {
108                                                                                 $axpath = new DOMXPath($adoc);
109                                                                                 $aentries = $axpath->query('(//img[@src])');
110
111                                                                                 foreach ($aentries as $aentry) {
112                                                                                         if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
113                                                                                                 $img = $doc->createElement('img');
114                                                                                                 $img->setAttribute("src", $aentry->getAttribute("src"));
115
116                                                                                                 $br = $doc->createElement('br');
117
118                                                                                                 $entry->parentNode->insertBefore($img, $entry);
119                                                                                                 $entry->parentNode->insertBefore($br, $entry);
120
121                                                                                                 $found = true;
122
123                                                                                                 break;
124                                                                                         }
125                                                                                 }
126                                                                         }
127                                                                 }
128                                                         }
129
130                                                         // linked albums, ffs
131                                                         if (preg_match("/^https?:\/\/imgur.com\/(a|album)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
132
133                                                                 $album_content = fetch_file_contents($entry->getAttribute("href"),
134                                                                         false, false, false, false, 10);
135
136                                                                 if ($album_content) {
137                                                                         $adoc = new DOMDocument();
138                                                                         @$adoc->loadHTML($album_content);
139
140                                                                         if ($adoc) {
141                                                                                 $axpath = new DOMXPath($adoc);
142                                                                                 $aentries = $axpath->query("//meta[@property='og:image']");
143
144                                                                                 foreach ($aentries as $aentry) {
145                                                                                         $img = $doc->createElement('img');
146                                                                                         $img->setAttribute("src", $aentry->getAttribute("content"));
147                                                                                         $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
148
149                                                                                         $br = $doc->createElement('br');
150
151                                                                                         $entry->parentNode->insertBefore($img, $entry);
152                                                                                         $entry->parentNode->insertBefore($br, $entry);
153
154                                                                                         $found = true;
155                                                                                 }
156                                                                         }
157                                                                 }
158                                                         }
159                                                 }
160
161                                                 // remove tiny thumbnails
162                                                 if ($entry->hasAttribute("src")) {
163                                                         if ($entry->parentNode && $entry->parentNode->parentNode) {
164                                                                 $entry->parentNode->parentNode->removeChild($entry->parentNode);
165                                                         }
166                                                 }
167                                         }
168
169                                         $node = $doc->getElementsByTagName('body')->item(0);
170
171                                         if ($node && $found) {
172                                                 $article["content"] = $doc->saveXML($node);
173                                         }
174                                 }
175                 }
176
177                 return $article;
178         }
179
180         function api_version() {
181                 return 2;
182         }
183
184 }
185 ?>