From 9d852e052c0ed706b9146ab3214ee94f1d91a4c5 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 15 Aug 2019 09:04:42 +0300 Subject: add HOOK_ARTICLE_IMAGE for Article::get_article_image() --- classes/article.php | 84 ++++++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 39 deletions(-) (limited to 'classes/article.php') diff --git a/classes/article.php b/classes/article.php index 02477b553..599f12021 100755 --- a/classes/article.php +++ b/classes/article.php @@ -828,59 +828,65 @@ class Article extends Handler_Protected { $article_image = ""; $article_stream = ""; - $tmpdoc = new DOMDocument(); - - if (@$tmpdoc->loadHTML('' . 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('' . 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]; -- cgit v1.2.3