From 0a3d50a270620c15c901dcfc96be808d4a7c1278 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 16 Mar 2021 06:37:43 +0000 Subject: [PATCH] Adjusted field names --- mod/item.php | 18 ++++++++++++------ src/Content/OEmbed.php | 18 +++++++++--------- src/Content/PageInfo.php | 2 +- src/Util/ParseUrl.php | 26 +++++++++++++------------- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/mod/item.php b/mod/item.php index 7c11d7311a..467d538f26 100644 --- a/mod/item.php +++ b/mod/item.php @@ -56,6 +56,7 @@ use Friendica\Protocol\Activity; use Friendica\Protocol\Diaspora; use Friendica\Security\Security; use Friendica\Util\DateTimeFormat; +use Friendica\Util\ParseUrl; use Friendica\Worker\Delivery; function item_post(App $a) { @@ -217,12 +218,15 @@ function item_post(App $a) { $attachment_img_width = $_REQUEST['attachment_img_width'] ?? 0; $attachment_img_height = $_REQUEST['attachment_img_height'] ?? 0; - $attachment = [ - 'type' => $attachment_type, - 'title' => $attachment_title, - 'text' => $attachment_text, - 'url' => $attachment_url, - ]; + + // Fetch the basic attachment data + $attachment = ParseUrl::getSiteinfoCached($attachment_url); + + // Overwrite the basic data with possible changes from the frontend + $attachment['type'] = $attachment_type; + $attachment['title'] = $attachment_title; + $attachment['text'] = $attachment_text; + $attachment['url'] = $attachment_url; if (!empty($attachment_img_src)) { $attachment['images'] = [ @@ -232,6 +236,8 @@ function item_post(App $a) { 'height' => $attachment_img_height ] ]; + } else { + unset($attachment['images']); } $att_bbcode = "\n" . PageInfo::getFooterFromData($attachment); diff --git a/src/Content/OEmbed.php b/src/Content/OEmbed.php index bb3f6290fe..4e26e45f94 100644 --- a/src/Content/OEmbed.php +++ b/src/Content/OEmbed.php @@ -163,31 +163,31 @@ class OEmbed } } - if (!empty($data['title']) && empty($oembed->title)) { + if (!empty($data['title'])) { $oembed->title = $data['title']; } - if (!empty($data['text']) && empty($oembed->description)) { + if (!empty($data['text'])) { $oembed->description = $data['text']; } - if (!empty($data['publisher']) && empty($oembed->provider_name)) { - $oembed->provider_name = $data['publisher']; + if (!empty($data['publisher_name'])) { + $oembed->provider_name = $data['publisher_name']; } - if (!empty($data['publisher_url']) && empty($oembed->provider_url)) { + if (!empty($data['publisher_url'])) { $oembed->provider_url = $data['publisher_url']; } - if (!empty($data['author']) && empty($oembed->author_name)) { - $oembed->author_name = $data['author']; + if (!empty($data['author_name'])) { + $oembed->author_name = $data['author_name']; } - if (!empty($data['author_url']) && empty($oembed->author_url)) { + if (!empty($data['author_url'])) { $oembed->author_url = $data['author_url']; } - if (!empty($data['images']) && empty($oembed->thumbnail_url)) { + if (!empty($data['images'])) { $oembed->thumbnail_url = $data['images'][0]['src']; $oembed->thumbnail_width = $data['images'][0]['width']; $oembed->thumbnail_height = $data['images'][0]['height']; diff --git a/src/Content/PageInfo.php b/src/Content/PageInfo.php index 806a92a03c..776dc15df5 100644 --- a/src/Content/PageInfo.php +++ b/src/Content/PageInfo.php @@ -131,7 +131,7 @@ class PageInfo // Escape some bad characters $text = "[attachment"; - foreach (['type', 'url', 'title', 'alternative_title', 'publisher', 'publisher_url', 'publisher_image', 'author', 'author_url', 'author_image'] as $field) { + foreach (['type', 'url', 'title', 'alternative_title', 'publisher_name', 'publisher_url', 'publisher_img', 'author_name', 'author_url', 'author_img'] as $field) { if (!empty($data[$field])) { $text .= " " . $field . "='" . str_replace(['[', ']'], ['[', ']'], htmlentities($data[$field], ENT_QUOTES, 'UTF-8', false)) . "'"; } diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php index 2661445e7e..8bde05f23b 100644 --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@ -392,7 +392,7 @@ class ParseUrl $siteinfo['text'] = trim($meta_tag['content']); break; case 'dc.creator': - $siteinfo['publisher'] = trim($meta_tag['content']); + $siteinfo['publisher_name'] = trim($meta_tag['content']); break; case 'keywords': $keywords = explode(',', $meta_tag['content']); @@ -441,7 +441,7 @@ class ParseUrl $siteinfo['text'] = trim($meta_tag['content']); break; case 'og:site_name': - $siteinfo['publisher'] = trim($meta_tag['content']); + $siteinfo['publisher_name'] = trim($meta_tag['content']); break; case 'twitter:description': $siteinfo['text'] = trim($meta_tag['content']); @@ -555,10 +555,10 @@ class ParseUrl { $jsonldinfo = []; - if (is_array($jsonld['publisher'])) { + if (!empty($jsonld['publisher']) && is_array($jsonld['publisher'])) { $content = JsonLD::fetchElement($jsonld, 'publisher', 'name', '@type', 'Organization'); if (!empty($content) && is_string($content)) { - $jsonldinfo['publisher'] = trim($content); + $jsonldinfo['publisher_name'] = trim($content); } $content = JsonLD::fetchElement($jsonld, 'publisher', 'url', '@type', 'Organization'); @@ -570,15 +570,15 @@ class ParseUrl if (!empty($brand)) { $content = JsonLD::fetchElement($brand, 'name', '@type', 'brand'); if (!empty($content) && is_string($content)) { - $jsonldinfo['publisher'] = trim($content); + $jsonldinfo['publisher_name'] = trim($content); } } } - if (is_array($jsonld['author'])) { + if (!empty($jsonld['author']) && is_array($jsonld['author'])) { $content = JsonLD::fetchElement($jsonld, 'author', 'name', '@type', 'Organization'); if (!empty($content) && is_string($content)) { - $jsonldinfo['publisher'] = trim($content); + $jsonldinfo['publisher_name'] = trim($content); } $content = JsonLD::fetchElement($jsonld, 'author', 'url', '@type', 'Organization'); @@ -588,7 +588,7 @@ class ParseUrl $content = JsonLD::fetchElement($jsonld, 'author', 'name', '@type', 'Person'); if (!empty($content) && is_string($content)) { - $jsonldinfo['author'] = trim($content); + $jsonldinfo['author_name'] = trim($content); } $content = JsonLD::fetchElement($jsonld, 'author', 'url', '@type', 'Person'); @@ -692,7 +692,7 @@ class ParseUrl $content = JsonLD::fetchElement($jsonld, 'name'); if (!empty($content)) { - $jsonldinfo['publisher'] = trim($content); + $jsonldinfo['publisher_name'] = trim($content); } $content = JsonLD::fetchElement($jsonld, 'description'); @@ -729,7 +729,7 @@ class ParseUrl $content = JsonLD::fetchElement($jsonld, 'name'); if (!empty($content)) { - $jsonldinfo['publisher'] = trim($content); + $jsonldinfo['publisher_name'] = trim($content); } $content = JsonLD::fetchElement($jsonld, 'url'); @@ -739,7 +739,7 @@ class ParseUrl $content = JsonLD::fetchElement($jsonld, 'logo', 'url', '@type', 'ImageObject'); if (!empty($content)) { - $jsonldinfo['publisher_image'] = trim($content); + $jsonldinfo['publisher_img'] = trim($content); } Logger::info('Fetched Organization information', ['url' => $siteinfo['url'], 'fetched' => $jsonldinfo]); @@ -759,7 +759,7 @@ class ParseUrl $content = JsonLD::fetchElement($jsonld, 'name'); if (!empty($content)) { - $jsonldinfo['author'] = trim($content); + $jsonldinfo['author_name'] = trim($content); } $content = JsonLD::fetchElement($jsonld, 'description'); @@ -774,7 +774,7 @@ class ParseUrl $content = JsonLD::fetchElement($jsonld, 'image', 'url', '@type', 'ImageObject'); if (!empty($content)) { - $jsonldinfo['author_image'] = trim($content); + $jsonldinfo['author_img'] = trim($content); } Logger::info('Fetched Person information', ['url' => $siteinfo['url'], 'fetched' => $jsonldinfo]);