From af745b72ae9144f8a2393511747b7570d4d14f8b Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Mon, 15 Aug 2011 15:56:01 +0200 Subject: [PATCH] Partial fix of the api_strrpos() function when using PHP 5.1 #3799 --- main/inc/lib/internationalization.lib.php | 20 ++++++++++++++------ main/inc/lib/main_api.lib.php | 7 +++++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/main/inc/lib/internationalization.lib.php b/main/inc/lib/internationalization.lib.php index 8684d4b498..afb3270707 100755 --- a/main/inc/lib/internationalization.lib.php +++ b/main/inc/lib/internationalization.lib.php @@ -1945,6 +1945,7 @@ function api_strripos($haystack, $needle, $offset = 0, $encoding = null) { * @link http://php.net/manual/en/function.mb-strrpos */ function api_strrpos($haystack, $needle, $offset = 0, $encoding = null) { + if (empty($encoding)) { $encoding = _api_mb_internal_encoding(); } @@ -1964,15 +1965,22 @@ function api_strrpos($haystack, $needle, $offset = 0, $encoding = null) { } if (_api_mb_supports($encoding) && IS_PHP_52) { return @mb_strrpos($haystack, $needle, $offset, $encoding); - } - elseif (api_is_encoding_supported($encoding)) { + } elseif (api_is_encoding_supported($encoding)) { + if (!api_is_utf8($encoding)) { - $haystack = api_utf8_encode($haystack, $encoding); - $needle = api_utf8_encode($needle, $encoding); - } + $haystack = api_utf8_encode($haystack, $encoding); + $needle = api_utf8_encode($needle, $encoding); + } + // In PHP 5.1 the $offset parameter didn't exist see http://php.net/manual/en/function.mb-strrpos.php + if (MBSTRING_INSTALLED && IS_PHP_51) { + //return @mb_strrpos($haystack, $needle, $offset, 'UTF-8'); + //@todo fix the missing $offset parameter + return @mb_strrpos($haystack, $needle, 'UTF-8'); + } if (MBSTRING_INSTALLED && IS_PHP_52) { - return @mb_strrpos($haystack, $needle, $offset, 'UTF-8'); + return @mb_strrpos($haystack, $needle, $offset, 'UTF-8'); } + // This branch (this fragment of code) is an adaptation from the CakePHP(tm) Project, http://www.cakefoundation.org $found = false; $haystack = _api_utf8_to_unicode($haystack); diff --git a/main/inc/lib/main_api.lib.php b/main/inc/lib/main_api.lib.php index 56a62791a9..f0b374518b 100755 --- a/main/inc/lib/main_api.lib.php +++ b/main/inc/lib/main_api.lib.php @@ -156,8 +156,11 @@ define('USERNAME_PURIFIER', '/[^0-9A-Za-z_\.]/'); define('USERNAME_PURIFIER_SHALLOW', '/\s/'); // Constants for detection some important PHP5 subversions. -define('IS_PHP_52', !((float)PHP_VERSION < 5.2)); -define('IS_PHP_53', !((float)PHP_VERSION < 5.3)); +$php_version = (float) PHP_VERSION; + +define('IS_PHP_53', ($php_version >= 5.3)); +define('IS_PHP_52', ($php_version >= 5.2 && !IS_PHP_53)); +define('IS_PHP_51', ($php_version >= 5.1 && !IS_PHP_52 && !IS_PHP_53)); // This constant is a result of Windows OS detection, it has a boolean value: // true whether the server runs on Windows OS, false otherwise.