Reworked media handling

This commit is contained in:
Michael 2021-04-26 06:50:12 +00:00
parent 5a8f202158
commit 8685e5ca32
14 changed files with 457 additions and 206 deletions

View file

@ -73,7 +73,7 @@ class PageInfo
// Additional link attachments are prepended before the existing [attachment] tag
$body = substr_replace($body, "\n[bookmark=" . $data['url'] . ']' . $linkTitle . "[/bookmark]\n", $existingAttachmentPos, 0);
} else {
$footer = PageInfo::getFooterFromData($data, $no_photos);
$footer = self::getFooterFromData($data, $no_photos);
$body = self::stripTrailingUrlFromBody($body, $data['url']);
$body .= "\n" . $footer;
}
@ -155,7 +155,7 @@ class PageInfo
if (empty($data['text'])) {
$data['text'] = $data['title'];
}
if (empty($data['text'])) {
$data['text'] = $data['url'];
}
@ -246,7 +246,7 @@ class PageInfo
* @param bool $searchNakedUrls Whether we should pick a naked URL (outside of BBCode tags) as a last resort
* @return string|null
*/
protected static function getRelevantUrlFromBody(string $body, bool $searchNakedUrls = false)
public static function getRelevantUrlFromBody(string $body, bool $searchNakedUrls = false)
{
$URLSearchString = 'https?://[^\[\]]*';

View file

@ -50,7 +50,7 @@ use Friendica\Util\XML;
class BBCode
{
// Update this value to the current date whenever changes are made to BBCode::convert
const VERSION = '2021-04-07';
const VERSION = '2021-04-24';
const INTERNAL = 0;
const API = 2;
@ -61,6 +61,7 @@ class BBCode
const BACKLINK = 8;
const ACTIVITYPUB = 9;
const ANCHOR = '<br class="anchor">';
/**
* Fetches attachment data that were generated the old way
*
@ -155,6 +156,7 @@ class BBCode
'image' => null,
'url' => '',
'author_name' => '',
'author_url' => '',
'provider_name' => '',
'provider_url' => '',
'title' => '',
@ -172,7 +174,7 @@ class BBCode
foreach (['type', 'url', 'title', 'image', 'preview', 'publisher_name', 'publisher_url', 'author_name', 'author_url'] as $field) {
preg_match('/' . preg_quote($field, '/') . '=("|\')(.*?)\1/ism', $attributes, $matches);
$value = $matches[2] ?? '';
if ($value != '') {
switch ($field) {
case 'publisher_name':
@ -939,6 +941,26 @@ class BBCode
return $newbody;
}
/**
*
* @param string $text A BBCode string
* @return array share attributes
*/
public static function fetchShareAttributes($text)
{
$attributes = [];
if (!preg_match("/(.*?)\[share(.*?)\](.*)\[\/share\]/ism", $text, $matches)) {
return $attributes;
}
$attribute_string = $matches[2];
foreach (['author', 'profile', 'avatar', 'link', 'posted', 'guid'] as $field) {
preg_match("/$field=(['\"])(.+?)\\1/ism", $attribute_string, $matches);
$attributes[$field] = html_entity_decode($matches[2] ?? '', ENT_QUOTES, 'UTF-8');
}
return $attributes;
}
/**
* This function converts a [share] block to text according to a provided callback function whose signature is:
*
@ -1064,7 +1086,7 @@ class BBCode
'$guid' => $attributes['guid'],
'$network_name' => ContactSelector::networkToName($network, $attributes['profile']),
'$network_icon' => ContactSelector::networkToIcon($network, $attributes['profile']),
'$content' => self::setMentions(trim($content), 0, $network),
'$content' => self::setMentions(trim($content), 0, $network) . self::ANCHOR,
]);
break;
}
@ -1102,7 +1124,7 @@ class BBCode
DI::cache()->set($cache_key, $text);
return $text;
}
$doc = new DOMDocument();
@$doc->loadHTML($body);
$xpath = new DOMXPath($doc);