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" ];
|
2021-01-22 12:44:44 +00:00
|
|
|
private $dump_json_data = false;
|
|
|
|
private $fallback_preview_urls = [];
|
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
|
|
|
}
|
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
private function process_post_media($data, $doc, $xpath, $anchor) {
|
|
|
|
$found = 0;
|
2012-11-26 10:33:18 +00:00
|
|
|
|
2021-02-05 20:41:32 +00:00
|
|
|
if (isset($data["media_metadata"])) {
|
2021-01-19 19:21:57 +00:00
|
|
|
foreach ($data["media_metadata"] as $media) {
|
2021-02-06 06:51:28 +00:00
|
|
|
if (!empty($media["s"]["u"])) {
|
|
|
|
$media_url = htmlspecialchars_decode($media["s"]["u"]);
|
2012-11-26 10:33:18 +00:00
|
|
|
|
2021-02-06 06:51:28 +00:00
|
|
|
Debug::log("found media_metadata (gallery): $media_url", Debug::$LOG_VERBOSE);
|
2013-03-12 06:47:06 +00:00
|
|
|
|
2021-02-06 06:51:28 +00:00
|
|
|
if ($media_url) {
|
|
|
|
$this->handle_as_image($doc, $anchor, $media_url);
|
|
|
|
$found = 1;
|
|
|
|
}
|
2021-01-19 19:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 05:06:18 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
// v.redd.it - see below
|
|
|
|
/* if (is_array($data["media"])) {
|
|
|
|
foreach ($data["media"] as $media) {
|
|
|
|
if (isset($media["fallback_url"])) {
|
|
|
|
$stream_url = $media["fallback_url"];
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if (isset($data["preview"]["images"][0]["source"]))
|
|
|
|
$poster_url = $data["preview"]["images"][0]["source"]["url"];
|
|
|
|
else
|
|
|
|
$poster_url = "";
|
2021-01-18 12:34:05 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
Debug::log("found stream fallback_url: $stream_url / poster $poster_url", Debug::$LOG_VERBOSE);
|
2021-01-18 12:34:05 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$this->handle_as_video($doc, $anchor, $stream_url, $poster_url);
|
|
|
|
}
|
2021-01-18 12:34:05 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$found = 1;
|
|
|
|
}
|
|
|
|
} */
|
2021-01-18 12:34:05 +00:00
|
|
|
|
2021-02-05 20:41:32 +00:00
|
|
|
$post_hint = $data["post_hint"] ?? false;
|
|
|
|
|
|
|
|
if (!$found && $post_hint == "hosted:video") {
|
2021-01-19 19:21:57 +00:00
|
|
|
$media_url = $data["url"];
|
2021-01-18 12:34:05 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if (isset($data["preview"]["images"][0]["source"]))
|
|
|
|
$poster_url = htmlspecialchars_decode($data["preview"]["images"][0]["source"]["url"]);
|
|
|
|
else
|
|
|
|
$poster_url = "";
|
2021-01-18 12:34:05 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
Debug::log("found hosted video url: $media_url / poster $poster_url, looking up fallback url...", Debug::$LOG_VERBOSE);
|
2021-01-18 12:34:05 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$fallback_url = $data["media"]["reddit_video"]["fallback_url"];
|
2021-01-18 12:34:05 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if ($fallback_url) {
|
|
|
|
Debug::log("found video fallback_url: $fallback_url", Debug::$LOG_VERBOSE);
|
|
|
|
$this->handle_as_video($doc, $anchor, $fallback_url, $poster_url);
|
2021-01-18 12:34:05 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$found = 1;
|
|
|
|
}
|
|
|
|
}
|
2021-01-18 12:34:05 +00:00
|
|
|
|
2021-02-05 20:41:32 +00:00
|
|
|
if (!$found && $post_hint == "video") {
|
2021-01-19 19:21:57 +00:00
|
|
|
$media_url = $data["url"];
|
2021-01-18 12:34:05 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if (isset($data["preview"]["images"][0]["source"]))
|
|
|
|
$poster_url = htmlspecialchars_decode($data["preview"]["images"][0]["source"]["url"]);
|
|
|
|
else
|
|
|
|
$poster_url = "";
|
2021-01-18 12:34:05 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
Debug::log("found video url: $media_url / poster $poster_url", Debug::$LOG_VERBOSE);
|
|
|
|
$this->handle_as_video($doc, $anchor, $media_url, $poster_url);
|
|
|
|
|
|
|
|
$found = 1;
|
|
|
|
}
|
|
|
|
|
2021-02-05 20:41:32 +00:00
|
|
|
if (!$found && $post_hint == "image") {
|
2021-01-19 19:21:57 +00:00
|
|
|
$media_url = $data["url"];
|
2015-07-05 11:56:57 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
Debug::log("found image url: $media_url", Debug::$LOG_VERBOSE);
|
|
|
|
$this->handle_as_image($doc, $anchor, $media_url);
|
2016-08-01 18:03:36 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$found = 1;
|
|
|
|
}
|
2016-08-01 18:03:36 +00:00
|
|
|
|
2021-02-05 20:41:32 +00:00
|
|
|
if (!$found && isset($data["preview"]["images"])) {
|
2021-01-21 05:28:55 +00:00
|
|
|
foreach ($data["preview"]["images"] as $img) {
|
|
|
|
if (isset($img["source"]["url"])) {
|
|
|
|
$media_url = htmlspecialchars_decode($img["source"]["url"]);
|
|
|
|
$target_url = $data["url"];
|
|
|
|
|
|
|
|
if ($media_url) {
|
2021-02-05 20:41:32 +00:00
|
|
|
if ($post_hint == "self") {
|
2021-01-22 12:44:44 +00:00
|
|
|
Debug::log("found preview image url: $media_url (link: $target_url)", Debug::$LOG_VERBOSE);
|
|
|
|
$this->handle_as_image($doc, $anchor, $media_url, $target_url);
|
2021-01-21 05:28:55 +00:00
|
|
|
|
2021-01-22 12:44:44 +00:00
|
|
|
$found = 1;
|
|
|
|
} else { // gonna use this later if nothing is found using generic link processing
|
|
|
|
Debug::log("found fallback preview image url: $media_url (link: $target_url);", Debug::$LOG_VERBOSE);
|
|
|
|
array_push($this->fallback_preview_urls, $media_url);
|
|
|
|
}
|
2021-01-21 05:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
return $found;
|
|
|
|
}
|
2016-08-01 18:03:36 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
private function inline_stuff($article, &$doc, $xpath) {
|
2016-08-01 18:03:36 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$found = false;
|
2016-08-01 18:03:36 +00:00
|
|
|
|
2021-01-22 12:44:44 +00:00
|
|
|
// embed before reddit <table> post layout
|
|
|
|
$anchor = $xpath->query('//body/*')->item(0);
|
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
// deal with json-provided media content first
|
2021-01-22 12:44:44 +00:00
|
|
|
if ($article["link"] && $anchor) {
|
2021-01-19 19:21:57 +00:00
|
|
|
Debug::log("JSON: requesting from URL: " . $article["link"] . "/.json", Debug::$LOG_VERBOSE);
|
2016-08-01 18:03:36 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$tmp = UrlHelper::fetch($article["link"] . "/.json");
|
2016-08-01 18:03:36 +00:00
|
|
|
|
2021-01-22 12:44:44 +00:00
|
|
|
$this->fallback_preview_urls = [];
|
2021-01-19 19:21:57 +00:00
|
|
|
|
|
|
|
if ($tmp && $anchor) {
|
|
|
|
$json = json_decode($tmp, true);
|
|
|
|
|
|
|
|
if ($json) {
|
2021-01-21 05:28:55 +00:00
|
|
|
Debug::log("JSON: processing media elements...", Debug::$LOG_EXTENDED);
|
|
|
|
|
2021-01-22 12:44:44 +00:00
|
|
|
if ($this->dump_json_data) print_r($json);
|
2021-01-21 05:28:55 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
foreach ($json as $listing) {
|
|
|
|
foreach ($listing["data"]["children"] as $child) {
|
|
|
|
|
|
|
|
$data = $child["data"];
|
|
|
|
|
2021-02-05 20:41:32 +00:00
|
|
|
if (isset($data["crosspost_parent_list"])) {
|
2021-01-19 19:21:57 +00:00
|
|
|
Debug::log("JSON: processing child crosspost_parent_list", Debug::$LOG_EXTENDED);
|
|
|
|
|
|
|
|
foreach ($data["crosspost_parent_list"] as $parent) {
|
|
|
|
if ($this->process_post_media($parent, $doc, $xpath, $anchor)) {
|
|
|
|
$found = 1;
|
2021-01-21 05:28:55 +00:00
|
|
|
|
|
|
|
break 2;
|
2021-01-19 19:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug::log("JSON: processing child data element...", Debug::$LOG_EXTENDED);
|
|
|
|
|
|
|
|
if (!$found && $this->process_post_media($data, $doc, $xpath, $anchor)) {
|
2016-08-01 18:03:36 +00:00
|
|
|
$found = 1;
|
2021-01-21 05:28:55 +00:00
|
|
|
|
|
|
|
break 2;
|
2016-08-01 18:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-21 05:28:55 +00:00
|
|
|
} else {
|
|
|
|
Debug::log("JSON: failed to parse received data.", Debug::$LOG_EXTENDED);
|
2016-08-01 18:03:36 +00:00
|
|
|
}
|
2021-01-19 19:21:57 +00:00
|
|
|
} else {
|
|
|
|
if (!$tmp) {
|
|
|
|
global $fetch_last_error;
|
|
|
|
Debug::log("JSON: failed to fetch post:" . $fetch_last_error, Debug::$LOG_EXTENDED);
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 12:44:44 +00:00
|
|
|
} else if (!$anchor) {
|
|
|
|
Debug::log("JSON: anchor element not found, unable to embed", Debug::$LOG_EXTENDED);
|
2021-01-19 19:21:57 +00:00
|
|
|
}
|
2015-07-10 23:08:53 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if ($found) {
|
|
|
|
Debug::log("JSON: found media data, skipping further processing of content", Debug::$LOG_VERBOSE);
|
|
|
|
$this->remove_post_thumbnail($doc, $xpath);
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-05 11:56:57 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$entries = $xpath->query('//a[@href]');
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
foreach ($entries as $entry) {
|
|
|
|
$entry_href = $entry->getAttribute("href");
|
2015-07-05 11:56:57 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$matches = [];
|
2018-09-10 13:09:38 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
/* skip links going back to reddit (and any other blacklisted stuff) */
|
|
|
|
if (!$found && $this->is_blacklisted($entry_href, ["reddit.com"])) {
|
|
|
|
Debug::log("BODY: domain of $entry_href is blacklisted, skipping", Debug::$LOG_EXTENDED);
|
|
|
|
continue;
|
|
|
|
}
|
2015-05-18 09:05:30 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
Debug::log("BODY: processing URL: " . $entry_href, Debug::$LOG_VERBOSE);
|
2017-09-16 07:08:30 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if (!$found && preg_match("/^https?:\/\/twitter.com\/(.*?)\/status\/(.*)/", $entry_href, $matches)) {
|
|
|
|
Debug::log("handling as twitter: " . $matches[1] . " " . $matches[2], Debug::$LOG_VERBOSE);
|
2017-09-16 07:08:30 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$oembed_result = UrlHelper::fetch("https://publish.twitter.com/oembed?url=" . urlencode($entry_href));
|
2017-09-16 07:08:30 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if ($oembed_result) {
|
|
|
|
$oembed_result = json_decode($oembed_result, true);
|
2017-09-16 07:08:30 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if ($oembed_result && isset($oembed_result["html"])) {
|
2017-09-17 06:18:05 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$tmp = new DOMDocument();
|
|
|
|
if (@$tmp->loadHTML('<?xml encoding="utf-8" ?>' . $oembed_result["html"])) {
|
|
|
|
$p = $doc->createElement("p");
|
2017-09-16 07:08:30 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$p->appendChild($doc->importNode(
|
|
|
|
$tmp->getElementsByTagName("blockquote")->item(0), TRUE));
|
2017-09-16 07:08:30 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$br = $doc->createElement('br');
|
|
|
|
$entry->parentNode->insertBefore($p, $entry);
|
|
|
|
$entry->parentNode->insertBefore($br, $entry);
|
2017-01-29 11:36:37 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-29 11:36:37 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if (!$found && preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry_href, $matches)) {
|
|
|
|
$entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]);
|
|
|
|
}
|
2017-01-29 11:36:37 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if (!$found && preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry_href, $matches)) {
|
2017-01-29 11:36:37 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
Debug::log("Handling as Gfycat", Debug::$LOG_VERBOSE);
|
2017-01-29 11:36:37 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$source_stream = 'https://giant.gfycat.com/' . $matches[2] . '.mp4';
|
|
|
|
$poster_url = 'https://thumbs.gfycat.com/' . $matches[2] . '-mobile.jpg';
|
2017-01-29 11:36:37 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$content_type = $this->get_content_type($source_stream);
|
2017-01-29 11:36:37 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if (strpos($content_type, "video/") !== false) {
|
|
|
|
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
|
|
|
$found = 1;
|
2017-01-29 11:36:37 +00:00
|
|
|
}
|
2021-01-19 19:21:57 +00:00
|
|
|
}
|
2017-01-29 11:36:37 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
// imgur .gif -> .gifv
|
|
|
|
if (!$found && preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry_href)) {
|
|
|
|
Debug::log("Handling as imgur gif (->gifv)", Debug::$LOG_VERBOSE);
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$entry->setAttribute("href",
|
|
|
|
str_replace(".gif", ".gifv", $entry_href));
|
|
|
|
}
|
2015-07-10 23:08:53 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if (!$found && preg_match("/\.(gifv|mp4)$/i", $entry_href)) {
|
|
|
|
Debug::log("Handling as imgur gifv", Debug::$LOG_VERBOSE);
|
2015-07-05 11:56:57 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$source_stream = str_replace(".gifv", ".mp4", $entry_href);
|
2015-07-10 06:02:52 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if (strpos($source_stream, "imgur.com") !== false)
|
|
|
|
$poster_url = str_replace(".mp4", "h.jpg", $source_stream);
|
2015-07-10 06:02:52 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
2015-05-18 09:05:30 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$found = true;
|
|
|
|
}
|
2015-05-14 09:03:06 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$matches = array();
|
|
|
|
if (!$found && (preg_match("/youtube\.com\/v\/([\w-]+)/", $entry_href, $matches) ||
|
|
|
|
preg_match("/youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry_href, $matches) ||
|
2021-01-23 05:57:36 +00:00
|
|
|
preg_match("/youtube\.com\/embed\/([\w-]+)/", $entry_href, $matches) ||
|
2021-01-19 19:21:57 +00:00
|
|
|
preg_match("/youtube\.com\/watch\?v=([\w-]+)/", $entry_href, $matches) ||
|
|
|
|
preg_match("/\/\/youtu.be\/([\w-]+)/", $entry_href, $matches))) {
|
2015-07-06 18:44:38 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$vid_id = $matches[1];
|
2015-07-06 18:44:38 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
Debug::log("Handling as youtube: $vid_id", Debug::$LOG_VERBOSE);
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2021-01-19 19:21:57 +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
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$br = $doc->createElement('br');
|
|
|
|
$entry->parentNode->insertBefore($iframe, $entry);
|
|
|
|
$entry->parentNode->insertBefore($br, $entry);
|
2015-07-06 18:44:38 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$found = true;
|
|
|
|
}
|
2015-07-06 18:44:38 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if (!$found && (preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?[$\?]/i", $entry_href) ||
|
|
|
|
/* mb_strpos($entry_href, "i.reddituploads.com") !== false || */
|
|
|
|
mb_strpos($this->get_content_type($entry_href), "image/") !== false)) {
|
2016-07-07 06:04:38 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
Debug::log("Handling as a picture", Debug::$LOG_VERBOSE);
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$img = $doc->createElement('img');
|
|
|
|
$img->setAttribute("src", $entry_href);
|
2012-11-26 10:33:18 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$br = $doc->createElement('br');
|
|
|
|
$entry->parentNode->insertBefore($img, $entry);
|
|
|
|
$entry->parentNode->insertBefore($br, $entry);
|
2013-03-12 06:47:06 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$found = true;
|
|
|
|
}
|
2013-03-12 06:47:06 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
// imgur via link rel="image_src" href="..."
|
|
|
|
if (!$found && preg_match("/imgur/", $entry_href)) {
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
Debug::log("handling as imgur page/whatever", Debug::$LOG_VERBOSE);
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$content = UrlHelper::fetch(["url" => $entry_href,
|
|
|
|
"http_accept" => "text/*"]);
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if ($content) {
|
|
|
|
$cdoc = new DOMDocument();
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if (@$cdoc->loadHTML($content)) {
|
|
|
|
$cxpath = new DOMXPath($cdoc);
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$rel_image = $cxpath->query("//link[@rel='image_src']")->item(0);
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if ($rel_image) {
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$img = $doc->createElement('img');
|
|
|
|
$img->setAttribute("src", $rel_image->getAttribute("href"));
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$br = $doc->createElement('br');
|
|
|
|
$entry->parentNode->insertBefore($img, $entry);
|
|
|
|
$entry->parentNode->insertBefore($br, $entry);
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$found = true;
|
2020-01-04 16:21:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-19 19:21:57 +00:00
|
|
|
}
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
// wtf is this even
|
|
|
|
if (!$found && preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry_href, $matches)) {
|
|
|
|
$img_id = $matches[1];
|
2015-10-17 19:11:12 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
Debug::log("handling as gyazo: $img_id", Debug::$LOG_VERBOSE);
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$img = $doc->createElement('img');
|
|
|
|
$img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg");
|
2015-10-17 19:11:12 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$br = $doc->createElement('br');
|
|
|
|
$entry->parentNode->insertBefore($img, $entry);
|
|
|
|
$entry->parentNode->insertBefore($br, $entry);
|
2015-10-17 19:11:12 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$found = true;
|
|
|
|
}
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
// let's try meta properties
|
|
|
|
if (!$found) {
|
|
|
|
Debug::log("looking for meta og:image", Debug::$LOG_VERBOSE);
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$content = UrlHelper::fetch(["url" => $entry_href,
|
|
|
|
"http_accept" => "text/*"]);
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if ($content) {
|
|
|
|
$cdoc = new DOMDocument();
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if (@$cdoc->loadHTML($content)) {
|
|
|
|
$cxpath = new DOMXPath($cdoc);
|
2018-09-10 13:09:38 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$og_image = $cxpath->query("//meta[@property='og:image']")->item(0);
|
|
|
|
$og_video = $cxpath->query("//meta[@property='og:video']")->item(0);
|
2018-09-10 13:09:38 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if ($og_video) {
|
2018-09-10 13:09:38 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$source_stream = $og_video->getAttribute("content");
|
2018-09-10 13:09:38 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if ($source_stream) {
|
2018-09-10 13:09:38 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if ($og_image) {
|
|
|
|
$poster_url = $og_image->getAttribute("content");
|
|
|
|
} else {
|
|
|
|
$poster_url = false;
|
2018-09-10 13:09:38 +00:00
|
|
|
}
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
|
|
|
$found = true;
|
|
|
|
}
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
} else if ($og_image) {
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$og_src = $og_image->getAttribute("content");
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if ($og_src) {
|
|
|
|
$img = $doc->createElement('img');
|
|
|
|
$img->setAttribute("src", $og_src);
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$br = $doc->createElement('br');
|
|
|
|
$entry->parentNode->insertBefore($img, $entry);
|
|
|
|
$entry->parentNode->insertBefore($br, $entry);
|
|
|
|
|
|
|
|
$found = true;
|
2018-05-26 07:25:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-07 05:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-26 10:33:18 +00:00
|
|
|
|
2021-01-22 12:44:44 +00:00
|
|
|
if (!$found && $anchor && count($this->fallback_preview_urls) > 0) {
|
|
|
|
Debug::log("JSON: processing fallback preview urls...", Debug::$LOG_VERBOSE);
|
|
|
|
|
|
|
|
foreach ($this->fallback_preview_urls as $media_url) {
|
|
|
|
$this->handle_as_image($doc, $anchor, $media_url);
|
|
|
|
|
|
|
|
$found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-23 05:57:36 +00:00
|
|
|
if ($found)
|
|
|
|
$this->remove_post_thumbnail($doc, $xpath);
|
|
|
|
|
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
|
|
|
|
2021-02-06 07:31:06 +00:00
|
|
|
if (strpos($article["link"], "reddit.com/r/") !== false && !empty($article["content"])) {
|
2015-07-07 05:44:31 +00:00
|
|
|
$doc = new DOMDocument();
|
2015-07-06 20:59:10 +00:00
|
|
|
|
2021-02-06 07:31:06 +00:00
|
|
|
if (@$doc->loadHTML($article["content"])) {
|
|
|
|
$xpath = new DOMXPath($doc);
|
2016-07-05 08:01:36 +00:00
|
|
|
|
2021-02-06 07:31:06 +00:00
|
|
|
$content_link = $xpath->query("(//a[contains(., '[link]')])")->item(0);
|
2016-01-31 11:01:18 +00:00
|
|
|
|
2021-02-06 07:31:06 +00:00
|
|
|
if ($this->host->get($this, "enable_content_dupcheck")) {
|
2016-01-31 11:01:18 +00:00
|
|
|
|
2021-02-06 07:31:06 +00:00
|
|
|
if ($content_link) {
|
|
|
|
$content_href = $content_link->getAttribute("href");
|
|
|
|
$entry_guid = $article["guid_hashed"];
|
|
|
|
$owner_uid = $article["owner_uid"];
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-02-06 07:31:06 +00:00
|
|
|
$sth = $this->pdo->prepare("SELECT COUNT(id) AS cid
|
|
|
|
FROM ttrss_entries, ttrss_user_entries WHERE
|
|
|
|
ref_id = id AND
|
|
|
|
$interval_qpart AND
|
|
|
|
guid != ? AND
|
|
|
|
owner_uid = ? AND
|
|
|
|
content LIKE ?");
|
2017-12-03 07:55:41 +00:00
|
|
|
|
2021-02-06 07:31:06 +00:00
|
|
|
$sth->execute([$entry_guid, $owner_uid, "%href=\"$content_href\">[link]%"]);
|
2016-01-31 11:01:18 +00:00
|
|
|
|
2021-02-06 07:31:06 +00:00
|
|
|
if ($row = $sth->fetch()) {
|
|
|
|
$num_found = $row['cid'];
|
2016-01-31 11:01:18 +00:00
|
|
|
|
2021-02-06 07:31:06 +00:00
|
|
|
if ($num_found > 0) $article["force_catchup"] = true;
|
|
|
|
}
|
2016-01-31 11:14:31 +00:00
|
|
|
}
|
2016-01-31 11:01:18 +00:00
|
|
|
}
|
2015-07-20 09:44:59 +00:00
|
|
|
|
2021-02-06 07:31:06 +00:00
|
|
|
if ($content_link && $this->is_blacklisted($content_link->getAttribute("href")))
|
|
|
|
return $article;
|
2020-12-18 05:12:31 +00:00
|
|
|
|
2021-02-06 07:31:06 +00:00
|
|
|
$found = $this->inline_stuff($article, $doc, $xpath);
|
2015-07-06 19:44:43 +00:00
|
|
|
|
2021-02-06 07:31:06 +00:00
|
|
|
$node = $doc->getElementsByTagName('body')->item(0);
|
2015-07-07 05:44:31 +00:00
|
|
|
|
2021-02-06 07:31:06 +00:00
|
|
|
if ($node && $found) {
|
|
|
|
$article["content"] = $doc->saveHTML($node);
|
|
|
|
$article["enclosures"] = [];
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
private function remove_post_thumbnail($doc, $xpath) {
|
|
|
|
$thumb = $xpath->query("//td/a/img[@src]")->item(0);
|
|
|
|
|
|
|
|
if ($thumb)
|
|
|
|
$thumb->parentNode->parentNode->removeChild($thumb->parentNode);
|
|
|
|
}
|
|
|
|
|
2021-01-21 05:28:55 +00:00
|
|
|
private function handle_as_image($doc, $entry, $image_url, $link_url = false) {
|
2021-01-19 19:21:57 +00:00
|
|
|
$img = $doc->createElement("img");
|
|
|
|
$img->setAttribute("src", $image_url);
|
|
|
|
|
|
|
|
$p = $doc->createElement("p");
|
2021-01-21 05:28:55 +00:00
|
|
|
|
|
|
|
if ($link_url) {
|
|
|
|
$a = $doc->createElement("a");
|
|
|
|
$a->setAttribute("href", $link_url);
|
|
|
|
|
|
|
|
$a->appendChild($img);
|
|
|
|
$p->appendChild($a);
|
|
|
|
} else {
|
|
|
|
$p->appendChild($img);
|
|
|
|
}
|
2021-01-19 19:21:57 +00:00
|
|
|
|
|
|
|
$entry->parentNode->insertBefore($p, $entry);
|
|
|
|
}
|
|
|
|
|
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() {
|
2021-01-21 05:28:55 +00:00
|
|
|
|
|
|
|
$url = clean($_POST["url"]);
|
|
|
|
$article_url = clean($_POST["article_url"]);
|
|
|
|
|
2021-01-22 12:44:44 +00:00
|
|
|
$this->dump_json_data = true;
|
|
|
|
|
2021-01-21 05:28:55 +00:00
|
|
|
if (!$url && !$article_url) {
|
|
|
|
header("Content-type: text/html");
|
|
|
|
?>
|
|
|
|
<style type="text/css">
|
|
|
|
fieldset { border : 0; }
|
|
|
|
label { display : inline-block; min-width : 120px; }
|
|
|
|
</style>
|
|
|
|
<form action="backend.php?op=pluginhandler&method=testurl&plugin=af_redditimgur" method="post">
|
|
|
|
<fieldset>
|
|
|
|
<label>URL:</label>
|
2021-02-14 06:15:51 +00:00
|
|
|
<input name="url" size="100" value="<?= htmlspecialchars($url) ?>"></input>
|
2021-01-21 05:28:55 +00:00
|
|
|
</fieldset>
|
|
|
|
<fieldset>
|
|
|
|
<label>Article URL:</label>
|
2021-02-14 06:15:51 +00:00
|
|
|
<input name="article_url" size="100" value="<?= htmlspecialchars($article_url) ?>"></input>
|
2021-01-21 05:28:55 +00:00
|
|
|
</fieldset>
|
|
|
|
<fieldset>
|
|
|
|
<button type="submit">Test</button>
|
|
|
|
</fieldset>
|
|
|
|
</form>
|
|
|
|
<?php
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-20 08:30:56 +00:00
|
|
|
header("Content-type: text/plain");
|
|
|
|
|
2021-01-18 12:34:05 +00:00
|
|
|
Debug::set_enabled(true);
|
2021-01-19 19:21:57 +00:00
|
|
|
Debug::set_loglevel(Debug::$LOG_EXTENDED);
|
2021-01-18 12:34:05 +00:00
|
|
|
|
|
|
|
Debug::log("URL: $url", Debug::$LOG_VERBOSE);
|
2016-03-20 08:30:56 +00:00
|
|
|
|
|
|
|
$doc = new DOMDocument();
|
2021-01-19 19:21:57 +00:00
|
|
|
@$doc->loadHTML("<html><body><table><tr><td><a href=\"$url\">[link]</a></td></tr></table></body>");
|
2016-03-20 08:30:56 +00:00
|
|
|
$xpath = new DOMXPath($doc);
|
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$found = $this->inline_stuff(["link" => $article_url], $doc, $xpath);
|
2016-08-02 09:25:54 +00:00
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
Debug::log("Inline result: $found", Debug::$LOG_VERBOSE);
|
2016-08-02 09:25:54 +00:00
|
|
|
|
|
|
|
if (!$found) {
|
2021-01-19 19:21:57 +00:00
|
|
|
Debug::log("Readability result:", Debug::$LOG_VERBOSE);
|
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 {
|
2021-01-19 19:21:57 +00:00
|
|
|
Debug::log("Resulting HTML:", Debug::$LOG_VERBOSE);
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2016-08-02 09:25:54 +00:00
|
|
|
print $doc->saveHTML();
|
|
|
|
}
|
2021-01-21 05:28:55 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-02-08 13:52:47 +00:00
|
|
|
$this->host->run_hooks_callback(PluginHost::HOOK_GET_FULL_TEXT,
|
|
|
|
function ($result) use (&$article) {
|
|
|
|
if ($result) {
|
|
|
|
$article["content"] = $result;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
$url);
|
2016-08-02 09:25:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $article;
|
|
|
|
}
|
2020-12-18 05:12:31 +00:00
|
|
|
|
2021-01-18 12:34:05 +00:00
|
|
|
private function is_blacklisted($src, $also_blacklist = []) {
|
2020-12-18 05:12:31 +00:00
|
|
|
$src_domain = parse_url($src, PHP_URL_HOST);
|
|
|
|
|
2021-01-18 12:34:05 +00:00
|
|
|
foreach (array_merge($this->domain_blacklist, $also_blacklist) as $domain) {
|
2020-12-18 05:12:31 +00:00
|
|
|
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
|
|
|
}
|