]>
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 $enable_dupecheck = $this->host
->get($this, "enable_dupecheck");
27 $enable_dupecheck_checked = $enable_dupecheck ?
"checked" : "";
29 print "<form dojoType=\"dijit.form.Form\">";
31 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
33 if (this.validate()) {
34 console.log(dojo.objectToQuery(this.getValues()));
35 new Ajax.Request('backend.php', {
36 parameters: dojo.objectToQuery(this.getValues()),
37 onComplete: function(transport) {
38 notify_info(transport.responseText);
45 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
46 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">";
47 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"af_redditimgur\">";
49 print "<h3>" . __("Global settings") . "</h3>";
51 print_notice("Uses Readability (full-text-rss) implementation by <a target='_blank' href='https://bitbucket.org/fivefilters/'>FiveFilters.org</a>");
54 print "<input dojoType=\"dijit.form.CheckBox\" id=\"enable_readability\"
55 $enable_readability_checked name=\"enable_readability\"> ";
57 print "<label for=\"enable_readability\">" . __("Extract missing content using Readability") . "</label>";
61 print "<input dojoType=\"dijit.form.CheckBox\" id=\"enable_dupecheck\"
62 $enable_dupecheck_checked name=\"enable_dupecheck\"> ";
64 print "<label for=\"enable_dupecheck\">" . __("Mark duplicates as read using content links") . "</label>";
66 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
67 __("Save")."</button>";
75 $enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]) == "true";
76 $enable_dupecheck = checkbox_to_sql_bool($_POST["enable_dupecheck"]) == "true";
78 $this->host
->set($this, "enable_readability", $enable_readability);
79 $this->host
->set($this, "enable_dupecheck", $enable_dupecheck);
81 echo __("Configuration saved");
84 private function inline_stuff($article, &$doc, $xpath) {
86 $entries = $xpath->query('(//a[@href]|//img[@src])');
90 foreach ($entries as $entry) {
91 if ($entry->hasAttribute("href")) {
95 if (preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
96 $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]);
99 if (preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
101 $tmp = fetch_file_contents($entry->getAttribute("href"));
104 $tmpdoc = new DOMDocument();
105 @$tmpdoc->loadHTML($tmp);
108 $tmpxpath = new DOMXPath($tmpdoc);
110 $source_meta = $tmpxpath->query("//meta[@property='og:video']")->item(0);
111 $poster_meta = $tmpxpath->query("//meta[@property='og:image' and contains(@content,'thumbs.gfycat.com')]")->item(0);
114 $source_stream = $source_meta->getAttribute("content");
117 if ($source_stream) {
120 $poster_url = $poster_meta->getAttribute("content");
122 $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
131 // imgur .gif -> .gifv
132 if (preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry->getAttribute("href"))) {
133 $entry->setAttribute("href",
134 str_replace(".gif", ".gifv", $entry->getAttribute("href")));
137 if (preg_match("/\.(gifv)$/i", $entry->getAttribute("href"))) {
139 $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
141 if (strpos($source_stream, "i.imgur.com") !== FALSE)
142 $poster_url = str_replace(".mp4", "h.jpg", $source_stream);
144 $this->handle_as_video($doc, $entry, $source_stream, $poster_url);
150 if (preg_match("/\.youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) ||
151 preg_match("/\.youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
152 preg_match("/\.youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
153 preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) {
155 $vid_id = $matches[1];
157 $iframe = $doc->createElement("iframe");
158 $iframe->setAttribute("class", "youtube-player");
159 $iframe->setAttribute("type", "text/html");
160 $iframe->setAttribute("width", "640");
161 $iframe->setAttribute("height", "385");
162 $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
163 $iframe->setAttribute("allowfullscreen", "1");
164 $iframe->setAttribute("frameborder", "0");
166 $br = $doc->createElement('br');
167 $entry->parentNode
->insertBefore($iframe, $entry);
168 $entry->parentNode
->insertBefore($br, $entry);
173 if (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href"))) {
174 $img = $doc->createElement('img');
175 $img->setAttribute("src", $entry->getAttribute("href"));
177 $br = $doc->createElement('br');
178 $entry->parentNode
->insertBefore($img, $entry);
179 $entry->parentNode
->insertBefore($br, $entry);
184 // linked albums & pages
186 if (preg_match("/^https?:\/\/(m\.)?imgur.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches) ||
187 preg_match("/^https?:\/\/imgur.com\/(a|album|gallery)\/[^\.]+$/", $entry->getAttribute("href"), $matches)) {
189 $album_content = fetch_file_contents($entry->getAttribute("href"),
190 false, false, false, false, 10);
192 if ($album_content) {
193 $adoc = new DOMDocument();
194 @$adoc->loadHTML($album_content);
197 $axpath = new DOMXPath($adoc);
198 $aentries = $axpath->query("//meta[@property='og:image']");
201 foreach ($aentries as $aentry) {
203 $url = str_replace("?fb", "", $aentry->getAttribute("content"));
205 if (!in_array($url, $urls)) {
206 $img = $doc->createElement('img');
207 $img->setAttribute("src", $url);
208 $entry->parentNode
->insertBefore($doc->createElement('br'), $entry);
210 $br = $doc->createElement('br');
212 $entry->parentNode
->insertBefore($img, $entry);
213 $entry->parentNode
->insertBefore($br, $entry);
215 array_push($urls, $url);
225 // remove tiny thumbnails
226 if ($entry->hasAttribute("src")) {
227 if ($entry->parentNode
&& $entry->parentNode
->parentNode
) {
228 $entry->parentNode
->parentNode
->removeChild($entry->parentNode
);
236 function hook_article_filter($article) {
238 if (strpos($article["link"], "reddit.com/r/") !== FALSE) {
239 $doc = new DOMDocument();
240 @$doc->loadHTML($article["content"]);
241 $xpath = new DOMXPath($doc);
243 $content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
245 if ($content_link && $this->host
->get($this, "enable_dupecheck")) {
247 if (DB_TYPE
== "pgsql") {
248 $date_qpart = "date_entered < NOW() - INTERVAL '1 day' ";
250 $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY) ";
253 $content_href = db_escape_string($content_link->getAttribute("href"));
254 $owner_uid = $article["owner_uid"];
255 $guid = db_escape_string($article["guid_hashed"]);
257 $result = db_query("SELECT id FROM ttrss_entries, ttrss_user_entries
259 content LIKE '%$content_href%' AND
262 owner_uid = $owner_uid
266 if (db_num_rows($result) != 0) {
268 $article["force_catchup"] = true;
272 if (!$found) $found = $this->inline_stuff($article, $doc, $xpath);
274 if (function_exists("curl_init") && !$found && $this->host
->get($this, "enable_readability") &&
275 mb_strlen(strip_tags($article["content"])) <= 150) {
277 if (!class_exists("Readability")) require_once(dirname(dirname(__DIR__
)). "/lib/readability/Readability.php");
280 strpos($content_link->getAttribute("href"), "twitter.com") === FALSE &&
281 strpos($content_link->getAttribute("href"), "youtube.com") === FALSE &&
282 strpos($content_link->getAttribute("href"), "reddit.com") === FALSE) {
284 /* link may lead to a huge video file or whatever, we need to check content type before trying to
285 parse it which p much requires curl */
287 $ch = curl_init($content_link->getAttribute("href"));
288 curl_setopt($ch, CURLOPT_TIMEOUT
, 5);
289 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
290 curl_setopt($ch, CURLOPT_HEADER
, true);
291 curl_setopt($ch, CURLOPT_NOBODY
, true);
292 curl_setopt($ch, CURLOPT_FOLLOWLOCATION
,
293 !ini_get("safe_mode") && !ini_get("open_basedir"));
294 curl_setopt($ch, CURLOPT_USERAGENT
, SELF_USER_AGENT
);
296 @$result = curl_exec($ch);
297 $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE
);
299 if ($content_type && strpos($content_type, "text/html") !== FALSE) {
301 $tmp = fetch_file_contents($content_link->getAttribute("href"));
304 $r = new Readability($tmp, $content_link->getAttribute("href"));
308 $tmpxpath = new DOMXPath($r->dom
);
310 $entries = $tmpxpath->query('(//a[@href]|//img[@src])');
312 foreach ($entries as $entry) {
313 if ($entry->hasAttribute("href")) {
314 $entry->setAttribute("href",
315 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("href")));
319 if ($entry->hasAttribute("src")) {
320 $entry->setAttribute("src",
321 rewrite_relative_url($content_link->getAttribute("href"), $entry->getAttribute("src")));
327 $article["content"] = $r->articleContent
->innerHTML
. "<hr/>" . $article["content"];
329 // prob not a very good idea (breaks wikipedia pages, etc) -
330 // inliner currently is not really fit for any random web content
332 //$doc = new DOMDocument();
333 //@$doc->loadHTML($article["content"]);
334 //$xpath = new DOMXPath($doc);
335 //$found = $this->inline_stuff($article, $doc, $xpath);
343 $node = $doc->getElementsByTagName('body')->item(0);
345 if ($node && $found) {
346 $article["content"] = $doc->saveXML($node);
353 function api_version() {
357 private function handle_as_video($doc, $entry, $source_stream, $poster_url = false) {
359 $video = $doc->createElement('video');
360 $video->setAttribute("autoplay", "1");
361 $video->setAttribute("controls", "1");
362 $video->setAttribute("loop", "1");
364 if ($poster_url) $video->setAttribute("poster", $poster_url);
366 $source = $doc->createElement('source');
367 $source->setAttribute("src", $source_stream);
368 $source->setAttribute("type", "video/mp4");
370 $video->appendChild($source);
372 $br = $doc->createElement('br');
373 $entry->parentNode
->insertBefore($video, $entry);
374 $entry->parentNode
->insertBefore($br, $entry);
376 $img = $doc->createElement('img');
377 $img->setAttribute("src",
378 "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
380 $entry->parentNode
->insertBefore($img, $entry);