]>
Commit | Line | Data |
---|---|---|
cc85704f | 1 | <?php |
0862a602 | 2 | class Af_RedditImgur extends Plugin { |
19c73507 AD |
3 | private $host; |
4 | ||
d2a421e3 | 5 | function about() { |
7a866114 | 6 | return array(1.0, |
a95fb169 | 7 | "Inline images (and other content) in Reddit RSS feeds", |
7a866114 AD |
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); | |
b90c4468 AD |
15 | $host->add_hook($host::HOOK_PREFS_TAB, $this); |
16 | } | |
17 | ||
18 | function hook_prefs_tab($args) { | |
19 | if ($args != "prefFeeds") return; | |
20 | ||
21 | print "<div id=\"af_redditimgur_prefs\" dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_redditimgur settings')."\">"; | |
22 | ||
23 | $enable_readability = $this->host->get($this, "enable_readability"); | |
24 | $enable_readability_checked = $enable_readability ? "checked" : ""; | |
25 | ||
26 | print "<form dojoType=\"dijit.form.Form\">"; | |
27 | ||
28 | print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\"> | |
29 | evt.preventDefault(); | |
30 | if (this.validate()) { | |
31 | console.log(dojo.objectToQuery(this.getValues())); | |
32 | new Ajax.Request('backend.php', { | |
33 | parameters: dojo.objectToQuery(this.getValues()), | |
34 | onComplete: function(transport) { | |
35 | notify_info(transport.responseText); | |
36 | } | |
37 | }); | |
38 | //this.reset(); | |
39 | } | |
40 | </script>"; | |
41 | ||
42 | print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">"; | |
43 | print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">"; | |
44 | print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"af_redditimgur\">"; | |
45 | ||
46 | print "<h3>" . __("Global settings") . "</h3>"; | |
47 | ||
48 | print_notice("Uses Readability (full-text-rss) implementation by <a target='_blank' href='https://bitbucket.org/fivefilters/'>FiveFilters.org</a>"); | |
49 | print "<p/>"; | |
50 | ||
51 | print "<input dojoType=\"dijit.form.CheckBox\" id=\"enable_readability\" | |
52 | $enable_readability_checked name=\"enable_readability\"> "; | |
53 | ||
54 | print "<label for=\"enable_readability\">" . __("Extract missing content using Readability") . "</label>"; | |
55 | ||
56 | print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">". | |
57 | __("Save")."</button>"; | |
58 | ||
59 | print "</form>"; | |
60 | ||
61 | print "</div>"; | |
62 | } | |
63 | ||
64 | function save() { | |
65 | $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]) == "true"; | |
66 | ||
67 | $this->host->set($this, "enable_readability", $enable_readability); | |
68 | ||
69 | echo __("Configuration saved"); | |
19c73507 AD |
70 | } |
71 | ||
72 | function hook_article_filter($article) { | |
0b5ef30d | 73 | |
cc85704f | 74 | if (strpos($article["link"], "reddit.com/r/") !== FALSE) { |
a16d7a5d AD |
75 | $doc = new DOMDocument(); |
76 | @$doc->loadHTML($article["content"]); | |
cc85704f | 77 | |
a16d7a5d AD |
78 | if ($doc) { |
79 | $xpath = new DOMXPath($doc); | |
80 | $entries = $xpath->query('(//a[@href]|//img[@src])'); | |
cc85704f | 81 | |
ce7d5e87 AD |
82 | $found = false; |
83 | ||
a16d7a5d AD |
84 | foreach ($entries as $entry) { |
85 | if ($entry->hasAttribute("href")) { | |
299aeb30 | 86 | |
5d429910 AD |
87 | $matches = array(); |
88 | ||
89 | if (preg_match("/https?:\/\/gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) { | |
90 | ||
91 | $tmp = fetch_file_contents($entry->getAttribute("href")); | |
92 | ||
93 | if ($tmp) { | |
94 | $tmpdoc = new DOMDocument(); | |
95 | @$tmpdoc->loadHTML($tmp); | |
96 | ||
97 | if ($tmpdoc) { | |
98 | $tmpxpath = new DOMXPath($tmpdoc); | |
99 | $source_meta = $tmpxpath->query("//meta[@property='og:video']")->item(0); | |
100 | ||
101 | if ($source_meta) { | |
102 | $source_stream = $source_meta->getAttribute("content"); | |
103 | ||
104 | if ($source_stream) { | |
105 | $this->handle_as_video($doc, $entry, $source_stream); | |
106 | $found = 1; | |
107 | } | |
108 | } | |
109 | } | |
110 | } | |
111 | ||
112 | } | |
113 | ||
9875d717 | 114 | if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) { |
115 | ||
5d429910 | 116 | /*$video = $doc->createElement('video'); |
1ae46c50 AD |
117 | $video->setAttribute("autoplay", "1"); |
118 | $video->setAttribute("loop", "1"); | |
299aeb30 | 119 | |
1ae46c50 AD |
120 | $source = $doc->createElement('source'); |
121 | $source->setAttribute("src", str_replace(".gifv", ".mp4", $entry->getAttribute("href"))); | |
122 | $source->setAttribute("type", "video/mp4"); | |
299aeb30 | 123 | |
1ae46c50 AD |
124 | $video->appendChild($source); |
125 | ||
126 | $br = $doc->createElement('br'); | |
127 | $entry->parentNode->insertBefore($video, $entry); | |
128 | $entry->parentNode->insertBefore($br, $entry); | |
129 | ||
98e20510 AD |
130 | $img = $doc->createElement('img'); |
131 | $img->setAttribute("src", | |
132 | "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D"); | |
133 | ||
5d429910 AD |
134 | $entry->parentNode->insertBefore($img, $entry);*/ |
135 | ||
136 | $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href")); | |
137 | $this->handle_as_video($doc, $entry, $source_stream); | |
98e20510 | 138 | |
1ae46c50 | 139 | $found = true; |
299aeb30 | 140 | } |
9875d717 | 141 | |
3b9ca4e6 AD |
142 | $matches = array(); |
143 | if (preg_match("/\/\/www\.youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) || | |
144 | preg_match("/\/\/www\.youtube\.com\/watch?v=([\w-]+)/", $entry->getAttribute("href"), $matches) || | |
145 | preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) { | |
146 | ||
147 | $vid_id = $matches[1]; | |
148 | ||
149 | $iframe = $doc->createElement("iframe"); | |
150 | $iframe->setAttribute("class", "youtube-player"); | |
151 | $iframe->setAttribute("type", "text/html"); | |
152 | $iframe->setAttribute("width", "640"); | |
153 | $iframe->setAttribute("height", "385"); | |
154 | $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id"); | |
155 | $iframe->setAttribute("allowfullscreen", "1"); | |
156 | $iframe->setAttribute("frameborder", "0"); | |
157 | ||
158 | $br = $doc->createElement('br'); | |
159 | $entry->parentNode->insertBefore($iframe, $entry); | |
160 | $entry->parentNode->insertBefore($br, $entry); | |
161 | ||
162 | $found = true; | |
163 | } | |
164 | ||
5f58daa2 AD |
165 | if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) { |
166 | $img = $doc->createElement('img'); | |
a16d7a5d | 167 | $img->setAttribute("src", $entry->getAttribute("href")); |
cc85704f | 168 | |
7ff4d1fa AD |
169 | $br = $doc->createElement('br'); |
170 | $entry->parentNode->insertBefore($img, $entry); | |
171 | $entry->parentNode->insertBefore($br, $entry); | |
ce7d5e87 AD |
172 | |
173 | $found = true; | |
174 | } | |
175 | ||
176 | // links to imgur pages | |
177 | $matches = array(); | |
248c5a6a | 178 | if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) { |
ce7d5e87 | 179 | |
1af9f54f | 180 | $token = $matches[2]; |
ce7d5e87 AD |
181 | |
182 | $album_content = fetch_file_contents($entry->getAttribute("href"), | |
183 | false, false, false, false, 10); | |
184 | ||
185 | if ($album_content && $token) { | |
186 | $adoc = new DOMDocument(); | |
187 | @$adoc->loadHTML($album_content); | |
188 | ||
189 | if ($adoc) { | |
190 | $axpath = new DOMXPath($adoc); | |
191 | $aentries = $axpath->query('(//img[@src])'); | |
192 | ||
193 | foreach ($aentries as $aentry) { | |
e44ea762 | 194 | if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) { |
ce7d5e87 AD |
195 | $img = $doc->createElement('img'); |
196 | $img->setAttribute("src", $aentry->getAttribute("src")); | |
7ff4d1fa AD |
197 | |
198 | $br = $doc->createElement('br'); | |
199 | ||
ce7d5e87 | 200 | $entry->parentNode->insertBefore($img, $entry); |
7ff4d1fa AD |
201 | $entry->parentNode->insertBefore($br, $entry); |
202 | ||
ce7d5e87 | 203 | $found = true; |
35055d05 AD |
204 | |
205 | break; | |
ce7d5e87 AD |
206 | } |
207 | } | |
208 | } | |
209 | } | |
a16d7a5d | 210 | } |
35055d05 AD |
211 | |
212 | // linked albums, ffs | |
09897321 | 213 | if (preg_match("/^https?:\/\/imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) { |
35055d05 AD |
214 | |
215 | $album_content = fetch_file_contents($entry->getAttribute("href"), | |
216 | false, false, false, false, 10); | |
217 | ||
218 | if ($album_content) { | |
219 | $adoc = new DOMDocument(); | |
220 | @$adoc->loadHTML($album_content); | |
221 | ||
222 | if ($adoc) { | |
223 | $axpath = new DOMXPath($adoc); | |
47cdc58c | 224 | $aentries = $axpath->query("//meta[@property='og:image']"); |
98e20510 | 225 | $urls = array(); |
35055d05 AD |
226 | |
227 | foreach ($aentries as $aentry) { | |
7ff4d1fa | 228 | |
98e20510 AD |
229 | if (!in_array($aentry->getAttribute("content"), $urls)) { |
230 | $img = $doc->createElement('img'); | |
231 | $img->setAttribute("src", $aentry->getAttribute("content")); | |
232 | $entry->parentNode->insertBefore($doc->createElement('br'), $entry); | |
7ff4d1fa | 233 | |
98e20510 | 234 | $br = $doc->createElement('br'); |
7ff4d1fa | 235 | |
98e20510 AD |
236 | $entry->parentNode->insertBefore($img, $entry); |
237 | $entry->parentNode->insertBefore($br, $entry); | |
238 | ||
239 | array_push($urls, $aentry->getAttribute("content")); | |
240 | ||
241 | $found = true; | |
242 | } | |
35055d05 AD |
243 | } |
244 | } | |
245 | } | |
246 | } | |
cc85704f AD |
247 | } |
248 | ||
a16d7a5d AD |
249 | // remove tiny thumbnails |
250 | if ($entry->hasAttribute("src")) { | |
251 | if ($entry->parentNode && $entry->parentNode->parentNode) { | |
252 | $entry->parentNode->parentNode->removeChild($entry->parentNode); | |
253 | } | |
cc85704f AD |
254 | } |
255 | } | |
256 | ||
b90c4468 | 257 | if (!$found && $this->host->get($this, "enable_readability") && mb_strlen(strip_tags($article["content"])) <= 150) { |
1ff7ae42 | 258 | if (!class_exists("Readability")) require_once(__DIR__ . "/classes/Readability.php"); |
b90c4468 AD |
259 | |
260 | $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0); | |
261 | ||
262 | if ($content_link) { | |
263 | $tmp = fetch_file_contents($content_link->getAttribute("href")); | |
264 | ||
265 | if ($tmp) { | |
266 | $r = new Readability($tmp, $content_link->getAttribute("href")); | |
267 | ||
268 | if ($r->init()) { | |
fd61fd6e AD |
269 | //$article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"]; |
270 | ||
271 | $tmpxpath = new DOMXPath($r->dom); | |
272 | ||
273 | $entries = $tmpxpath->query('(//a[@href]|//img[@src])'); | |
274 | ||
275 | foreach ($entries as $entry) { | |
276 | if ($entry->hasAttribute("href")) { | |
277 | $entry->setAttribute("href", | |
7975ace2 | 278 | rewrite_relative_url($content_link, $entry->getAttribute("href"))); |
fd61fd6e AD |
279 | |
280 | } | |
b90c4468 | 281 | |
fd61fd6e AD |
282 | if ($entry->hasAttribute("src")) { |
283 | $entry->setAttribute("src", | |
7975ace2 | 284 | rewrite_relative_url($content_link, $entry->getAttribute("src"))); |
fd61fd6e AD |
285 | |
286 | } | |
287 | ||
288 | } | |
289 | ||
290 | $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"]; | |
b90c4468 AD |
291 | } |
292 | } | |
293 | ||
294 | } | |
295 | ||
296 | } | |
297 | ||
a16d7a5d | 298 | $node = $doc->getElementsByTagName('body')->item(0); |
cc85704f | 299 | |
ce7d5e87 | 300 | if ($node && $found) { |
cc38c8e5 | 301 | $article["content"] = $doc->saveXML($node); |
a16d7a5d | 302 | } |
cc85704f | 303 | } |
cc85704f AD |
304 | } |
305 | ||
306 | return $article; | |
307 | } | |
106a3de9 AD |
308 | |
309 | function api_version() { | |
310 | return 2; | |
311 | } | |
312 | ||
5d429910 AD |
313 | private function handle_as_video($doc, $entry, $source_stream) { |
314 | ||
315 | $video = $doc->createElement('video'); | |
316 | $video->setAttribute("autoplay", "1"); | |
5dcc7bf1 | 317 | $video->setAttribute("controls", "1"); |
5d429910 AD |
318 | $video->setAttribute("loop", "1"); |
319 | ||
320 | $source = $doc->createElement('source'); | |
321 | $source->setAttribute("src", $source_stream); | |
322 | $source->setAttribute("type", "video/mp4"); | |
323 | ||
324 | $video->appendChild($source); | |
325 | ||
326 | $br = $doc->createElement('br'); | |
327 | $entry->parentNode->insertBefore($video, $entry); | |
328 | $entry->parentNode->insertBefore($br, $entry); | |
329 | ||
330 | $img = $doc->createElement('img'); | |
331 | $img->setAttribute("src", | |
332 | "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D"); | |
333 | ||
334 | $entry->parentNode->insertBefore($img, $entry); | |
335 | } | |
cc85704f AD |
336 | } |
337 | ?> |