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
|
|
|
|
2021-02-27 10:50:28 +00:00
|
|
|
/** @var PluginHost $host */
|
2012-12-23 10:52:18 +00:00
|
|
|
private $host;
|
2021-11-14 17:51:22 +00:00
|
|
|
|
|
|
|
/** @var array<string> */
|
2020-12-18 05:12:31 +00:00
|
|
|
private $domain_blacklist = [ "github.com" ];
|
2021-11-14 17:51:22 +00:00
|
|
|
|
|
|
|
/** @var bool */
|
2021-01-22 12:44:44 +00:00
|
|
|
private $dump_json_data = false;
|
2021-11-14 17:51:22 +00:00
|
|
|
|
|
|
|
/** @var array<string> */
|
2021-01-22 12:44:44 +00:00
|
|
|
private $fallback_preview_urls = [];
|
2021-11-14 17:51:22 +00:00
|
|
|
|
|
|
|
/** @var int */
|
2021-03-07 12:21:31 +00:00
|
|
|
private $default_max_score = 100;
|
2021-11-14 17:51:22 +00:00
|
|
|
|
|
|
|
/** @var array<int, array<int, string|null>> */
|
2021-04-23 17:30:29 +00:00
|
|
|
private $generated_enclosures = [];
|
2012-12-23 10:52:18 +00:00
|
|
|
|
2012-12-25 06:02:08 +00:00
|
|
|
function about() {
|
2021-03-01 09:11:42 +00:00
|
|
|
return array(null,
|
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);
|
2021-10-22 10:25:43 +00:00
|
|
|
|
|
|
|
$host->add_hook($host::HOOK_PRE_SUBSCRIBE, $this);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hook_pre_subscribe(&$url, $auth_login, $auth_pass) {
|
|
|
|
$reddit_to_teddit = $this->host->get($this, "reddit_to_teddit");
|
|
|
|
|
|
|
|
if ($reddit_to_teddit) {
|
|
|
|
$url = $this->rewrite_to_reddit($url);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-07-06 19:44:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function hook_prefs_tab($args) {
|
|
|
|
if ($args != "prefFeeds") return;
|
2021-02-17 10:35:10 +00:00
|
|
|
$enable_readability = $this->host->get($this, "enable_readability");
|
|
|
|
$enable_content_dupcheck = $this->host->get($this, "enable_content_dupcheck");
|
|
|
|
$reddit_to_teddit = $this->host->get($this, "reddit_to_teddit");
|
2021-02-27 10:50:28 +00:00
|
|
|
$apply_nsfw_tags = $this->host->get_array($this, "apply_nsfw_tags");
|
2021-03-07 12:21:31 +00:00
|
|
|
$max_score = $this->host->get($this, "max_score", $this->default_max_score);
|
|
|
|
$import_score = $this->host->get($this, "import_score");
|
2021-02-17 10:35:10 +00:00
|
|
|
?>
|
|
|
|
|
|
|
|
<div dojoType="dijit.layout.AccordionPane"
|
|
|
|
title="<i class='material-icons'>extension</i> <?= __('Reddit content settings (af_redditimgur)') ?>">
|
|
|
|
|
|
|
|
<form dojoType='dijit.form.Form'>
|
|
|
|
|
2021-02-17 18:44:21 +00:00
|
|
|
<?= \Controls\pluginhandler_tags($this, "save") ?>
|
2021-02-17 10:35:10 +00:00
|
|
|
|
2021-02-18 09:27:26 +00:00
|
|
|
<script type="dojo/method" event="onSubmit" args="evt">
|
2021-02-17 10:35:10 +00:00
|
|
|
evt.preventDefault();
|
|
|
|
if (this.validate()) {
|
2021-02-18 09:27:26 +00:00
|
|
|
Notify.progress('Saving data...', true);
|
2021-02-19 10:44:56 +00:00
|
|
|
xhr.post("backend.php", this.getValues(), (reply) => {
|
|
|
|
Notify.info(reply);
|
2021-02-18 09:27:26 +00:00
|
|
|
})
|
2015-07-06 19:44:43 +00:00
|
|
|
}
|
2021-02-17 10:35:10 +00:00
|
|
|
</script>
|
|
|
|
|
2021-02-27 10:50:28 +00:00
|
|
|
<fieldset class='narrow'>
|
|
|
|
<label>
|
|
|
|
<?= __("Apply tags to NSFW posts (comma-separated list):") ?>
|
|
|
|
</label>
|
|
|
|
<input dojoType="dijit.form.TextBox" name="apply_nsfw_tags" size="20"
|
|
|
|
value="<?= htmlspecialchars(implode(", ", $apply_nsfw_tags)) ?>">
|
|
|
|
</fieldset>
|
|
|
|
|
2021-02-17 10:35:10 +00:00
|
|
|
<fieldset class='narrow'>
|
|
|
|
<label class='checkbox'>
|
|
|
|
<?= \Controls\checkbox_tag("enable_readability", $enable_readability) ?>
|
|
|
|
<?= __("Extract missing content using Readability (requires af_readability)") ?>
|
|
|
|
</label>
|
|
|
|
</fieldset>
|
|
|
|
|
|
|
|
<fieldset class='narrow'>
|
|
|
|
<label class='checkbox'>
|
|
|
|
<?= \Controls\checkbox_tag("enable_content_dupcheck", $enable_content_dupcheck) ?>
|
|
|
|
<?= __("Enable additional duplicate checking") ?>
|
|
|
|
</label>
|
|
|
|
</fieldset>
|
|
|
|
|
|
|
|
<fieldset class='narrow'>
|
|
|
|
<label class='checkbox'>
|
|
|
|
<?= \Controls\checkbox_tag("reddit_to_teddit", $reddit_to_teddit) ?>
|
|
|
|
<?= T_sprintf("Rewrite Reddit URLs to %s",
|
|
|
|
"<a target=\"_blank\" href=\"https://teddit.net/about\">Teddit</a>") ?>
|
|
|
|
</label>
|
|
|
|
</fieldset>
|
|
|
|
|
2021-03-07 12:21:31 +00:00
|
|
|
<fieldset class='narrow'>
|
|
|
|
<label class='checkbox'>
|
|
|
|
<?= \Controls\checkbox_tag("import_score", $import_score) ?>
|
|
|
|
<?= __("Import score, limit maximum to:") ?>
|
|
|
|
<input dojoType="dijit.form.TextBox" name="max_score" size="20"
|
|
|
|
placeholder="<?= $this->default_max_score ?>" value="<?= $max_score ?>">
|
|
|
|
</label>
|
|
|
|
</fieldset>
|
|
|
|
|
2021-02-17 10:35:10 +00:00
|
|
|
<hr/>
|
|
|
|
<?= \Controls\submit_tag(__("Save")) ?>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<?php
|
2015-07-06 19:44:43 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 17:51:22 +00:00
|
|
|
function save() : void {
|
2021-02-16 15:50:18 +00:00
|
|
|
$enable_readability = checkbox_to_sql_bool($_POST["enable_readability"] ?? "");
|
|
|
|
$enable_content_dupcheck = checkbox_to_sql_bool($_POST["enable_content_dupcheck"] ?? "");
|
|
|
|
$reddit_to_teddit = checkbox_to_sql_bool($_POST["reddit_to_teddit"] ?? "");
|
2021-02-27 10:50:28 +00:00
|
|
|
$apply_nsfw_tags = FeedItem_Common::normalize_categories(explode(",", $_POST["apply_nsfw_tags"] ?? ""));
|
2021-03-07 12:21:31 +00:00
|
|
|
$import_score = checkbox_to_sql_bool($_POST["import_score"] ?? "");
|
|
|
|
$max_score = (int) $_POST['max_score'];
|
|
|
|
|
|
|
|
$this->host->set_array($this, [
|
|
|
|
"enable_readability" => $enable_readability,
|
|
|
|
"reddit_to_teddit" => $reddit_to_teddit,
|
|
|
|
"enable_content_dupcheck" => $enable_content_dupcheck,
|
|
|
|
"apply_nsfw_tags" => $apply_nsfw_tags,
|
|
|
|
"import_score" => $import_score,
|
|
|
|
"max_score" => $max_score
|
|
|
|
]);
|
2015-07-06 19:44:43 +00:00
|
|
|
|
|
|
|
echo __("Configuration saved");
|
2012-12-23 10:52:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 17:51:22 +00:00
|
|
|
/**
|
|
|
|
* @param array<string,mixed> $data (this is a huge blob of random crap returned by reddit API)
|
|
|
|
* @param DOMDocument $doc
|
|
|
|
* @param DOMXPath $xpath
|
|
|
|
* @param DOMElement $anchor
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function process_post_media(array $data, DOMDocument $doc, DOMXPath $xpath, DOMElement $anchor) : bool {
|
2021-01-19 19:21:57 +00:00
|
|
|
$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-03-13 08:18:59 +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-03-13 08:18:59 +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-03-13 08:18:59 +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) {
|
2021-03-13 08:18:59 +00:00
|
|
|
Debug::log("found video fallback_url: $fallback_url", Debug::LOG_VERBOSE);
|
2021-01-19 19:21:57 +00:00
|
|
|
$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-03-13 08:18:59 +00:00
|
|
|
Debug::log("found video url: $media_url / poster $poster_url", Debug::LOG_VERBOSE);
|
2021-01-19 19:21:57 +00:00
|
|
|
$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-03-13 08:18:59 +00:00
|
|
|
Debug::log("found image url: $media_url", Debug::LOG_VERBOSE);
|
2021-01-19 19:21:57 +00:00
|
|
|
$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-03-13 08:18:59 +00:00
|
|
|
Debug::log("found preview image url: $media_url (link: $target_url)", Debug::LOG_VERBOSE);
|
2021-01-22 12:44:44 +00:00
|
|
|
$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
|
2021-03-13 08:18:59 +00:00
|
|
|
Debug::log("found fallback preview image url: $media_url (link: $target_url);", Debug::LOG_VERBOSE);
|
2021-01-22 12:44:44 +00:00
|
|
|
array_push($this->fallback_preview_urls, $media_url);
|
|
|
|
}
|
2021-01-21 05:28:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-14 17:51:22 +00:00
|
|
|
return $found > 0;
|
2021-01-19 19:21:57 +00:00
|
|
|
}
|
2016-08-01 18:03:36 +00:00
|
|
|
|
2021-03-07 12:21:31 +00:00
|
|
|
/* function score_convert(int $value, int $from1, int $from2, int $to1, int $to2) {
|
|
|
|
return ($value - $from1) / ($from2 - $from1) * ($to2 - $to1) + $to1;
|
|
|
|
} */
|
|
|
|
|
2021-11-14 17:51:22 +00:00
|
|
|
/**
|
|
|
|
* @param array<string, mixed> $article
|
|
|
|
* @param DOMDocument $doc
|
|
|
|
* @param DOMXPath $xpath
|
|
|
|
* @return bool
|
|
|
|
* @throws PDOException
|
|
|
|
*/
|
|
|
|
private function inline_stuff(array &$article, DOMDocument &$doc, DOMXpath $xpath) : bool {
|
2016-08-01 18:03:36 +00:00
|
|
|
|
2021-03-07 12:21:31 +00:00
|
|
|
$max_score = (int) $this->host->get($this, "max_score", $this->default_max_score);
|
|
|
|
$import_score = (bool) $this->host->get($this, "import_score", $this->default_max_score);
|
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
$found = false;
|
2021-02-27 10:50:28 +00:00
|
|
|
$post_is_nsfw = false;
|
2021-03-02 18:32:20 +00:00
|
|
|
$num_comments = 0;
|
2021-03-07 12:21:31 +00:00
|
|
|
$score = 0;
|
2021-03-09 05:19:55 +00:00
|
|
|
$link_flairs = [];
|
2021-02-27 10:50:28 +00:00
|
|
|
$apply_nsfw_tags = FeedItem_Common::normalize_categories($this->host->get_array($this, "apply_nsfw_tags", []));
|
2016-08-01 18:03:36 +00:00
|
|
|
|
2021-04-23 17:30:29 +00:00
|
|
|
$this->generated_enclosures = [];
|
|
|
|
|
2021-11-15 12:24:38 +00:00
|
|
|
/** @var DOMElement|null $anchor -- embed anchor element, before reddit <table> post layout */
|
2021-02-22 21:27:52 +00:00
|
|
|
$anchor = $xpath->query('//body/*')->item(0);
|
2021-01-22 12:44:44 +00:00
|
|
|
|
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-03-13 08:18:59 +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
|
|
|
|
2021-02-22 11:41:09 +00:00
|
|
|
// @phpstan-ignore-next-line
|
2021-01-19 19:21:57 +00:00
|
|
|
if ($tmp && $anchor) {
|
|
|
|
$json = json_decode($tmp, true);
|
|
|
|
|
|
|
|
if ($json) {
|
2021-03-13 08:18:59 +00:00
|
|
|
Debug::log("JSON: processing media elements...", Debug::LOG_EXTENDED);
|
2021-01-21 05:28:55 +00:00
|
|
|
|
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-27 10:50:28 +00:00
|
|
|
$over_18 = $data["over_18"] ?? 0 == 1;
|
|
|
|
|
2021-03-07 12:21:31 +00:00
|
|
|
$score += $data['score'] ?? 0;
|
2021-03-02 18:32:20 +00:00
|
|
|
$num_comments += $data["num_comments"] ?? 0;
|
|
|
|
|
2021-03-09 05:19:55 +00:00
|
|
|
if (!empty($data["link_flair_text"])) {
|
|
|
|
array_push($link_flairs, $data["link_flair_text"]);
|
|
|
|
}
|
|
|
|
|
2021-02-27 10:50:28 +00:00
|
|
|
if ($over_18) {
|
2021-03-13 08:18:59 +00:00
|
|
|
Debug::log("JSON: post is NSFW", Debug::LOG_EXTENDED);
|
2021-02-27 10:50:28 +00:00
|
|
|
$post_is_nsfw = true;
|
|
|
|
}
|
2021-01-19 19:21:57 +00:00
|
|
|
|
2021-02-05 20:41:32 +00:00
|
|
|
if (isset($data["crosspost_parent_list"])) {
|
2021-03-13 08:18:59 +00:00
|
|
|
Debug::log("JSON: processing child crosspost_parent_list", Debug::LOG_EXTENDED);
|
2021-01-19 19:21:57 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 08:18:59 +00:00
|
|
|
Debug::log("JSON: processing child data element...", Debug::LOG_EXTENDED);
|
2021-01-19 19:21:57 +00:00
|
|
|
|
|
|
|
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 {
|
2021-03-13 08:18:59 +00:00
|
|
|
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) {
|
2021-03-13 08:18:59 +00:00
|
|
|
Debug::log("JSON: failed to fetch post:" . UrlHelper::$fetch_last_error, Debug::LOG_EXTENDED);
|
2021-01-19 19:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-22 12:44:44 +00:00
|
|
|
} else if (!$anchor) {
|
2021-03-13 08:18:59 +00:00
|
|
|
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-02-27 10:50:28 +00:00
|
|
|
if ($post_is_nsfw && count($apply_nsfw_tags) > 0) {
|
|
|
|
$article["tags"] = array_merge($article["tags"], $apply_nsfw_tags);
|
|
|
|
}
|
|
|
|
|
2021-03-09 05:19:55 +00:00
|
|
|
if (count($link_flairs) > 0) {
|
|
|
|
$article["tags"] = array_merge($article["tags"], FeedItem_Common::normalize_categories($link_flairs));
|
|
|
|
}
|
|
|
|
|
2021-03-02 18:32:20 +00:00
|
|
|
$article["num_comments"] = $num_comments;
|
|
|
|
|
2021-03-07 12:21:31 +00:00
|
|
|
if ($import_score && $score > 0)
|
|
|
|
$article["score_modifier"] = ($article["score_modifier"] ?? 0) + ($score > $max_score ? $max_score : $score);
|
|
|
|
|
2021-01-19 19:21:57 +00:00
|
|
|
if ($found) {
|
2021-03-13 08:18:59 +00:00
|
|
|
Debug::log("JSON: found media data, skipping further processing of content", Debug::LOG_VERBOSE);
|
2021-01-19 19:21:57 +00:00
|
|
|
$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"])) {
|
2021-03-13 08:18:59 +00:00
|
|
|
Debug::log("BODY: domain of $entry_href is blacklisted, skipping", Debug::LOG_EXTENDED);
|
2021-01-19 19:21:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-05-18 09:05:30 +00:00
|
|
|
|
2021-03-13 08:18:59 +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)) {
|
2021-03-13 08:18:59 +00:00
|
|
|
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-03-13 08:18:59 +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)) {
|
2021-03-13 08:18:59 +00:00
|
|
|
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)) {
|
2021-03-13 08:18:59 +00:00
|
|
|
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);
|
2021-02-22 11:41:09 +00:00
|
|
|
else
|
|
|
|
$poster_url = false;
|
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();
|
2021-05-07 04:42:24 +00:00
|
|
|
if (!$found && $vid_id = UrlHelper::url_to_youtube_vid($entry_href)) {
|
2015-07-06 18:44:38 +00:00
|
|
|
|
2021-03-13 08:18:59 +00:00
|
|
|
Debug::log("Handling as youtube: $vid_id", Debug::LOG_VERBOSE);
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2021-04-23 17:30:29 +00:00
|
|
|
/* normalize video URL for af_youtube_... plugins */
|
|
|
|
$video_url = "https://www.youtube.com/v/$vid_id";
|
|
|
|
|
|
|
|
/* push generated video URL to enclosures so that youtube embed plugins would deal with it later (if enabled) */
|
|
|
|
$this->generated_enclosures[] = [$video_url, "text/html", null, null, '', ''];
|
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-03-13 08:18:59 +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-03-13 08:18:59 +00:00
|
|
|
Debug::log("handling as imgur page/whatever", Debug::LOG_VERBOSE);
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
$content_type = $this->get_content_type($entry_href);
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
if ($content_type && strpos($content_type, "text/html") !== false) {
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
$content = UrlHelper::fetch(["url" => $entry_href,
|
|
|
|
"http_accept" => "text/*"]);
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
if ($content) {
|
|
|
|
$cdoc = new DOMDocument();
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
if (@$cdoc->loadHTML($content)) {
|
|
|
|
$cxpath = new DOMXPath($cdoc);
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
/** @var ?DOMElement $rel_image */
|
|
|
|
$rel_image = $cxpath->query("//link[@rel='image_src']")->item(0);
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
if ($rel_image) {
|
|
|
|
|
|
|
|
$img = $doc->createElement('img');
|
|
|
|
$img->setAttribute("src", $rel_image->getAttribute("href"));
|
2019-02-06 13:24:31 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
$br = $doc->createElement('br');
|
|
|
|
$entry->parentNode->insertBefore($img, $entry);
|
|
|
|
$entry->parentNode->insertBefore($br, $entry);
|
|
|
|
|
|
|
|
$found = true;
|
|
|
|
}
|
2020-01-04 16:21:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-24 05:27:10 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
Debug::log("skipping imgur $entry_href because of content type: $content_type", Debug::LOG_VERBOSE);
|
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-03-13 08:18:59 +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) {
|
2021-03-13 08:18:59 +00:00
|
|
|
Debug::log("looking for meta og:image", Debug::LOG_VERBOSE);
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
$content_type = $this->get_content_type($entry_href);
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
if ($content_type && strpos($content_type, "text/html") !== false) {
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
$content = UrlHelper::fetch(["url" => $entry_href,
|
|
|
|
"http_accept" => "text/*"]);
|
2018-09-10 13:09:38 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
if ($content) {
|
|
|
|
$cdoc = new DOMDocument();
|
2021-10-22 09:33:55 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
if (@$cdoc->loadHTML($content)) {
|
|
|
|
$cxpath = new DOMXPath($cdoc);
|
2018-09-10 13:09:38 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
/** @var ?DOMElement $og_image */
|
|
|
|
$og_image = $cxpath->query("//meta[@property='og:image']")->item(0);
|
2018-09-10 13:09:38 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
/** @var ?DOMElement $og_video */
|
|
|
|
$og_video = $cxpath->query("//meta[@property='og:video']")->item(0);
|
2018-09-10 13:09:38 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
if ($og_video) {
|
2018-09-10 13:09:38 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
$source_stream = $og_video->getAttribute("content");
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
if ($source_stream) {
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
if ($og_image) {
|
|
|
|
$poster_url = $og_image->getAttribute("content");
|
|
|
|
} else {
|
|
|
|
$poster_url = false;
|
|
|
|
}
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
$this->handle_as_video($doc, $entry, $source_stream, $poster_url);
|
|
|
|
$found = true;
|
|
|
|
}
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
} else if ($og_image) {
|
2018-05-26 07:25:39 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
$og_src = $og_image->getAttribute("content");
|
2021-01-19 19:21:57 +00:00
|
|
|
|
2021-11-24 05:27:10 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-05-26 07:25:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-24 05:27:10 +00:00
|
|
|
} else {
|
|
|
|
Debug::log("BODY: skipping $entry_href because of content type: $content_type", Debug::LOG_VERBOSE);
|
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) {
|
2021-03-13 08:18:59 +00:00
|
|
|
Debug::log("JSON: processing fallback preview urls...", Debug::LOG_VERBOSE);
|
2021-01-22 12:44:44 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-11-14 17:51:22 +00:00
|
|
|
return $found > 0;
|
2015-07-07 05:44:31 +00:00
|
|
|
}
|
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-10-22 09:33:55 +00:00
|
|
|
/** @var ?DOMElement $content_link */
|
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"];
|
|
|
|
|
2021-02-22 18:47:48 +00:00
|
|
|
if (Config::get(Config::DB_TYPE) == "pgsql") {
|
2021-02-06 07:31:06 +00:00
|
|
|
$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);
|
2021-04-23 17:30:29 +00:00
|
|
|
$article["enclosures"] = $this->generated_enclosures;
|
2021-02-06 07:31:06 +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;
|
|
|
|
}
|
|
|
|
|
2021-11-14 17:51:22 +00:00
|
|
|
private function remove_post_thumbnail(DOMDocument $doc, DOMXpath $xpath) : void {
|
2021-01-19 19:21:57 +00:00
|
|
|
$thumb = $xpath->query("//td/a/img[@src]")->item(0);
|
|
|
|
|
|
|
|
if ($thumb)
|
|
|
|
$thumb->parentNode->parentNode->removeChild($thumb->parentNode);
|
|
|
|
}
|
|
|
|
|
2021-11-14 17:51:22 +00:00
|
|
|
private function handle_as_image(DOMDocument $doc, DOMElement $entry, string $image_url, string $link_url = "") : void {
|
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);
|
|
|
|
}
|
|
|
|
|
2021-11-14 17:51:22 +00:00
|
|
|
private function handle_as_video(DOMDocument $doc, DOMElement $entry, string $source_stream, string $poster_url = "") : void {
|
2016-03-20 08:30:56 +00:00
|
|
|
|
2021-03-13 08:18:59 +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
|
|
|
|
2021-02-17 18:44:21 +00:00
|
|
|
function csrf_ignore($method) {
|
|
|
|
return $method === "testurl";
|
|
|
|
}
|
|
|
|
|
2021-11-14 17:51:22 +00:00
|
|
|
function testurl() : void {
|
2021-01-21 05:28:55 +00:00
|
|
|
|
2021-03-09 05:19:55 +00:00
|
|
|
$url = clean($_POST["url"] ?? "");
|
|
|
|
$article_url = clean($_POST["article_url"] ?? "");
|
2021-03-13 08:18:59 +00:00
|
|
|
$article_id = clean($_POST["article_id"] ?? "");
|
2021-01-21 05:28:55 +00:00
|
|
|
|
2021-01-22 12:44:44 +00:00
|
|
|
$this->dump_json_data = true;
|
|
|
|
|
2021-03-13 08:18:59 +00:00
|
|
|
if (!$url && !$article_url && !$article_id) {
|
2021-01-21 05:28:55 +00:00
|
|
|
header("Content-type: text/html");
|
|
|
|
?>
|
|
|
|
<style type="text/css">
|
|
|
|
fieldset { border : 0; }
|
|
|
|
label { display : inline-block; min-width : 120px; }
|
|
|
|
</style>
|
2021-02-17 11:56:36 +00:00
|
|
|
<form action="backend.php" method="post">
|
|
|
|
<input type="hidden" name="op" value="pluginhandler">
|
|
|
|
<input type="hidden" name="method" value="testurl">
|
|
|
|
<input type="hidden" name="plugin" value="af_redditimgur">
|
2021-01-21 05:28:55 +00:00
|
|
|
<fieldset>
|
2021-03-13 08:18:59 +00:00
|
|
|
<label>Test 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>
|
2021-03-13 08:18:59 +00:00
|
|
|
<hr/>
|
|
|
|
<fieldset>
|
|
|
|
<label>Article ID:</label>
|
|
|
|
<input name="article_id" size="10" value="<?= htmlspecialchars($article_id) ?>"></input>
|
|
|
|
</fieldset>
|
2021-01-21 05:28:55 +00:00
|
|
|
<fieldset>
|
2021-03-13 08:18:59 +00:00
|
|
|
<label>or 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-03-13 08:18:59 +00:00
|
|
|
Debug::set_loglevel(Debug::LOG_EXTENDED);
|
|
|
|
|
|
|
|
if ($article_id) {
|
|
|
|
$stored_article = ORM::for_table('ttrss_entries')
|
|
|
|
->table_alias('e')
|
|
|
|
->join('ttrss_user_entries', [ 'ref_id', '=', 'e.id'], 'ue')
|
|
|
|
->where('ue.owner_uid', $_SESSION['uid'])
|
|
|
|
->find_one($article_id);
|
|
|
|
|
|
|
|
if (!$stored_article) {
|
|
|
|
Debug::log("Article not found: $article_id", Debug::LOG_VERBOSE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$article = [
|
|
|
|
"link" => $stored_article->link,
|
|
|
|
"content" => $stored_article->content,
|
|
|
|
"tags" => explode(",", $stored_article->tag_cache)
|
|
|
|
];
|
2021-01-18 12:34:05 +00:00
|
|
|
|
2021-03-13 08:18:59 +00:00
|
|
|
} else {
|
|
|
|
$article = [
|
|
|
|
"link" => $article_url,
|
|
|
|
"content" => "<html><body><table><tr><td><a href=\"$url\">[link]</a></td></tr></table></body>",
|
|
|
|
"tags" => []];
|
|
|
|
}
|
2016-03-20 08:30:56 +00:00
|
|
|
|
|
|
|
$doc = new DOMDocument();
|
2021-03-13 08:18:59 +00:00
|
|
|
@$doc->loadHTML($article["content"]);
|
2016-03-20 08:30:56 +00:00
|
|
|
$xpath = new DOMXPath($doc);
|
|
|
|
|
2021-02-27 10:50:28 +00:00
|
|
|
$found = $this->inline_stuff($article, $doc, $xpath);
|
2016-08-02 09:25:54 +00:00
|
|
|
|
2021-03-13 08:18:59 +00:00
|
|
|
Debug::log("Inline result: $found", Debug::LOG_VERBOSE);
|
2016-08-02 09:25:54 +00:00
|
|
|
|
2021-03-09 05:19:55 +00:00
|
|
|
print_r($article);
|
|
|
|
|
2016-08-02 09:25:54 +00:00
|
|
|
if (!$found) {
|
2021-03-13 08:18:59 +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-03-13 08:18:59 +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
|
|
|
|
2021-08-23 07:56:31 +00:00
|
|
|
/** $useragent defaults to Config::get_user_agent() */
|
2021-11-14 17:51:22 +00:00
|
|
|
private function get_header(string $url, int $header, string $useragent = "") : string {
|
|
|
|
$ret = "";
|
2016-08-01 13:20:14 +00:00
|
|
|
|
2021-02-23 06:01:27 +00:00
|
|
|
if (function_exists("curl_init")) {
|
2016-08-01 13:20:14 +00:00
|
|
|
$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"));
|
2021-08-23 07:56:31 +00:00
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $useragent ? $useragent : Config::get_user_agent());
|
2016-08-01 13:20:14 +00:00
|
|
|
|
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
|
|
|
|
2021-11-14 17:51:22 +00:00
|
|
|
private function get_content_type(string $url, string $useragent = "") : string {
|
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
|
|
|
|
2021-11-14 17:51:22 +00:00
|
|
|
/*private function get_location(string $url, string $useragent = "") : string {
|
2020-12-12 15:40:26 +00:00
|
|
|
return $this->get_header($url, CURLINFO_EFFECTIVE_URL, $useragent);
|
2021-11-14 17:51:22 +00:00
|
|
|
}*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string,mixed> $article
|
|
|
|
* @param string $url
|
|
|
|
* @param DOMDocument $doc
|
|
|
|
* @param DOMXPath $xpath
|
|
|
|
* @param bool $debug
|
|
|
|
* @return array<string,mixed>
|
|
|
|
* @throws PDOException
|
|
|
|
*/
|
|
|
|
private function readability(array $article, string $url, DOMDocument $doc, DOMXpath $xpath, bool $debug = false) : array {
|
2016-08-02 09:25:54 +00:00
|
|
|
|
2021-02-23 06:01:27 +00:00
|
|
|
if (function_exists("curl_init") && $this->host->get($this, "enable_readability") &&
|
2016-08-02 09:25:54 +00:00
|
|
|
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-11-14 17:51:22 +00:00
|
|
|
/**
|
|
|
|
* @param string $src
|
|
|
|
* @param array<string> $also_blacklist
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function is_blacklisted(string $src, array $also_blacklist = []) : bool {
|
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);
|
|
|
|
}
|
|
|
|
|
2021-11-14 17:51:22 +00:00
|
|
|
private function rewrite_to_teddit(string $str) : string {
|
2020-12-20 08:28:48 +00:00
|
|
|
if (strpos($str, "reddit.com") !== false) {
|
|
|
|
return preg_replace("/https?:\/\/([a-z]+\.)?reddit\.com/", "https://teddit.net", $str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2021-11-14 17:51:22 +00:00
|
|
|
private function rewrite_to_reddit(string $str) : string {
|
2021-10-22 10:25:43 +00:00
|
|
|
if (strpos($str, "teddit.net") !== false) {
|
|
|
|
$str = preg_replace("/https?:\/\/teddit.net/", "https://reddit.com", $str);
|
|
|
|
|
|
|
|
if (strpos($str, "/.rss") === false)
|
|
|
|
$str .= "/.rss";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2020-12-20 08:28:48 +00:00
|
|
|
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
|
|
|
}
|