<?php

function u2utf8($c)
{
	for($i=0;$i<count($c);$i++)
	$str="";
	/*
	if ($c < 0x80) {
		//$str.=(0x80 | $c>>6);
		$str .= $c;
	}
	else
	*/	
	if ($c < 0x800) {
		$str.=(0xC0 | $c>>6);
		$str.=(0x80 | $c & 0x3F);
	}
	else if ($c < 0x10000) {
		$str.=(0xE0 | $c>>12);
		$str.=(0x80 | $c>>6 & 0x3F);
		$str.=(0x80 | $c & 0x3F);
	}
	else if ($c < 0x200000) {
		$str.=(0xF0 | $c>>18);
		$str.=(0x80 | $c>>12 & 0x3F);
		$str.=(0x80 | $c>>6 & 0x3F);
		$str.=(0x80 | $c & 0x3F);
	}
	return $str;
}

function chkDiffChr($inChr)
{
	switch($inChr)
	{
		case 2:
			$outChr = 36;//'$'
			break;
		case 0:
			$outChr = 64;//'@'
			break;
		case 17:
			$outChr = 95;//'_'
			break;
//		case 47:
//			$outChr = 92;//'/'
//			break;
		default:
			return($inChr);
	}
	return($outChr);
}

function decode7bitTo8bit($in_buffer)
{
	$DEBUG=0;
	$out_buffer="";
	$len_of_data=strlen($in_buffer);
	$in_buffer_len = ($len_of_data+1)*8/7;
	if($DEBUG) echo "<br>in_buffer_len=$in_buffer_len";
	for ($i=0; $i<$in_buffer_len; $i+=14)
	{
		$len_of_data=strlen($in_buffer);
		$tmp7byte=substr($in_buffer,0,($len_of_data >= 14 ? 14 : $len_of_data));
		if($DEBUG) echo "<br>tmp7byte=$tmp7byte";
		$out_buffer .= decode7byteTo8byte($tmp7byte);
		if($DEBUG) echo "<br>out_buffer=$out_buffer";
		$in_buffer=substr($in_buffer,14);
		if($DEBUG) echo "<br>in_buffer=$in_buffer,i=$i";
	}
	return($out_buffer);
}

function decode7byteTo8byte($Str)
{
	$DEBUG=0;
	$len_of_data=strlen($Str);
	$len_of_byte=0;
	for ($i=0;$i<$len_of_data;$i+=2)
	{
		$in_buffer[$len_of_byte]=hexdec(substr($Str,$i,2));
		if($DEBUG) echo "<br>in_buffer[$len_of_byte]={$in_buffer[$len_of_byte]}";
		$len_of_byte++;
	}
	$out_buffer='';
	for ($i=0;$i<$len_of_byte;$i++)
	{
		switch ($i)
		{
			case 0:
				$out_buffer .= chr(chkDiffChr(($in_buffer[0]) & 0x7f));
				if($DEBUG) echo "<br>outChar=$outChar  out_buffer=$out_buffer";
				break;
			case 1:
				$out_buffer .= chr(chkDiffChr((($in_buffer[1] & 0x3f) << 1 ) | (($in_buffer[0] >> 7) & 0x01)));
				if($DEBUG) echo "<br>out_buffer=$out_buffer";
				break;
			case 2:
				$out_buffer .= chr(chkDiffChr((($in_buffer[2] & 0x1f) << 2 ) | (($in_buffer[1] >> 6) & 0x03)));
				if($DEBUG) echo "<br>out_buffer=$out_buffer";
				break;
			case 3:
				$out_buffer .= chr(chkDiffChr((($in_buffer[3] & 0x0f) << 3 ) | (($in_buffer[2] >> 5) & 0x07)));
				if($DEBUG) echo "<br>out_buffer=$out_buffer";
				break;
			case 4:
				$out_buffer .= chr(chkDiffChr((($in_buffer[4] & 0x07) << 4 ) | (($in_buffer[3] >> 4) & 0x0f)));
				if($DEBUG) echo "<br>out_buffer=$out_buffer";
				break;
			case 5:
				$out_buffer .= chr(chkDiffChr((($in_buffer[5] & 0x03) << 5 ) | (($in_buffer[4] >> 3) & 0x1f)));
				if($DEBUG) echo "<br>out_buffer=$out_buffer";
				break;
			case 6:
				$out_buffer .= chr(chkDiffChr((($in_buffer[6] & 0x01) << 6 ) | (($in_buffer[5] >> 2) & 0x3f)));
				if($DEBUG) echo "<br>out_buffer=$out_buffer";
				if ( $in_buffer[6] >> 1 )
				{
					$out_buffer .= chr(chkDiffChr(($in_buffer[6] >> 1) & 0x7f));
					if($DEBUG) echo "<br>out_buffer=$out_buffer";
				}
				break;
			default:
				break;
		}
	}
	return($out_buffer);
}

function encodeUTF8($smsContent,$smsDCS)
{
	if(!trim($smsContent))
		return $smsContent;
	//echo "smsDCS=$smsDCS";
	$encodeType=substr(decbin($smsDCS),-4,-2);
	//echo "encodeType=$encodeType";
	switch($encodeType){
		case '00':$encodeStr=decode7bitTo8bit($smsContent);
		case '0':$encodeStr=decode7bitTo8bit($smsContent);
		case '':$encodeStr=decode7bitTo8bit($smsContent);
			return($encodeStr);
		case '01':
		case '10':
			$encodeStr=$smsContent;
			break;
		default: return('');
	}

	$utf8="";
	$rrr="";
	
	while($encodeStr){
		$now=substr($encodeStr,0,4);
		//echo "<BR>this=$this";
		$trr="&#x".$now.";";
		$rrr.=$trr;
		$utf8.=u2utf8(hexdec($now));
		
		$encodeStr=substr($encodeStr,4,strlen($encodeStr));
		
		//echo "<BR>utf8-1=$utf8,encodeStr=$encodeStr";
	}
	return $rrr;
	/*
	//echo "<BR>utf8=$utf8";
	$ret="";
	for($i=0;$i<strlen($utf8);$i+=3)
	$ret.=chr(substr($utf8,$i,3));

	return $ret;
	*/
}
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">";
//$UTF8Str=encodeUTF8($smsContent,$smsDCS);
//echo "$UTF8Str";
?>