]>
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 | ||
6322fc68 | 72 | private function inline_stuff($article, &$doc, $xpath) { |
cc85704f | 73 | |
6322fc68 | 74 | $entries = $xpath->query('(//a[@href]|//img[@src])'); |
cc85704f | 75 | |
6322fc68 | 76 | $found = false; |
ce7d5e87 | 77 | |
6322fc68 AD |
78 | foreach ($entries as $entry) { |
79 | if ($entry->hasAttribute("href")) { | |
299aeb30 | 80 | |
6322fc68 | 81 | $matches = array(); |
5d429910 | 82 | |
6322fc68 | 83 | if (preg_match("/https?:\/\/gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) { |
5d429910 | 84 | |
6322fc68 | 85 | $tmp = fetch_file_contents($entry->getAttribute("href")); |
5d429910 | 86 | |
6322fc68 AD |
87 | if ($tmp) { |
88 | $tmpdoc = new DOMDocument(); | |
89 | @$tmpdoc->loadHTML($tmp); | |
5d429910 | 90 | |
6322fc68 AD |
91 | if ($tmpdoc) { |
92 | $tmpxpath = new DOMXPath($tmpdoc); | |
93 | $source_meta = $tmpxpath->query("//meta[@property='og:video']")->item(0); | |
5d429910 | 94 | |
6322fc68 AD |
95 | if ($source_meta) { |
96 | $source_stream = $source_meta->getAttribute("content"); | |
5d429910 | 97 | |
6322fc68 AD |
98 | if ($source_stream) { |
99 | $this->handle_as_video($doc, $entry, $source_stream); | |
100 | $found = 1; | |
5d429910 | 101 | } |
5d429910 | 102 | } |
6322fc68 AD |
103 | } |
104 | } | |
5d429910 | 105 | |
6322fc68 | 106 | } |
98e20510 | 107 | |
6322fc68 | 108 | if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) { |
5d429910 | 109 | |
6322fc68 AD |
110 | $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href")); |
111 | $this->handle_as_video($doc, $entry, $source_stream); | |
98e20510 | 112 | |
6322fc68 AD |
113 | $found = true; |
114 | } | |
9875d717 | 115 | |
6322fc68 | 116 | $matches = array(); |
f7745af9 AD |
117 | if (preg_match("/\.youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) || |
118 | preg_match("/\.youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) || | |
6322fc68 | 119 | preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) { |
3b9ca4e6 | 120 | |
6322fc68 | 121 | $vid_id = $matches[1]; |
3b9ca4e6 | 122 | |
6322fc68 AD |
123 | $iframe = $doc->createElement("iframe"); |
124 | $iframe->setAttribute("class", "youtube-player"); | |
125 | $iframe->setAttribute("type", "text/html"); | |
126 | $iframe->setAttribute("width", "640"); | |
127 | $iframe->setAttribute("height", "385"); | |
128 | $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id"); | |
129 | $iframe->setAttribute("allowfullscreen", "1"); | |
130 | $iframe->setAttribute("frameborder", "0"); | |
3b9ca4e6 | 131 | |
6322fc68 AD |
132 | $br = $doc->createElement('br'); |
133 | $entry->parentNode->insertBefore($iframe, $entry); | |
134 | $entry->parentNode->insertBefore($br, $entry); | |
3b9ca4e6 | 135 | |
6322fc68 AD |
136 | $found = true; |
137 | } | |
3b9ca4e6 | 138 | |
6322fc68 AD |
139 | if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) { |
140 | $img = $doc->createElement('img'); | |
141 | $img->setAttribute("src", $entry->getAttribute("href")); | |
cc85704f | 142 | |
6322fc68 AD |
143 | $br = $doc->createElement('br'); |
144 | $entry->parentNode->insertBefore($img, $entry); | |
145 | $entry->parentNode->insertBefore($br, $entry); | |
ce7d5e87 | 146 | |
6322fc68 AD |
147 | $found = true; |
148 | } | |
ce7d5e87 | 149 | |
6322fc68 AD |
150 | // links to imgur pages |
151 | $matches = array(); | |
152 | if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) { | |
ce7d5e87 | 153 | |
6322fc68 | 154 | $token = $matches[2]; |
ce7d5e87 | 155 | |
6322fc68 AD |
156 | $album_content = fetch_file_contents($entry->getAttribute("href"), |
157 | false, false, false, false, 10); | |
ce7d5e87 | 158 | |
6322fc68 AD |
159 | if ($album_content && $token) { |
160 | $adoc = new DOMDocument(); | |
161 | @$adoc->loadHTML($album_content); | |
ce7d5e87 | 162 | |
6322fc68 AD |
163 | if ($adoc) { |
164 | $axpath = new DOMXPath($adoc); | |
165 | $aentries = $axpath->query('(//img[@src])'); | |
ce7d5e87 | 166 | |
6322fc68 AD |
167 | foreach ($aentries as $aentry) { |
168 | if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) { | |
169 | $img = $doc->createElement('img'); | |
170 | $img->setAttribute("src", $aentry->getAttribute("src")); | |
7ff4d1fa | 171 | |
6322fc68 | 172 | $br = $doc->createElement('br'); |
7ff4d1fa | 173 | |
6322fc68 AD |
174 | $entry->parentNode->insertBefore($img, $entry); |
175 | $entry->parentNode->insertBefore($br, $entry); | |
7ff4d1fa | 176 | |
6322fc68 | 177 | $found = true; |
35055d05 | 178 | |
6322fc68 | 179 | break; |
ce7d5e87 | 180 | } |
a16d7a5d | 181 | } |
6322fc68 AD |
182 | } |
183 | } | |
184 | } | |
35055d05 | 185 | |
6322fc68 AD |
186 | // linked albums, ffs |
187 | if (preg_match("/^https?:\/\/imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) { | |
35055d05 | 188 | |
6322fc68 AD |
189 | $album_content = fetch_file_contents($entry->getAttribute("href"), |
190 | false, false, false, false, 10); | |
35055d05 | 191 | |
6322fc68 AD |
192 | if ($album_content) { |
193 | $adoc = new DOMDocument(); | |
194 | @$adoc->loadHTML($album_content); | |
35055d05 | 195 | |
6322fc68 AD |
196 | if ($adoc) { |
197 | $axpath = new DOMXPath($adoc); | |
198 | $aentries = $axpath->query("//meta[@property='og:image']"); | |
199 | $urls = array(); | |
35055d05 | 200 | |
6322fc68 | 201 | foreach ($aentries as $aentry) { |
7ff4d1fa | 202 | |
6322fc68 AD |
203 | if (!in_array($aentry->getAttribute("content"), $urls)) { |
204 | $img = $doc->createElement('img'); | |
205 | $img->setAttribute("src", $aentry->getAttribute("content")); | |
206 | $entry->parentNode->insertBefore($doc->createElement('br'), $entry); | |
7ff4d1fa | 207 | |
6322fc68 | 208 | $br = $doc->createElement('br'); |
7ff4d1fa | 209 | |
6322fc68 AD |
210 | $entry->parentNode->insertBefore($img, $entry); |
211 | $entry->parentNode->insertBefore($br, $entry); | |
98e20510 | 212 | |
6322fc68 | 213 | array_push($urls, $aentry->getAttribute("content")); |
98e20510 | 214 | |
6322fc68 | 215 | $found = true; |
35055d05 AD |
216 | } |
217 | } | |
cc85704f | 218 | } |
cc85704f | 219 | } |
6322fc68 AD |
220 | } |
221 | } | |
222 | ||
223 | // remove tiny thumbnails | |
224 | if ($entry->hasAttribute("src")) { | |
225 | if ($entry->parentNode && $entry->parentNode->parentNode) { | |
226 | $entry->parentNode->parentNode->removeChild($entry->parentNode); | |
227 | } | |
228 | } | |
229 | } | |
cc85704f | 230 | |
6322fc68 AD |
231 | return $found; |
232 | } | |
b90c4468 | 233 | |
6322fc68 | 234 | function hook_article_filter($article) { |
b90c4468 | 235 | |
6322fc68 AD |
236 | if (strpos($article["link"], "reddit.com/r/") !== FALSE) { |
237 | $doc = new DOMDocument(); | |
238 | @$doc->loadHTML($article["content"]); | |
239 | $xpath = new DOMXPath($doc); | |
99bb8b3b | 240 | |
6322fc68 | 241 | $found = $this->inline_stuff($article, $doc, $xpath); |
b90c4468 | 242 | |
6475fc7e AD |
243 | if (function_exists("curl_init") && !$found && $this->host->get($this, "enable_readability") && |
244 | mb_strlen(strip_tags($article["content"])) <= 150) { | |
245 | ||
8b08d9d7 | 246 | if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__)). "/lib/readability/Readability.php"); |
b90c4468 | 247 | |
6322fc68 | 248 | $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0); |
fd61fd6e | 249 | |
6322fc68 | 250 | if ($content_link && strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) { |
fd61fd6e | 251 | |
6475fc7e AD |
252 | /* link may lead to a huge video file or whatever, we need to check content type before trying to |
253 | parse it which p much requires curl */ | |
fd61fd6e | 254 | |
6475fc7e AD |
255 | $ch = curl_init($content_link->getAttribute("href")); |
256 | curl_setopt($ch, CURLOPT_TIMEOUT, 5); | |
257 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
258 | curl_setopt($ch, CURLOPT_HEADER, true); | |
259 | curl_setopt($ch, CURLOPT_NOBODY, true); | |
260 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, | |
261 | !ini_get("safe_mode") && !ini_get("open_basedir")); | |
262 | curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT); | |
fd61fd6e | 263 | |
6475fc7e AD |
264 | @$result = curl_exec($ch); |
265 | $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); | |
b90c4468 | 266 | |
6475fc7e | 267 | if ($content_type && strpos($content_type, "text/html") !== FALSE) { |
fd61fd6e | 268 | |
6475fc7e | 269 | $tmp = fetch_file_contents($content_link->getAttribute("href")); |
fd61fd6e | 270 | |
6475fc7e AD |
271 | if ($tmp) { |
272 | $r = new Readability($tmp, $content_link->getAttribute("href")); | |
fd61fd6e | 273 | |
6475fc7e | 274 | if ($r->init()) { |
b90c4468 | 275 | |
6475fc7e | 276 | $tmpxpath = new DOMXPath($r->dom); |
b90c4468 | 277 | |
6475fc7e | 278 | $entries = $tmpxpath->query('(//a[@href]|//img[@src])'); |
6322fc68 | 279 | |
6475fc7e AD |
280 | foreach ($entries as $entry) { |
281 | if ($entry->hasAttribute("href")) { | |
282 | $entry->setAttribute("href", | |
283 | rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("href"))); | |
284 | ||
285 | } | |
b90c4468 | 286 | |
6475fc7e AD |
287 | if ($entry->hasAttribute("src")) { |
288 | $entry->setAttribute("src", | |
289 | rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src"))); | |
cc85704f | 290 | |
6475fc7e | 291 | } |
6322fc68 | 292 | |
6475fc7e AD |
293 | } |
294 | ||
295 | $article["content"] = $r->articleContent->innerHTML . "<hr/>" . $article["content"]; | |
296 | ||
47888b3d AD |
297 | // prob not a very good idea (breaks wikipedia pages, etc) - |
298 | // inliner currently is not really fit for any random web content | |
6475fc7e | 299 | |
47888b3d AD |
300 | //$doc = new DOMDocument(); |
301 | //@$doc->loadHTML($article["content"]); | |
302 | //$xpath = new DOMXPath($doc); | |
303 | //$found = $this->inline_stuff($article, $doc, $xpath); | |
6475fc7e | 304 | } |
6322fc68 | 305 | } |
a16d7a5d | 306 | } |
cc85704f | 307 | } |
6322fc68 AD |
308 | |
309 | } | |
310 | ||
311 | $node = $doc->getElementsByTagName('body')->item(0); | |
312 | ||
313 | if ($node && $found) { | |
314 | $article["content"] = $doc->saveXML($node); | |
315 | } | |
cc85704f AD |
316 | } |
317 | ||
318 | return $article; | |
319 | } | |
106a3de9 AD |
320 | |
321 | function api_version() { | |
322 | return 2; | |
323 | } | |
324 | ||
5d429910 AD |
325 | private function handle_as_video($doc, $entry, $source_stream) { |
326 | ||
327 | $video = $doc->createElement('video'); | |
328 | $video->setAttribute("autoplay", "1"); | |
5dcc7bf1 | 329 | $video->setAttribute("controls", "1"); |
5d429910 AD |
330 | $video->setAttribute("loop", "1"); |
331 | ||
332 | $source = $doc->createElement('source'); | |
333 | $source->setAttribute("src", $source_stream); | |
334 | $source->setAttribute("type", "video/mp4"); | |
335 | ||
336 | $video->appendChild($source); | |
337 | ||
338 | $br = $doc->createElement('br'); | |
339 | $entry->parentNode->insertBefore($video, $entry); | |
340 | $entry->parentNode->insertBefore($br, $entry); | |
341 | ||
342 | $img = $doc->createElement('img'); | |
343 | $img->setAttribute("src", | |
344 | "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D"); | |
345 | ||
346 | $entry->parentNode->insertBefore($img, $entry); | |
347 | } | |
cc85704f AD |
348 | } |
349 | ?> |