', $data['url'], $data['title']);
}
+ }
- 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['description']) && $data['description'] != $data['title']) {
+ // Sanitize the HTML by converting it to BBCode
+ $bbcode = HTML::toBBCode($data['description']);
+ $return .= sprintf('
', $match[0]);
- },
- $text
- );
- }
+ // leave open the posibility 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(
+ "/\[map\](.*?)\[\/map\]/ism",
+ function ($match) use ($simple_html) {
+ return str_replace($match[0], '
", $text);
+ // oembed tag
+ $text = OEmbed::BBCode2HTML($text);
- // If we found an event earlier, strip out all the event code and replace with a reformatted version.
- // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
- // Summary (e.g. title) is required, earlier revisions only required description (in addition to
- // start which is always required). Allow desc with a missing summary for compatibility.
+ // Avoid triple linefeeds through oembed
+ $text = str_replace("
", "
", $text);
- if ((!empty($ev['desc']) || !empty($ev['summary'])) && !empty($ev['start'])) {
- $sub = Event::getHTML($ev, $simple_html);
+ // If we found an event earlier, strip out all the event code and replace with a reformatted version.
+ // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
+ // Summary (e.g. title) is required, earlier revisions only required description (in addition to
+ // start which is always required). Allow desc with a missing summary for compatibility.
- $text = preg_replace("/\[event\-summary\](.*?)\[\/event\-summary\]/ism", '', $text);
- $text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/ism", '', $text);
- $text = preg_replace("/\[event\-start\](.*?)\[\/event\-start\]/ism", $sub, $text);
- $text = preg_replace("/\[event\-finish\](.*?)\[\/event\-finish\]/ism", '', $text);
- $text = preg_replace("/\[event\-location\](.*?)\[\/event\-location\]/ism", '', $text);
- $text = preg_replace("/\[event\-adjust\](.*?)\[\/event\-adjust\]/ism", '', $text);
- $text = preg_replace("/\[event\-id\](.*?)\[\/event\-id\]/ism", '', $text);
- }
+ if ((!empty($ev['desc']) || !empty($ev['summary'])) && !empty($ev['start'])) {
+ $sub = Event::getHTML($ev, $simple_html);
- // Replace non graphical smilies for external posts
- if (!$nosmile && !$for_plaintext) {
- $text = Smilies::replace($text);
- }
+ $text = preg_replace("/\[event\-summary\](.*?)\[\/event\-summary\]/ism", '', $text);
+ $text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/ism", '', $text);
+ $text = preg_replace("/\[event\-start\](.*?)\[\/event\-start\]/ism", $sub, $text);
+ $text = preg_replace("/\[event\-finish\](.*?)\[\/event\-finish\]/ism", '', $text);
+ $text = preg_replace("/\[event\-location\](.*?)\[\/event\-location\]/ism", '', $text);
+ $text = preg_replace("/\[event\-adjust\](.*?)\[\/event\-adjust\]/ism", '', $text);
+ $text = preg_replace("/\[event\-id\](.*?)\[\/event\-id\]/ism", '', $text);
+ }
- // if the HTML is used to generate plain text, then don't do this search, but replace all URL of that kind to text
- if (!$for_plaintext) {
- $text = preg_replace(Strings::autoLinkRegEx(), '[url]$1[/url]', $text);
- if (in_array($simple_html, [7, 9])) {
- $text = preg_replace_callback("/\[url\](.*?)\[\/url\]/ism", 'self::convertUrlForOStatusCallback', $text);
- $text = preg_replace_callback("/\[url\=(.*?)\](.*?)\[\/url\]/ism", 'self::convertUrlForOStatusCallback', $text);
- }
- } else {
- $text = preg_replace("(\[url\](.*?)\[\/url\])ism", " $1 ", $text);
- $text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'self::removePictureLinksCallback', $text);
- }
+ // Replace non graphical smilies for external posts
+ if (!$nosmile && !$for_plaintext) {
+ $text = Smilies::replace($text);
+ }
- $text = str_replace(["\r","\n"], [' ', ' '], $text);
-
- // Remove all hashtag addresses
- if ((!$try_oembed || $simple_html) && !in_array($simple_html, [3, 7, 9])) {
- $text = preg_replace("/([#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '$1$3', $text);
- } elseif ($simple_html == 3) {
- // The ! is converted to @ since Diaspora only understands the @
- $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
- '@$3',
- $text);
- } elseif (in_array($simple_html, [7, 9])) {
- $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
- '$1$3',
- $text);
- } elseif (!$simple_html) {
- $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
- '$1$3',
- $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 . '';
+ }
+ }
- // 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);
+ if (!$for_plaintext) {
+ if (in_array($simple_html, [self::OSTATUS, self::ACTIVITYPUB])) {
+ $text = preg_replace_callback("/\[url\](.*?)\[\/url\]/ism", 'self::convertUrlForActivityPubCallback', $text);
+ $text = preg_replace_callback("/\[url\=(.*?)\](.*?)\[\/url\]/ism", 'self::convertUrlForActivityPubCallback', $text);
+ }
+ } else {
+ $text = preg_replace("(\[url\](.*?)\[\/url\])ism", " $1 ", $text);
+ $text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'self::removePictureLinksCallback', $text);
+ }
- if (in_array($simple_html, [2, 6, 7, 8])) {
- $text = preg_replace_callback("/([^#@!])\[url\=([^\]]*)\](.*?)\[\/url\]/ism", "self::expandLinksCallback", $text);
- //$Text = preg_replace("/[^#@!]\[url\=([^\]]*)\](.*?)\[\/url\]/ism", ' $2 [url]$1[/url]', $Text);
- $text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", ' $2 [url]$1[/url]',$text);
- }
+ $text = str_replace(["\r","\n"], [' ', ' '], $text);
+
+ // Remove all hashtag addresses
+ if ($simple_html && !in_array($simple_html, [self::DIASPORA, self::OSTATUS, self::ACTIVITYPUB])) {
+ $text = preg_replace("/([#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '$1$3', $text);
+ } elseif ($simple_html == self::DIASPORA) {
+ // The ! is converted to @ since Diaspora only understands the @
+ $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
+ '@$3',
+ $text);
+ } elseif (in_array($simple_html, [self::OSTATUS, self::ACTIVITYPUB])) {
+ $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
+ '$1$3',
+ $text);
+ } elseif (!$simple_html) {
+ $text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
+ '$1$3',
+ $text);
+ }
- if ($simple_html == 5) {
- $text = preg_replace("/[^#@!]\[url\=(.*?)\](.*?)\[\/url\]/ism", '[url]$1[/url]', $text);
- }
+ // 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);
- // Perform URL Search
- if ($try_oembed) {
- $text = preg_replace_callback("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", $try_oembed_callback, $text);
- }
+ if (in_array($simple_html, [self::API, self::OSTATUS, self::TWITTER])) {
+ $text = preg_replace_callback("/([^#@!])\[url\=([^\]]*)\](.*?)\[\/url\]/ism", "self::expandLinksCallback", $text);
+ //$Text = preg_replace("/[^#@!]\[url\=([^\]]*)\](.*?)\[\/url\]/ism", ' $2 [url]$1[/url]', $Text);
+ $text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", ' $2 [url]$1[/url]',$text);
+ }
- if ($simple_html == 5) {
- $text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", '[url]$1[/url]', $text);
- } else {
- $text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", '[url=$1]$2[/url]', $text);
- }
+ // Perform URL Search
+ if ($try_oembed) {
+ $text = preg_replace_callback("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", $try_oembed_callback, $text);
+ }
- // Handle Diaspora posts
- $text = preg_replace_callback(
- "&\[url=/?posts/([^\[\]]*)\](.*)\[\/url\]&Usi",
- function ($match) {
- return "[url=" . System::baseUrl() . "/display/" . $match[1] . "]" . $match[2] . "[/url]";
- }, $text
- );
+ $text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", '[url=$1]$2[/url]', $text);
+
+ // Handle Diaspora posts
+ $text = preg_replace_callback(
+ "&\[url=/?posts/([^\[\]]*)\](.*)\[\/url\]&Usi",
+ function ($match) {
+ return "[url=" . DI::baseUrl() . "/display/" . $match[1] . "]" . $match[2] . "[/url]";
+ }, $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
+ );
+
+ // 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);
+
+ /* Tag conversion
+ * Supports:
+ * - #[url=][/url]
+ * - [url=]#[/url]
+ */
+ $text = preg_replace_callback("/(?:#\[url\=[^\[\]]*\]|\[url\=[^\[\]]*\]#)(.*?)\[\/url\]/ism", function($matches) use ($simple_html) {
+ if ($simple_html == BBCode::ACTIVITYPUB) {
+ return '#'
+ . XML::escape($matches[1]) . '';
+ } else {
+ return '#'
+ . XML::escape($matches[1]) . '';
+ }
+ }, $text);
- $text = preg_replace_callback(
- "&\[url=/people\?q\=(.*)\](.*)\[\/url\]&Usi",
- function ($match) {
- return "[url=" . System::baseUrl() . "/search?search=%40" . $match[1] . "]" . $match[2] . "[/url]";
- }, $text
- );
+ // 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);
- // 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, System::baseUrl()."/display/$1", $text);
+ $text = preg_replace("/\[url\](.*?)\[\/url\]/ism", '$1', $text);
+ $text = preg_replace("/\[url\=(.*?)\](.*?)\[\/url\]/ism", '$2', $text);
- /* Tag conversion
- * Supports:
- * - #[url=][/url]
- * - [url=]#[/url]
- */
- $text = preg_replace_callback("/(?:#\[url\=.*?\]|\[url\=.*?\]#)(.*?)\[\/url\]/ism", function($matches) {
- return '#'
- . XML::escape($matches[1])
- . '';
- }, $text);
+ // Red compatibility, though the link can't be authenticated on Friendica
+ $text = preg_replace("/\[zrl\=(.*?)\](.*?)\[\/zrl\]/ism", '$2', $text);
- // We need no target="_blank" for local links
- // convert links start with System::baseUrl() as local link without the target="_blank" attribute
- $escapedBaseUrl = preg_quote(System::baseUrl(), '/');
- $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);
+ // we may need to restrict this further if it picks up too many strays
+ // link acct:user@host to a webfinger profile redirector
- // Red compatibility, though the link can't be authenticated on Friendica
- $text = preg_replace("/\[zrl\=(.*?)\](.*?)\[\/zrl\]/ism", '$2', $text);
+ $text = preg_replace('/acct:([^@]+)@((?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63})/', 'acct:$1@$2', $text);
+ // Perform MAIL Search
+ $text = preg_replace("/\[mail\](.*?)\[\/mail\]/", '$1', $text);
+ $text = preg_replace("/\[mail\=(.*?)\](.*?)\[\/mail\]/", '$2', $text);
- // we may need to restrict this further if it picks up too many strays
- // link acct:user@host to a webfinger profile redirector
+ /// @todo What is the meaning of these lines?
+ $text = preg_replace('/\[\&\;([#a-z0-9]+)\;\]/', '&$1;', $text);
+ $text = preg_replace('/\&\#039\;/', '\'', $text);
- $text = preg_replace('/acct:([^@]+)@((?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63})/', 'acct:$1@$2', $text);
+ // Currently deactivated, it made problems with " inside of alt texts.
+ //$text = preg_replace('/\"\;/', '"', $text);
- // Perform MAIL Search
- $text = preg_replace("/\[mail\](.*?)\[\/mail\]/", '$1', $text);
- $text = preg_replace("/\[mail\=(.*?)\](.*?)\[\/mail\]/", '$2', $text);
+ // fix any escaped ampersands that may have been converted into links
+ $text = preg_replace('/\<([^>]*?)(src|href)=(.*?)\&\;(.*?)\>/ism', '<$1$2=$3&$4>', $text);
- // Unhide all [noparse] contained bbtags unspacefying them
- // and triming the [noparse] tag.
+ // 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://', 'redir/', 'cid:'];
- $text = preg_replace_callback("/\[noparse\](.*?)\[\/noparse\]/ism", 'self::unescapeNoparseCallback', $text);
- $text = preg_replace_callback("/\[nobb\](.*?)\[\/nobb\]/ism", 'self::unescapeNoparseCallback', $text);
- $text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", 'self::unescapeNoparseCallback', $text);
+ array_walk($allowed_src_protocols, function(&$value) { $value = preg_quote($value, '#');});
- /// @todo What is the meaning of these lines?
- $text = preg_replace('/\[\&\;([#a-z0-9]+)\;\]/', '&$1;', $text);
- $text = preg_replace('/\&\#039\;/', '\'', $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);
- // Currently deactivated, it made problems with " inside of alt texts.
- //$text = preg_replace('/\"\;/', '"', $text);
+ // sanitize href attributes (only allowlisted protocols URLs)
+ // default value for backward compatibility
+ $allowed_link_protocols = DI::config()->get('system', 'allowed_link_protocols', []);
- // fix any escaped ampersands that may have been converted into links
- $text = preg_replace('/\<([^>]*?)(src|href)=(.*?)\&\;(.*?)\>/ism', '<$1$2=$3&$4>', $text);
+ // Always allowed protocol even if config isn't set or not including it
+ $allowed_link_protocols[] = '//';
+ $allowed_link_protocols[] = 'http://';
+ $allowed_link_protocols[] = 'https://';
+ $allowed_link_protocols[] = 'redir/';
- // sanitizes src attributes (http and redir URLs for displaying in a web page, cid used for inline images in emails)
- $allowed_src_protocols = ['http', 'redir', 'cid'];
- $text = preg_replace('#<([^>]*?)(src)="(?!' . implode('|', $allowed_src_protocols) . ')(.*?)"(.*?)>#ism',
- '<$1$2=""$4 data-original-src="$3" class="invalid-src" title="' . L10n::t('Invalid source protocol') . '">', $text);
+ array_walk($allowed_link_protocols, function(&$value) { $value = preg_quote($value, '#');});
- // sanitize href attributes (only whitelisted protocols URLs)
- // default value for backward compatibility
- $allowed_link_protocols = Config::get('system', 'allowed_link_protocols', ['ftp', 'mailto', 'gopher', 'cid']);
+ $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);
- // Always allowed protocol even if config isn't set or not including it
- $allowed_link_protocols[] = 'http';
- $allowed_link_protocols[] = 'redir/';
+ // Shared content
+ $text = self::convertShare(
+ $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);
+ }
+ );
- $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="' . L10n::t('Invalid link protocol') . '">', $text);
+ $text = self::interpolateSavedImagesIntoItemBody($text, $saved_image);
- // Shared content
- $text = self::convertShare(
- $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);
- }
- );
+ return $text;
+ }); // Escaped noparse, nobb, pre
- if ($saved_image) {
- $text = self::interpolateSavedImagesIntoItemBody($text, $saved_image);
- }
+ // Remove escaping tags
+ $text = preg_replace("/\[noparse\](.*?)\[\/noparse\]/ism", '\1', $text);
+ $text = preg_replace("/\[nobb\](.*?)\[\/nobb\]/ism", '\1', $text);
+ $text = preg_replace("/\[pre\](.*?)\[\/pre\]/ism", '\1', $text);
- // Restore code blocks
- $text = preg_replace_callback('/#codeblock-([0-9]+)#/iU',
- function ($matches) use ($codeblocks) {
- $return = $matches[0];
- if (isset($codeblocks[intval($matches[1])])) {
- $return = $codeblocks[$matches[1]];
+ return $text;
+ }); // Escaped code
+
+ $text = preg_replace_callback("#\[code(?:=([^\]]*))?\](.*?)\[\/code\]#ism",
+ function ($matches) {
+ if (strpos($matches[2], "\n") !== false) {
+ $return = '
';
+ } else {
+ $return = '' . htmlspecialchars($matches[2], ENT_NOQUOTES, 'UTF-8') . '';
}
+
return $return;
},
$text
@@ -1795,7 +1884,7 @@ class BBCode extends BaseObject
// Clean up the HTML by loading and saving the HTML with the DOM.
// Bad structured html can break a whole page.
// For performance reasons do it only with activated item cache or at export.
- if (!$try_oembed || (get_itemcachepath() != "")) {
+ if (!$try_oembed || (get_itemcachepath() != '')) {
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
@@ -1803,10 +1892,10 @@ class BBCode extends BaseObject
$doctype = '';
$encoding = '';
- @$doc->loadHTML($encoding.$doctype."".$text."");
+ @$doc->loadHTML($encoding . $doctype . '' . $text . '');
$doc->encoding = 'UTF-8';
$text = $doc->saveHTML();
- $text = str_replace(["", "", $doctype, $encoding], ["", "", "", ""], $text);
+ $text = str_replace(['', '', $doctype, $encoding], ['', '', '', ''], $text);
$text = str_replace(' ', '', $text);
@@ -1826,7 +1915,7 @@ class BBCode extends BaseObject
}
/**
- * @brief Strips the "abstract" tag from the provided text
+ * Strips the "abstract" tag from the provided text
*
* @param string $text The text with BBCode
* @return string The same text - but without "abstract" element
@@ -1840,15 +1929,15 @@ class BBCode extends BaseObject
}
/**
- * @brief Returns the value of the "abstract" element
+ * Returns the value of the "abstract" element
*
* @param string $text The text that maybe contains the element
* @param string $addon The addon for which the abstract is meant for
* @return string The abstract
*/
- public static function getAbstract($text, $addon = "")
+ public static function getAbstract($text, $addon = '')
{
- $abstract = "";
+ $abstract = '';
$abstracts = [];
$addon = strtolower($addon);
@@ -1862,7 +1951,7 @@ class BBCode extends BaseObject
$abstract = $abstracts[$addon];
}
- if ($abstract == "" && preg_match("/\[abstract\](.*?)\[\/abstract\]/ism", $text, $result)) {
+ if ($abstract == '' && preg_match("/\[abstract\](.*?)\[\/abstract\]/ism", $text, $result)) {
$abstract = $result[1];
}
@@ -1870,7 +1959,7 @@ class BBCode extends BaseObject
}
/**
- * @brief Callback function to replace a Friendica style mention in a mention for Diaspora
+ * Callback function to replace a Friendica style mention in a mention for Diaspora
*
* @param array $match Matching values for the callback
* [1] = Mention type (! or @)
@@ -1897,7 +1986,7 @@ class BBCode extends BaseObject
}
/**
- * @brief Converts a BBCode text into Markdown
+ * Converts a BBCode text into Markdown
*
* This function converts a BBCode item body to be sent to Markdown-enabled
* systems like Diaspora and Libertree
@@ -1909,8 +1998,6 @@ class BBCode extends BaseObject
*/
public static function toMarkdown($text, $for_diaspora = true)
{
- $a = self::getApp();
-
$original_text = $text;
// Since Diaspora is creating a summary for links, this function removes them before posting
@@ -1934,11 +2021,11 @@ class BBCode extends BaseObject
// Convert it to HTML - don't try oembed
if ($for_diaspora) {
- $text = self::convert($text, false, 3);
+ $text = self::convert($text, false, self::DIASPORA);
// Add all tags that maybe were removed
if (preg_match_all("/#\[url\=([$url_search_string]*)\](.*?)\[\/url\]/ism", $original_text, $tags)) {
- $tagline = "";
+ $tagline = '';
foreach ($tags[2] as $tag) {
$tag = html_entity_decode($tag, ENT_QUOTES, 'UTF-8');
if (!strpos(html_entity_decode($text, ENT_QUOTES, 'UTF-8'), '#' . $tag)) {
@@ -1948,25 +2035,19 @@ class BBCode extends BaseObject
$text = $text . " " . $tagline;
}
} else {
- $text = self::convert($text, false, 4);
+ $text = self::convert($text, false, self::CONNECTORS);
}
- // mask some special HTML chars from conversation to markdown
- $text = str_replace(['<', '>', '&'], ['&_lt_;', '&_gt_;', '&_amp_;'], $text);
-
// 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);
+ $text = str_replace(['
'], ['
'], $text);
$stamp1 = microtime(true);
// Now convert HTML to Markdown
$text = HTML::toMarkdown($text);
- // unmask the special chars back to HTML
- $text = str_replace(['&\_lt\_;', '&\_gt\_;', '&\_amp\_;'], ['<', '>', '&'], $text);
-
- $a->getProfiler()->saveTimestamp($stamp1, "parser", System::callstack());
+ DI::profiler()->saveTimestamp($stamp1, "parser", System::callstack());
// Libertree has a problem with escaped hashtags.
$text = str_replace(['\#'], ['#'], $text);
@@ -1990,76 +2071,97 @@ class BBCode extends BaseObject
}
/**
- * @brief Pull out all #hashtags and @person tags from $string.
- *
- * We also get @person@domain.com - which would make
- * the regex quite complicated as tags can also
- * end a sentence. So we'll run through our results
- * and strip the period from any tags which end with one.
- * Returns array of tags found, or empty array.
- *
- * @param string $string Post content
- *
- * @return array List of tag and person names
- */
- public static function getTags($string)
- {
- $ret = [];
-
- // Convert hashtag links to hashtags
- $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2', $string);
-
- // ignore anything in a code block
- $string = preg_replace('/\[code.*?\].*?\[\/code\]/sm', '', $string);
-
- // Force line feeds at bbtags
- $string = str_replace(['[', ']'], ["\n[", "]\n"], $string);
-
- // ignore anything in a bbtag
- $string = preg_replace('/\[(.*?)\]/sm', '', $string);
-
- // Match full names against @tags including the space between first and last
- // We will look these up afterward to see if they are full names or not recognisable.
-
- if (preg_match_all('/(@[^ \x0D\x0A,:?]+ [^ \x0D\x0A@,:?]+)([ \x0D\x0A@,:?]|$)/', $string, $matches)) {
- foreach ($matches[1] as $match) {
- if (strstr($match, ']')) {
- // we might be inside a bbcode color tag - leave it alone
- continue;
- }
-
- if (substr($match, -1, 1) === '.') {
- $ret[] = substr($match, 0, -1);
- } else {
- $ret[] = $match;
- }
- }
- }
-
- // Otherwise pull out single word tags. These can be @nickname, @first_last
- // and #hash tags.
-
- if (preg_match_all('/([!#@][^\^ \x0D\x0A,;:?]+)([ \x0D\x0A,;:?]|$)/', $string, $matches)) {
- foreach ($matches[1] as $match) {
- if (strstr($match, ']')) {
- // we might be inside a bbcode color tag - leave it alone
- continue;
- }
- if (substr($match, -1, 1) === '.') {
- $match = substr($match,0,-1);
- }
- // ignore strictly numeric tags like #1
- if ((strpos($match, '#') === 0) && ctype_digit(substr($match, 1))) {
- continue;
- }
- // try not to catch url fragments
- if (strpos($string, $match) && preg_match('/[a-zA-z0-9\/]/', substr($string, strpos($string, $match) - 1, 1))) {
- continue;
- }
- $ret[] = $match;
- }
- }
-
- return $ret;
- }
+ * Pull out all #hashtags and @person tags from $string.
+ *
+ * We also get @person@domain.com - which would make
+ * the regex quite complicated as tags can also
+ * end a sentence. So we'll run through our results
+ * and strip the period from any tags which end with one.
+ * Returns array of tags found, or empty array.
+ *
+ * @param string $string Post content
+ *
+ * @return array List of tag and person names
+ */
+ public static function getTags($string)
+ {
+ $ret = [];
+
+ // Convert hashtag links to hashtags
+ $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2 ', $string);
+
+ // ignore anything in a code block
+ $string = preg_replace('/\[code.*?\].*?\[\/code\]/sm', '', $string);
+
+ // Force line feeds at bbtags
+ $string = str_replace(['[', ']'], ["\n[", "]\n"], $string);
+
+ // ignore anything in a bbtag
+ $string = preg_replace('/\[(.*?)\]/sm', '', $string);
+
+ // Match full names against @tags including the space between first and last
+ // We will look these up afterward to see if they are full names or not recognisable.
+
+ if (preg_match_all('/(@[^ \x0D\x0A,:?]+ [^ \x0D\x0A@,:?]+)([ \x0D\x0A@,:?]|$)/', $string, $matches)) {
+ foreach ($matches[1] as $match) {
+ if (strstr($match, ']')) {
+ // we might be inside a bbcode color tag - leave it alone
+ continue;
+ }
+
+ if (substr($match, -1, 1) === '.') {
+ $ret[] = substr($match, 0, -1);
+ } else {
+ $ret[] = $match;
+ }
+ }
+ }
+
+ // Otherwise pull out single word tags. These can be @nickname, @first_last
+ // and #hash tags.
+
+ if (preg_match_all('/([!#@][^\^ \x0D\x0A,;:?]+)([ \x0D\x0A,;:?]|$)/', $string, $matches)) {
+ foreach ($matches[1] as $match) {
+ if (strstr($match, ']')) {
+ // we might be inside a bbcode color tag - leave it alone
+ continue;
+ }
+
+ if (substr($match, -1, 1) === '.') {
+ $match = substr($match,0,-1);
+ }
+
+ // ignore strictly numeric tags like #1
+ if ((strpos($match, '#') === 0) && ctype_digit(substr($match, 1))) {
+ continue;
+ }
+
+ // try not to catch url fragments
+ if (strpos($string, $match) && preg_match('/[a-zA-z0-9\/]/', substr($string, strpos($string, $match) - 1, 1))) {
+ continue;
+ }
+ $ret[] = $match;
+ }
+ }
+
+ return $ret;
+ }
+
+ /**
+ * Perform a custom function on a text after having escaped blocks enclosed in the provided tag list.
+ *
+ * @param string $text
+ * @param array $tagList A list of tag names, e.g ['noparse', 'nobb', 'pre']
+ * @param callable $callback
+ * @return string
+ * @throws Exception
+ *@see Strings::performWithEscapedBlocks
+ *
+ */
+ public static function performWithEscapedTags(string $text, array $tagList, callable $callback)
+ {
+ $tagList = array_map('preg_quote', $tagList);
+
+ return Strings::performWithEscapedBlocks($text, '#\[(?:' . implode('|', $tagList) . ').*?\[/(?:' . implode('|', $tagList) . ')]#ism', $callback);
+ }
}