]>
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 "<p>" . __("Uses Readability (full-text-rss) implementation by <a target='_blank' href='https://bitbucket.org/fivefilters/'>FiveFilters.org</a>");
49 print "<input dojoType=\"dijit.form.CheckBox\" id=\"enable_readability\"
50 $enable_readability_checked name=\"enable_readability\"> ";
52 print "<label for=\"enable_readability\">" . __("Extract missing content using Readability") . "</label>";
54 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
55 __("Save")."</button>";
63 $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]) == "true";
65 $this->host
->set($this, "enable_readability", $enable_readability);
67 echo __("Configuration saved");
70 private function inline_stuff($article, &$doc, $xpath) {
72 $entries = $xpath->query('(//a[@href]|//img[@src])');
76 foreach ($entries as $entry) {
77 if ($entry->hasAttribute("href")) {
81 if (preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
82 $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]);
85 if (preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
87 $tmp = fetch_file_contents($entry->getAttribute("href"));
90 $tmpdoc = new DOMDocument();
91 @$tmpdoc->loadHTML($tmp);
94 $tmpxpath = new DOMXPath($tmpdoc);
96 $source_meta = $tmpxpath->query("//meta[@name='twitter:player:stream' and contains(@content, '.mp4')]")->item(0);
97 $poster_meta = $tmpxpath->query("//meta[@property='og:image' and contains(@content,'thumbs.gfycat.com')]")->item(0);
100 $source_stream = $source_meta->getAttribute("content");
103 if ($source_stream) {
106 $poster_url = $poster_meta->getAttribute("content");
108 $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
117 // imgur .gif -> .gifv
118 if (preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry->getAttribute("href"))) {
119 $entry->setAttribute("href",
120 str_replace(".gif", ".gifv", $entry->getAttribute("href")));
123 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
125 $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
127 if (strpos($source_stream, "i.imgur.com") !== FALSE)
128 $poster_url = str_replace(".mp4", "h.jpg", $source_stream);
130 $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
136 if (preg_match("/\.youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) ||
137 preg_match("/\.youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
138 preg_match("/\.youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
139 preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) {
141 $vid_id = $matches[1];
143 $iframe = $doc->createElement("iframe");
144 $iframe->setAttribute("class", "youtube-player");
145 $iframe->setAttribute("type", "text/html");
146 $iframe->setAttribute("width", "640");
147 $iframe->setAttribute("height", "385");
148 $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
149 $iframe->setAttribute("allowfullscreen", "1");
150 $iframe->setAttribute("frameborder", "0");
152 $br = $doc->createElement('br');
153 $entry->parentNode
->insertBefore($iframe, $entry);
154 $entry->parentNode
->insertBefore($br, $entry);
159 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) {
160 $img = $doc->createElement('img');
161 $img->setAttribute("src", $entry->getAttribute("href"));
163 $br = $doc->createElement('br');
164 $entry->parentNode
->insertBefore($img, $entry);
165 $entry->parentNode
->insertBefore($br, $entry);
170 // linked albums & pages
172 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches) ||
173 preg_match("/^https?:\/\/imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
175 $album_content = fetch_file_contents($entry->getAttribute("href"),
176 false, false, false, false, 10);
178 if ($album_content) {
179 $adoc = new DOMDocument();
180 @$adoc->loadHTML($album_content);
183 $axpath = new DOMXPath($adoc);
184 $aentries = $axpath->query("//meta[@property='og:image']");
187 foreach ($aentries as $aentry) {
189 $url = str_replace("?fb", "", $aentry->getAttribute("content"));
190 $check_url = basename($url);
191 $check_url = mb_substr($check_url, 0, strrpos($check_url, "."));
193 if (!in_array($check_url, $urls)) {
194 $img = $doc->createElement('img');
195 $img->setAttribute("src", $url);
196 $entry->parentNode
->insertBefore($doc->createElement('br'), $entry);
198 $br = $doc->createElement('br');
200 $entry->parentNode
->insertBefore($img, $entry);
201 $entry->parentNode
->insertBefore($br, $entry);
203 array_push($urls, $check_url);
213 // remove tiny thumbnails
214 if ($entry->hasAttribute("src")) {
215 if ($entry->parentNode
&& $entry->parentNode
->parentNode
) {
216 $entry->parentNode
->parentNode
->removeChild($entry->parentNode
);
224 function hook_article_filter($article) {
226 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
227 $doc = new DOMDocument();
228 @$doc->loadHTML($article["content"]);
229 $xpath = new DOMXPath($doc);
231 $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
233 $found = $this->inline_stuff($article, $doc, $xpath);
235 if (function_exists("curl_init") && !$found && $this->host
->get($this, "enable_readability") &&
236 mb_strlen(strip_tags($article["content"])) <= 150) {
238 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__
)). "/lib/readability/Readability.php");
241 strpos($content_link->getAttribute("href"), "twitter.com") === FALSE &&
242 strpos($content_link->getAttribute("href"), "youtube.com") === FALSE &&
243 strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) {
245 /* link may lead to a huge video file or whatever, we need to check content type before trying to
246 parse it which p much requires curl */
248 $ch = curl_init($content_link->getAttribute("href"));
249 curl_setopt($ch, CURLOPT_TIMEOUT
, 5);
250 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
251 curl_setopt($ch, CURLOPT_HEADER
, true);
252 curl_setopt($ch, CURLOPT_NOBODY
, true);
253 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
,
254 !ini_get("safe_mode") && !ini_get("open_basedir"));
255 curl_setopt($ch, CURLOPT_USERAGENT
, SELF_USER_AGENT
);
257 @$result = curl_exec($ch);
258 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE
);
260 if ($content_type && strpos($content_type, "text/html") !== FALSE) {
262 $tmp = fetch_file_contents($content_link->getAttribute("href"));
264 //_debug("tmplen: " . mb_strlen($tmp));
266 if ($tmp && mb_strlen($tmp) < 65535 * 4) {
268 $r = new Readability($tmp, $content_link->getAttribute("href"));
272 $tmpxpath = new DOMXPath($r->dom
);
274 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
276 foreach ($entries as $entry) {
277 if ($entry->hasAttribute("href")) {
278 $entry->setAttribute("href",
279 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("href")));
283 if ($entry->hasAttribute("src")) {
284 $entry->setAttribute("src",
285 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src")));
291 $article["content"] = $r->articleContent
->innerHTML
. "<hr/>" . $article["content"];
293 // prob not a very good idea (breaks wikipedia pages, etc) -
294 // inliner currently is not really fit for any random web content
296 //$doc = new DOMDocument();
297 //@$doc->loadHTML($article["content"]);
298 //$xpath = new DOMXPath($doc);
299 //$found = $this->inline_stuff($article, $doc, $xpath);
307 $node = $doc->getElementsByTagName('body')->item(0);
309 if ($node && $found) {
310 $article["content"] = $doc->saveXML($node);
317 function api_version() {
321 private function handle_as_video($doc, $entry, $source_stream, $poster_url = false) {
323 $video = $doc->createElement('video');
324 $video->setAttribute("autoplay", "1");
325 $video->setAttribute("controls", "1");
326 $video->setAttribute("loop", "1");
328 if ($poster_url) $video->setAttribute("poster", $poster_url);
330 $source = $doc->createElement('source');
331 $source->setAttribute("src", $source_stream);
332 $source->setAttribute("type", "video/mp4");
334 $video->appendChild($source);
336 $br = $doc->createElement('br');
337 $entry->parentNode
->insertBefore($video, $entry);
338 $entry->parentNode
->insertBefore($br, $entry);
340 $img = $doc->createElement('img');
341 $img->setAttribute("src",
342 "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
344 $entry->parentNode
->insertBefore($img, $entry);