Files
kiri-wchat/wx/Help.php
T

146 lines
3.1 KiB
PHP
Raw Normal View History

2019-07-12 19:25:47 +08:00
<?php
namespace wchat;
2019-07-17 17:17:37 +08:00
class Help extends Miniprogarampage
2019-07-12 19:25:47 +08:00
{
public static $OK = 0;
public static $IllegalAesKey = -41001;
public static $IllegalIv = -41002;
public static $IllegalBuffer = -41003;
public static $DecodeBase64Error = -41004;
/**
* @param $encryptedData
* @param $iv
* @param $sessionKey
2019-07-17 17:43:08 +08:00
* @param $asArray
* @return object|array
2019-07-17 17:17:37 +08:00
* @throws
*
* * <li>-41001: encodingAesKey 非法</li>
* <li>-41003: aes 解密失败</li>
* <li>-41004: 解密后得到的buffer非法</li>
* <li>-41005: base64加密失败</li>
* <li>-41016: base64解密失败</li>
2019-07-12 19:25:47 +08:00
*/
2019-07-17 17:43:08 +08:00
public static function decode($encryptedData, $iv, $sessionKey, $asArray = false)
2019-07-12 19:25:47 +08:00
{
2019-07-17 17:17:37 +08:00
$config = Wx::getMiniProGaRamPage()->getConfig();
2019-07-12 19:25:47 +08:00
if (strlen($sessionKey) != 24) {
2019-07-17 17:17:37 +08:00
throw new \Exception('encodingAesKey 非法', self::$IllegalAesKey);
2019-07-12 19:25:47 +08:00
}
$aesKey = base64_decode($sessionKey);
if (strlen($iv) != 24) {
2019-07-17 17:17:37 +08:00
throw new \Exception('base64解密失败', self::$IllegalIv);
2019-07-12 19:25:47 +08:00
}
$aesIV = base64_decode($iv);
$aesCipher = base64_decode($encryptedData);
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, OPENSSL_RAW_DATA, $aesIV);
if ($result === false) {
2019-07-17 17:17:37 +08:00
throw new \Exception('aes 解密失败', self::$IllegalBuffer);
2019-07-12 19:25:47 +08:00
}
$dataObj = json_decode($result);
2019-07-17 17:17:37 +08:00
if ($dataObj->watermark->appid != $config->getAppid()) {
throw new \Exception('aes 解密失败', self::$IllegalBuffer);
2019-07-12 19:25:47 +08:00
}
2019-07-17 17:43:08 +08:00
if ($asArray) {
return get_object_vars($dataObj);
}
2019-07-17 17:17:37 +08:00
return $dataObj;
2019-07-12 19:25:47 +08:00
}
/**
* @param array $data
* @return string
*/
public static function toXml(array $data)
{
$xml = "<xml>";
foreach ($data as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
/**
* @param $xml
* @return mixed
*/
public static function toArray($xml)
{
if (!is_null($json = json_decode($xml, TRUE))) {
return $json;
}
if (is_array($json)) {
return $json;
}
$data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
return json_decode(json_encode($data), TRUE);
}
2019-07-17 17:17:37 +08:00
/**
* @param int $length
* @return string
*
* 随机字符串
*/
public static function random($length = 20)
{
$res = [];
$str = 'abcdefghijklmnopqrstuvwxyz';
$str .= strtoupper($str) . '1234567890';
for ($i = 0; $i < $length; $i++) {
$rand = substr($str, rand(0, strlen($str) - 2), 1);
if (empty($rand)) {
$rand = substr($str, strlen($str) - 3, 1);
}
array_push($res, $rand);
}
return implode($res);
}
2019-07-12 19:25:47 +08:00
/**
* @param array $array
* @param $key
* @param $type
* @return string
*/
public static function sign(array $array, $key, $type)
{
ksort($array, SORT_STRING);
$string = '';
foreach ($array as $key => $val) {
if (empty($string)) {
$string = $key . '=' . $val;
} else {
$string .= '&' . $key . '=' . $val;
}
}
$string .= '&key=' . $key;
if ($type == 'MD5') {
return strtoupper(md5($string));
} else {
return hash('sha256', $string);
}
}
}