add HOOK_ARTICLE_IMAGE for Article::get_article_image()

master
Andrew Dolgov 5 years ago
parent ffb842f752
commit 9d852e052c

@ -828,59 +828,65 @@ class Article extends Handler_Protected {
$article_image = "";
$article_stream = "";
$tmpdoc = new DOMDocument();
if (@$tmpdoc->loadHTML('<?xml encoding="UTF-8">' . mb_substr($content, 0, 131070))) {
$tmpxpath = new DOMXPath($tmpdoc);
$elems = $tmpxpath->query('(//img[@src]|//video[@poster]|//iframe[contains(@src , "youtube.com/embed/")])');
foreach ($elems as $e) {
if ($e->nodeName == "iframe") {
$matches = [];
if ($rrr = preg_match("/\/embed\/([\w-]+)/", $e->getAttribute("src"), $matches)) {
$article_image = "https://img.youtube.com/vi/" . $matches[1] . "/hqdefault.jpg";
$article_stream = "https://youtu.be/" . $matches[1];
break;
}
} else if ($e->nodeName == "video") {
$article_image = $e->getAttribute("poster");
foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_ARTICLE_IMAGE) as $p) {
list ($article_image, $article_stream) = $p->hook_article_image($enclosures, $content, $site_url);
}
$src = $tmpxpath->query("//source[@src]", $e)->item(0);
if (!$article_image && !$article_stream) {
$tmpdoc = new DOMDocument();
if ($src) {
$article_stream = $src->getAttribute("src");
}
if (@$tmpdoc->loadHTML('<?xml encoding="UTF-8">' . mb_substr($content, 0, 131070))) {
$tmpxpath = new DOMXPath($tmpdoc);
$elems = $tmpxpath->query('(//img[@src]|//video[@poster]|//iframe[contains(@src , "youtube.com/embed/")])');
break;
} else if ($e->nodeName == 'img') {
if (mb_strpos($e->getAttribute("src"), "data:") !== 0) {
$article_image = $e->getAttribute("src");
foreach ($elems as $e) {
if ($e->nodeName == "iframe") {
$matches = [];
if ($rrr = preg_match("/\/embed\/([\w-]+)/", $e->getAttribute("src"), $matches)) {
$article_image = "https://img.youtube.com/vi/" . $matches[1] . "/hqdefault.jpg";
$article_stream = "https://youtu.be/" . $matches[1];
break;
}
} else if ($e->nodeName == "video") {
$article_image = $e->getAttribute("poster");
$src = $tmpxpath->query("//source[@src]", $e)->item(0);
if ($src) {
$article_stream = $src->getAttribute("src");
}
break;
} else if ($e->nodeName == 'img') {
if (mb_strpos($e->getAttribute("src"), "data:") !== 0) {
$article_image = $e->getAttribute("src");
}
break;
}
break;
}
}
}
if ($article_image)
$article_image = rewrite_relative_url($site_url, $article_image);
if (!$article_image)
foreach ($enclosures as $enc) {
if (strpos($enc["content_type"], "image/") !== FALSE) {
$article_image = $enc["content_url"];
break;
}
}
if ($article_stream)
$article_stream = rewrite_relative_url($site_url, $article_stream);
if ($article_image)
$article_image = rewrite_relative_url($site_url, $article_image);
if (!$article_image)
foreach ($enclosures as $enc) {
if (strpos($enc["content_type"], "image/") !== FALSE) {
$article_image = rewrite_relative_url($site_url, $enc["content_url"]);
break;
}
}
if ($article_stream)
$article_stream = rewrite_relative_url($site_url, $article_stream);
}
$cache = new DiskCache("images");
if ($cache->exists(sha1($article_image)))
if ($article_image && $cache->exists(sha1($article_image)))
$article_image = $cache->getUrl(sha1($article_image));
if ($cache->exists(sha1($article_stream)))
if ($article_stream && $cache->exists(sha1($article_stream)))
$article_stream = $cache->getUrl(sha1($article_stream));
return [$article_image, $article_stream];

@ -59,6 +59,7 @@ class PluginHost {
const HOOK_SEND_MAIL = 39;
const HOOK_FILTER_TRIGGERED = 40;
const HOOK_GET_FULL_TEXT = 41;
const HOOK_ARTICLE_IMAGE = 42;
const KIND_ALL = 1;
const KIND_SYSTEM = 2;

Loading…
Cancel
Save