af_redditimgur: check content-type before downloading data for og:image and imgur pages

This commit is contained in:
Andrew Dolgov 2021-11-24 08:27:10 +03:00
parent 204f92b926
commit 831648e3c8
1 changed files with 60 additions and 45 deletions

View File

@ -496,30 +496,38 @@ class Af_RedditImgur extends Plugin {
Debug::log("handling as imgur page/whatever", Debug::LOG_VERBOSE); Debug::log("handling as imgur page/whatever", Debug::LOG_VERBOSE);
$content = UrlHelper::fetch(["url" => $entry_href, $content_type = $this->get_content_type($entry_href);
"http_accept" => "text/*"]);
if ($content) { if ($content_type && strpos($content_type, "text/html") !== false) {
$cdoc = new DOMDocument();
if (@$cdoc->loadHTML($content)) { $content = UrlHelper::fetch(["url" => $entry_href,
$cxpath = new DOMXPath($cdoc); "http_accept" => "text/*"]);
/** @var ?DOMElement $rel_image */ if ($content) {
$rel_image = $cxpath->query("//link[@rel='image_src']")->item(0); $cdoc = new DOMDocument();
if ($rel_image) { if (@$cdoc->loadHTML($content)) {
$cxpath = new DOMXPath($cdoc);
$img = $doc->createElement('img'); /** @var ?DOMElement $rel_image */
$img->setAttribute("src", $rel_image->getAttribute("href")); $rel_image = $cxpath->query("//link[@rel='image_src']")->item(0);
$br = $doc->createElement('br'); if ($rel_image) {
$entry->parentNode->insertBefore($img, $entry);
$entry->parentNode->insertBefore($br, $entry);
$found = true; $img = $doc->createElement('img');
$img->setAttribute("src", $rel_image->getAttribute("href"));
$br = $doc->createElement('br');
$entry->parentNode->insertBefore($img, $entry);
$entry->parentNode->insertBefore($br, $entry);
$found = true;
}
} }
} }
} else {
Debug::log("skipping imgur $entry_href because of content type: $content_type", Debug::LOG_VERBOSE);
} }
} }
@ -543,53 +551,60 @@ class Af_RedditImgur extends Plugin {
if (!$found) { if (!$found) {
Debug::log("looking for meta og:image", Debug::LOG_VERBOSE); Debug::log("looking for meta og:image", Debug::LOG_VERBOSE);
$content = UrlHelper::fetch(["url" => $entry_href, $content_type = $this->get_content_type($entry_href);
"http_accept" => "text/*"]);
if ($content) { if ($content_type && strpos($content_type, "text/html") !== false) {
$cdoc = new DOMDocument();
if (@$cdoc->loadHTML($content)) { $content = UrlHelper::fetch(["url" => $entry_href,
$cxpath = new DOMXPath($cdoc); "http_accept" => "text/*"]);
/** @var ?DOMElement $og_image */ if ($content) {
$og_image = $cxpath->query("//meta[@property='og:image']")->item(0); $cdoc = new DOMDocument();
/** @var ?DOMElement $og_video */ if (@$cdoc->loadHTML($content)) {
$og_video = $cxpath->query("//meta[@property='og:video']")->item(0); $cxpath = new DOMXPath($cdoc);
if ($og_video) { /** @var ?DOMElement $og_image */
$og_image = $cxpath->query("//meta[@property='og:image']")->item(0);
$source_stream = $og_video->getAttribute("content"); /** @var ?DOMElement $og_video */
$og_video = $cxpath->query("//meta[@property='og:video']")->item(0);
if ($source_stream) { if ($og_video) {
if ($og_image) { $source_stream = $og_video->getAttribute("content");
$poster_url = $og_image->getAttribute("content");
} else { if ($source_stream) {
$poster_url = false;
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;
} }
$this->handle_as_video($doc, $entry, $source_stream, $poster_url); } else if ($og_image) {
$found = true;
}
} else if ($og_image) { $og_src = $og_image->getAttribute("content");
$og_src = $og_image->getAttribute("content"); if ($og_src) {
$img = $doc->createElement('img');
$img->setAttribute("src", $og_src);
if ($og_src) { $br = $doc->createElement('br');
$img = $doc->createElement('img'); $entry->parentNode->insertBefore($img, $entry);
$img->setAttribute("src", $og_src); $entry->parentNode->insertBefore($br, $entry);
$br = $doc->createElement('br'); $found = true;
$entry->parentNode->insertBefore($img, $entry); }
$entry->parentNode->insertBefore($br, $entry);
$found = true;
} }
} }
} }
} else {
Debug::log("BODY: skipping $entry_href because of content type: $content_type", Debug::LOG_VERBOSE);
} }
} }
} }