X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=src%2FContent%2FText%2FBBCode.php;h=3bb9fda125b1e22b62ba0c75f3285c0ca37e8d86;hb=f23ecaff6af1982112469f90d6bcdf0408b0f22e;hp=949f9d9cf085919b0e14f007e1244ea5a0d76dae;hpb=1d6f5c33a1a7c538d4e529d22701fd735855da15;p=friendica.git
diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php
index 949f9d9cf0..3bb9fda125 100644
--- a/src/Content/Text/BBCode.php
+++ b/src/Content/Text/BBCode.php
@@ -36,14 +36,10 @@ use Friendica\Core\Renderer;
use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Model\Event;
-use Friendica\Model\Photo;
use Friendica\Model\Post;
use Friendica\Model\Tag;
use Friendica\Network\HTTPClient\Client\HttpClientAccept;
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
-use Friendica\Object\Image;
-use Friendica\Protocol\Activity;
-use Friendica\Util\Images;
use Friendica\Util\Map;
use Friendica\Util\ParseUrl;
use Friendica\Util\Proxy;
@@ -61,12 +57,15 @@ class BBCode
const DIASPORA = 3;
const CONNECTORS = 4;
const TWITTER_API = 5;
+ const NPF = 6;
const OSTATUS = 7;
const TWITTER = 8;
const BACKLINK = 8;
const ACTIVITYPUB = 9;
+ const BLUESKY = 10;
- const TOP_ANCHOR = ' ';
+ const SHARED_ANCHOR = '
';
+ const TOP_ANCHOR = ' ';
const BOTTOM_ANCHOR = ' ';
const PREVIEW_NONE = 0;
@@ -74,77 +73,6 @@ class BBCode
const PREVIEW_LARGE = 2;
const PREVIEW_SMALL = 3;
- /**
- * Fetches attachment data that were generated the old way
- *
- * @param string $body Message body
- * @return array
- * 'type' -> Message type ('link', 'video', 'photo')
- * 'text' -> Text before the shared message
- * 'after' -> Text after the shared message
- * 'image' -> Preview image of the message
- * 'url' -> Url to the attached message
- * 'title' -> Title of the attachment
- * 'description' -> Description of the attachment
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
- */
- private static function getOldAttachmentData(string $body): array
- {
- $post = [];
-
- // Simplify image codes
- $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
-
- if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism", $body, $attached, PREG_SET_ORDER)) {
- foreach ($attached as $data) {
- if (!in_array($data[1], ['type-link', 'type-video', 'type-photo'])) {
- continue;
- }
-
- $post['type'] = substr($data[1], 5);
-
- $pos = strpos($body, $data[0]);
- if ($pos > 0) {
- $post['text'] = trim(substr($body, 0, $pos));
- $post['after'] = trim(substr($body, $pos + strlen($data[0])));
- } else {
- $post['text'] = trim(str_replace($data[0], '', $body));
- $post['after'] = '';
- }
-
- $attacheddata = $data[2];
-
- if (preg_match("/\[img\](.*?)\[\/img\]/ism", $attacheddata, $matches)) {
-
- $picturedata = Images::getInfoFromURLCached($matches[1]);
-
- if ($picturedata) {
- if (($picturedata[0] >= 500) && ($picturedata[0] >= $picturedata[1])) {
- $post['image'] = $matches[1];
- } else {
- $post['preview'] = $matches[1];
- }
- }
- }
-
- if (preg_match("/\[bookmark\=(.*?)\](.*?)\[\/bookmark\]/ism", $attacheddata, $matches)) {
- $post['url'] = $matches[1];
- $post['title'] = $matches[2];
- }
- if (!empty($post['url']) && (in_array($post['type'], ['link', 'video']))
- && preg_match("/\[url\=(.*?)\](.*?)\[\/url\]/ism", $attacheddata, $matches)) {
- $post['url'] = $matches[1];
- }
-
- // Search for description
- if (preg_match("/\[quote\](.*?)\[\/quote\]/ism", $attacheddata, $matches)) {
- $post['description'] = $matches[1];
- }
- }
- }
- return $post;
- }
-
/**
* Fetches attachment data that were generated with the "attachment" element
*
@@ -178,7 +106,7 @@ class BBCode
if (!preg_match("/(.*)\[attachment(.*?)\](.*?)\[\/attachment\](.*)/ism", $body, $match)) {
DI::profiler()->stopRecording();
- return self::getOldAttachmentData($body);
+ return [];
}
$attributes = $match[2];
@@ -214,7 +142,7 @@ class BBCode
break;
case 'title':
- $value = self::convert(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), false, true);
+ $value = self::toPlaintext(html_entity_decode($value, ENT_QUOTES, 'UTF-8'));
$value = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
$value = str_replace(['[', ']'], ['[', ']'], $value);
$data['title'] = $value;
@@ -253,183 +181,6 @@ class BBCode
return $data;
}
- public static function getAttachedData(string $body, array $item = []): array
- {
- /*
- - text:
- - type: link, video, photo
- - title:
- - url:
- - image:
- - description:
- - (thumbnail)
- */
-
- DI::profiler()->startRecording('rendering');
- $has_title = !empty($item['title']);
- $plink = $item['plink'] ?? '';
- $post = self::getAttachmentData($body);
-
- // Get all linked images with alternative image description
- if (preg_match_all("/\[img=(http[^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
- foreach ($pictures as $picture) {
- if ($id = Photo::getIdForName($picture[1])) {
- $post['images'][] = ['url' => str_replace('-1.', '-0.', $picture[1]), 'description' => $picture[2], 'id' => $id];
- } else {
- $post['remote_images'][] = ['url' => $picture[1], 'description' => $picture[2]];
- }
- }
- if (!empty($post['images']) && !empty($post['images'][0]['description'])) {
- $post['image_description'] = $post['images'][0]['description'];
- }
- }
-
- if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
- foreach ($pictures as $picture) {
- if ($id = Photo::getIdForName($picture[1])) {
- $post['images'][] = ['url' => str_replace('-1.', '-0.', $picture[1]), 'description' => '', 'id' => $id];
- } else {
- $post['remote_images'][] = ['url' => $picture[1], 'description' => ''];
- }
- }
- }
-
- if (!isset($post['type'])) {
- $post['text'] = $body;
- }
-
- // Simplify image codes
- $post['text'] = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $post['text']);
- $post['text'] = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $post['text']);
-
- // if nothing is found, it maybe having an image.
- if (!isset($post['type'])) {
- if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $post['text'], $pictures, PREG_SET_ORDER)) {
- if ((count($pictures) == 1) && !$has_title && !Photo::isLocal($pictures[0][2])) {
- if (!empty($item['object-type']) && ($item['object-type'] == Activity\ObjectType::IMAGE)) {
- // Replace the preview picture with the real picture
- $url = str_replace('-1.', '-0.', $pictures[0][2]);
- $data = ['url' => $url, 'type' => 'photo'];
- } else {
- // Checking, if the link goes to a picture
- $data = ParseUrl::getSiteinfoCached($pictures[0][1]);
- }
-
- // Workaround:
- // Sometimes photo posts to the own album are not detected at the start.
- // So we seem to cannot use the cache for these cases. That's strange.
- if (($data['type'] != 'photo') && strstr($pictures[0][1], '/photos/')) {
- $data = ParseUrl::getSiteinfo($pictures[0][1]);
- }
-
- if ($data['type'] == 'photo') {
- $post['type'] = 'photo';
- if (isset($data['images'][0])) {
- $post['image'] = $data['images'][0]['src'];
- $post['url'] = $data['url'];
- } else {
- $post['image'] = $data['url'];
- }
-
- $post['preview'] = $pictures[0][2];
- $post['text'] = trim(str_replace($pictures[0][0], '', $post['text']));
- } else {
- $imgdata = Images::getInfoFromURLCached($pictures[0][1]);
- if (($imgdata) && substr($imgdata['mime'], 0, 6) == 'image/') {
- $post['type'] = 'photo';
- $post['image'] = $pictures[0][1];
- $post['preview'] = $pictures[0][2];
- $post['text'] = trim(str_replace($pictures[0][0], '', $post['text']));
- }
- }
- } elseif (count($pictures) > 0) {
- if (count($pictures) > 4) {
- $post['type'] = 'link';
- $post['url'] = $plink;
- } else {
- $post['type'] = 'photo';
- }
-
- $post['image'] = $pictures[0][2];
-
- foreach ($pictures as $picture) {
- $post['text'] = trim(str_replace($picture[0], '', $post['text']));
- }
- }
- } elseif (preg_match_all("(\[img\](.*?)\[\/img\])ism", $post['text'], $pictures, PREG_SET_ORDER)) {
- if ($has_title) {
- $post['type'] = 'link';
- $post['url'] = $plink;
- } else {
- $post['type'] = 'photo';
- }
-
- $post['image'] = $pictures[0][1];
- foreach ($pictures as $picture) {
- $post['text'] = trim(str_replace($picture[0], '', $post['text']));
- }
- }
-
- // Test for the external links
- preg_match_all("(\[url\](.*?)\[\/url\])ism", $post['text'], $links1, PREG_SET_ORDER);
- preg_match_all("(\[url\=(.*?)\].*?\[\/url\])ism", $post['text'], $links2, PREG_SET_ORDER);
-
- $links = array_merge($links1, $links2);
-
- // If there is only a single one, then use it.
- // This should cover link posts via API.
- if ((count($links) == 1) && !isset($post['preview']) && !$has_title) {
- $post['type'] = 'link';
- $post['url'] = $links[0][1];
- }
-
- // Simplify "video" element
- $post['text'] = preg_replace('(\[video.*?\ssrc\s?=\s?([^\s\]]+).*?\].*?\[/video\])ism', '[video]$1[/video]', $post['text']);
-
- // Now count the number of external media links
- preg_match_all("(\[vimeo\](.*?)\[\/vimeo\])ism", $post['text'], $links1, PREG_SET_ORDER);
- preg_match_all("(\[youtube\\](.*?)\[\/youtube\\])ism", $post['text'], $links2, PREG_SET_ORDER);
- preg_match_all("(\[video\\](.*?)\[\/video\\])ism", $post['text'], $links3, PREG_SET_ORDER);
- preg_match_all("(\[audio\\](.*?)\[\/audio\\])ism", $post['text'], $links4, PREG_SET_ORDER);
-
- // Add them to the other external links
- $links = array_merge($links, $links1, $links2, $links3, $links4);
-
- // Are there more than one?
- if (count($links) > 1) {
- // The post will be the type "text", which means a blog post
- unset($post['type']);
- $post['url'] = $plink;
- }
-
- if (!isset($post['type'])) {
- $post['type'] = 'text';
- }
-
- if (($post['type'] == 'photo') && empty($post['images']) && !empty($post['remote_images'])) {
- $post['images'] = $post['remote_images'];
- $post['image'] = $post['images'][0]['url'];
- if (!empty($post['images']) && !empty($post['images'][0]['description'])) {
- $post['image_description'] = $post['images'][0]['description'];
- }
- }
- unset($post['remote_images']);
- } elseif (isset($post['url']) && ($post['type'] == 'video')) {
- $data = ParseUrl::getSiteinfoCached($post['url']);
-
- if (isset($data['images'][0])) {
- $post['image'] = $data['images'][0]['src'];
- }
- } elseif (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $post['text'], $pictures, PREG_SET_ORDER)) {
- foreach ($pictures as $picture) {
- $post['text'] = trim(str_replace($picture[0], '', $post['text']));
- }
- }
-
- DI::profiler()->stopRecording();
- return $post;
- }
-
/**
* Remove [attachment] BBCode and replaces it with a regular [url]
*
@@ -439,7 +190,8 @@ class BBCode
*/
public static function replaceAttachment(string $body, bool $no_link_desc = false): string
{
- return preg_replace_callback("/\s*\[attachment (.*?)\](.*?)\[\/attachment\]\s*/ism",
+ return preg_replace_callback(
+ "/\s*\[attachment (.*?)\](.*?)\[\/attachment\]\s*/ism",
function ($match) use ($body, $no_link_desc) {
$attach_data = self::getAttachmentData($match[0]);
if (empty($attach_data['url'])) {
@@ -451,7 +203,9 @@ class BBCode
} else {
return " \n[url=" . $attach_data['url'] . ']' . $attach_data['title'] . "[/url]\n";
}
- }, $body);
+ },
+ $body
+ );
}
/**
@@ -476,18 +230,73 @@ class BBCode
{
DI::profiler()->startRecording('rendering');
// Remove pictures in advance to avoid unneeded proxy calls
+ $text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", ' ', $text);
$text = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", ' $2 ', $text);
$text = preg_replace("/\[img.*?\[\/img\]/ism", ' ', $text);
// Remove attachment
$text = self::replaceAttachment($text);
- $naked_text = HTML::toPlaintext(self::convert($text, false, 0, true), 0, !$keep_urls);
+ $naked_text = HTML::toPlaintext(self::convert($text, false, self::EXTERNAL, true), 0, !$keep_urls);
DI::profiler()->stopRecording();
return $naked_text;
}
+ /**
+ * Converts text into a format that can be used for the channel search and the language detection.
+ *
+ * @param string $text
+ * @param integer $uri_id
+ * @return string
+ */
+ public static function toSearchText(string $text, int $uri_id): string
+ {
+ // Removes attachments
+ $text = self::removeAttachment($text);
+
+ // Add images because of possible alt texts
+ if (!empty($uri_id)) {
+ $text = Post\Media::addAttachmentsToBody($uri_id, $text, [Post\Media::IMAGE]);
+ }
+
+ if (empty($text)) {
+ return '';
+ }
+
+ // Remove links without a link description
+ $text = preg_replace("~\[url\=.*\]https?:.*\[\/url\]~", ' ', $text);
+
+ // Remove pictures
+ $text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", ' ', $text);
+
+ // Replace picture with the alt description
+ $text = preg_replace("/\[img\=.*?\](.*?)\[\/img\]/ism", ' $1 ', $text);
+
+ // Remove the other pictures
+ $text = preg_replace("/\[img.*?\[\/img\]/ism", ' ', $text);
+
+ // Removes mentions, remove links from hashtags
+ $text = preg_replace('/[@!]\[url\=.*?\].*?\[\/url\]/ism', ' ', $text);
+ $text = preg_replace('/[#]\[url\=.*?\](.*?)\[\/url\]/ism', ' #$1 ', $text);
+ $text = preg_replace('/[@!#]?\[url.*?\[\/url\]/ism', ' ', $text);
+ $text = preg_replace("/\[url=[^\[\]]*\](.*)\[\/url\]/Usi", ' $1 ', $text);
+
+ // Convert it to plain text
+ $text = self::toPlaintext($text, false);
+
+ // Remove possibly remaining links
+ $text = preg_replace(Strings::autoLinkRegEx(), '', $text);
+
+ // Remove all unneeded white space
+ do {
+ $oldtext = $text;
+ $text = str_replace([' ', "\n", "\r", '"'], ' ', $text);
+ } while ($oldtext != $text);
+
+ return trim($text);
+ }
+
private static function proxyUrl(string $image, int $simplehtml = self::INTERNAL, int $uriid = 0, string $size = ''): string
{
// Only send proxied pictures to API and for internal display
@@ -756,26 +565,7 @@ class BBCode
*/
private static function convertUrlForActivityPub(string $url): string
{
- return sprintf('%s ', $url, self::getStyledURL($url));
- }
-
- /**
- * Converts an URL in a nicer format (without the scheme and possibly shortened)
- *
- * @param string $url URL that is about to be reformatted
- * @return string reformatted link
- */
- private static function getStyledURL(string $url): string
- {
- $parts = parse_url($url);
- $scheme = $parts['scheme'] . '://';
- $styled_url = str_replace($scheme, '', $url);
-
- if (strlen($styled_url) > 30) {
- $styled_url = substr($styled_url, 0, 30) . "â¦";
- }
-
- return $styled_url;
+ return sprintf('%s ', $url, Strings::getStyledURL($url));
}
/*
@@ -797,7 +587,7 @@ class BBCode
/*
* The previously spacefied [noparse][ i ]italic[ /i ][/noparse],
- * now turns back and the [noparse] tags are trimed
+ * now turns back and the [noparse] tags are trimmed
* returning [i]italic[/i]
*
* @param array $match
@@ -956,8 +746,11 @@ class BBCode
// We're depending on the property of 'foreach' (specified on the PHP website) that
// it loops over the array starting from the first element and going sequentially
// to the last element
- $newbody = str_replace('[$#saved_image' . $cnt . '#$]',
- ' ', $newbody);
+ $newbody = str_replace(
+ '[$#saved_image' . $cnt . '#$]',
+ ' ',
+ $newbody
+ );
$cnt++;
}
@@ -1112,8 +905,7 @@ class BBCode
$attributes[$field] = html_entity_decode($matches[2] ?? '', ENT_QUOTES, 'UTF-8');
}
- $img_str = ' $value) {
if (!empty($value)) {
$img_str .= ' ' . $key . '="' . htmlspecialchars($value, ENT_COMPAT) . '"';
@@ -1150,13 +942,13 @@ class BBCode
switch ($simplehtml) {
case self::MASTODON_API:
case self::TWITTER_API:
- $text = ($is_quote_share? ' ' : '') .
- '' . html_entity_decode('♲', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . " : \n" .
- '' . $content . ' ';
+ $text = ($is_quote_share ? ' ' : '') .
+ '' . html_entity_decode('♲', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . " : \n" .
+ '' . $content . ' ';
break;
case self::DIASPORA:
if (stripos(Strings::normaliseLink($attributes['link']), 'http://twitter.com/') === 0) {
- $text = ($is_quote_share? ' ' : '') . '' . $attributes['link'] . '
' . "\n";
+ $text = ($is_quote_share ? ' ' : '') . '' . $attributes['link'] . '
' . "\n";
} else {
$headline = 'â² ' . $attributes['author'] . ' :
' . "\n";
@@ -1164,7 +956,7 @@ class BBCode
$headline = 'â² ' . $attributes['author'] . ' - ' . $attributes['posted'] . ' GMT
' . "\n";
}
- $text = ($is_quote_share? ' ' : '') . $headline . '' . trim($content) . ' ' . "\n";
+ $text = ($is_quote_share ? ' ' : '') . $headline . '' . trim($content) . ' ' . "\n";
if (empty($attributes['posted']) && !empty($attributes['link'])) {
$text .= '[Source]
' . "\n";
@@ -1177,24 +969,24 @@ class BBCode
$headline .= DI::l10n()->t('%2$s %3$s', $attributes['link'], $mention, $attributes['posted']);
$headline .= ':' . "\n";
- $text = ($is_quote_share? ' ' : '') . $headline . '' . trim($content) . ' ' . "\n";
+ $text = ($is_quote_share ? ' ' : '') . $headline . '' . trim($content) . ' ' . "\n";
break;
case self::OSTATUS:
- $text = ($is_quote_share? ' ' : '') . '' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' @' . $author_contact['addr'] . ': ' . $content . '
' . "\n";
+ $text = ($is_quote_share ? ' ' : '') . '' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' @' . $author_contact['addr'] . ': ' . $content . '
' . "\n";
break;
case self::ACTIVITYPUB:
$author = '@' . $author_contact['addr'] . ' : ';
$text = '' . "\n";
break;
default:
- $text = ($is_quote_share? "\n" : '');
+ $text = ($is_quote_share ? "\n" : '');
$contact = Contact::getByURL($attributes['profile'], false, ['network']);
$network = $contact['network'] ?? Protocol::PHANTOM;
$tpl = Renderer::getMarkupTemplate('shared_content.tpl');
- $text .= Renderer::replaceMacros($tpl, [
+ $text .= self::SHARED_ANCHOR . Renderer::replaceMacros($tpl, [
'$profile' => $attributes['profile'],
'$avatar' => $attributes['avatar'],
'$author' => $attributes['author'],
@@ -1375,6 +1167,7 @@ class BBCode
public static function removeLinks(string $bbcode): string
{
DI::profiler()->startRecording('rendering');
+ $bbcode = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", ' ', $bbcode);
$bbcode = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", ' $1 ', $bbcode);
$bbcode = preg_replace("/\[img.*?\[\/img\]/ism", ' ', $bbcode);
@@ -1421,6 +1214,40 @@ class BBCode
return $match[1] . '[url=' . $data['url'] . ']' . $data['nick'] . '[/url]';
}
+ /**
+ * Normalize links to Youtube and Vimeo to a unified format.
+ *
+ * @param string $text
+ * @return string
+ */
+ private static function normalizeVideoLinks(string $text): string
+ {
+ $text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
+ $text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
+ $text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/shorts\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
+ $text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
+
+ $text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism", '[vimeo]$1[/vimeo]', $text);
+ $text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism", '[vimeo]$1[/vimeo]', $text);
+
+ return $text;
+ }
+
+ /**
+ * Expand Youtube and Vimeo links to
+ *
+ * @param string $text
+ * @return string
+ */
+ public static function expandVideoLinks(string $text): string
+ {
+ $text = self::normalizeVideoLinks($text);
+ $text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", '[url=https://www.youtube.com/watch?v=$1]https://www.youtube.com/watch?v=$1[/url]', $text);
+ $text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", '[url=https://vimeo.com/$1]https://vimeo.com/$1[/url]', $text);
+
+ return $text;
+ }
+
/**
* Converts a BBCode message for a given URI-ID to a HTML message
*
@@ -1500,8 +1327,7 @@ class BBCode
* $match[1] = $url
* $match[2] = $title or absent
*/
- $try_oembed_callback = function (array $match)
- {
+ $try_oembed_callback = function (array $match) {
$url = $match[1];
$title = $match[2] ?? '';
@@ -1554,18 +1380,24 @@ class BBCode
}
// Remove linefeeds inside of the table elements. See issue #6799
- $search = ["\n[th]", "[th]\n", " [th]", "\n[/th]", "[/th]\n", "[/th] ",
+ $search = [
+ "\n[th]", "[th]\n", " [th]", "\n[/th]", "[/th]\n", "[/th] ",
"\n[td]", "[td]\n", " [td]", "\n[/td]", "[/td]\n", "[/td] ",
"\n[tr]", "[tr]\n", " [tr]", "[tr] ", "\n[/tr]", "[/tr]\n", " [/tr]", "[/tr] ",
"\n[hr]", "[hr]\n", " [hr]", "[hr] ",
"\n[attachment ", " [attachment ", "\n[/attachment]", "[/attachment]\n", " [/attachment]", "[/attachment] ",
- "[table]\n", "[table] ", " [table]", "\n[/table]", " [/table]", "[/table] "];
- $replace = ["[th]", "[th]", "[th]", "[/th]", "[/th]", "[/th]",
+ "[table]\n", "[table] ", " [table]", "\n[/table]", " [/table]", "[/table] ",
+ " \n", "\t\n", "[/li]\n", "\n[li]", "\n[*]",
+ ];
+ $replace = [
+ "[th]", "[th]", "[th]", "[/th]", "[/th]", "[/th]",
"[td]", "[td]", "[td]", "[/td]", "[/td]", "[/td]",
"[tr]", "[tr]", "[tr]", "[tr]", "[/tr]", "[/tr]", "[/tr]", "[/tr]",
"[hr]", "[hr]", "[hr]", "[hr]",
"[attachment ", "[attachment ", "[/attachment]", "[/attachment]", "[/attachment]", "[/attachment]",
- "[table]", "[table]", "[table]", "[/table]", "[/table]", "[/table]"];
+ "[table]", "[table]", "[table]", "[/table]", "[/table]", "[/table]",
+ "\n", "\n", "[/li]", "[li]", "[*]",
+ ];
do {
$oldtext = $text;
$text = str_replace($search, $replace, $text);
@@ -1581,10 +1413,14 @@ class BBCode
// removing multiplicated newlines
if (DI::config()->get('system', 'remove_multiplicated_lines')) {
- $search = ["\n\n\n", "\n ", " \n", "[/quote]\n\n", "\n[/quote]", "[/li]\n", "\n[li]", "\n[*]", "\n[ul]", "[/ul]\n", "\n\n[share ", "[/attachment]\n",
- "\n[h1]", "[/h1]\n", "\n[h2]", "[/h2]\n", "\n[h3]", "[/h3]\n", "\n[h4]", "[/h4]\n", "\n[h5]", "[/h5]\n", "\n[h6]", "[/h6]\n"];
- $replace = ["\n\n", "\n", "\n", "[/quote]\n", "[/quote]", "[/li]", "[li]", "[*]", "[ul]", "[/ul]", "\n[share ", "[/attachment]",
- "[h1]", "[/h1]", "[h2]", "[/h2]", "[h3]", "[/h3]", "[h4]", "[/h4]", "[h5]", "[/h5]", "[h6]", "[/h6]"];
+ $search = [
+ "\n\n\n", "[/quote]\n\n", "\n[/quote]", "\n[ul]", "[/ul]\n", "\n[ol]", "[/ol]\n", "\n\n[share ", "[/attachment]\n",
+ "\n[h1]", "[/h1]\n", "\n[h2]", "[/h2]\n", "\n[h3]", "[/h3]\n", "\n[h4]", "[/h4]\n", "\n[h5]", "[/h5]\n", "\n[h6]", "[/h6]\n"
+ ];
+ $replace = [
+ "\n\n", "[/quote]\n", "[/quote]", "[ul]", "[/ul]", "[ol]", "[/ol]", "\n[share ", "[/attachment]",
+ "[h1]", "[/h1]", "[h2]", "[/h2]", "[h3]", "[/h3]", "[h4]", "[/h4]", "[h5]", "[/h5]", "[h6]", "[/h6]"
+ ];
do {
$oldtext = $text;
$text = str_replace($search, $replace, $text);
@@ -1593,7 +1429,9 @@ class BBCode
/// @todo Have a closer look at the different html modes
// Handle attached links or videos
- if (in_array($simple_html, [self::MASTODON_API, self::TWITTER_API, self::ACTIVITYPUB])) {
+ if ($simple_html == self::NPF) {
+ $text = self::removeAttachment($text);
+ } elseif (in_array($simple_html, [self::MASTODON_API, self::TWITTER_API, self::ACTIVITYPUB])) {
$text = self::replaceAttachment($text);
} elseif (!in_array($simple_html, [self::INTERNAL, self::EXTERNAL, self::CONNECTORS])) {
$text = self::replaceAttachment($text, true);
@@ -1611,7 +1449,7 @@ class BBCode
});
}
- // leave open the posibility of [map=something]
+ // leave open the possibility of [map=something]
// this is replaced in Item::prepareBody() which has knowledge of the item location
if (strpos($text, '[/map]') !== false) {
$text = preg_replace_callback(
@@ -1638,12 +1476,32 @@ class BBCode
}
// Check for headers
- $text = preg_replace("(\[h1\](.*?)\[\/h1\])ism", '$1 ', $text);
- $text = preg_replace("(\[h2\](.*?)\[\/h2\])ism", '$1 ', $text);
- $text = preg_replace("(\[h3\](.*?)\[\/h3\])ism", '$1 ', $text);
- $text = preg_replace("(\[h4\](.*?)\[\/h4\])ism", '$1 ', $text);
- $text = preg_replace("(\[h5\](.*?)\[\/h5\])ism", '$1 ', $text);
- $text = preg_replace("(\[h6\](.*?)\[\/h6\])ism", '$1 ', $text);
+
+ if ($simple_html == self::INTERNAL) {
+ //Ensure to always start with if possible
+ $heading_count = 0;
+ for ($level = 6; $level > 0; $level--) {
+ if (preg_match("(\[h$level\].*?\[\/h$level\])ism", $text)) {
+ $heading_count++;
+ }
+ }
+ if ($heading_count > 0) {
+ $heading = min($heading_count + 3, 6);
+ for ($level = 6; $level > 0; $level--) {
+ if (preg_match("(\[h$level\].*?\[\/h$level\])ism", $text)) {
+ $text = preg_replace("(\[h$level\](.*?)\[\/h$level\])ism", "$1 ", $text);
+ $heading--;
+ }
+ }
+ }
+ } else {
+ $text = preg_replace("(\[h1\](.*?)\[\/h1\])ism", '
$1 ', $text);
+ $text = preg_replace("(\[h2\](.*?)\[\/h2\])ism", '
$1 ', $text);
+ $text = preg_replace("(\[h3\](.*?)\[\/h3\])ism", '
$1 ', $text);
+ $text = preg_replace("(\[h4\](.*?)\[\/h4\])ism", '
$1 ', $text);
+ $text = preg_replace("(\[h5\](.*?)\[\/h5\])ism", '
$1 ', $text);
+ $text = preg_replace("(\[h6\](.*?)\[\/h6\])ism", '
$1 ', $text);
+ }
// Check for paragraph
$text = preg_replace("(\[p\](.*?)\[\/p\])ism", '
$1
', $text);
@@ -1690,42 +1548,47 @@ class BBCode
$text = preg_replace("(\[style=(.*?)\](.*?)\[\/style\])ism", '$2 ', $text);
// Mastodon Emoji (internal tag, do not document for users)
- $text = preg_replace("(\[emoji=(.*?)](.*?)\[/emoji])ism", ' ', $text);
+ if ($simple_html == self::MASTODON_API) {
+ $text = preg_replace("(\[emoji=(.*?)](.*?)\[/emoji])ism", '$2', $text);
+ } else {
+ $text = preg_replace("(\[emoji=(.*?)](.*?)\[/emoji])ism", ' ', $text);
+ }
// Check for CSS classes
// @deprecated since 2021.12, left for backward-compatibility reasons
$text = preg_replace("(\[class=(.*?)\](.*?)\[\/class\])ism", '$2 ', $text);
// Add HTML new lines
+ $text = str_replace("\n\n", '', $text);
$text = str_replace("\n", ' ', $text);
// handle nested lists
$endlessloop = 0;
while ((((strpos($text, "[/list]") !== false) && (strpos($text, "[list") !== false)) ||
- ((strpos($text, "[/ol]") !== false) && (strpos($text, "[ol]") !== false)) ||
- ((strpos($text, "[/ul]") !== false) && (strpos($text, "[ul]") !== false)) ||
- ((strpos($text, "[/li]") !== false) && (strpos($text, "[li]") !== false))) && (++$endlessloop < 20)) {
- $text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '
', $text);
- $text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '', $text);
- $text = preg_replace("/\[list=1\](.*?)\[\/list\]/ism", '', $text);
- $text = preg_replace("/\[list=((?-i)i)\](.*?)\[\/list\]/ism", '', $text);
- $text = preg_replace("/\[list=((?-i)I)\](.*?)\[\/list\]/ism", '', $text);
- $text = preg_replace("/\[list=((?-i)a)\](.*?)\[\/list\]/ism", '', $text);
- $text = preg_replace("/\[list=((?-i)A)\](.*?)\[\/list\]/ism", '', $text);
- $text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '', $text);
- $text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '', $text);
+ ((strpos($text, "[/ol]") !== false) && (strpos($text, "[ol]") !== false)) ||
+ ((strpos($text, "[/ul]") !== false) && (strpos($text, "[ul]") !== false)) ||
+ ((strpos($text, "[/li]") !== false) && (strpos($text, "[li]") !== false))) && (++$endlessloop < 20)) {
+ $text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '', $text);
+ $text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '
', $text);
+ $text = preg_replace("/\[list=1\](.*?)\[\/list\]/ism", '
', $text);
+ $text = preg_replace("/\[list=((?-i)i)\](.*?)\[\/list\]/ism", '
', $text);
+ $text = preg_replace("/\[list=((?-i)I)\](.*?)\[\/list\]/ism", '
', $text);
+ $text = preg_replace("/\[list=((?-i)a)\](.*?)\[\/list\]/ism", '
', $text);
+ $text = preg_replace("/\[list=((?-i)A)\](.*?)\[\/list\]/ism", '
', $text);
+ $text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '
', $text);
+ $text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '
$1 ', $text);
$text = preg_replace("/\[li\](.*?)\[\/li\]/ism", '
$1 ', $text);
}
$text = preg_replace("/\[th\](.*?)\[\/th\]/sm", '$1 ', $text);
$text = preg_replace("/\[td\](.*?)\[\/td\]/sm", '$1 ', $text);
$text = preg_replace("/\[tr\](.*?)\[\/tr\]/sm", '$1 ', $text);
- $text = preg_replace("/\[table\](.*?)\[\/table\]/sm", '', $text);
+ $text = preg_replace("/\[table\](.*?)\[\/table\]/sm", '', $text);
- $text = preg_replace("/\[table border=1\](.*?)\[\/table\]/sm", '
', $text);
- $text = preg_replace("/\[table border=0\](.*?)\[\/table\]/sm", '', $text);
+ $text = preg_replace("/\[table border=1\](.*?)\[\/table\]/sm", '', $text);
+ $text = preg_replace("/\[table border=0\](.*?)\[\/table\]/sm", '
', $text);
- $text = str_replace('[hr]', '
', $text);
+ $text = str_replace('[hr]', '', $text);
if (!$for_plaintext) {
$text = self::performWithEscapedTags($text, ['url', 'img', 'audio', 'video', 'youtube', 'vimeo', 'share', 'attachment', 'iframe', 'bookmark'], function ($text) {
@@ -1750,14 +1613,16 @@ class BBCode
// handle nested quotes
$endlessloop = 0;
- while ((strpos($text, "[/spoiler]")!== false) && (strpos($text, "[spoiler=") !== false) && (++$endlessloop < 20)) {
- $text = preg_replace("/\[spoiler=[\"\']*(.*?)[\"\']*\](.*?)\[\/spoiler\]/ism",
+ while ((strpos($text, "[/spoiler]") !== false) && (strpos($text, "[spoiler=") !== false) && (++$endlessloop < 20)) {
+ $text = preg_replace(
+ "/\[spoiler=[\"\']*(.*?)[\"\']*\](.*?)\[\/spoiler\]/ism",
'$1 $2 ',
- $text);
+ $text
+ );
}
// Declare the format for [quote] layout
- $QuoteLayout = '
$1 ';
+ $QuoteLayout = '$1 ';
// Check for [quote] text
// handle nested quotes
@@ -1772,10 +1637,12 @@ class BBCode
// handle nested quotes
$endlessloop = 0;
- while ((strpos($text, "[/quote]")!== false) && (strpos($text, "[quote=") !== false) && (++$endlessloop < 20)) {
- $text = preg_replace("/\[quote=[\"\']*(.*?)[\"\']*\](.*?)\[\/quote\]/ism",
- "
" . $t_wrote . "
$2 ",
- $text);
+ while ((strpos($text, "[/quote]") !== false) && (strpos($text, "[quote=") !== false) && (++$endlessloop < 20)) {
+ $text = preg_replace(
+ "/\[quote=[\"\']*(.*?)[\"\']*\](.*?)\[\/quote\]/ism",
+ "" . $t_wrote . "
$2 ",
+ $text
+ );
}
@@ -1796,18 +1663,25 @@ class BBCode
$text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", ' ', $text);
$text = preg_replace("/\[zmg\=([0-9]*)x([0-9]*)\](.*?)\[\/zmg\]/ism", ' ', $text);
- $text = preg_replace_callback("/\[img\=(.*?)\](.*?)\[\/img\]/ism",
+ $text = preg_replace_callback(
+ "/\[[iz]mg\=(.*?)\](.*?)\[\/[iz]mg\]/ism",
function ($matches) use ($simple_html, $uriid) {
$matches[1] = self::proxyUrl($matches[1], $simple_html, $uriid);
- $matches[2] = htmlspecialchars($matches[2], ENT_COMPAT);
- return ' ';
+ $alt = htmlspecialchars($matches[2], ENT_COMPAT);
+ // Fix for Markdown problems with Diaspora, see issue #12701
+ if (($simple_html != self::DIASPORA) || strpos($matches[2], '"') === false) {
+ return ' ';
+ } else {
+ return ' ';
+ }
},
- $text);
+ $text
+ );
// Images
// [img]pathtoimage[/img]
$text = preg_replace_callback(
- "/\[img\](.*?)\[\/img\]/ism",
+ "/\[[iz]mg\](.*?)\[\/[iz]mg\]/ism",
function ($matches) use ($simple_html, $uriid) {
if (strpos($matches[1], "data:image/") === 0) {
return $matches[0];
@@ -1824,67 +1698,79 @@ class BBCode
$text = self::convertImages($text, $simple_html, $uriid);
- $text = preg_replace("/\[crypt\](.*?)\[\/crypt\]/ism", ' ', $text);
- $text = preg_replace("/\[crypt(.*?)\](.*?)\[\/crypt\]/ism", ' ', $text);
+ $text = preg_replace("/\[crypt\](.*?)\[\/crypt\]/ism", ' ', $text);
+ $text = preg_replace("/\[crypt(.*?)\](.*?)\[\/crypt\]/ism", ' ', $text);
//$text = preg_replace("/\[crypt=(.*?)\](.*?)\[\/crypt\]/ism", ' ', $text);
// Simplify "video" element
$text = preg_replace('(\[video[^\]]*?\ssrc\s?=\s?([^\s\]]+)[^\]]*?\].*?\[/video\])ism', '[video]$1[/video]', $text);
- if ($try_oembed) {
+ if ($simple_html == self::NPF) {
+ $text = preg_replace(
+ "/\[video\](.*?)\[\/video\]/ism",
+ '$1 ',
+ $text
+ );
+ $text = preg_replace(
+ "/\[audio\](.*?)\[\/audio\]/ism",
+ '
$1">$1 ',
+ $text
+ );
+ } elseif ($try_oembed) {
// html5 video and audio
- $text = preg_replace("/\[video\](.*?\.(ogg|ogv|oga|ogm|webm|mp4).*?)\[\/video\]/ism",
- '$1 ', $text);
+ $text = preg_replace(
+ "/\[video\](.*?\.(ogg|ogv|oga|ogm|webm|mp4).*?)\[\/video\]/ism",
+ '$1 ',
+ $text
+ );
$text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", $try_oembed_callback, $text);
$text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", $try_oembed_callback, $text);
- $text = preg_replace("/\[video\](.*?)\[\/video\]/ism",
- '$1 ', $text);
+ $text = preg_replace(
+ "/\[video\](.*?)\[\/video\]/ism",
+ '$1 ',
+ $text
+ );
$text = preg_replace("/\[audio\](.*?)\[\/audio\]/ism", '$1 ', $text);
} else {
- $text = preg_replace("/\[video\](.*?)\[\/video\]/ism",
- '$1 ', $text);
- $text = preg_replace("/\[audio\](.*?)\[\/audio\]/ism",
- '$1 ', $text);
+ $text = preg_replace(
+ "/\[video\](.*?)\[\/video\]/ism",
+ '$1 ',
+ $text
+ );
+ $text = preg_replace(
+ "/\[audio\](.*?)\[\/audio\]/ism",
+ '$1 ',
+ $text
+ );
}
// Backward compatibility, [iframe] support has been removed in version 2020.12
$text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/ism", '$1 ', $text);
- // Youtube extensions
- if ($try_oembed) {
- $text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", $try_oembed_callback, $text);
- $text = preg_replace_callback("/\[youtube\](www.youtube.com\/watch\?v\=.*?)\[\/youtube\]/ism", $try_oembed_callback, $text);
- $text = preg_replace_callback("/\[youtube\](https?:\/\/www.youtube.com\/shorts\/.*?)\[\/youtube\]/ism", $try_oembed_callback, $text);
- $text = preg_replace_callback("/\[youtube\](https?:\/\/youtu.be\/.*?)\[\/youtube\]/ism", $try_oembed_callback, $text);
- }
-
- $text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
- $text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/embed\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
- $text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/shorts\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
- $text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.*?)\[\/youtube\]/ism", '[youtube]$1[/youtube]', $text);
+ $text = self::normalizeVideoLinks($text);
+ // Youtube extensions
if ($try_oembed) {
$text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism", 'VIDEO ', $text);
} else {
- $text = preg_replace("/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism",
- 'https://www.youtube.com/watch?v=$1 ', $text);
- }
-
- if ($try_oembed) {
- $text = preg_replace_callback("/\[vimeo\](https?:\/\/player.vimeo.com\/video\/[0-9]+).*?\[\/vimeo\]/ism", $try_oembed_callback, $text);
- $text = preg_replace_callback("/\[vimeo\](https?:\/\/vimeo.com\/[0-9]+).*?\[\/vimeo\]/ism", $try_oembed_callback, $text);
+ $text = preg_replace(
+ "/\[youtube\]([A-Za-z0-9\-_=]+)(.*?)\[\/youtube\]/ism",
+ 'https://www.youtube.com/watch?v=$1 ',
+ $text
+ );
}
- $text = preg_replace("/\[vimeo\]https?:\/\/player.vimeo.com\/video\/([0-9]+)(.*?)\[\/vimeo\]/ism", '[vimeo]$1[/vimeo]', $text);
- $text = preg_replace("/\[vimeo\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/vimeo\]/ism", '[vimeo]$1[/vimeo]', $text);
-
+ // Vimeo extensions
if ($try_oembed) {
$text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism", '', $text);
} else {
- $text = preg_replace("/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism",
- 'https://vimeo.com/$1 ', $text);
+ $text = preg_replace(
+ "/\[vimeo\]([0-9]+)(.*?)\[\/vimeo\]/ism",
+ 'https://vimeo.com/$1 ',
+ $text
+ );
}
// oembed tag
@@ -1909,38 +1795,46 @@ class BBCode
$text = preg_replace("/\[event\-id\](.*?)\[\/event\-id\]/ism", '', $text);
}
- if (!$for_plaintext && DI::config()->get('system', 'big_emojis') && ($simple_html != self::DIASPORA)) {
- $conv = html_entity_decode(str_replace([' ', "\n", "\r"], '', $text));
- // Emojis are always 4 byte Unicode characters
- if (!empty($conv) && (strlen($conv) / mb_strlen($conv) == 4)) {
- $text = '' . $text . ' ';
- }
+ if (!$for_plaintext && DI::config()->get('system', 'big_emojis') && ($simple_html != self::DIASPORA) && Smilies::isEmojiPost($text)) {
+ $text = '' . $text . ' ';
}
// Handle mentions and hashtag links
if ($simple_html == self::DIASPORA) {
// The ! is converted to @ since Diaspora only understands the @
- $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
+ $text = preg_replace(
+ "/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
'@$3 ',
- $text);
+ $text
+ );
} elseif (in_array($simple_html, [self::OSTATUS, self::ACTIVITYPUB])) {
- $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
+ $text = preg_replace(
+ "/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
'$1$3 ',
- $text);
- $text = preg_replace("/([#])\[url\=(.*?)\](.*?)\[\/url\]/ism",
+ $text
+ );
+ $text = preg_replace(
+ "/([#])\[url\=(.*?)\](.*?)\[\/url\]/ism",
'$1$3 ',
- $text);
+ $text
+ );
} elseif (in_array($simple_html, [self::INTERNAL, self::EXTERNAL, self::TWITTER_API])) {
- $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
+ $text = preg_replace(
+ "/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
'$1$3 ',
- $text);
+ $text
+ );
} elseif ($simple_html == self::MASTODON_API) {
- $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
+ $text = preg_replace(
+ "/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
'$1$3 ',
- $text);
- $text = preg_replace("/([#])\[url\=(.*?)\](.*?)\[\/url\]/ism",
+ $text
+ );
+ $text = preg_replace(
+ "/([#])\[url\=(.*?)\](.*?)\[\/url\]/ism",
'$1$3 ',
- $text);
+ $text
+ );
} else {
$text = preg_replace("/([#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '$1$3', $text);
}
@@ -1958,10 +1852,13 @@ class BBCode
// Bookmarks in red - will be converted to bookmarks in friendica
$text = preg_replace("/#\^\[url\](.*?)\[\/url\]/ism", '[bookmark=$1]$1[/bookmark]', $text);
$text = preg_replace("/#\^\[url\=(.*?)\](.*?)\[\/url\]/ism", '[bookmark=$1]$2[/bookmark]', $text);
- $text = preg_replace("/#\[url\=.*?\]\^\[\/url\]\[url\=(.*?)\](.*?)\[\/url\]/i",
- "[bookmark=$1]$2[/bookmark]", $text);
+ $text = preg_replace(
+ "/#\[url\=.*?\]\^\[\/url\]\[url\=(.*?)\](.*?)\[\/url\]/i",
+ "[bookmark=$1]$2[/bookmark]",
+ $text
+ );
- if (in_array($simple_html, [self::OSTATUS, self::TWITTER])) {
+ if (in_array($simple_html, [self::OSTATUS, self::TWITTER, self::BLUESKY])) {
$text = preg_replace_callback("/([^#@!])\[url\=([^\]]*)\](.*?)\[\/url\]/ism", [self::class, 'expandLinksCallback'], $text);
//$text = preg_replace("/[^#@!]\[url\=([^\]]*)\](.*?)\[\/url\]/ism", ' $2 [url]$1[/url]', $text);
$text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", ' $2 [url]$1[/url]', $text);
@@ -1979,20 +1876,22 @@ class BBCode
"&\[url=/?posts/([^\[\]]*)\](.*)\[\/url\]&Usi",
function ($match) {
return "[url=" . DI::baseUrl() . "/display/" . $match[1] . "]" . $match[2] . "[/url]";
- }, $text
+ },
+ $text
);
$text = preg_replace_callback(
"&\[url=/people\?q\=(.*)\](.*)\[\/url\]&Usi",
function ($match) {
return "[url=" . DI::baseUrl() . "/search?search=%40" . $match[1] . "]" . $match[2] . "[/url]";
- }, $text
+ },
+ $text
);
// Server independent link to posts and comments
// See issue: https://github.com/diaspora/diaspora_federation/issues/75
$expression = "=diaspora://.*?/post/([0-9A-Za-z\-_@.:]{15,254}[0-9A-Za-z])=ism";
- $text = preg_replace($expression, DI::baseUrl()."/display/$1", $text);
+ $text = preg_replace($expression, DI::baseUrl() . "/display/$1", $text);
/* Tag conversion
* Supports:
@@ -2000,7 +1899,7 @@ class BBCode
* - [url=]#[/url]
*/
self::performWithEscapedTags($text, ['url', 'share'], function ($text) use ($simple_html) {
- $text = preg_replace_callback("/(?:#\[url\=[^\[\]]*\]|\[url\=[^\[\]]*\]#)(.*?)\[\/url\]/ism", function($matches) use ($simple_html) {
+ $text = preg_replace_callback("/(?:#\[url\=[^\[\]]*\]|\[url\=[^\[\]]*\]#)(.*?)\[\/url\]/ism", function ($matches) use ($simple_html) {
if ($simple_html == self::ACTIVITYPUB) {
return '#'
@@ -2017,8 +1916,8 @@ class BBCode
// We need no target="_blank" rel="noopener noreferrer" for local links
// convert links start with DI::baseUrl() as local link without the target="_blank" rel="noopener noreferrer" attribute
$escapedBaseUrl = preg_quote(DI::baseUrl(), '/');
- $text = preg_replace("/\[url\](".$escapedBaseUrl.".*?)\[\/url\]/ism", ' $1 ', $text);
- $text = preg_replace("/\[url\=(".$escapedBaseUrl.".*?)\](.*?)\[\/url\]/ism", '$2 ', $text);
+ $text = preg_replace("/\[url\](" . $escapedBaseUrl . ".*?)\[\/url\]/ism", '$1 ', $text);
+ $text = preg_replace("/\[url\=(" . $escapedBaseUrl . ".*?)\](.*?)\[\/url\]/ism", '$2 ', $text);
$text = preg_replace("/\[url\](.*?)\[\/url\]/ism", '$1 ', $text);
$text = preg_replace("/\[url\=(.*?)\](.*?)\[\/url\]/ism", '$2 ', $text);
@@ -2049,10 +1948,15 @@ class BBCode
// sanitizes src attributes (http and redir URLs for displaying in a web page, cid used for inline images in emails)
$allowed_src_protocols = ['//', 'http://', 'https://', 'contact/redir/', 'cid:'];
- array_walk($allowed_src_protocols, function(&$value) { $value = preg_quote($value, '#');});
+ array_walk($allowed_src_protocols, function (&$value) {
+ $value = preg_quote($value, '#');
+ });
- $text = preg_replace('#<([^>]*?)(src)="(?!' . implode('|', $allowed_src_protocols) . ')(.*?)"(.*?)>#ism',
- '<$1$2=""$4 data-original-src="$3" class="invalid-src" title="' . DI::l10n()->t('Invalid source protocol') . '">', $text);
+ $text = preg_replace(
+ '#<([^>]*?)(src)="(?!' . implode('|', $allowed_src_protocols) . ')(.*?)"(.*?)>#ism',
+ '<$1$2=""$4 data-original-src="$3" class="invalid-src" title="' . DI::l10n()->t('Invalid source protocol') . '">',
+ $text
+ );
// sanitize href attributes (only allowlisted protocols URLs)
// default value for backward compatibility
@@ -2064,7 +1968,9 @@ class BBCode
$allowed_link_protocols[] = 'https://';
$allowed_link_protocols[] = 'contact/redir/';
- array_walk($allowed_link_protocols, function(&$value) { $value = preg_quote($value, '#');});
+ array_walk($allowed_link_protocols, function (&$value) {
+ $value = preg_quote($value, '#');
+ });
$regex = '#<([^>]*?)(href)="(?!' . implode('|', $allowed_link_protocols) . ')(.*?)"(.*?)>#ism';
$text = preg_replace($regex, '<$1$2="javascript:void(0)"$4 data-original-href="$3" class="invalid-href" title="' . DI::l10n()->t('Invalid link protocol') . '">', $text);
@@ -2074,7 +1980,8 @@ class BBCode
$text,
function (array $attributes, array $author_contact, $content, $is_quote_share) use ($simple_html) {
return self::convertShareCallback($attributes, $author_contact, $content, $is_quote_share, $simple_html);
- }, $uriid
+ },
+ $uriid
);
$text = self::interpolateSavedImagesIntoItemBody($uriid, $text, $saved_image);
@@ -2089,13 +1996,14 @@ class BBCode
// Additionally, [pre] tags preserve spaces
$text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", function ($match) {
- return str_replace([' ', "\n"], [' ', " "], htmlentities($match[1], ENT_NOQUOTES,'UTF-8'));
+ return str_replace([' ', "\n"], [' ', " "], htmlentities($match[1], ENT_NOQUOTES, 'UTF-8'));
}, $text);
return $text;
}); // Escaped code
- $text = preg_replace_callback("#\[code(?:=([^\]]*))?\](.*?)\[\/code\]#ism",
+ $text = preg_replace_callback(
+ "#\[code(?:=([^\]]*))?\](.*?)\[\/code\]#ism",
function ($matches) {
if (strpos($matches[2], "\n") !== false) {
$return = '' . htmlentities(trim($matches[2], "\n\r"), ENT_NOQUOTES, 'UTF-8') . '
';
@@ -2111,8 +2019,8 @@ class BBCode
// Default iframe allowed domains/path
$allowedIframeDomains = [
DI::baseUrl()->getHost()
- . (DI::baseUrl()->getPath() ? '/' . DI::baseUrl()->getPath() : '')
- . '/oembed/', # The path part has to change with the source in Content\Oembed::iframe
+ . (DI::baseUrl()->getPath() ? '/' . DI::baseUrl()->getPath() : '')
+ . '/oembed/', # The path part has to change with the source in Content\Oembed::iframe
'www.youtube.com/embed/',
'player.vimeo.com/video/',
];
@@ -2124,6 +2032,10 @@ class BBCode
: []
);
+ if (strpos($text, '') !== false || strpos($text, '
') !== false) {
+ $text = '' . $text . '
';
+ }
+
$text = HTML::purify($text, $allowedIframeDomains);
DI::profiler()->stopRecording();
@@ -2140,7 +2052,7 @@ class BBCode
{
DI::profiler()->startRecording('rendering');
- $text = BBCode::performWithEscapedTags($text, ['code', 'noparse', 'nobb', 'pre'], function ($text) {
+ $text = self::performWithEscapedTags($text, ['code', 'noparse', 'nobb', 'pre'], function ($text) {
$text = preg_replace("/[\s|\n]*\[abstract\].*?\[\/abstract\][\s|\n]*/ism", ' ', $text);
$text = preg_replace("/[\s|\n]*\[abstract=.*?\].*?\[\/abstract][\s|\n]*/ism", ' ', $text);
return $text;
@@ -2162,7 +2074,7 @@ class BBCode
DI::profiler()->startRecording('rendering');
$addon = strtolower($addon);
- $abstract = BBCode::performWithEscapedTags($text, ['code', 'noparse', 'nobb', 'pre'], function ($text) use ($addon) {
+ $abstract = self::performWithEscapedTags($text, ['code', 'noparse', 'nobb', 'pre'], function ($text) use ($addon) {
if ($addon && preg_match('#\[abstract=' . preg_quote($addon, '#') . '](.*?)\[/abstract]#ism', $text, $matches)) {
return $matches[1];
}
@@ -2225,7 +2137,8 @@ class BBCode
* Transform #tags, strip off the [url] and replace spaces with underscore
*/
$url_search_string = "^\[\]";
- $text = preg_replace_callback("/#\[url\=([$url_search_string]*)\](.*?)\[\/url\]/i",
+ $text = preg_replace_callback(
+ "/#\[url\=([$url_search_string]*)\](.*?)\[\/url\]/i",
function ($matches) {
return '#' . str_replace(' ', '_', $matches[2]);
},
@@ -2237,7 +2150,7 @@ class BBCode
// Convert it to HTML - don't try oembed
if ($for_diaspora) {
- $text = self::convert($text, false, self::DIASPORA);
+ $text = self::convertForUriId(0, $text, self::DIASPORA);
// Add all tags that maybe were removed
if (preg_match_all("/#\[url\=([$url_search_string]*)\](.*?)\[\/url\]/ism", $original_text, $tags)) {
@@ -2251,13 +2164,16 @@ class BBCode
$text = $text . ' ' . $tagline;
}
} else {
- $text = self::convert($text, false, self::CONNECTORS);
+ $text = self::convertForUriId(0, $text, self::CONNECTORS);
}
// If a link is followed by a quote then there should be a newline before it
// Maybe we should make this newline at every time before a quote.
$text = str_replace([''], [''], $text);
+ // The converter doesn't convert these elements
+ $text = str_replace(['', '
'], ['', '
'], $text);
+
// Now convert HTML to Markdown
$text = HTML::toMarkdown($text);
@@ -2360,7 +2276,8 @@ class BBCode
*/
public static function expandTags(string $body): string
{
- return preg_replace_callback("/(?<=\W|^)([!#@])([^\^ \x0D\x0A,;:?'\"]*[^\^ \x0D\x0A,;:?!'\".])/",
+ return preg_replace_callback(
+ "/(?<=\W|^)([!#@])([^\^ \x0D\x0A,;:?'\"]*[^\^ \x0D\x0A,;:?!'\".])/",
function (array $match) {
switch ($match[1]) {
case '!':
@@ -2377,7 +2294,9 @@ class BBCode
default:
return $match[1] . '[url=' . DI::baseUrl() . '/search?tag=' . $match[2] . ']' . $match[2] . '[/url]';
}
- }, $body);
+ },
+ $body
+ );
}
/**