From dddcddc453bdbd59e1cafcb8ca8aeb2225dfda9d Mon Sep 17 00:00:00 2001 From: Mario Date: Fri, 15 Mar 2024 11:30:28 +0000 Subject: refactor sodium b2b encryption --- include/bbcode.php | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'include') diff --git a/include/bbcode.php b/include/bbcode.php index b39822b05..20a866073 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -246,38 +246,45 @@ function bb_replace_images($body, $images) { function bb_parse_crypt($match) { $matches = []; - $attributes = $match[1]; $hint = ''; $algorithm = ''; + $payload = $match[1]; + + if (isset($match[2])) { + // backwards compatibility - preg_match("/alg='(.*?)'/ism", $attributes, $matches); - $algorithm = $matches[1] ?? ''; + $attributes = $match[1]; + $payload = $match[2]; - if (!$algorithm) { - preg_match("/alg=\"\;(.*?)\"\;/ism", $attributes, $matches); + preg_match("/alg='(.*?)'/ism", $attributes, $matches); $algorithm = $matches[1] ?? ''; - } - preg_match("/hint='(.*?)'/ism", $attributes, $matches); - $hint = $matches[1] ?? ''; + if (!$algorithm) { + preg_match("/alg=\"\;(.*?)\"\;/ism", $attributes, $matches); + $algorithm = $matches[1] ?? ''; + } - if (!$hint) { - preg_match("/hint=\"\;(.*?)\"\;/ism", $attributes, $matches); + preg_match("/hint='(.*?)'/ism", $attributes, $matches); $hint = $matches[1] ?? ''; + + if (!$hint) { + preg_match("/hint=\"\;(.*?)\"\;/ism", $attributes, $matches); + $hint = $matches[1] ?? ''; + } } - $x = random_string(); + $x = random_string(32); - $f = 'sodium_decrypt'; + $onclick = 'onclick="sodium_decrypt(\'' . $payload . '\',\'#' . $x . '\');"'; if (in_array($algorithm, ['AES-128-CCM', 'rot13', 'triple-rot13'])) { - $f = 'hz_decrypt'; // deprecated + // backwards compatibility + $onclick = 'onclick="hz_decrypt(\'' . $algorithm . '\',\'' . $hint . '\',\'' . $payload . '\',\'#' . $x . '\');"'; } - $onclick = 'onclick="' . $f . '(\'' . $algorithm . '\',\'' . $hint . '\',\'' . $match[2] . '\',\'#' . $x . '\');"'; $label = t('Encrypted content'); - $text = '
' . $label . '

'; + $text = '
' . $label . '
'; return $text; } @@ -1627,8 +1634,7 @@ function bbcode($text, $options = []) { // crypt if (strpos($text,'[/crypt]') !== false) { - $x = random_string(); - $text = preg_replace("/\[crypt\](.*?)\[\/crypt\]/ism",'
' . t('Encrypted content') . '
', $text); + $text = preg_replace_callback("/\[crypt\](.*?)\[\/crypt\]/ism", 'bb_parse_crypt', $text); $text = preg_replace_callback("/\[crypt (.*?)\](.*?)\[\/crypt\]/ism", 'bb_parse_crypt', $text); } -- cgit v1.2.3