From 68e2b05f657dca2a389cb9d585215c126e5da290 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 14 Aug 2019 16:55:38 +0300 Subject: [PATCH] * move get_article_image to Article; implement better og:image detection (similar to android app) * pass article image to API clients in headlines row object --- classes/api.php | 8 +++++--- classes/article.php | 40 ++++++++++++++++++++++++++++++++++++++ classes/handler/public.php | 32 ++---------------------------- 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/classes/api.php b/classes/api.php index 45e4d3062..0c5c30135 100755 --- a/classes/api.php +++ b/classes/api.php @@ -759,9 +759,10 @@ class API extends Handler { "tags" => $tags, ); + $enclosures = Article::get_article_enclosures($line['id']); + if ($include_attachments) - $headline_row['attachments'] = Article::get_article_enclosures( - $line['id']); + $headline_row['attachments'] = $enclosures; if ($show_excerpt) $headline_row["excerpt"] = $line["content_preview"]; @@ -801,7 +802,8 @@ class API extends Handler { $headline_row = $p->hook_render_article_api(array("headline" => $headline_row)); } - $headline_row['content'] = DiskCache::rewriteUrls($headline_row['content']); + $headline_row["content"] = DiskCache::rewriteUrls($headline_row['content']); + $headline_row["flavor_image"] = Article::get_article_image($enclosures, $line["content"], $line["site_url"]); array_push($headlines, $headline_row); } diff --git a/classes/article.php b/classes/article.php index 67b1a4a05..f22c28066 100755 --- a/classes/article.php +++ b/classes/article.php @@ -823,4 +823,44 @@ class Article extends Handler_Protected { return true; } + static function get_article_image($enclosures, $content, $site_url) { + + $article_image = false; + $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"; + break; + } + } else if ($e->nodeName == "video") { + $article_image = $e->getAttribute("poster"); + break; + } else if ($e->nodeName == 'img') { + if (mb_strpos($e->getAttribute("src"), "data:") !== 0) { + $article_image = $e->getAttribute("src"); + } + break; + } + } + } + + if ($article_image) + return rewrite_relative_url($site_url, $article_image); + + foreach ($enclosures as $enc) { + if (strpos($enc["content_type"], "image/") !== FALSE) { + return rewrite_relative_url($site_url, $enc["content_url"]); + } + } + + return false; + } + } diff --git a/classes/handler/public.php b/classes/handler/public.php index eb5363eab..b476805e1 100755 --- a/classes/handler/public.php +++ b/classes/handler/public.php @@ -157,7 +157,7 @@ class Handler_Public extends Handler { } $tpl->setVariable('ARTICLE_OG_IMAGE', - $this->get_article_image($enclosures, $line['content'], $feed_site_url), true); + Article::get_article_image($enclosures, $line['content'], $feed_site_url), true); $tpl->addBlock('entry'); } @@ -319,34 +319,6 @@ class Handler_Public extends Handler { print "Article not found."; } - private function get_article_image($enclosures, $content, $site_url) { - $og_image = false; - - foreach ($enclosures as $enc) { - if (strpos($enc["content_type"], "image/") !== FALSE) { - return rewrite_relative_url($site_url, $enc["content_url"]); - } - } - - if (!$og_image) { - $tmpdoc = new DOMDocument(); - - if (@$tmpdoc->loadHTML('' . mb_substr($content, 0, 131070))) { - $tmpxpath = new DOMXPath($tmpdoc); - $imgs = $tmpxpath->query("//img"); - - foreach ($imgs as $img) { - $src = $img->getAttribute("src"); - - if (mb_strpos($src, "data:") !== 0) - return rewrite_relative_url($site_url, $src); - } - } - } - - return false; - } - private function format_article($id, $owner_uid) { $pdo = Db::pdo(); @@ -409,7 +381,7 @@ class Handler_Public extends Handler { $rv .= ""; - $og_image = $this->get_article_image($enclosures, $line['content'], $line["site_url"]); + $og_image = Article::get_article_image($enclosures, $line['content'], $line["site_url"]); if ($og_image) { $rv .= "";