diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php
index cf3eb93714..c2b54821aa 100644
--- a/src/Content/Text/BBCode.php
+++ b/src/Content/Text/BBCode.php
@@ -388,21 +388,22 @@ class BBCode extends BaseObject
/**
* Remove [attachment] BBCode and replaces it with a regular [url]
*
- * @param string $body
+ * @param string $body
+ * @param boolean $no_link_desc No link description
*
* @return string with replaced body
*/
- public static function removeAttachment($body)
+ public static function removeAttachment($body, $no_link_desc = false)
{
return preg_replace_callback("/\[attachment (.*)\](.*?)\[\/attachment\]/ism",
- function ($match) {
+ function ($match) use ($no_link_desc) {
$attach_data = self::getAttachmentData($match[0]);
if (empty($attach_data['url'])) {
return $match[0];
- } elseif (empty($attach_data['title'])) {
- return '[url]' . $attach_data['url'] . '[/url]';
+ } elseif (empty($attach_data['title']) || $no_link_desc) {
+ return '[url]' . $attach_data['url'] . "[/url]\n";
} else {
- return '[url=' . $attach_data['url'] . ']' . $attach_data['title'] . '[/url]';
+ return '[url=' . $attach_data['url'] . ']' . $attach_data['title'] . "[/url]\n";
}
}, $body);
}
@@ -622,53 +623,44 @@ class BBCode extends BaseObject
}
$return = '';
- if (in_array($simplehtml, [7, 9])) {
- // Only add the link when it isn't already part of the body
- if (substr_count($text, $data['url']) == 1) {
- $return = self::convertUrlForActivityPub($data['url']);
+ try {
+ if ($tryoembed && OEmbed::isAllowedURL($data['url'])) {
+ $return = OEmbed::getHTML($data['url'], $data['title']);
+ } else {
+ throw new Exception('OEmbed is disabled for this attachment.');
}
- } elseif (($simplehtml != 4) && ($simplehtml != 0)) {
- $return = sprintf('%s
', $data['url'], $data['title']);
- } else {
- try {
- if ($tryoembed && OEmbed::isAllowedURL($data['url'])) {
- $return = OEmbed::getHTML($data['url'], $data['title']);
+ } catch (Exception $e) {
+ $data['title'] = ($data['title'] ?? '') ?: $data['url'];
+
+ if ($simplehtml != 4) {
+ $return = sprintf('
%s', trim(self::convert($bbcode))); - } + if (!empty($data['description']) && $data['description'] != $data['title']) { + // Sanitize the HTML by converting it to BBCode + $bbcode = HTML::toBBCode($data['description']); + $return .= sprintf('
%s', trim(self::convert($bbcode))); + } - if (!empty($data['url'])) { - $return .= sprintf('%s', $data['url'], parse_url($data['url'], PHP_URL_HOST)); - } + if (!empty($data['url'])) { + $return .= sprintf('%s', $data['url'], parse_url($data['url'], PHP_URL_HOST)); + } - if ($simplehtml != 4) { - $return .= '