<?php
/*--------------------------------------------------------- 
 * 全形半形轉換
 * Modified history:2023/08/23
 * Copyright © 2023 蘋果世界股份有限公司 版權所有
 * ------------------------------------------------------*/

/* 將unicode轉換成字元
 * @param		string		$unicode
 * @return		string
 */
function unicode2Char(int $unicode): string {
	if ($unicode < 128) return chr($unicode);
	if ($unicode < 2048) return chr(($unicode >> 6) + 192) . chr(($unicode & 63) + 128);
	if ($unicode < 65536) return chr(($unicode >> 12) + 224) . chr((($unicode >> 6) & 63) + 128) . chr(($unicode & 63) + 128);
	if ($unicode < 2097152) return chr(($unicode >> 18) + 240) . chr((($unicode >> 12) & 63) + 128) . chr((($unicode >> 6) & 63) + 128) . chr(($unicode & 63) + 128);
	return '';
}

/* 將字元轉換成unicode
 * @param		string		$char				必須是UTF-8字元
 * @return		int
 */
function char2Unicode(string $char): int {
	switch (strlen($char)) {
		case 1:
			return ord($char);
		case 2:
			return (ord($char[1]) & 63) | ((ord($char[0]) & 31) << 6);
		case 3:
			return (ord($char[2]) & 63) | ((ord($char[1]) & 63) << 6) | ((ord($char[0]) & 15) << 12);
		case 4:
			return (ord($char[3]) & 63) | ((ord($char[2]) & 63) << 6) | ((ord($char[1]) & 63) << 12) | ((ord($char[0]) & 7) << 18);
		default:
			trigger_error('Character is not UTF-8!', E_USER_WARNING);
			return 0;
	}
}

/* 全形轉半形 */
function sbc2Dbc(string $str): string {
	return preg_replace_callback(
		'/[\x{3000}\x{ff01}-\x{ff5f}]/u',
		function($matches) {
			$unicode = char2Unicode($matches[0]);
			if ($unicode === 0x3000) {
				return " ";
			} else {
				$code = $unicode - 0xfee0;
				return $code > 256 ? unicode2Char($code) : chr($code);
			}
		},
		$str
	);
}

/* 半形轉全形 */
function dbc2Sbc(string $str): string {
	return preg_replace_callback(
		'/[\x{0020}\x{0020}-\x{007e}]/u',
		function($matches) {
			$unicode = char2Unicode($matches[0]);
			if ($unicode === 0x0020) {
				return unicode2Char(0x3000);
			} else {
				$code = $unicode + 0xfee0;
				return $code > 256 ? unicode2Char($code) : chr($code);
			}
		},
		$str
	);
}

/* 清空資料夾 */
function rrmdir(string $dir): void {
	if (is_dir($dir)) {
		$objects = scandir($dir);
		foreach ($objects as $object) {
			if ($object != "." && $object != "..") {
				if (filetype($dir . "/" . $object) == "dir") {
					rrmdir($dir . "/" . $object);
				} else {
					unlink($dir . "/" . $object);
				}
			}
		}
		reset($objects);
		rmdir($dir);
	}
}

/* -------------------------------------------------------- 
 * 藍新金流
 * Modified history:2023/08/23
 * Copyright © 2023 蘋果世界股份有限公司 版權所有
 * ------------------------------------------------------*/

/* AES256加密 */
function create_mpg_aes_encrypt(array $parameter = [], string $key = "", string $iv = ""): string {
    $return_str = http_build_query($parameter);
    return bin2hex(openssl_encrypt(addpadding($return_str), 'aes-256-cbc', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv));
}
function addpadding(string $string, int $blocksize = 32): string {
	$len = strlen($string);
	$pad = $blocksize - ($len % $blocksize);
	$string .= str_repeat(chr($pad), $pad);
	return $string;
}

/* AES256解密 */
function create_aes_decrypt(string $parameter = "", string $key = "", string $iv = ""): string {
	return strippadding(openssl_decrypt(hex2bin($parameter), 'AES-256-CBC', $key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv));
}
function strippadding(string $string): string|false {
	$slast = ord(substr($string, -1));
	$slastc = chr($slast);
	$pcheck = substr($string, -$slast);
	if (preg_match("/$slastc{".$slast."}/", $string)) {
		$string = substr($string, 0, strlen($string) - $slast);
		return $string;
	} else {
		return false;
	}
}

/* 錯誤代碼 */
function error_code_message(string $code): string|false {
	$aryEC = [
		"MPG01001" => "會員參數 不可空白/設定錯誤(LoginType)",
		"MPG01002" => "時間戳記不可空白(TimeStamp)",
		"MPG01005" => "TokenTerm 不可空白/設定錯誤(TokenTerm)",
		"MPG01008" => "分期參數設定錯誤(InstFlag)",
		"MPG01009" => "商店代號不可空白(MerchantID)",
		"MPG01010" => "程式版本設定錯誤(Version)",
		"MPG01011" => "回傳規格設定錯誤(RespondType)",
		"MPG01012" => "商店訂單編號不可空白/設定錯誤(MerchantOrderNo:限英數字、底線，長度20字)",
		"MPG01013" => "付款人電子信箱設定錯誤(Email)",
		"MPG01014" => "網址設定錯誤(ReturnURL、NotifyURL、CustomerURL、ClientBackURL)",
		"MPG01015" => "訂單金額不可空白/設定錯誤(Amt)",
		"MPG01017" => "商品資訊不可空白(ItemDesc)",
		"MPG01018" => "繳費有效期限設定錯誤(ExpireDate)",
		"MPG01023" => "交易加密資料不可空白(TradeInfo)",
		"MPG01024" => "交易加密 SHA 資料不可空白(TradeSha)",
		"MPG02002" => "查無商店開啟任何金流服務",
		"MPG02003" => "支付方式未啟用，請洽客服中心",
		"MPG02004" => "送出後檢查，超過交易限制秒數",
		"MPG02005" => "送出後檢查，驗證資料錯誤",
		"MPG02006" => "系統發生異常，請洽客服中心",
		"MPG03001" => "FormPost 加密失敗",
		"MPG03002" => "拒絕交易 IP",
		"MPG03003" => "IP 交易次數限制(N分鐘內不可交易達M次)",
		"MPG03004" => "商店狀態已被暫停或是關閉，無法進行交易",
		"MPG03007" => "查無此商店代號",
		"MPG03008" => "已存在相同的商店訂單編號",
		"MPG03009" => "交易失敗"
	];
	return $aryEC[$code] ?? false;
}
?>