]> git.wh0rd.org - tt-rss.git/blame - plugins/af_redditimgur/init.php
af_redditimgur: gifv: fix iframe not being wide enough, fix indenting
[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
9875d717 32 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
33
299aeb30
AD
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 }
9875d717 81
7adf9556 82 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9])?$/i", $entry->getAttribute("href"))) {
cc85704f 83
a16d7a5d
AD
84 $img = $doc->createElement('img');
85 $img->setAttribute("src", $entry->getAttribute("href"));
cc85704f 86
7ff4d1fa
AD
87 $br = $doc->createElement('br');
88 $entry->parentNode->insertBefore($img, $entry);
89 $entry->parentNode->insertBefore($br, $entry);
ce7d5e87
AD
90
91 $found = true;
92 }
93
94 // links to imgur pages
95 $matches = array();
248c5a6a 96 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
ce7d5e87 97
1af9f54f 98 $token = $matches[2];
ce7d5e87
AD
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) {
e44ea762 112 if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
ce7d5e87
AD
113 $img = $doc->createElement('img');
114 $img->setAttribute("src", $aentry->getAttribute("src"));
7ff4d1fa
AD
115
116 $br = $doc->createElement('br');
117
ce7d5e87 118 $entry->parentNode->insertBefore($img, $entry);
7ff4d1fa
AD
119 $entry->parentNode->insertBefore($br, $entry);
120
ce7d5e87 121 $found = true;
35055d05
AD
122
123 break;
ce7d5e87
AD
124 }
125 }
126 }
127 }
a16d7a5d 128 }
35055d05
AD
129
130 // linked albums, ffs
47cdc58c 131 if (preg_match("/^https?:\/\/imgur.com\/(a|album)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
35055d05
AD
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);
47cdc58c 142 $aentries = $axpath->query("//meta[@property='og:image']");
35055d05
AD
143
144 foreach ($aentries as $aentry) {
145 $img = $doc->createElement('img');
47cdc58c 146 $img->setAttribute("src", $aentry->getAttribute("content"));
0b5ef30d 147 $entry->parentNode->insertBefore($doc->createElement('br'), $entry);
7ff4d1fa
AD
148
149 $br = $doc->createElement('br');
150
35055d05 151 $entry->parentNode->insertBefore($img, $entry);
7ff4d1fa
AD
152 $entry->parentNode->insertBefore($br, $entry);
153
35055d05
AD
154 $found = true;
155 }
156 }
157 }
158 }
cc85704f
AD
159 }
160
a16d7a5d
AD
161 // remove tiny thumbnails
162 if ($entry->hasAttribute("src")) {
163 if ($entry->parentNode && $entry->parentNode->parentNode) {
164 $entry->parentNode->parentNode->removeChild($entry->parentNode);
165 }
cc85704f
AD
166 }
167 }
168
a16d7a5d 169 $node = $doc->getElementsByTagName('body')->item(0);
cc85704f 170
ce7d5e87 171 if ($node && $found) {
cc38c8e5 172 $article["content"] = $doc->saveXML($node);
a16d7a5d 173 }
cc85704f 174 }
cc85704f
AD
175 }
176
177 return $article;
178 }
106a3de9
AD
179
180 function api_version() {
181 return 2;
182 }
183
cc85704f
AD
184}
185?>