diff options
| author | Simon Wisselink <wisskid@users.noreply.github.com> | 2022-07-19 22:55:07 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-19 22:55:07 +0200 |
| commit | 20a8026ccd6970432a789dfe2941655b4d7e2617 (patch) | |
| tree | 820b4ffb68d094cd2f34702149888cde1c1cdb6a /libs | |
| parent | 27910bf2c1ee69e14eeaa54585cd2f9ccce7e38b (diff) | |
| download | smarty-20a8026ccd6970432a789dfe2941655b4d7e2617.tar.gz smarty-20a8026ccd6970432a789dfe2941655b4d7e2617.tar.bz2 smarty-20a8026ccd6970432a789dfe2941655b4d7e2617.zip | |
Merge branch 'fix-issue-549-v3' of github.com:AnrDaemon/smarty into AnrDaemon-fix-issue-549-v3 (#771)
Diffstat (limited to 'libs')
| -rw-r--r-- | libs/plugins/shared.mb_str_replace.php | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/libs/plugins/shared.mb_str_replace.php b/libs/plugins/shared.mb_str_replace.php index 226d9035..c6079c12 100644 --- a/libs/plugins/shared.mb_str_replace.php +++ b/libs/plugins/shared.mb_str_replace.php @@ -44,9 +44,43 @@ if (!function_exists('smarty_mb_str_replace')) { } } } else { + $mb_reg_charset = mb_regex_encoding(); + // Check if mbstring regex is using UTF-8 + $reg_is_unicode = !strcasecmp($mb_reg_charset, "UTF-8"); + if(!$reg_is_unicode) { + // ...and set to UTF-8 if not + mb_regex_encoding("UTF-8"); + } + + // See if charset used by Smarty is matching one used by regex... + $current_charset = mb_regex_encoding(); + $convert_result = (bool)strcasecmp(Smarty::$_CHARSET, $current_charset); + if($convert_result) { + // ...convert to it if not. + $subject = mb_convert_encoding($subject, $current_charset, Smarty::$_CHARSET); + $search = mb_convert_encoding($search, $current_charset, Smarty::$_CHARSET); + $replace = mb_convert_encoding($replace, $current_charset, Smarty::$_CHARSET); + } + $parts = mb_split(preg_quote($search), $subject) ?: array(); + // If original regex encoding was not unicode... + if(!$reg_is_unicode) { + // ...restore original regex encoding to avoid breaking the system. + mb_regex_encoding($mb_reg_charset); + } + if($parts === false) { + // This exception is thrown if call to mb_split failed. + // Usually it happens, when $search or $replace are not valid for given mb_regex_encoding(). + // There may be other cases for it to fail, please file an issue if you find a reproducible one. + throw new SmartyException("Source string is not a valid $current_charset sequence (probably)"); + } + $count = count($parts) - 1; $subject = implode($replace, $parts); + // Convert results back to charset used by Smarty, if needed. + if($convert_result) { + $subject = mb_convert_encoding($subject, Smarty::$_CHARSET, $current_charset); + } } return $subject; } |
