diff options
Diffstat (limited to 'phpBB/includes/utf/utf_tools.php')
| -rw-r--r-- | phpBB/includes/utf/utf_tools.php | 207 | 
1 files changed, 110 insertions, 97 deletions
| diff --git a/phpBB/includes/utf/utf_tools.php b/phpBB/includes/utf/utf_tools.php index 6f3ac93305..e60a40a195 100644 --- a/phpBB/includes/utf/utf_tools.php +++ b/phpBB/includes/utf/utf_tools.php @@ -1,10 +1,13 @@  <?php  /**  * -* @package utf -* @version $Id$ -* @copyright (c) 2006 phpBB Group -* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file.  *  */ @@ -24,7 +27,6 @@ setlocale(LC_CTYPE, 'C');  * Whenever possible, these functions will try to use PHP's built-in functions or  * extensions, otherwise they will default to custom routines.  * -* @package utf  */  if (!extension_loaded('xml')) @@ -109,70 +111,26 @@ if (extension_loaded('mbstring'))  	/**  	* UTF-8 aware alternative to strrpos  	* Find position of last occurrence of a char in a string -	* -	* Notes: -	* - offset for mb_strrpos was added in 5.2.0, we emulate if it is lower  	*/ -	if (version_compare(PHP_VERSION, '5.2.0', '>=')) +	/** +	* UTF-8 aware alternative to strrpos +	* @ignore +	*/ +	function utf8_strrpos($str,	$needle, $offset = null)  	{ -		/** -		* UTF-8 aware alternative to strrpos -		* @ignore -		*/ -		function utf8_strrpos($str,	$needle, $offset = null) +		// Emulate behaviour of strrpos rather than raising warning +		if (empty($str))  		{ -			// Emulate behaviour of strrpos rather than raising warning -			if (empty($str)) -			{ -				return false; -			} +			return false; +		} -			if (is_null($offset)) -			{ -				return mb_strrpos($str, $needle); -			} -			else -			{ -				return mb_strrpos($str, $needle, $offset); -			} +		if (is_null($offset)) +		{ +			return mb_strrpos($str, $needle);  		} -	} -	else -	{ -		/** -		* UTF-8 aware alternative to strrpos -		* @ignore -		*/ -		function utf8_strrpos($str,	$needle, $offset = null) +		else  		{ -			// offset for mb_strrpos was added in 5.2.0 -			if (is_null($offset)) -			{ -				// Emulate behaviour of strrpos rather than raising warning -				if (empty($str)) -				{ -					return false; -				} - -				return mb_strrpos($str, $needle); -			} -			else -			{ -				if (!is_int($offset)) -				{ -					trigger_error('utf8_strrpos expects parameter 3 to be long', E_USER_ERROR); -					return false; -				} - -				$str = mb_substr($str, $offset); - -				if (false !== ($pos = mb_strrpos($str, $needle))) -				{ -					return $pos + $offset; -				} - -				return false; -			} +			return mb_strrpos($str, $needle, $offset);  		}  	} @@ -576,7 +534,7 @@ else  					return '';  				} -				$lx = (int)((-$length) / 65535); +				$lx = (int) ((-$length) / 65535);  				$ly = (-$length) % 65535;  				// negative length requires ... capture everything @@ -1756,49 +1714,106 @@ function utf8_case_fold_nfc($text, $option = 'full')  	return $text;  } -/** -* A wrapper function for the normalizer which takes care of including the class if required and modifies the passed strings -* to be in NFC (Normalization Form Composition). -* -* @param	mixed	$strings	a string or an array of strings to normalize -* @return	mixed				the normalized content, preserving array keys if array given. -*/ -function utf8_normalize_nfc($strings) +if (extension_loaded('intl'))  { -	if (empty($strings)) +	/** +	* wrapper around PHP's native normalizer from intl +	* previously a PECL extension, included in the core since PHP 5.3.0 +	* http://php.net/manual/en/normalizer.normalize.php +	* +	* @param	mixed	$strings	a string or an array of strings to normalize +	* @return	mixed				the normalized content, preserving array keys if array given. +	*/ +	function utf8_normalize_nfc($strings)  	{ -		return $strings; -	} +		if (empty($strings)) +		{ +			return $strings; +		} -	if (!class_exists('utf_normalizer')) -	{ -		global $phpbb_root_path, $phpEx; -		include($phpbb_root_path . 'includes/utf/utf_normalizer.' . $phpEx); -	} +		if (!is_array($strings)) +		{ +			if (Normalizer::isNormalized($strings)) +			{ +				return $strings; +			} +			return (string) Normalizer::normalize($strings); +		} +		else +		{ +			foreach ($strings as $key => $string) +			{ +				if (is_array($string)) +				{ +					foreach ($string as $_key => $_string) +					{ +						if (Normalizer::isNormalized($strings[$key][$_key])) +						{ +							continue; +						} +						$strings[$key][$_key] = (string) Normalizer::normalize($strings[$key][$_key]); +					} +				} +				else +				{ +					if (Normalizer::isNormalized($strings[$key])) +					{ +						continue; +					} +					$strings[$key] = (string) Normalizer::normalize($strings[$key]); +				} +			} +		} -	if (!is_array($strings)) -	{ -		utf_normalizer::nfc($strings); +		return $strings;  	} -	else if (is_array($strings)) +} +else +{ +	/** +	* A wrapper function for the normalizer which takes care of including the class if +	* required and modifies the passed strings to be in NFC (Normalization Form Composition). +	* +	* @param	mixed	$strings	a string or an array of strings to normalize +	* @return	mixed				the normalized content, preserving array keys if array given. +	*/ +	function utf8_normalize_nfc($strings)  	{ -		foreach ($strings as $key => $string) +		if (empty($strings)) +		{ +			return $strings; +		} + +		if (!class_exists('utf_normalizer')) +		{ +			global $phpbb_root_path, $phpEx; +			include($phpbb_root_path . 'includes/utf/utf_normalizer.' . $phpEx); +		} + +		if (!is_array($strings)) +		{ +			utf_normalizer::nfc($strings); +		} +		else if (is_array($strings))  		{ -			if (is_array($string)) +			foreach ($strings as $key => $string)  			{ -				foreach ($string as $_key => $_string) +				if (is_array($string))  				{ -					utf_normalizer::nfc($strings[$key][$_key]); +					foreach ($string as $_key => $_string) +					{ +						utf_normalizer::nfc($strings[$key][$_key]); +					} +				} +				else +				{ +					utf_normalizer::nfc($strings[$key]);  				} -			} -			else -			{ -				utf_normalizer::nfc($strings[$key]);  			}  		} -	} -	return $strings; +		return $strings; +	}  }  /** @@ -1921,7 +1936,7 @@ function utf8_wordwrap($string, $width = 75, $break = "\n", $cut = false)  * UTF8-safe basename() function  *  * basename() has some limitations and is dependent on the locale setting -* according to the PHP manual. Therefore we provide our own locale independant +* according to the PHP manual. Therefore we provide our own locale independent  * basename function.  *  * @param string $filename The filename basename() should be applied to @@ -1991,5 +2006,3 @@ function utf8_str_replace($search, $replace, $subject)  	return $subject;  } - -?>
\ No newline at end of file | 
