2012-11-26 10:33:18 +00:00
|
|
|
<?php
|
2012-12-30 08:31:30 +00:00
|
|
|
class Af_RedditImgur extends Plugin {
|
2017-12-03 07:55:41 +00:00
|
|
|
|
|
|
|
/* @var PluginHost $host */
|
2012-12-23 10:52:18 +00:00
|
|
|
private $host;
|
2020-12-18 05:12:31 +00:00
|
|
|
private $domain_blacklist = [ "github.com" ];
|
2012-12-23 10:52:18 +00:00
|
|
|
|
2012-12-25 06:02:08 +00:00
|
|
|
function about() {
|
2012-12-24 11:39:42 +00:00
|
|
|
return array(1.0,
|
2015-07-06 19:46:46 +00:00
|
|
|
"Inline images (and other content) in Reddit RSS feeds",
|
2012-12-24 11:39:42 +00:00
|
|
|
"fox");
|
|
|
|
}
|
|
|
|
|
2016-01-26 08:45:47 +00:00
|
|
|
function flags() {
|
|
|
|
return array("needs_curl" => true);
|
|
|
|
}
|
|
|
|
|
2012-12-25 06:02:08 +00:00
|
|
|
function init($host) {
|
2012-12-23 10:52:18 +00:00
|
|
|
$this->host = $host;
|
|
|
|
|
|
|
|
$host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
|
2015-07-06 19:44:43 +00:00
|
|
|
$host->add_hook($host::HOOK_PREFS_TAB, $this);
|
2020-12-20 08:28:48 +00:00
|
|
|
|
|
|
|
$host->add_hook($host::HOOK_RENDER_ARTICLE, $this);
|
|
|
|
$host->add_hook($host::HOOK_RENDER_ARTICLE_CDM, $this);
|
2020-12-20 10:12:50 +00:00
|
|
|
$host->add_hook($host::HOOK_RENDER_ARTICLE_API, $this);
|
2015-07-06 19:44:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function hook_prefs_tab($args) {
|
|
|
|
if ($args != "prefFeeds") return;
|
|
|
|
|
2020-10-01 10:20:07 +00:00
|
|
|
print "<div dojoType=\"dijit.layout.AccordionPane\"
|
2018-12-06 05:56:28 +00:00
|
|
|
title=\"<i class='material-icons'>extension</i> ".__('Reddit content settings (af_redditimgur)')."\">";
|
2015-07-06 19:44:43 +00:00
|
|
|
|
|
|
|
$enable_readability = $this->host->get($this, "enable_readability");
|
2016-01-31 11:14:31 +00:00
|
|
|
$enable_content_dupcheck = $this->host->get($this, "enable_content_dupcheck");
|
2020-12-20 08:28:48 +00:00
|
|
|
$reddit_to_teddit = $this->host->get($this, "reddit_to_teddit");
|
2016-08-01 18:03:36 +00:00
|
|
|
|
2018-06-21 05:12:11 +00:00
|
|
|
if (version_compare(PHP_VERSION, '5.6.0', '<')) {
|
|
|
|
print_error("Readability requires PHP version 5.6.");
|
|
|
|
}
|
|
|
|
|
2019-03-05 16:45:48 +00:00
|
|
|
print "<form dojoType='dijit.form.Form'>";
|
2015-07-06 19:44:43 +00:00
|
|
|
|
2019-03-05 16:45:48 +00:00
|
|
|
print "<script type='dojo/method' event='onSubmit' args='evt'>
|
2015-07-06 19:44:43 +00:00
|
|
|
evt.preventDefault();
|
|
|
|
if (this.validate()) {
|
|
|
|
console.log(dojo.objectToQuery(this.getValues()));
|
|
|
|
new Ajax.Request('backend.php', {
|
|
|
|
parameters: dojo.objectToQuery(this.getValues()),
|
|
|
|
onComplete: function(transport) {
|
2018-12-02 17:56:30 +00:00
|
|
|
Notify.info(transport.responseText);
|
2015-07-06 19:44:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
//this.reset();
|
|
|
|
}
|
|
|
|
</script>";
|
|
|
|
|
2017-02-10 11:36:21 +00:00
|
|
|
print_hidden("op", "pluginhandler");
|
|
|
|
print_hidden("method", "save");
|
|
|
|
print_hidden("plugin", "af_redditimgur");
|
2015-07-06 19:44:43 +00:00
|
|
|
|
2019-03-05 16:45:48 +00:00
|
|
|
print "<fieldset class='narrow'>";
|
|
|
|
print "<label class='checkbox'>";
|
2017-02-10 11:57:25 +00:00
|
|
|
print_checkbox("enable_readability", $enable_readability);
|
2019-04-17 05:53:33 +00:00
|
|
|
print " " . __("Extract missing content using Readability (requires af_readability)") . "</label>";
|
2019-03-05 16:45:48 +00:00
|
|
|
print "</fieldset>";
|
2016-08-01 18:03:36 +00:00
|
|
|
|
2019-03-05 16:45:48 +00:00
|
|
|
print "<fieldset class='narrow'>";
|
|
|
|
print "<label class='checkbox'>";
|
2017-02-10 11:57:25 +00:00
|
|
|
print_checkbox("enable_content_dupcheck", $enable_content_dupcheck);
|
2019-03-05 16:45:48 +00:00
|
|
|
print " " . __("Enable additional duplicate checking") . "</label>";
|
|
|
|
print "</fieldset>";
|
|
|
|
|
2020-12-20 08:28:48 +00:00
|
|
|
print "<fieldset class='narrow'>";
|
|
|
|
print "<label class='checkbox'>";
|
|
|
|
print_checkbox("reddit_to_teddit", $reddit_to_teddit);
|
|
|
|
print " " . T_sprintf("Rewrite Reddit URLs to %s",
|
|
|
|
"<a target=\"_blank\" href=\"https://teddit.net/about\">Teddit</a>") . "</label>";
|
|
|
|
|
2019-03-05 16:45:48 +00:00
|
|
|
print_button("submit", __("Save"), 'class="alt-primary"');
|
2015-07-06 19:44:43 +00:00
|
|
|
print "</form>";
|
|
|
|
|
|
|
|
print "</div>";
|
|
|
|
}
|
|
|
|
|
|
|
|
function save() {
|
2017-12-02 11:07:48 +00:00
|
|
|
$enable_readability = checkbox_to_sql_bool($_POST["enable_readability"]);
|
|
|
|
$enable_content_dupcheck = checkbox_to_sql_bool($_POST["enable_content_dupcheck"]);
|
2020-12-20 08:28:48 +00:00
|
|
|
$reddit_to_teddit = checkbox_to_sql_bool($_POST["reddit_to_teddit"]);
|
2015-07-27 10:29:27 +00:00
|
|
|
|
2016-01-31 11:14:31 +00:00
|
|
|
$this->host->set($this, "enable_readability", $enable_readability, false);
|
2020-12-20 08:28:48 +00:00
|
|
|
$this->host->set($this, "reddit_to_teddit", $reddit_to_teddit, false);
|
2016-01-31 11:14:31 +00:00
|
|
|
$this->host->set($this, "enable_content_dupcheck", $enable_content_dupcheck);
|
2015-07-06 19:44:43 +00:00
|
|
|
|
|
|
|
echo __("Configuration saved");
|
2012-12-23 10:52:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 17:57:36 +00:00
|
|
|
/**
|
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
|
|
|
*/
|
2018-11-30 05:34:29 +00:00
|
|
|
private function inline_stuff($article, &$doc, $xpath) {
|
2012-11-26 10:33:18 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
$entries = $xpath->query('(//a[@href]|//img[@src])');
|
2017-09-16 07:08:30 +00:00
|
|
|
$img_entries = $xpath->query("(//img[@src])");
|
2012-11-26 10:33:18 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
$found = false;
|
2018-05-26 07:25:39 +00:00
|
|
|
//$debug = 1;
|
2013-03-12 06:47:06 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
foreach ($entries as $entry) {
|
2020-09-17 16:02:27 +00:00
|
|
|
if ($entry->hasAttribute("href") && strpos($entry->getAttribute("href"), "reddit.com") === false) {
|
2015-05-15 05:06:18 +00:00
|
|
|
|
2021-01-03 07:55:57 +00:00
|
|
|
if ($this->is_blacklisted($entry->getAttribute("href")))
|
|
|
|
continue;
|
|
|
|
|
2018-11-30 05:34:29 +00:00
|
|
|
Debug::log("processing href: " . $entry->getAttribute("href"), Debug::$LOG_VERBOSE);
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
$matches = array();
|
2015-07-05 11:56:57 +00:00
|
|
|
|
2018-05-26 07:25:39 +00:00
|
|
|
if (!$found && preg_match("/^https?:\/\/twitter.com\/(.*?)\/status\/(.*)/", $entry->getAttribute("href"), $matches)) {
|
2018-11-30 05:34:29 +00:00
|
|
|
Debug::log("handling as twitter: " . $matches[1] . " " . $matches[2], Debug::$LOG_VERBOSE);
|
2016-08-01 18:03:36 +00:00
|
|
|
|
2020-09-22 06:04:33 +00:00
|
|
|
$oembed_result = UrlHelper::fetch("https://publish.twitter.com/oembed?url=" . urlencode($entry->getAttribute("href")));
|
2016-08-01 18:03:36 +00:00
|
|
|
|
|
|
|
if ($oembed_result) {
|
|
|
|
$oembed_result = json_decode($oembed_result, true);
|
|
|
|
|
|
|
|
if ($oembed_result && isset($oembed_result["html"])) {
|
|
|
|
|
|
|
|
$tmp = new DOMDocument();
|
2020-10-01 10:20:07 +00:00
|
|
|
if (@$tmp->loadHTML('<?xml encoding="utf-8" ?>' . $oembed_result["html"])) {
|
2016-08-01 18:03:36 +00:00
|
|
|
$p = $doc->createElement("p");
|
|
|
|
|
|
|
|
$p->appendChild($doc->importNode(
|
|
|
|
$tmp->getElementsByTagName("blockquote")->item(0), TRUE));
|
|
|
|
|
|
|
|
$br = $doc->createElement('br');
|
|
|
|
$entry->parentNode->insertBefore($p, $entry);
|
|
|
|
$entry->parentNode->insertBefore($br, $entry);
|
|
|
|
|
|
|
|
$found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 18:07:18 +00:00
|
|
|
if (!$found && preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
|
2015-07-10 23:08:53 +00:00
|
|
|
$entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]);
|
|
|
|
}
|
|
|
|
|
2016-08-01 18:07:18 +00:00
|
|
|
if (!$found && preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) {
|
2015-07-05 11:56:57 +00:00
|
|
|
|
2018-11-30 05:34:29 +00:00
|
|
|
Debug::log("Handling as Gfycat", Debug::$LOG_VERBOSE);
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2018-09-09 09:45:02 +00:00
|
|
|
$source_stream = 'https://giant.gfycat.com/' . $matches[2] . '.mp4';
|
|
|
|
$poster_url = 'https://thumbs.gfycat.com/' . $matches[2] . '-mobile.jpg';
|
2015-07-05 11:56:57 +00:00
|
|
|
|
2018-09-10 13:09:38 +00:00
|
|
|
$content_type = $this->get_content_type($source_stream);
|
|
|
|
|
2020-09-17 16:02:27 +00:00
|
|
|
if (strpos($content_type, "video/") !== false) {
|
2018-09-10 13:09:38 +00:00
|
|
|
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
|
|
|
$found = 1;
|
|
|
|
}
|
2015-07-07 05:44:31 +00:00
|
|
|
}
|
2015-05-18 09:05:30 +00:00
|
|
|
|
2017-09-16 07:08:30 +00:00
|
|
|
if (!$found && preg_match("/https?:\/\/v\.redd\.it\/(.*)$/i", $entry->getAttribute("href"), $matches)) {
|
|
|
|
|
2018-11-30 05:34:29 +00:00
|
|
|
Debug::log("Handling as reddit inline video", Debug::$LOG_VERBOSE);
|
2017-09-16 07:08:30 +00:00
|
|
|
|
|
|
|
$img = $img_entries->item(0);
|
|
|
|
|
|
|
|
if ($img) {
|
|
|
|
$poster_url = $img->getAttribute("src");
|
|
|
|
} else {
|
|
|
|
$poster_url = false;
|
|
|
|
}
|
|
|
|
|
2017-09-17 18:34:49 +00:00
|
|
|
// Get original article URL from v.redd.it redirects
|
|
|
|
$source_article_url = $this->get_location($matches[0]);
|
2018-11-30 05:34:29 +00:00
|
|
|
Debug::log("Resolved ".$matches[0]." to ".$source_article_url, Debug::$LOG_VERBOSE);
|
2017-09-17 18:34:49 +00:00
|
|
|
|
2017-09-17 06:18:05 +00:00
|
|
|
$source_stream = false;
|
2017-09-17 18:34:49 +00:00
|
|
|
|
|
|
|
if ($source_article_url) {
|
2020-09-22 06:04:33 +00:00
|
|
|
$j = json_decode(UrlHelper::fetch($source_article_url.".json"), true);
|
2017-09-17 18:34:49 +00:00
|
|
|
|
|
|
|
if ($j) {
|
|
|
|
foreach ($j as $listing) {
|
|
|
|
foreach ($listing["data"]["children"] as $child) {
|
|
|
|
if ($child["data"]["url"] == $matches[0]) {
|
|
|
|
try {
|
|
|
|
$source_stream = $child["data"]["media"]["reddit_video"]["fallback_url"];
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
}
|
|
|
|
break 2;
|
2017-09-17 06:18:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$source_stream) {
|
|
|
|
$source_stream = "https://v.redd.it/" . $matches[1] . "/DASH_600_K";
|
|
|
|
}
|
2017-09-16 07:08:30 +00:00
|
|
|
|
|
|
|
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
|
|
|
$found = 1;
|
|
|
|
}
|
|
|
|
|
2017-01-29 11:36:37 +00:00
|
|
|
if (!$found && preg_match("/https?:\/\/(www\.)?streamable.com\//i", $entry->getAttribute("href"))) {
|
|
|
|
|
2018-11-30 05:34:29 +00:00
|
|
|
Debug::log("Handling as Streamable", Debug::$LOG_VERBOSE);
|
2017-01-29 11:36:37 +00:00
|
|
|
|
2020-09-22 06:04:33 +00:00
|
|
|
$tmp = UrlHelper::fetch($entry->getAttribute("href"));
|
2017-01-29 11:36:37 +00:00
|
|
|
|
|
|
|
if ($tmp) {
|
|
|
|
$tmpdoc = new DOMDocument();
|
|
|
|
|
|
|
|
if (@$tmpdoc->loadHTML($tmp)) {
|
|
|
|
$tmpxpath = new DOMXPath($tmpdoc);
|
|
|
|
|
|
|
|
$source_node = $tmpxpath->query("//video[contains(@class,'video-player-tag')]//source[contains(@src, '.mp4')]")->item(0);
|
|
|
|
$poster_node = $tmpxpath->query("//video[contains(@class,'video-player-tag') and @poster]")->item(0);
|
|
|
|
|
|
|
|
if ($source_node && $poster_node) {
|
|
|
|
$source_stream = $source_node->getAttribute("src");
|
|
|
|
$poster_url = $poster_node->getAttribute("poster");
|
|
|
|
|
|
|
|
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
|
|
|
$found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-10 23:08:53 +00:00
|
|
|
// imgur .gif -> .gifv
|
2016-08-01 18:07:18 +00:00
|
|
|
if (!$found && preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry->getAttribute("href"))) {
|
2018-11-30 05:34:29 +00:00
|
|
|
Debug::log("Handling as imgur gif (->gifv)", Debug::$LOG_VERBOSE);
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2015-07-10 23:08:53 +00:00
|
|
|
$entry->setAttribute("href",
|
|
|
|
str_replace(".gif", ".gifv", $entry->getAttribute("href")));
|
|
|
|
}
|
|
|
|
|
2016-08-02 13:45:06 +00:00
|
|
|
if (!$found && preg_match("/\.(gifv|mp4)$/i", $entry->getAttribute("href"))) {
|
2018-11-30 05:34:29 +00:00
|
|
|
Debug::log("Handling as imgur gifv", Debug::$LOG_VERBOSE);
|
2015-07-05 11:56:57 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
$source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href"));
|
2015-07-10 06:02:52 +00:00
|
|
|
|
2020-09-17 16:02:27 +00:00
|
|
|
if (strpos($source_stream, "imgur.com") !== false)
|
2015-07-10 06:02:52 +00:00
|
|
|
$poster_url = str_replace(".mp4", "h.jpg", $source_stream);
|
|
|
|
|
2018-11-30 05:34:29 +00:00
|
|
|
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
2015-05-18 09:05:30 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
$found = true;
|
|
|
|
}
|
2015-05-14 09:03:06 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
$matches = array();
|
2020-01-04 16:20:33 +00:00
|
|
|
if (!$found && (preg_match("/youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) ||
|
2016-03-20 09:05:23 +00:00
|
|
|
preg_match("/youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
|
|
|
|
preg_match("/youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) ||
|
2020-01-04 16:20:33 +00:00
|
|
|
preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches))) {
|
2015-07-06 18:44:38 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
$vid_id = $matches[1];
|
2015-07-06 18:44:38 +00:00
|
|
|
|
2018-11-30 05:34:29 +00:00
|
|
|
Debug::log("Handling as youtube: $vid_id", Debug::$LOG_VERBOSE);
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
$iframe = $doc->createElement("iframe");
|
|
|
|
$iframe->setAttribute("class", "youtube-player");
|
|
|
|
$iframe->setAttribute("type", "text/html");
|
|
|
|
$iframe->setAttribute("width", "640");
|
|
|
|
$iframe->setAttribute("height", "385");
|
|
|
|
$iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id");
|
|
|
|
$iframe->setAttribute("allowfullscreen", "1");
|
|
|
|
$iframe->setAttribute("frameborder", "0");
|
2015-07-06 18:44:38 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
$br = $doc->createElement('br');
|
|
|
|
$entry->parentNode->insertBefore($iframe, $entry);
|
|
|
|
$entry->parentNode->insertBefore($br, $entry);
|
2015-07-06 18:44:38 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
$found = true;
|
|
|
|
}
|
2015-07-06 18:44:38 +00:00
|
|
|
|
2020-01-04 16:22:58 +00:00
|
|
|
if (!$found && (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?[$\?]/i", $entry->getAttribute("href")) ||
|
2020-09-17 16:02:27 +00:00
|
|
|
mb_strpos($entry->getAttribute("href"), "i.reddituploads.com") !== false ||
|
|
|
|
mb_strpos($this->get_content_type($entry->getAttribute("href")), "image/") !== false)) {
|
2016-07-07 06:04:38 +00:00
|
|
|
|
2018-11-30 05:34:29 +00:00
|
|
|
Debug::log("Handling as a picture", Debug::$LOG_VERBOSE);
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
$img = $doc->createElement('img');
|
|
|
|
$img->setAttribute("src", $entry->getAttribute("href"));
|
2012-11-26 10:33:18 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
$br = $doc->createElement('br');
|
|
|
|
$entry->parentNode->insertBefore($img, $entry);
|
|
|
|
$entry->parentNode->insertBefore($br, $entry);
|
2013-03-12 06:47:06 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
$found = true;
|
|
|
|
}
|
2013-03-12 06:47:06 +00:00
|
|
|
|
2020-01-04 16:21:05 +00:00
|
|
|
// imgur via link rel="image_src" href="..."
|
|
|
|
if (!$found && preg_match("/imgur/", $entry->getAttribute("href"))) {
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2020-01-04 16:21:05 +00:00
|
|
|
Debug::log("handling as imgur page/whatever", Debug::$LOG_VERBOSE);
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2020-09-22 06:04:33 +00:00
|
|
|
$content = UrlHelper::fetch(["url" => $entry->getAttribute("href"),
|
2020-01-04 16:21:05 +00:00
|
|
|
"http_accept" => "text/*"]);
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2020-01-04 16:21:05 +00:00
|
|
|
if ($content) {
|
|
|
|
$cdoc = new DOMDocument();
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2020-01-04 16:21:05 +00:00
|
|
|
if (@$cdoc->loadHTML($content)) {
|
|
|
|
$cxpath = new DOMXPath($cdoc);
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2020-01-04 16:21:05 +00:00
|
|
|
$rel_image = $cxpath->query("//link[@rel='image_src']")->item(0);
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2020-01-04 16:21:05 +00:00
|
|
|
if ($rel_image) {
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2020-01-04 16:21:05 +00:00
|
|
|
$img = $doc->createElement('img');
|
|
|
|
$img->setAttribute("src", $rel_image->getAttribute("href"));
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2020-01-04 16:21:05 +00:00
|
|
|
$br = $doc->createElement('br');
|
|
|
|
$entry->parentNode->insertBefore($img, $entry);
|
|
|
|
$entry->parentNode->insertBefore($br, $entry);
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2020-01-04 16:21:05 +00:00
|
|
|
$found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2015-10-17 19:11:12 +00:00
|
|
|
// wtf is this even
|
2016-08-01 18:07:18 +00:00
|
|
|
if (!$found && preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) {
|
2015-10-17 19:11:12 +00:00
|
|
|
$img_id = $matches[1];
|
|
|
|
|
2018-11-30 05:34:29 +00:00
|
|
|
Debug::log("handling as gyazo: $img_id", Debug::$LOG_VERBOSE);
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2015-10-17 19:11:12 +00:00
|
|
|
$img = $doc->createElement('img');
|
|
|
|
$img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg");
|
|
|
|
|
|
|
|
$br = $doc->createElement('br');
|
|
|
|
$entry->parentNode->insertBefore($img, $entry);
|
|
|
|
$entry->parentNode->insertBefore($br, $entry);
|
|
|
|
|
|
|
|
$found = true;
|
|
|
|
}
|
2018-05-26 07:25:39 +00:00
|
|
|
|
|
|
|
// let's try meta properties
|
|
|
|
if (!$found) {
|
2018-11-30 05:34:29 +00:00
|
|
|
Debug::log("looking for meta og:image", Debug::$LOG_VERBOSE);
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2020-09-22 06:04:33 +00:00
|
|
|
$content = UrlHelper::fetch(["url" => $entry->getAttribute("href"),
|
2018-05-26 07:25:39 +00:00
|
|
|
"http_accept" => "text/*"]);
|
|
|
|
|
|
|
|
if ($content) {
|
|
|
|
$cdoc = new DOMDocument();
|
|
|
|
|
|
|
|
if (@$cdoc->loadHTML($content)) {
|
|
|
|
$cxpath = new DOMXPath($cdoc);
|
|
|
|
|
|
|
|
$og_image = $cxpath->query("//meta[@property='og:image']")->item(0);
|
2018-09-10 13:09:38 +00:00
|
|
|
$og_video = $cxpath->query("//meta[@property='og:video']")->item(0);
|
|
|
|
|
|
|
|
if ($og_video) {
|
|
|
|
|
|
|
|
$source_stream = $og_video->getAttribute("content");
|
|
|
|
|
|
|
|
if ($source_stream) {
|
|
|
|
|
|
|
|
if ($og_image) {
|
|
|
|
$poster_url = $og_image->getAttribute("content");
|
|
|
|
} else {
|
|
|
|
$poster_url = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
|
|
|
$found = true;
|
|
|
|
}
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2018-09-10 13:09:38 +00:00
|
|
|
} else if ($og_image) {
|
2018-05-26 07:25:39 +00:00
|
|
|
|
|
|
|
$og_src = $og_image->getAttribute("content");
|
|
|
|
|
|
|
|
if ($og_src) {
|
|
|
|
$img = $doc->createElement('img');
|
|
|
|
$img->setAttribute("src", $og_src);
|
|
|
|
|
|
|
|
$br = $doc->createElement('br');
|
|
|
|
$entry->parentNode->insertBefore($img, $entry);
|
|
|
|
$entry->parentNode->insertBefore($br, $entry);
|
|
|
|
|
|
|
|
$found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove tiny thumbnails
|
|
|
|
if ($entry->hasAttribute("src")) {
|
|
|
|
if ($entry->parentNode && $entry->parentNode->parentNode) {
|
|
|
|
$entry->parentNode->parentNode->removeChild($entry->parentNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-26 10:33:18 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
return $found;
|
|
|
|
}
|
2015-07-06 19:44:43 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
function hook_article_filter($article) {
|
2015-07-06 19:44:43 +00:00
|
|
|
|
2020-09-17 16:02:27 +00:00
|
|
|
if (strpos($article["link"], "reddit.com/r/") !== false) {
|
2015-07-07 05:44:31 +00:00
|
|
|
$doc = new DOMDocument();
|
|
|
|
@$doc->loadHTML($article["content"]);
|
|
|
|
$xpath = new DOMXPath($doc);
|
2015-07-06 20:59:10 +00:00
|
|
|
|
2016-07-05 08:01:36 +00:00
|
|
|
$content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
|
|
|
|
|
2016-01-31 11:14:31 +00:00
|
|
|
if ($this->host->get($this, "enable_content_dupcheck")) {
|
2016-01-31 11:01:18 +00:00
|
|
|
|
2016-01-31 11:14:31 +00:00
|
|
|
if ($content_link) {
|
2017-12-03 07:55:41 +00:00
|
|
|
$content_href = $content_link->getAttribute("href");
|
|
|
|
$entry_guid = $article["guid_hashed"];
|
2016-01-31 11:14:31 +00:00
|
|
|
$owner_uid = $article["owner_uid"];
|
2016-01-31 11:01:18 +00:00
|
|
|
|
2016-01-31 11:14:31 +00:00
|
|
|
if (DB_TYPE == "pgsql") {
|
|
|
|
$interval_qpart = "date_entered < NOW() - INTERVAL '1 day'";
|
|
|
|
} else {
|
|
|
|
$interval_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY)";
|
|
|
|
}
|
2016-01-31 11:01:18 +00:00
|
|
|
|
2017-12-03 07:55:41 +00:00
|
|
|
$sth = $this->pdo->prepare("SELECT COUNT(id) AS cid
|
2016-01-31 11:14:31 +00:00
|
|
|
FROM ttrss_entries, ttrss_user_entries WHERE
|
|
|
|
ref_id = id AND
|
|
|
|
$interval_qpart AND
|
2017-12-03 07:55:41 +00:00
|
|
|
guid != ? AND
|
|
|
|
owner_uid = ? AND
|
|
|
|
content LIKE ?");
|
|
|
|
|
|
|
|
$sth->execute([$entry_guid, $owner_uid, "%href=\"$content_href\">[link]%"]);
|
2016-01-31 11:01:18 +00:00
|
|
|
|
2017-12-03 07:55:41 +00:00
|
|
|
if ($row = $sth->fetch()) {
|
|
|
|
$num_found = $row['cid'];
|
2016-01-31 11:01:18 +00:00
|
|
|
|
2016-01-31 11:14:31 +00:00
|
|
|
if ($num_found > 0) $article["force_catchup"] = true;
|
|
|
|
}
|
2016-01-31 11:01:18 +00:00
|
|
|
}
|
2016-01-31 11:14:31 +00:00
|
|
|
}
|
2015-07-20 09:44:59 +00:00
|
|
|
|
2020-12-18 05:12:31 +00:00
|
|
|
if ($content_link && $this->is_blacklisted($content_link->getAttribute("href")))
|
|
|
|
return $article;
|
|
|
|
|
2015-07-27 10:29:27 +00:00
|
|
|
$found = $this->inline_stuff($article, $doc, $xpath);
|
2015-07-06 19:44:43 +00:00
|
|
|
|
2015-07-07 05:44:31 +00:00
|
|
|
$node = $doc->getElementsByTagName('body')->item(0);
|
|
|
|
|
|
|
|
if ($node && $found) {
|
2017-09-09 18:51:59 +00:00
|
|
|
$article["content"] = $doc->saveHTML($node);
|
2020-06-24 19:54:14 +00:00
|
|
|
$article["enclosures"] = [];
|
2016-08-02 09:25:54 +00:00
|
|
|
} else if ($content_link) {
|
|
|
|
$article = $this->readability($article, $content_link->getAttribute("href"), $doc, $xpath);
|
2015-07-07 05:44:31 +00:00
|
|
|
}
|
2012-11-26 10:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $article;
|
|
|
|
}
|
2013-04-19 13:31:56 +00:00
|
|
|
|
|
|
|
function api_version() {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2018-11-30 05:34:29 +00:00
|
|
|
private function handle_as_video($doc, $entry, $source_stream, $poster_url = false) {
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2018-11-30 05:34:29 +00:00
|
|
|
Debug::log("handle_as_video: $source_stream", Debug::$LOG_VERBOSE);
|
2015-07-05 11:56:57 +00:00
|
|
|
|
|
|
|
$video = $doc->createElement('video');
|
|
|
|
$video->setAttribute("autoplay", "1");
|
2015-07-06 18:31:46 +00:00
|
|
|
$video->setAttribute("controls", "1");
|
2015-07-05 11:56:57 +00:00
|
|
|
$video->setAttribute("loop", "1");
|
|
|
|
|
2015-07-10 06:02:52 +00:00
|
|
|
if ($poster_url) $video->setAttribute("poster", $poster_url);
|
|
|
|
|
2015-07-05 11:56:57 +00:00
|
|
|
$source = $doc->createElement('source');
|
|
|
|
$source->setAttribute("src", $source_stream);
|
|
|
|
$source->setAttribute("type", "video/mp4");
|
|
|
|
|
|
|
|
$video->appendChild($source);
|
|
|
|
|
|
|
|
$br = $doc->createElement('br');
|
|
|
|
$entry->parentNode->insertBefore($video, $entry);
|
|
|
|
$entry->parentNode->insertBefore($br, $entry);
|
|
|
|
|
2020-09-15 03:55:22 +00:00
|
|
|
/*$img = $doc->createElement('img');
|
2015-07-05 11:56:57 +00:00
|
|
|
$img->setAttribute("src",
|
|
|
|
"data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D");
|
|
|
|
|
2020-09-15 03:55:22 +00:00
|
|
|
$entry->parentNode->insertBefore($img, $entry);*/
|
2015-07-05 11:56:57 +00:00
|
|
|
}
|
2016-03-20 08:30:56 +00:00
|
|
|
|
|
|
|
function testurl() {
|
|
|
|
$url = htmlspecialchars($_REQUEST["url"]);
|
|
|
|
|
|
|
|
header("Content-type: text/plain");
|
|
|
|
|
|
|
|
print "URL: $url\n";
|
|
|
|
|
|
|
|
$doc = new DOMDocument();
|
|
|
|
@$doc->loadHTML("<html><body><a href=\"$url\">[link]</a></body>");
|
|
|
|
$xpath = new DOMXPath($doc);
|
|
|
|
|
2018-11-30 05:34:29 +00:00
|
|
|
$found = $this->inline_stuff([], $doc, $xpath);
|
2016-08-02 09:25:54 +00:00
|
|
|
|
|
|
|
print "Inline result: $found\n";
|
|
|
|
|
|
|
|
if (!$found) {
|
|
|
|
print "\nReadability result:\n";
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2018-11-30 05:34:29 +00:00
|
|
|
$article = $this->readability([], $url, $doc, $xpath);
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2016-08-02 09:25:54 +00:00
|
|
|
print_r($article);
|
|
|
|
} else {
|
|
|
|
print "\nResulting HTML:\n";
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2016-08-02 09:25:54 +00:00
|
|
|
print $doc->saveHTML();
|
|
|
|
}
|
2016-03-20 08:30:56 +00:00
|
|
|
}
|
2016-08-01 13:20:14 +00:00
|
|
|
|
2020-12-12 15:40:26 +00:00
|
|
|
private function get_header($url, $header, $useragent = SELF_USER_AGENT) {
|
2017-09-17 18:56:57 +00:00
|
|
|
$ret = false;
|
2016-08-01 13:20:14 +00:00
|
|
|
|
|
|
|
if (function_exists("curl_init") && !defined("NO_CURL")) {
|
|
|
|
$ch = curl_init($url);
|
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_NOBODY, true);
|
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("open_basedir"));
|
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
|
|
|
|
|
2017-04-26 17:57:36 +00:00
|
|
|
@curl_exec($ch);
|
2017-09-17 18:56:57 +00:00
|
|
|
$ret = curl_getinfo($ch, $header);
|
2016-08-01 13:20:14 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 18:56:57 +00:00
|
|
|
return $ret;
|
2016-08-01 13:20:14 +00:00
|
|
|
}
|
2016-08-02 09:25:54 +00:00
|
|
|
|
2017-09-17 18:56:57 +00:00
|
|
|
private function get_content_type($url, $useragent = SELF_USER_AGENT) {
|
2020-12-12 15:40:26 +00:00
|
|
|
return $this->get_header($url, CURLINFO_CONTENT_TYPE, $useragent);
|
2017-09-17 18:56:57 +00:00
|
|
|
}
|
2017-09-17 18:34:49 +00:00
|
|
|
|
2017-09-17 18:56:57 +00:00
|
|
|
private function get_location($url, $useragent = SELF_USER_AGENT) {
|
2020-12-12 15:40:26 +00:00
|
|
|
return $this->get_header($url, CURLINFO_EFFECTIVE_URL, $useragent);
|
2017-09-17 18:34:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 17:57:36 +00:00
|
|
|
/**
|
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
|
|
|
*/
|
2016-08-02 09:25:54 +00:00
|
|
|
private function readability($article, $url, $doc, $xpath, $debug = false) {
|
|
|
|
|
|
|
|
if (!defined('NO_CURL') && function_exists("curl_init") && $this->host->get($this, "enable_readability") &&
|
|
|
|
mb_strlen(strip_tags($article["content"])) <= 150) {
|
|
|
|
|
2018-06-20 12:56:08 +00:00
|
|
|
// do not try to embed posts linking back to other reddit posts
|
2018-06-21 05:12:11 +00:00
|
|
|
// readability.php requires PHP 5.6
|
2020-09-17 16:02:27 +00:00
|
|
|
if ($url && strpos($url, "reddit.com") === false && version_compare(PHP_VERSION, '5.6.0', '>=')) {
|
2016-08-02 09:25:54 +00:00
|
|
|
|
|
|
|
/* link may lead to a huge video file or whatever, we need to check content type before trying to
|
|
|
|
parse it which p much requires curl */
|
|
|
|
|
|
|
|
$useragent_compat = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)";
|
|
|
|
$content_type = $this->get_content_type($url, $useragent_compat);
|
|
|
|
|
2020-09-17 16:02:27 +00:00
|
|
|
if ($content_type && strpos($content_type, "text/html") !== false) {
|
2016-08-02 09:25:54 +00:00
|
|
|
|
2019-04-17 05:51:17 +00:00
|
|
|
foreach ($this->host->get_hooks(PluginHost::HOOK_GET_FULL_TEXT) as $p) {
|
|
|
|
$extracted_content = $p->hook_get_full_text($url);
|
2016-08-02 09:25:54 +00:00
|
|
|
|
2019-04-17 05:51:17 +00:00
|
|
|
if ($extracted_content) {
|
|
|
|
$article["content"] = $extracted_content;
|
|
|
|
break;
|
2016-08-02 09:25:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $article;
|
|
|
|
}
|
2020-12-18 05:12:31 +00:00
|
|
|
|
|
|
|
private function is_blacklisted($src) {
|
|
|
|
$src_domain = parse_url($src, PHP_URL_HOST);
|
|
|
|
|
|
|
|
foreach ($this->domain_blacklist as $domain) {
|
|
|
|
if (strstr($src_domain, $domain) !== false) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-20 08:28:48 +00:00
|
|
|
|
|
|
|
function hook_render_article($article) {
|
|
|
|
return $this->hook_render_article_cdm($article);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function rewrite_to_teddit($str) {
|
|
|
|
if (strpos($str, "reddit.com") !== false) {
|
|
|
|
return preg_replace("/https?:\/\/([a-z]+\.)?reddit\.com/", "https://teddit.net", $str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
function hook_render_article_cdm($article) {
|
|
|
|
if ($this->host->get($this, "reddit_to_teddit")) {
|
|
|
|
$need_saving = false;
|
|
|
|
|
|
|
|
$article["link"] = $this->rewrite_to_teddit($article["link"]);
|
|
|
|
|
|
|
|
$doc = new DOMDocument();
|
|
|
|
if (@$doc->loadHTML('<?xml encoding="UTF-8">' . $article["content"])) {
|
|
|
|
$xpath = new DOMXPath($doc);
|
|
|
|
$elems = $xpath->query("//a[@href]");
|
|
|
|
|
|
|
|
foreach ($elems as $elem) {
|
|
|
|
$href = $elem->getAttribute("href");
|
|
|
|
$rewritten_href = $this->rewrite_to_teddit($href);
|
|
|
|
|
|
|
|
if ($href != $rewritten_href) {
|
|
|
|
$elem->setAttribute("href", $rewritten_href);
|
|
|
|
$need_saving = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($need_saving) $article["content"] = $doc->saveHTML();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $article;
|
|
|
|
}
|
|
|
|
|
2020-12-20 10:12:50 +00:00
|
|
|
function hook_render_article_api($params) {
|
|
|
|
$article = isset($params["article"]) ? $params["article"] : $params["headline"];
|
|
|
|
|
|
|
|
return $this->hook_render_article_cdm($article);
|
|
|
|
}
|
|
|
|
|
2012-11-26 10:33:18 +00:00
|
|
|
}
|