<?php
/**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
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 as ProxyUtils;
+use Friendica\Util\Proxy;
use Friendica\Util\Strings;
use Friendica\Util\XML;
}
}
} elseif (count($pictures) > 0) {
- $post['type'] = 'link';
- $post['url'] = $plink;
+ if (count($pictures) > 4) {
+ $post['type'] = 'link';
+ $post['url'] = $plink;
+ } else {
+ $post['type'] = 'photo';
+ }
+
$post['image'] = $pictures[0][2];
$post['text'] = $body;
public static function removeAttachment($body, $no_link_desc = false)
{
return preg_replace_callback("/\s*\[attachment (.*?)\](.*?)\[\/attachment\]\s*/ism",
- function ($match) use ($no_link_desc) {
+ function ($match) use ($body, $no_link_desc) {
$attach_data = self::getAttachmentData($match[0]);
if (empty($attach_data['url'])) {
return $match[0];
+ } elseif (strpos(str_replace($match[0], '', $body), $attach_data['url']) !== false) {
+ return '';
} elseif (empty($attach_data['title']) || $no_link_desc) {
return " \n[url]" . $attach_data['url'] . "[/url]\n";
} else {
} elseif ($uriid > 0) {
return Post\Link::getByLink($uriid, $image, $size);
} else {
- return ProxyUtils::proxifyUrl($image, $size);
+ return Proxy::proxifyUrl($image, $size);
}
}
continue;
}
- $curlResult = DI::httpRequest()->get($mtch[1]);
+ $curlResult = DI::httpClient()->get($mtch[1], HttpClientAccept::IMAGE);
if (!$curlResult->isSuccess()) {
continue;
}
+ Logger::debug('Got picture', ['Content-Type' => $curlResult->getHeader('Content-Type'), 'url' => $mtch[1]]);
+
$i = $curlResult->getBody();
$type = $curlResult->getContentType();
$type = Images::getMimeTypeByData($i, $mtch[1], $type);
} elseif (!empty($data['preview'])) {
$return .= sprintf('<a href="%s" target="_blank" rel="noopener noreferrer"><img src="%s" alt="" title="%s" class="attachment-preview" /></a><br>', $data['url'], self::proxyUrl($data['preview'], $simplehtml, $uriid), $data['title']);
}
- $return .= sprintf('<h4><a href="%s">%s</a></h4>', $data['url'], $data['title']);
+ $return .= sprintf('<h4><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></h4>', $data['url'], $data['title']);
}
}
if (!empty($data['provider_url']) && !empty($data['provider_name'])) {
if (!empty($data['author_name'])) {
- $return .= sprintf('<sup><a href="%s">%s (%s)</a></sup>', $data['provider_url'], $data['author_name'], $data['provider_name']);
+ $return .= sprintf('<sup><a href="%s" target="_blank" rel="noopener noreferrer">%s (%s)</a></sup>', $data['provider_url'], $data['author_name'], $data['provider_name']);
} else {
- $return .= sprintf('<sup><a href="%s">%s</a></sup>', $data['provider_url'], $data['provider_name']);
+ $return .= sprintf('<sup><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></sup>', $data['provider_url'], $data['provider_name']);
}
}
$author_contact = Contact::getByURL($attributes['profile'], false, ['id', 'url', 'addr', 'name', 'micro']);
$author_contact['url'] = ($author_contact['url'] ?? $attributes['profile']);
- $author_contact['addr'] = ($author_contact['addr'] ?? '') ?: Protocol::getAddrFromProfileUrl($attributes['profile']);
+ $author_contact['addr'] = ($author_contact['addr'] ?? '');
$attributes['author'] = ($author_contact['name'] ?? '') ?: $attributes['author'];
$attributes['avatar'] = ($author_contact['micro'] ?? '') ?: $attributes['avatar'];
$attributes['profile'] = ($author_contact['url'] ?? '') ?: $attributes['profile'];
if (!empty($author_contact['id'])) {
- $attributes['avatar'] = Contact::getAvatarUrlForId($author_contact['id'], ProxyUtils::SIZE_THUMB);
+ $attributes['avatar'] = Contact::getAvatarUrlForId($author_contact['id'], Proxy::SIZE_THUMB);
} elseif ($attributes['avatar']) {
- $attributes['avatar'] = self::proxyUrl($attributes['avatar'], self::INTERNAL, $uriid, ProxyUtils::SIZE_THUMB);
+ $attributes['avatar'] = self::proxyUrl($attributes['avatar'], self::INTERNAL, $uriid, Proxy::SIZE_THUMB);
}
$content = preg_replace(Strings::autoLinkRegEx(), '<a href="$1">$1</a>', $match[3]);
$attributes[$field] = html_entity_decode($matches[2] ?? '', ENT_QUOTES, 'UTF-8');
}
- $img_str = '<img src="' .
+ $img_str = '<img src="' .
self::proxyUrl($match[2], $simplehtml, $uriid) . '"';
foreach ($attributes as $key => $value) {
if (!empty($value)) {
private static function convertShareCallback(array $attributes, array $author_contact, $content, $is_quote_share, $simplehtml)
{
DI::profiler()->startRecording('rendering');
- $mention = Protocol::formatMention($attributes['profile'], $attributes['author']);
+ $mention = $attributes['author'] . ' (' . ($author_contact['addr'] ?? '') . ')';
switch ($simplehtml) {
case self::API:
$text = ($is_quote_share? '<br>' : '') .
- '<b><a href="' . $attributes['link'] . '">' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . "</a>:</b><br>\n" .
- '<blockquote class="shared_content">' . $content . '</blockquote>';
+ '<b><a href="' . $attributes['link'] . '">' . html_entity_decode('♲', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . "</a>:</b><br>\n" .
+ '<blockquote class="shared_content" dir="auto">' . $content . '</blockquote>';
break;
case self::DIASPORA:
if (stripos(Strings::normaliseLink($attributes['link']), 'http://twitter.com/') === 0) {
$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";
+ $text = ($is_quote_share? '<hr />' : '') . $headline . '<blockquote class="shared_content" dir="auto">' . trim($content) . '</blockquote>' . "\n";
break;
case self::OSTATUS:
$text = DI::cache()->get($cache_key);
if (is_null($text)) {
- $curlResult = DI::httpRequest()->head($match[1], ['timeout' => DI::config()->get('system', 'xrd_timeout')]);
+ $curlResult = DI::httpClient()->head($match[1], [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]);
if ($curlResult->isSuccess()) {
$mimetype = $curlResult->getHeader('Content-Type')[0] ?? '';
} else {
$text = "[url=" . $match[2] . ']' . $match[2] . "[/url]";
// if its not a picture then look if its a page that contains a picture link
- $body = DI::httpRequest()->fetch($match[1]);
+ $body = DI::httpClient()->fetch($match[1], HttpClientAccept::HTML, 0);
if (empty($body)) {
DI::cache()->set($cache_key, $text);
return $text;
return $text;
}
- $curlResult = DI::httpRequest()->head($match[1], ['timeout' => DI::config()->get('system', 'xrd_timeout')]);
+ $curlResult = DI::httpClient()->head($match[1], [HttpClientOptions::TIMEOUT => DI::config()->get('system', 'xrd_timeout')]);
if ($curlResult->isSuccess()) {
$mimetype = $curlResult->getHeader('Content-Type')[0] ?? '';
} else {
}
// if its not a picture then look if its a page that contains a picture link
- $body = DI::httpRequest()->fetch($match[1]);
+ $body = DI::httpClient()->fetch($match[1], HttpClientAccept::HTML, 0);
if (empty($body)) {
DI::cache()->set($cache_key, $text);
return $text;
$text = self::convertAttachment($text, $simple_html, $try_oembed, [], $uriid);
}
- // Add HTML new lines
- $text = str_replace("\n", '<br>', $text);
-
$nosmile = strpos($text, '[nosmile]') !== false;
$text = str_replace('[nosmile]', '', $text);
// Check for list text
$text = str_replace("[*]", "<li>", $text);
- // Check for style sheet commands
+ // Check for block-level custom CSS
+ $text = preg_replace('#(?<=^|\n)\[style=(.*?)](.*?)\[/style](?:\n|$)#ism', '<div style="$1">$2</div>', $text);
+
+ // Check for inline custom CSS
$text = preg_replace("(\[style=(.*?)\](.*?)\[\/style\])ism", '<span style="$1">$2</span>', $text);
+ // Mastodon Emoji (internal tag, do not document for users)
+ $text = preg_replace("(\[emoji=(.*?)](.*?)\[/emoji])ism", '<span class="mastodon emoji"><img src="$1" alt="$2" title="$2"/></span>', $text);
+
// Check for CSS classes
+ // @deprecated since 2021.12, left for backward-compatibility reasons
$text = preg_replace("(\[class=(.*?)\](.*?)\[\/class\])ism", '<span class="$1">$2</span>', $text);
+ // Add HTML new lines
+ $text = str_replace("\n", '<br>', $text);
// handle nested lists
$endlessloop = 0;
$text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '<img src="$1" alt="' . DI::l10n()->t('Image/photo') . '" />', $text);
$text = preg_replace("/\[zmg\](.*?)\[\/zmg\]/ism", '<img src="$1" alt="' . DI::l10n()->t('Image/photo') . '" />', $text);
-
+
$text = self::convertImages($text, $simple_html, $uriid);
$text = preg_replace("/\[crypt\](.*?)\[\/crypt\]/ism", '<br><img src="' .DI::baseUrl() . '/images/lock_icon.gif" alt="' . DI::l10n()->t('Encrypted content') . '" title="' . DI::l10n()->t('Encrypted content') . '" /><br>', $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);
}
} else {
$text = preg_replace("/([#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '$1$3', $text);
}
-
+
if (!$for_plaintext) {
if (in_array($simple_html, [self::OSTATUS, self::API, self::ACTIVITYPUB])) {
$text = preg_replace_callback("/\[url\](.*?)\[\/url\]/ism", 'self::convertUrlForActivityPubCallback', $text);
* @param string $text The text with BBCode
* @return string The same text - but without "abstract" element
*/
- public static function stripAbstract($text)
+ public static function stripAbstract(string $text): string
{
DI::profiler()->startRecording('rendering');
- $text = preg_replace("/[\s|\n]*\[abstract\].*?\[\/abstract\][\s|\n]*/ism", '', $text);
- $text = preg_replace("/[\s|\n]*\[abstract=.*?\].*?\[\/abstract][\s|\n]*/ism", '', $text);
+
+ $text = BBCode::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;
+ });
DI::profiler()->stopRecording();
return $text;
/**
* Returns the value of the "abstract" element
*
- * @param string $text The text that maybe contains the 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(string $text, string $addon = ''): string
{
DI::profiler()->startRecording('rendering');
- $abstract = '';
- $abstracts = [];
$addon = strtolower($addon);
- if (preg_match_all("/\[abstract=(.*?)\](.*?)\[\/abstract\]/ism", $text, $results, PREG_SET_ORDER)) {
- foreach ($results AS $result) {
- $abstracts[strtolower($result[1])] = $result[2];
+ $abstract = BBCode::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];
}
- }
- if (isset($abstracts[$addon])) {
- $abstract = $abstracts[$addon];
- }
+ if (preg_match("#\[abstract](.*?)\[/abstract]#ism", $text, $matches)) {
+ return $matches[1];
+ }
- if ($abstract == '' && preg_match("/\[abstract\](.*?)\[\/abstract\]/ism", $text, $result)) {
- $abstract = $result[1];
- }
+ return '';
+ });
DI::profiler()->stopRecording();
return $abstract;
}
/**
- * Expand tags to URLs
+ * Expand tags to URLs, checks the tag is at the start of a line or preceded by a non-word character
*
* @param string $body
* @return string body with expanded tags
*/
public static function expandTags(string $body)
{
- return preg_replace_callback("/([!#@])([^\^ \x0D\x0A,;:?\']*[^\^ \x0D\x0A,;:?!\'.])/",
+ return preg_replace_callback("/(?<=\W|^)([!#@])([^\^ \x0D\x0A,;:?'\"]*[^\^ \x0D\x0A,;:?!'\".])/",
function ($match) {
switch ($match[1]) {
case '!':
}
break;
case '#':
- return $match[1] . '[url=' . 'https://' . DI::baseUrl() . '/search?tag=' . $match[2] . ']' . $match[2] . '[/url]';
+ default:
+ return $match[1] . '[url=' . DI::baseUrl() . '/search?tag=' . $match[2] . ']' . $match[2] . '[/url]';
}
}, $body);
}
* @param array $tagList A list of tag names, e.g ['noparse', 'nobb', 'pre']
* @param callable $callback
* @return string
- * @throws Exception
- *@see Strings::performWithEscapedBlocks
- *
+ * @see Strings::performWithEscapedBlocks
*/
- public static function performWithEscapedTags(string $text, array $tagList, callable $callback)
+ public static function performWithEscapedTags(string $text, array $tagList, callable $callback): string
{
$tagList = array_map('preg_quote', $tagList);
}
/**
- * Replaces mentions in the provided message body for the provided user and network if any
+ * Replaces mentions in the provided message body in BBCode links for the provided user and network if any
*
* @param $body
* @param $profile_uid
public static function setMentions($body, $profile_uid = 0, $network = '')
{
DI::profiler()->startRecording('rendering');
- self::performWithEscapedTags($body, ['noparse', 'pre', 'code', 'img'], function ($body) use ($profile_uid, $network) {
+ $body = self::performWithEscapedTags($body, ['noparse', 'pre', 'code', 'img'], function ($body) use ($profile_uid, $network) {
$tags = self::getTags($body);
$tagged = [];
- $inform = '';
foreach ($tags as $tag) {
$tag_type = substr($tag, 0, 1);
}
}
- if (($success = Item::replaceTag($body, $inform, $profile_uid, $tag, $network)) && $success['replaced']) {
+ if (($success = Item::replaceTag($body, $profile_uid, $tag, $network)) && $success['replaced']) {
$tagged[] = $tag;
}
}