]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_redditimgur/init.php
2 class Af_RedditImgur
extends Plugin
{
7 "Inline images (and other content) in Reddit RSS feeds",
11 function init($host) {
14 $host->add_hook($host::HOOK_ARTICLE_FILTER
, $this);
15 $host->add_hook($host::HOOK_PREFS_TAB
, $this);
18 function hook_prefs_tab($args) {
19 if ($args != "prefFeeds") return;
21 print "<div id=\"af_redditimgur_prefs\" dojoType=\"dijit.layout.AccordionPane\" title=\"".__('af_redditimgur settings')."\">";
23 $enable_readability = $this->host
->get($this, "enable_readability");
24 $enable_readability_checked = $enable_readability ?
"checked" : "";
26 print "<form dojoType=\"dijit.form.Form\">";
28 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
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);
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\">";
46 print "<h3>" . __("Global settings") . "</h3>";
48 print_notice("Uses Readability (full-text-rss) implementation by <a target='_blank' href='https://bitbucket.org/fivefilters/'>FiveFilters.org</a>");
51 print "<input dojoType=\"dijit.form.CheckBox\" id=\"enable_readability\"
52 $enable_readability_checked name=\"enable_readability\"> ";
54 print "<label for=\"enable_readability\">" . __("Extract missing content using Readability") . "</label>";
56 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
57 __("Save")."</button>";
65 $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]) == "true";
67 $this->host
->set($this, "enable_readability", $enable_readability);
69 echo __("Configuration saved");
72 private function inline_stuff($article, &$doc, $xpath) {
74 $entries = $xpath->query('(//a[@href]|//img[@src])');
78 foreach ($entries as $entry) {
79 if ($entry->hasAttribute("href")) {
83 if (preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
85 $tmp = fetch_file_contents($entry->getAttribute("href"));
88 $tmpdoc = new DOMDocument();
89 @$tmpdoc->loadHTML($tmp);
92 $tmpxpath = new DOMXPath($tmpdoc);
94 $source_meta = $tmpxpath->query("//meta[@property='og:video']")->item(0);
95 $poster_meta = $tmpxpath->query("//meta[@property='og:image' and contains(@content,'thumbs.gfycat.com')]")->item(0);
98 $source_stream = $source_meta->getAttribute("content");
101 if ($source_stream) {
104 $poster_url = $poster_meta->getAttribute("content");
106 $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
115 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
117 $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
119 if (strpos($source_stream, "i.imgur.com") !== FALSE)
120 $poster_url = str_replace(".mp4", "h.jpg", $source_stream);
122 $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
128 if (preg_match("/\.youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) ||
129 preg_match("/\.youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
130 preg_match("/\.youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
131 preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) {
133 $vid_id = $matches[1];
135 $iframe = $doc->createElement("iframe");
136 $iframe->setAttribute("class", "youtube-player");
137 $iframe->setAttribute("type", "text/html");
138 $iframe->setAttribute("width", "640");
139 $iframe->setAttribute("height", "385");
140 $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
141 $iframe->setAttribute("allowfullscreen", "1");
142 $iframe->setAttribute("frameborder", "0");
144 $br = $doc->createElement('br');
145 $entry->parentNode
->insertBefore($iframe, $entry);
146 $entry->parentNode
->insertBefore($br, $entry);
151 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) {
152 $img = $doc->createElement('img');
153 $img->setAttribute("src", $entry->getAttribute("href"));
155 $br = $doc->createElement('br');
156 $entry->parentNode
->insertBefore($img, $entry);
157 $entry->parentNode
->insertBefore($br, $entry);
162 // links to imgur pages
164 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
166 $token = $matches[2];
168 $album_content = fetch_file_contents($entry->getAttribute("href"),
169 false, false, false, false, 10);
171 if ($album_content && $token) {
172 $adoc = new DOMDocument();
173 @$adoc->loadHTML($album_content);
176 $axpath = new DOMXPath($adoc);
177 $aentries = $axpath->query('(//img[@src])');
179 foreach ($aentries as $aentry) {
180 if (preg_match("/\/\/i.imgur.com\/$token\./", $aentry->getAttribute("src"))) {
181 $img = $doc->createElement('img');
182 $img->setAttribute("src", $aentry->getAttribute("src"));
184 $br = $doc->createElement('br');
186 $entry->parentNode
->insertBefore($img, $entry);
187 $entry->parentNode
->insertBefore($br, $entry);
198 // linked albums, ffs
199 if (preg_match("/^https?:\/\/imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
201 $album_content = fetch_file_contents($entry->getAttribute("href"),
202 false, false, false, false, 10);
204 if ($album_content) {
205 $adoc = new DOMDocument();
206 @$adoc->loadHTML($album_content);
209 $axpath = new DOMXPath($adoc);
210 $aentries = $axpath->query("//meta[@property='og:image']");
213 foreach ($aentries as $aentry) {
215 if (!in_array($aentry->getAttribute("content"), $urls)) {
216 $img = $doc->createElement('img');
217 $img->setAttribute("src", $aentry->getAttribute("content"));
218 $entry->parentNode
->insertBefore($doc->createElement('br'), $entry);
220 $br = $doc->createElement('br');
222 $entry->parentNode
->insertBefore($img, $entry);
223 $entry->parentNode
->insertBefore($br, $entry);
225 array_push($urls, $aentry->getAttribute("content"));
235 // remove tiny thumbnails
236 if ($entry->hasAttribute("src")) {
237 if ($entry->parentNode
&& $entry->parentNode
->parentNode
) {
238 $entry->parentNode
->parentNode
->removeChild($entry->parentNode
);
246 function hook_article_filter($article) {
248 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
249 $doc = new DOMDocument();
250 @$doc->loadHTML($article["content"]);
251 $xpath = new DOMXPath($doc);
253 $found = $this->inline_stuff($article, $doc, $xpath);
255 if (function_exists("curl_init") && !$found && $this->host
->get($this, "enable_readability") &&
256 mb_strlen(strip_tags($article["content"])) <= 150) {
258 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__
)). "/lib/readability/Readability.php");
260 $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
263 strpos($content_link->getAttribute("href"), "twitter.com") === FALSE &&
264 strpos($content_link->getAttribute("href"), "youtube.com") === FALSE &&
265 strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) {
267 /* link may lead to a huge video file or whatever, we need to check content type before trying to
268 parse it which p much requires curl */
270 $ch = curl_init($content_link->getAttribute("href"));
271 curl_setopt($ch, CURLOPT_TIMEOUT
, 5);
272 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
273 curl_setopt($ch, CURLOPT_HEADER
, true);
274 curl_setopt($ch, CURLOPT_NOBODY
, true);
275 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
,
276 !ini_get("safe_mode") && !ini_get("open_basedir"));
277 curl_setopt($ch, CURLOPT_USERAGENT
, SELF_USER_AGENT
);
279 @$result = curl_exec($ch);
280 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE
);
282 if ($content_type && strpos($content_type, "text/html") !== FALSE) {
284 $tmp = fetch_file_contents($content_link->getAttribute("href"));
287 $r = new Readability($tmp, $content_link->getAttribute("href"));
291 $tmpxpath = new DOMXPath($r->dom
);
293 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
295 foreach ($entries as $entry) {
296 if ($entry->hasAttribute("href")) {
297 $entry->setAttribute("href",
298 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("href")));
302 if ($entry->hasAttribute("src")) {
303 $entry->setAttribute("src",
304 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src")));
310 $article["content"] = $r->articleContent
->innerHTML
. "<hr/>" . $article["content"];
312 // prob not a very good idea (breaks wikipedia pages, etc) -
313 // inliner currently is not really fit for any random web content
315 //$doc = new DOMDocument();
316 //@$doc->loadHTML($article["content"]);
317 //$xpath = new DOMXPath($doc);
318 //$found = $this->inline_stuff($article, $doc, $xpath);
326 $node = $doc->getElementsByTagName('body')->item(0);
328 if ($node && $found) {
329 $article["content"] = $doc->saveXML($node);
336 function api_version() {
340 private function handle_as_video($doc, $entry, $source_stream, $poster_url = false) {
342 $video = $doc->createElement('video');
343 $video->setAttribute("autoplay", "1");
344 $video->setAttribute("controls", "1");
345 $video->setAttribute("loop", "1");
347 if ($poster_url) $video->setAttribute("poster", $poster_url);
349 $source = $doc->createElement('source');
350 $source->setAttribute("src", $source_stream);
351 $source->setAttribute("type", "video/mp4");
353 $video->appendChild($source);
355 $br = $doc->createElement('br');
356 $entry->parentNode
->insertBefore($video, $entry);
357 $entry->parentNode
->insertBefore($br, $entry);
359 $img = $doc->createElement('img');
360 $img->setAttribute("src",
361 "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
363 $entry->parentNode
->insertBefore($img, $entry);