Merge pull request #10553 from annando/issue-10545

Issue 10545: Convert complex zmg BBCode elements
This commit is contained in:
Hypolite Petovan 2021-07-28 06:37:52 -04:00 committed by GitHub
commit f89cc6bd45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -51,7 +51,7 @@ use Friendica\Util\XML;
class BBCode class BBCode
{ {
// Update this value to the current date whenever changes are made to BBCode::convert // Update this value to the current date whenever changes are made to BBCode::convert
const VERSION = '2021-07-13'; const VERSION = '2021-07-28';
const INTERNAL = 0; const INTERNAL = 0;
const EXTERNAL = 1; const EXTERNAL = 1;
@ -1067,6 +1067,43 @@ class BBCode
return $return; return $return;
} }
/**
* Convert complex IMG and ZMG elements
*
* @param [type] $text
* @param integer $simplehtml
* @param integer $uriid
* @return string
*/
private static function convertImages(string $text, int $simplehtml, int $uriid = 0):string
{
DI::profiler()->startRecording('rendering');
$return = preg_replace_callback(
"/\[[zi]mg(.*?)\]([^\[\]]*)\[\/[zi]mg\]/ism",
function ($match) use ($simplehtml, $uriid) {
$attribute_string = $match[1];
$attributes = [];
foreach (['alt', 'width', 'height'] as $field) {
preg_match("/$field=(['\"])(.+?)\\1/ism", $attribute_string, $matches);
$attributes[$field] = html_entity_decode($matches[2] ?? '', ENT_QUOTES, 'UTF-8');
}
$img_str = '<img src="' .
self::proxyUrl($match[2], $simplehtml, $uriid) . '"';
foreach ($attributes as $key => $value) {
if (!empty($value)) {
$img_str .= ' ' . $key . '="' . htmlspecialchars($value, ENT_COMPAT) . '"';
}
}
return $img_str . '>';
},
$text
);
DI::profiler()->stopRecording();
return $return;
}
/** /**
* Default [share] tag conversion callback * Default [share] tag conversion callback
* *
@ -1730,6 +1767,8 @@ class BBCode
$text = preg_replace("/\[img\](.*?)\[\/img\]/ism", '<img src="$1" alt="' . DI::l10n()->t('Image/photo') . '" />', $text); $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 = 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("/\[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("/\[crypt(.*?)\](.*?)\[\/crypt\]/ism", '<br><img src="' .DI::baseUrl() . '/images/lock_icon.gif" alt="' . DI::l10n()->t('Encrypted content') . '" title="' . '$1' . ' ' . DI::l10n()->t('Encrypted content') . '" /><br>', $text); $text = preg_replace("/\[crypt(.*?)\](.*?)\[\/crypt\]/ism", '<br><img src="' .DI::baseUrl() . '/images/lock_icon.gif" alt="' . DI::l10n()->t('Encrypted content') . '" title="' . '$1' . ' ' . DI::l10n()->t('Encrypted content') . '" /><br>', $text);
//$Text = preg_replace("/\[crypt=(.*?)\](.*?)\[\/crypt\]/ism", '<br><img src="' .DI::baseUrl() . '/images/lock_icon.gif" alt="' . DI::l10n()->t('Encrypted content') . '" title="' . '$1' . ' ' . DI::l10n()->t('Encrypted content') . '" /><br>', $Text); //$Text = preg_replace("/\[crypt=(.*?)\](.*?)\[\/crypt\]/ism", '<br><img src="' .DI::baseUrl() . '/images/lock_icon.gif" alt="' . DI::l10n()->t('Encrypted content') . '" title="' . '$1' . ' ' . DI::l10n()->t('Encrypted content') . '" /><br>', $Text);