X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FContent%2FPageInfo.php;h=be8d055c96ef8b25fbed99b13635e295a3a9b932;hb=fbc68329789948d4aff9d97fc89e9118a8d7d99f;hp=642c579387468375e3b09f49cf1a057aa93b0402;hpb=713eda1dfe07dbb60c3f54e08f55e480e85a000f;p=friendica.git diff --git a/src/Content/PageInfo.php b/src/Content/PageInfo.php index 642c579387..be8d055c96 100644 --- a/src/Content/PageInfo.php +++ b/src/Content/PageInfo.php @@ -1,6 +1,6 @@ $body]); + Logger::debug('add_page_info_to_body: fetch page info for body', ['body' => $body]); $url = self::getRelevantUrlFromBody($body, $searchNakedUrls); if (!$url) { return $body; } - $footer = self::getFooterFromUrl($url, $no_photos); - if (!$footer) { + $data = self::queryUrl($url); + if (!$data) { return $body; } - $body = self::stripTrailingUrlFromBody($body, $url); + return self::appendDataToBody($body, $data, $no_photos); + } - $body .= "\n" . $footer; + /** + * @param string $body + * @param array $data + * @param bool $no_photos + * @return string + * @throws HTTPException\InternalServerErrorException + */ + public static function appendDataToBody(string $body, array $data, bool $no_photos = false): string + { + // Only one [attachment] tag per body is allowed + $existingAttachmentPos = strpos($body, '[attachment'); + if ($existingAttachmentPos !== false) { + $linkTitle = $data['title'] ?: $data['url']; + // Additional link attachments are prepended before the existing [attachment] tag + $body = substr_replace($body, "\n[bookmark=" . $data['url'] . ']' . $linkTitle . "[/bookmark]\n", $existingAttachmentPos, 0); + } else { + $footer = self::getFooterFromData($data, $no_photos); + $body = self::stripTrailingUrlFromBody($body, $data['url']); + $body .= "\n" . $footer; + } return $body; } @@ -70,7 +90,7 @@ class PageInfo * @return string * @throws HTTPException\InternalServerErrorException */ - public static function getFooterFromUrl(string $url, bool $no_photos = false, string $photo = '', bool $keywords = false, string $keyword_denylist = '') + public static function getFooterFromUrl(string $url, bool $no_photos = false, string $photo = '', bool $keywords = false, string $keyword_denylist = ''): string { $data = self::queryUrl($url, $photo, $keywords, $keyword_denylist); @@ -83,7 +103,7 @@ class PageInfo * @return string * @throws HTTPException\InternalServerErrorException */ - public static function getFooterFromData(array $data, bool $no_photos = false) + public static function getFooterFromData(array $data, bool $no_photos = false): string { Hook::callAll('page_info_data', $data); @@ -109,28 +129,19 @@ class PageInfo } // Escape some bad characters - $data['url'] = str_replace(['[', ']'], ['[', ']'], htmlentities($data['url'], ENT_QUOTES, 'UTF-8', false)); - $data['title'] = str_replace(['[', ']'], ['[', ']'], htmlentities($data['title'], ENT_QUOTES, 'UTF-8', false)); + $text = "[attachment"; - $text = "[attachment type='" . $data['type'] . "'"; - - if (empty($data['text'])) { - $data['text'] = $data['title']; + 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)) . "'"; + } } if (empty($data['text'])) { - $data['text'] = $data['url']; - } - - if (!empty($data['url'])) { - $text .= " url='" . $data['url'] . "'"; + $data['text'] = ''; } - if (!empty($data['title'])) { - $text .= " title='" . $data['title'] . "'"; - } - - // Only embedd a picture link when it seems to be a valid picture ("width" is set) + // Only embed a picture link when it seems to be a valid picture ("width" is set) if (!empty($data['images']) && !empty($data['images'][0]['width'])) { $preview = str_replace(['[', ']'], ['[', ']'], htmlentities($data['images'][0]['src'], ENT_QUOTES, 'UTF-8', false)); // if the preview picture is larger than 500 pixels then show it in a larger mode @@ -140,10 +151,18 @@ class PageInfo $text .= " image='" . $preview . "'"; } else { $text .= " preview='" . $preview . "'"; + + if (empty($data['text'])) { + $data['text'] = $data['title']; + } + + if (empty($data['text'])) { + $data['text'] = $data['url']; + } } } - $text .= ']' . $data['text'] . '[/attachment]'; + $text .= ']' . str_replace(['[', ']'], ['[', ']'], $data['text']) . '[/attachment]'; $hashtags = ''; if (!empty($data['keywords'])) { @@ -168,7 +187,7 @@ class PageInfo */ public static function queryUrl(string $url, string $photo = '', bool $keywords = false, string $keyword_denylist = '') { - $data = ParseUrl::getSiteinfoCached($url, true); + $data = ParseUrl::getSiteinfoCached($url); if ($photo != '') { $data['images'][0]['src'] = $photo; @@ -189,7 +208,7 @@ class PageInfo } } - Logger::info('fetch page info for URL', ['url' => $url, 'data' => $data]); + Logger::debug('fetch page info for URL', ['url' => $url, 'data' => $data]); return $data; } @@ -201,7 +220,7 @@ class PageInfo * @return array * @throws HTTPException\InternalServerErrorException */ - public static function getTagsFromUrl(string $url, string $photo = '', string $keyword_denylist = '') + public static function getTagsFromUrl(string $url, string $photo = '', string $keyword_denylist = ''): array { $data = self::queryUrl($url, $photo, true, $keyword_denylist); @@ -227,21 +246,26 @@ class PageInfo * @param bool $searchNakedUrls Whether we should pick a naked URL (outside of BBCode tags) as a last resort * @return string|null */ - protected static function getRelevantUrlFromBody(string $body, bool $searchNakedUrls = false) + public static function getRelevantUrlFromBody(string $body, bool $searchNakedUrls = false) { $URLSearchString = 'https?://[^\[\]]*'; // Fix for Mastodon where the mentions are in a different format $body = preg_replace("~\[url=($URLSearchString)]([#!@])(.*?)\[/url]~is", '$2[url=$1]$3[/url]', $body); - preg_match("~(?