All pastes #86465 Raw Edit

Someone

public text v1 · immutable
·published 2006-07-12 22:04 UTC
rendered paste body
<?php
/** 
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

/**
* UTF-8 transcoder
*
* That class will recode strings from other encodings to UTF-8.
* Whenever possible, it will try to use PHP's built-in functions or extensions,
* otherwise it will default to custom routines. 
*
* @package phpBB3
*/
class utf_transcoder
{
	/**
	* Recode a string to UTF-8
	*
	* If the encoding is not supported, the string is returned as-is
	*
	* @param	string	$string		Original string
	* @param	string	$encoding	Original encoding
	* @return	string				The string, encoded in UTF-8
	*/
	function recode($string, $encoding)
	{
		$encoding = strtolower($encoding);

		if ($encoding == 'utf-8' || !is_string($string) || !isset($string[0]))
		{
			return $string;
		}

		/**
		* PHP has a built-in function for encoding from iso-8859-1, let's use that
		*/
		if ($encoding == 'iso-8859-1')
		{
			return utf8_encode($string);
		}

		/**
		* First, try iconv()
		*/
		if (function_exists('iconv'))
		{
			$ret = @iconv($encoding, 'UTF-8', $string);

			if (isset($ret[0]))
			{
				return $ret;
			}
		}

		/**
		* Try the mb_string extension
		*/
		if (function_exists('mb_convert_encoding'))
		{
			$ret = @mb_convert_encoding($string, 'utf-8', $encoding);

			if (isset($ret[0]))
			{
				return $ret;
			}
		}

		/**
		* Try the recode extension
		*/
		if (function_exists('recode_string'))
		{
			$ret = @recode_string($encoding . '..utf-8', $string);

			if (isset($ret[0]))
			{
				return $ret;
			}
		}

		/**
		* If nothing works, check if we have a custom transcoder available
		*/
		if (!preg_match('#^[a-z0-9\\-]+$#', $encoding))
		{
			/**
			* Make sure the encoding name is alphanumeric, we don't want it
			* to be abused into loading arbitrary files
			*/
			trigger_error('Unknown encoding: ' . $encoding);
		}

		global $phpbb_root_path;
		if (!file_exists($phpbb_root_path . 'includes/utf/data/'))
		{
			return $string;
		}

		die('Finish me!! '.basename(__FILE__).' at line '.__LINE__);
	}
}

?>