use Friendica\Model\Photo;
use Friendica\Model\Post;
use Friendica\Model\Tag;
+use Friendica\Network\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]);
if (!$curlResult->isSuccess()) {
continue;
}
$i = $curlResult->getBody();
- $contType = $curlResult->getContentType();
- $type = Images::getMimeTypeByData($i, $mtch[1], $contType);
+ $type = $curlResult->getContentType();
+ $type = Images::getMimeTypeByData($i, $mtch[1], $type);
if ($i) {
$Image = new Image($i, $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']);
}
}
$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]);
$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');
+ $mimetype = $curlResult->getHeader('Content-Type')[0] ?? '';
} else {
$mimetype = '';
}
$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]);
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');
+ $mimetype = $curlResult->getHeader('Content-Type')[0] ?? '';
} else {
$mimetype = '';
}
}
// 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]);
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("/\[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);
}
$addon = strtolower($addon);
if (preg_match_all("/\[abstract=(.*?)\](.*?)\[\/abstract\]/ism", $text, $results, PREG_SET_ORDER)) {
- foreach ($results AS $result) {
+ foreach ($results as $result) {
$abstracts[strtolower($result[1])] = $result[2];
}
}
}
/**
- * 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 '#':
+ default:
return $match[1] . '[url=' . 'https://' . DI::baseUrl() . '/search?tag=' . $match[2] . ']' . $match[2] . '[/url]';
}
}, $body);