<?php

if (!function_exists('mb_strtoupper'))
{
	function mb_strtoupper($s, $encoding = INF)
	{
		if ('' === $s .= '') return '';

		if (INF === $encoding) $encoding = 'UTF-8';
		else $encoding = strtoupper($encoding);

		if ('UTF-8' === $encoding || 'UTF8' === $encoding) $encoding = INF;
		else $s = function_exists('iconv') ? iconv($encoding, 'UTF-8//IGNORE', $s) : $s;

		static $upper;
		isset($upper) || $upper = unserialize(base64_decode('YToxMDUxOntzOjE6ImEiO3M6MToiQSI7czoxOiJiIjtzOjE6IkIiO3M6MToiYyI7czoxOiJDIjtz
[encoded data, 27027 characters]
czo0OiLwkJCmIjtzOjQ6IvCQkY8iO3M6NDoi8JCQpyI7fQ=='));
		$map = $upper;
		
		static $ulen_mask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4);

		$i = 0;
		$len = strlen($s);

		while ($i < $len)
		{
			$ulen = $s[$i] < "\x80" ? 1 : $ulen_mask[$s[$i] & "\xF0"];
			$uchr = substr($s, $i, $ulen);
			$i += $ulen;

			if (isset($map[$uchr]))
			{
				$uchr = $map[$uchr];
				$nlen = strlen($uchr);

				if ($nlen == $ulen)
				{
					$nlen = $i;
					do $s[--$nlen] = $uchr[--$ulen];
					while ($ulen);
				}
				else
				{
					$s = substr_replace($s, $uchr, $i - $ulen, $ulen);
					$len += $nlen - $ulen;
					$i   += $nlen - $ulen;
				}
			}
		}

		if (INF === $encoding) return $s;
		else return function_exists('iconv') ? iconv('UTF-8', $encoding, $s) : $s;
	}
}

if (!function_exists('mb_strcut'))
{
	function mb_strcut($str, $start, $length = null, $encoding = '')
	{
		$match = array();
        // use the regex unicode support to separate the UTF-8 characters into an array
        preg_match_all( '/./us', $str, $match );
        $chars = is_null( $length )? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
        return implode( '', $chars );
	}
}

if (!function_exists('mb_detect_encoding'))
{
	function mb_detect_encoding($str, $encoding_list = INF, $strict = false)
	{
		if (INF === $encoding_list) $encoding_list = 'UTF-8';
		else
		{
			if (! is_array($encoding_list)) $encoding_list = array_map('trim', explode(',', $encoding_list));
			$encoding_list = array_map('strtoupper', $encoding_list);
		}

		foreach ($encoding_list as $enc)
		{
			switch ($enc)
			{
				case 'ASCII':
					if (! preg_match('/[\x80-\xFF]/', $str)) return $enc;
					break;

				case 'UTF8':
				case 'UTF-8':
					if (preg_match('//u', $str)) return $enc;
					break;

				default:
					return strncmp($enc, 'ISO-8859-', 9) ? false : $enc;
			}
		}

		return false;
	}
}

if (!function_exists('mb_check_encoding'))
{
	function mb_check_encoding($var = INF, $encoding = INF)
	{
		if (INF === $encoding)
		{
			if (INF === $var) return false;
			$encoding = 'UTF-8';
		}

		return false !== mb_detect_encoding($var, array($encoding), true);
	}
}
