* @return string
* @throws HTTPException\InternalServerErrorException
*/
- public static function appendToBody(string $body, bool $searchNakedUrls = false, bool $no_photos = false)
+ public static function searchAndAppendToBody(string $body, bool $searchNakedUrls = false, bool $no_photos = false)
{
Logger::info('add_page_info_to_body: fetch page info for body', ['body' => $body]);
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)
+ {
+ // 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 = PageInfo::getFooterFromData($data, $no_photos);
+ $body = self::stripTrailingUrlFromBody($body, $data['url']);
+ $body .= "\n" . $footer;
+ }
return $body;
}
$text = "[attachment type='" . $data['type'] . "'";
- if (empty($data['text'])) {
- $data['text'] = $data['title'];
- }
-
- if (empty($data['text'])) {
- $data['text'] = $data['url'];
- }
-
if (!empty($data['url'])) {
$text .= " url='" . $data['url'] . "'";
}
$text .= " title='" . $data['title'] . "'";
}
+ if (empty($data['text'])) {
+ $data['text'] = '';
+ }
+
// Only embedd 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));
$text .= " image='" . $preview . "'";
} else {
$text .= " preview='" . $preview . "'";
+
+ if (empty($data['text'])) {
+ $data['text'] = $data['title'];
+ }
+
+ if (empty($data['text'])) {
+ $data['text'] = $data['url'];
+ }
}
}
}
if (!$matches && $searchNakedUrls) {
- preg_match('~(?<=\W|^)(?<![=\]])(https?://.+)$~is', $body, $matches);
+ preg_match(Strings::autoLinkRegEx(), $body, $matches);
if ($matches && !Strings::endsWith($body, $matches[1])) {
unset($matches);
}
$quotedUrl
)$#isx", function ($match) use ($url) {
// Stripping URLs with no label
- if (!isset($match[1])) {
+ if (empty($match[1])) {
return '';
}