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