class BBCode
{
+ const INTERNAL = 0;
+ const API = 2;
+ const DIASPORA = 3;
+ const CONNECTORS = 4;
+ const OSTATUS = 7;
+ const TWITTER = 8;
+ const BACKLINK = 8;
+ const ACTIVITYPUB = 9;
+
/**
* Fetches attachment data that were generated the old way
*
*/
public static function toPlaintext($text, $keep_urls = true)
{
- $naked_text = HTML::toPlaintext(BBCode::convert($text, false, 0, true), 0, !$keep_urls);
+ $naked_text = HTML::toPlaintext(self::convert($text, false, 0, true), 0, !$keep_urls);
return $naked_text;
}
- private static function proxyUrl($image, $simplehtml = false)
+ private static function proxyUrl($image, $simplehtml = self::INTERNAL)
{
// Only send proxied pictures to API and for internal display
- if (in_array($simplehtml, [false, 2])) {
+ if (in_array($simplehtml, [self::INTERNAL, self::API])) {
return ProxyUtils::proxifyUrl($image);
} else {
return $image;
}
}
- public static function scaleExternalImages($srctext)
+ /**
+ * This function changing the visual size (not the real size) of images.
+ * The function does not work for pictures with an alternate text description.
+ * This could only be changed by using some new "img" BBCode format.
+ *
+ * @param string $srctext The body with images
+ * @return string The body with possibly scaled images
+ */
+ public static function scaleExternalImages(string $srctext)
{
$s = $srctext;
+ // Simplify image links
+ $s = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $s);
+
$matches = null;
$c = preg_match_all('/\[img.*?\](.*?)\[\/img\]/ism', $s, $matches, PREG_SET_ORDER);
if ($c) {
continue;
}
- $i = Network::fetchUrl($mtch[1]);
- if (!$i) {
- return $srctext;
+ $curlResult = Network::curl($mtch[1], true);
+ if (!$curlResult->isSuccess()) {
+ continue;
}
- // guess mimetype from headers or filename
- $type = Images::guessType($mtch[1], true);
+ $i = $curlResult->getBody();
+ $type = $curlResult->getContentType();
+ $type = Images::getMimeTypeByData($i, $mtch[1], $type);
if ($i) {
$Image = new Image($i, $type);
*
* Note: Can produce a [bookmark] tag in the returned string
*
- * @param string $text
- * @param bool|int $simplehtml
- * @param bool $tryoembed
+ * @param string $text
+ * @param integer $simplehtml
+ * @param bool $tryoembed
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
- private static function convertAttachment($text, $simplehtml = false, $tryoembed = true)
+ private static function convertAttachment($text, $simplehtml = self::INTERNAL, $tryoembed = true)
{
$data = self::getAttachmentData($text);
if (empty($data) || empty($data['url'])) {
} catch (Exception $e) {
$data['title'] = ($data['title'] ?? '') ?: $data['url'];
- if ($simplehtml != 4) {
+ if ($simplehtml != self::CONNECTORS) {
$return = sprintf('<div class="type-%s">', $data['type']);
}
$return .= sprintf('<sup><a href="%s">%s</a></sup>', $data['url'], parse_url($data['url'], PHP_URL_HOST));
}
- if ($simplehtml != 4) {
+ if ($simplehtml != self::CONNECTORS) {
$return .= '</div>';
}
}
$mention = Protocol::formatMention($attributes['profile'], $attributes['author']);
switch ($simplehtml) {
- case 1:
- $text = ($is_quote_share? '<br />' : '') . '<p>' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' <a href="' . $attributes['profile'] . '">' . $mention . '</a>: </p>' . "\n" . '«' . $content . '»';
- break;
- case 2:
+ case self::API:
$text = ($is_quote_share? '<br />' : '') . '<p>' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ': </p>' . "\n" . $content;
break;
- case 3: // Diaspora
+ case self::DIASPORA:
if (stripos(Strings::normaliseLink($attributes['link']), 'http://twitter.com/') === 0) {
$text = ($is_quote_share? '<hr />' : '') . '<p><a href="' . $attributes['link'] . '">' . $attributes['link'] . '</a></p>' . "\n";
} else {
}
break;
- case 4:
+ case self::CONNECTORS:
$headline = '<p><b>' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8');
$headline .= DI::l10n()->t('<a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a> %3$s', $attributes['link'], $mention, $attributes['posted']);
$headline .= ':</b></p>' . "\n";
$text = ($is_quote_share? '<hr />' : '') . $headline . '<blockquote class="shared_content">' . trim($content) . '</blockquote>' . "\n";
break;
- case 5:
- $text = ($is_quote_share? '<br />' : '') . '<p>' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ': </p>' . "\n" . $content;
- break;
- case 7: // statusnet/GNU Social
+ case self::OSTATUS:
$text = ($is_quote_share? '<br />' : '') . '<p>' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' @' . $author_contact['addr'] . ': ' . $content . '</p>' . "\n";
break;
- case 9: // ActivityPub
+ case self::ACTIVITYPUB:
$author = '@<span class="vcard"><a href="' . $author_contact['url'] . '" class="url u-url mention" title="' . $author_contact['addr'] . '"><span class="fn nickname mention">' . $author_contact['addr'] . '</span></a>:</span>';
$text = '<div><a href="' . $attributes['link'] . '">' . html_entity_decode('♲', ENT_QUOTES, 'UTF-8') . '</a> ' . $author . '<blockquote>' . $content . '</blockquote></div>' . "\n";
break;
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
- public static function convert($text, $try_oembed = true, $simple_html = 0, $for_plaintext = false)
+ public static function convert($text, $try_oembed = true, $simple_html = self::INTERNAL, $for_plaintext = false)
{
$a = DI::app();
/// @todo Have a closer look at the different html modes
// Handle attached links or videos
- if (in_array($simple_html, [9])) {
+ if ($simple_html == self::ACTIVITYPUB) {
$text = self::removeAttachment($text);
- } elseif (!in_array($simple_html, [0, 4])) {
+ } elseif (!in_array($simple_html, [self::INTERNAL, self::CONNECTORS])) {
$text = self::removeAttachment($text, true);
} else {
$text = self::convertAttachment($text, $simple_html, $try_oembed);
// Check for sized text
// [size=50] --> font-size: 50px (with the unit).
- if ($simple_html != 3) {
+ if ($simple_html != self::DIASPORA) {
$text = preg_replace("(\[size=(\d*?)\](.*?)\[\/size\])ism", "<span style=\"font-size: $1px; line-height: initial;\">$2</span>", $text);
$text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])ism", "<span style=\"font-size: $1; line-height: initial;\">$2</span>", $text);
} else {
// Try to Oembed
if ($try_oembed) {
$text = preg_replace("/\[video\](.*?\.(ogg|ogv|oga|ogm|webm|mp4).*?)\[\/video\]/ism", '<video src="$1" controls="controls" width="' . $a->videowidth . '" height="' . $a->videoheight . '" loop="true"><a href="$1">$1</a></video>', $text);
- $text = preg_replace("/\[audio\](.*?\.(ogg|ogv|oga|ogm|webm|mp4|mp3).*?)\[\/audio\]/ism", '<audio src="$1" controls="controls"><a href="$1">$1</a></audio>', $text);
+ $text = preg_replace("/\[audio\](.*?)\[\/audio\]/ism", '<audio src="$1" controls="controls"><a href="$1">$1</a></audio>', $text);
$text = preg_replace_callback("/\[video\](.*?)\[\/video\]/ism", $try_oembed_callback, $text);
$text = preg_replace_callback("/\[audio\](.*?)\[\/audio\]/ism", $try_oembed_callback, $text);
// Replace non graphical smilies for external posts
if (!$nosmile && !$for_plaintext) {
- $text = Smilies::replace($text);
+ $oldtext = $text;
+ $text = Smilies::replace($text);
+ if (DI::config()->get('system', 'big_emojis') && ($simple_html != self::DIASPORA) && ($oldtext != $text)) {
+ $conv = html_entity_decode(str_replace([' ', "\n", "\r"], '', $text));
+ // Emojis are always 4 byte Unicode characters
+ if (strlen($conv) / mb_strlen($conv) == 4) {
+ $text = '<span style="font-size: xx-large; line-height: initial;">' . $text . '</span>';
+ }
+ }
}
if (!$for_plaintext) {
- if (in_array($simple_html, [7, 9])) {
+ 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);
}
$text = str_replace(["\r","\n"], ['<br />', '<br />'], $text);
// Remove all hashtag addresses
- if ($simple_html && !in_array($simple_html, [3, 7, 9])) {
+ 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 == 3) {
+ } elseif ($simple_html == self::DIASPORA) {
// The ! is converted to @ since Diaspora only understands the @
$text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
'@<a href="$2">$3</a>',
$text);
- } elseif (in_array($simple_html, [7, 9])) {
+ } elseif (in_array($simple_html, [self::OSTATUS, self::ACTIVITYPUB])) {
$text = preg_replace("/([@!])\[url\=(.*?)\](.*?)\[\/url\]/ism",
'$1<span class="vcard"><a href="$2" class="url u-url mention" title="$3"><span class="fn nickname mention">$3</span></a></span>',
$text);
$text = preg_replace("/#\[url\=.*?\]\^\[\/url\]\[url\=(.*?)\](.*?)\[\/url\]/i",
"[bookmark=$1]$2[/bookmark]", $text);
- if (in_array($simple_html, [2, 6, 7, 8])) {
+ 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("/[^#@!]\[url\=(.*?)\](.*?)\[\/url\]/ism", '[url]$1[/url]', $text);
- }
-
// Perform URL Search
if ($try_oembed) {
$text = preg_replace_callback("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", $try_oembed_callback, $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);
- }
+ $text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", '[url=$1]$2[/url]', $text);
// Handle Diaspora posts
$text = preg_replace_callback(
* - #[url=<anything>]<term>[/url]
* - [url=<anything>]#<term>[/url]
*/
- $text = preg_replace_callback("/(?:#\[url\=[^\[\]]*\]|\[url\=[^\[\]]*\]#)(.*?)\[\/url\]/ism", function($matches) {
- return '#<a href="'
- . DI::baseUrl() . '/search?tag=' . rawurlencode($matches[1])
- . '" class="tag" rel="tag" title="' . XML::escape($matches[1]) . '">'
- . XML::escape($matches[1])
- . '</a>';
+ $text = preg_replace_callback("/(?:#\[url\=[^\[\]]*\]|\[url\=[^\[\]]*\]#)(.*?)\[\/url\]/ism", function($matches) use ($simple_html) {
+ if ($simple_html == BBCode::ACTIVITYPUB) {
+ return '<a href="' . DI::baseUrl() . '/search?tag=' . rawurlencode($matches[1])
+ . '" data-tag="' . XML::escape($matches[1]) . '" rel="tag ugc">#'
+ . XML::escape($matches[1]) . '</a>';
+ } else {
+ return '#<a href="' . DI::baseUrl() . '/search?tag=' . rawurlencode($matches[1])
+ . '" class="tag" rel="tag" title="' . XML::escape($matches[1]) . '">'
+ . XML::escape($matches[1]) . '</a>';
+ }
}, $text);
// We need no target="_blank" rel="noopener noreferrer" for local links
// 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)) {
$text = $text . " " . $tagline;
}
} else {
- $text = self::convert($text, false, 4);
+ $text = self::convert($text, false, self::CONNECTORS);
}
// If a link is followed by a quote then there should be a newline before it
$ret = [];
// Convert hashtag links to hashtags
- $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2', $string);
+ $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2 ', $string);
// ignore anything in a code block
$string = preg_replace('/\[code.*?\].*?\[\/code\]/sm', '', $string);