add clear
This commit is contained in:
@@ -0,0 +1,360 @@
|
||||
<?php
|
||||
|
||||
namespace officialaccount\dcaler;
|
||||
|
||||
|
||||
class Authorization
|
||||
{
|
||||
private $subscribe;
|
||||
private $openid;
|
||||
private $nickname;
|
||||
private $sex;
|
||||
private $language;
|
||||
private $city;
|
||||
private $province;
|
||||
private $country;
|
||||
private $headimgurl;
|
||||
private $subscribe_time;
|
||||
private $unionid;
|
||||
private $remark;
|
||||
private $groupid;
|
||||
private $tagid_list;
|
||||
private $subscribe_scene;
|
||||
private $qr_scene;
|
||||
private $qr_scene_str;
|
||||
|
||||
/**
|
||||
* @param $object
|
||||
* @return Authorization
|
||||
*/
|
||||
public static function instance($object)
|
||||
{
|
||||
$class = new Authorization();
|
||||
$class->setSubscribe($object['subscribe']);
|
||||
$class->setOpenid($object['openid']);
|
||||
$class->setNickname($object['nickname']);
|
||||
$class->setSex($object['sex']);
|
||||
$class->setLanguage($object['language']);
|
||||
$class->setCity($object['city']);
|
||||
$class->setProvince($object['province']);
|
||||
$class->setCountry($object['country']);
|
||||
$class->setHeadimgurl($object['headimgurl']);
|
||||
$class->setSubscribeTime($object['subscribe_time']);
|
||||
$class->setUnionid($object['unionid']);
|
||||
$class->setRemark($object['remark']);
|
||||
$class->setGroupid($object['groupid']);
|
||||
$class->setTagidList($object['tagid_list']);
|
||||
$class->setSubscribeScene($object['subscribe_scene']);
|
||||
$class->setQrScene($object['qr_scene']);
|
||||
$class->setQrSceneStr($object['qr_scene_str']);
|
||||
return $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSubscribe()
|
||||
{
|
||||
return $this->subscribe;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $subscribe
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setSubscribe($subscribe)
|
||||
{
|
||||
$this->subscribe = $subscribe;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getOpenid()
|
||||
{
|
||||
return $this->openid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $openid
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setOpenid($openid)
|
||||
{
|
||||
$this->openid = $openid;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getNickname()
|
||||
{
|
||||
return $this->nickname;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $nickname
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setNickname($nickname)
|
||||
{
|
||||
$this->nickname = $nickname;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSex()
|
||||
{
|
||||
return $this->sex;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $sex
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setSex($sex)
|
||||
{
|
||||
$this->sex = $sex;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLanguage()
|
||||
{
|
||||
return $this->language;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $language
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setLanguage($language)
|
||||
{
|
||||
$this->language = $language;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCity()
|
||||
{
|
||||
return $this->city;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $city
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setCity($city)
|
||||
{
|
||||
$this->city = $city;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getProvince()
|
||||
{
|
||||
return $this->province;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $province
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setProvince($province)
|
||||
{
|
||||
$this->province = $province;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCountry()
|
||||
{
|
||||
return $this->country;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $country
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setCountry($country)
|
||||
{
|
||||
$this->country = $country;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getHeadimgurl()
|
||||
{
|
||||
return $this->headimgurl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $headimgurl
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setHeadimgurl($headimgurl)
|
||||
{
|
||||
$this->headimgurl = $headimgurl;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSubscribeTime()
|
||||
{
|
||||
return $this->subscribe_time;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $subscribe_time
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setSubscribeTime($subscribe_time)
|
||||
{
|
||||
$this->subscribe_time = $subscribe_time;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getUnionid()
|
||||
{
|
||||
return $this->unionid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $unionid
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setUnionid($unionid)
|
||||
{
|
||||
$this->unionid = $unionid;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getRemark()
|
||||
{
|
||||
return $this->remark;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $remark
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setRemark($remark)
|
||||
{
|
||||
$this->remark = $remark;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getGroupid()
|
||||
{
|
||||
return $this->groupid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $groupid
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setGroupid($groupid)
|
||||
{
|
||||
$this->groupid = $groupid;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTagidList()
|
||||
{
|
||||
return $this->tagid_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $tagid_list
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setTagidList($tagid_list)
|
||||
{
|
||||
$this->tagid_list = $tagid_list;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSubscribeScene()
|
||||
{
|
||||
return $this->subscribe_scene;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $subscribe_scene
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setSubscribeScene($subscribe_scene)
|
||||
{
|
||||
$this->subscribe_scene = $subscribe_scene;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getQrScene()
|
||||
{
|
||||
return $this->qr_scene;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $qr_scene
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setQrScene($qr_scene)
|
||||
{
|
||||
$this->qr_scene = $qr_scene;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getQrSceneStr()
|
||||
{
|
||||
return $this->qr_scene_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $qr_scene_str
|
||||
* @return Authorization
|
||||
*/
|
||||
public function setQrSceneStr($qr_scene_str)
|
||||
{
|
||||
$this->qr_scene_str = $qr_scene_str;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount\dcaler;
|
||||
|
||||
|
||||
class PKCS7Encoder
|
||||
{
|
||||
public static $block_size = 32;
|
||||
|
||||
/**
|
||||
* @param $text
|
||||
* @return string
|
||||
*/
|
||||
function encode($text)
|
||||
{
|
||||
$block_size = PKCS7Encoder::$block_size;
|
||||
$text_length = strlen($text);
|
||||
//计算需要填充的位数
|
||||
$amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
|
||||
if ($amount_to_pad == 0) {
|
||||
$amount_to_pad = PKCS7Encoder::$block_size;
|
||||
}
|
||||
//获得补位所用的字符
|
||||
$pad_chr = chr($amount_to_pad);
|
||||
$tmp = "";
|
||||
for ($index = 0; $index < $amount_to_pad; $index++) {
|
||||
$tmp .= $pad_chr;
|
||||
}
|
||||
return $text . $tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $text
|
||||
* @return false|string
|
||||
*/
|
||||
function decode($text)
|
||||
{
|
||||
|
||||
$pad = ord(substr($text, -1));
|
||||
if ($pad < 1 || $pad > 32) {
|
||||
$pad = 0;
|
||||
}
|
||||
return substr($text, 0, (strlen($text) - $pad));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount\dcaler;
|
||||
|
||||
|
||||
use officialaccount\NewsManager;
|
||||
|
||||
class Prpcrypt
|
||||
{
|
||||
public $key;
|
||||
|
||||
/**
|
||||
* Prpcrypt constructor.
|
||||
* @param $k
|
||||
*/
|
||||
public function __construct($k)
|
||||
{
|
||||
$this->key = base64_decode($k . "=");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $text
|
||||
* @param $appid
|
||||
* @return array
|
||||
*/
|
||||
public function encrypt($text, $appid)
|
||||
{
|
||||
|
||||
try {
|
||||
//获得16位随机字符串,填充到明文之前
|
||||
$random = $this->getRandomStr();
|
||||
$text = $random . pack("N", strlen($text)) . $text . $appid;
|
||||
// 网络字节序
|
||||
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
|
||||
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
|
||||
$iv = substr($this->key, 0, 16);
|
||||
//使用自定义的填充方式对明文进行补位填充
|
||||
$pkc_encoder = new PKCS7Encoder;
|
||||
$text = $pkc_encoder->encode($text);
|
||||
mcrypt_generic_init($module, $this->key, $iv);
|
||||
//加密
|
||||
$encrypted = mcrypt_generic($module, $text);
|
||||
mcrypt_generic_deinit($module);
|
||||
mcrypt_module_close($module);
|
||||
|
||||
//print(base64_encode($encrypted));
|
||||
//使用BASE64对加密后的字符串进行编码
|
||||
return array(NewsManager::OK, base64_encode($encrypted));
|
||||
} catch (\Exception $e) {
|
||||
//print $e;
|
||||
return array(NewsManager::EncryptAESError, null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $encrypted
|
||||
* @param $appid
|
||||
* @return array|string
|
||||
*/
|
||||
public function decrypt($encrypted, $appid)
|
||||
{
|
||||
|
||||
try {
|
||||
//使用BASE64对需要解密的字符串进行解码
|
||||
$ciphertext_dec = base64_decode($encrypted);
|
||||
$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
|
||||
$iv = substr($this->key, 0, 16);
|
||||
mcrypt_generic_init($module, $this->key, $iv);
|
||||
|
||||
//解密
|
||||
$decrypted = mdecrypt_generic($module, $ciphertext_dec);
|
||||
mcrypt_generic_deinit($module);
|
||||
mcrypt_module_close($module);
|
||||
} catch (\Exception $e) {
|
||||
return array(NewsManager::DecryptAESError, null);
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
//去除补位字符
|
||||
$pkc_encoder = new PKCS7Encoder;
|
||||
$result = $pkc_encoder->decode($decrypted);
|
||||
//去除16位随机字符串,网络字节序和AppId
|
||||
if (strlen($result) < 16)
|
||||
return "";
|
||||
$content = substr($result, 16, strlen($result));
|
||||
$len_list = unpack("N", substr($content, 0, 4));
|
||||
$xml_len = $len_list[1];
|
||||
$xml_content = substr($content, 4, $xml_len);
|
||||
$from_appid = substr($content, $xml_len + 4);
|
||||
} catch (\Exception $e) {
|
||||
//print $e;
|
||||
return array(NewsManager::IllegalBuffer, null);
|
||||
}
|
||||
if ($from_appid != $appid)
|
||||
return array(NewsManager::ValidateAppidError, null);
|
||||
return array(0, $xml_content);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 随机生成16位字符串
|
||||
* @return string 生成的字符串
|
||||
*/
|
||||
function getRandomStr()
|
||||
{
|
||||
|
||||
$str = "";
|
||||
$str_pol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
|
||||
$max = strlen($str_pol) - 1;
|
||||
for ($i = 0; $i < 16; $i++) {
|
||||
$str .= $str_pol[mt_rand(0, $max)];
|
||||
}
|
||||
return $str;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount\dcaler;
|
||||
|
||||
|
||||
class SnsInfo
|
||||
{
|
||||
|
||||
private $openid;
|
||||
private $nickname;
|
||||
private $sex;
|
||||
private $province;
|
||||
private $city;
|
||||
private $country;
|
||||
private $headimgurl;
|
||||
private $privilege;
|
||||
private $unionid;
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getOpenid()
|
||||
{
|
||||
return $this->openid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $openid
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setOpenid($openid)
|
||||
{
|
||||
$this->openid = $openid;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getNickname()
|
||||
{
|
||||
return $this->nickname;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $nickname
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setNickname($nickname)
|
||||
{
|
||||
$this->nickname = $nickname;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSex()
|
||||
{
|
||||
return $this->sex;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $sex
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setSex($sex)
|
||||
{
|
||||
$this->sex = $sex;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getProvince()
|
||||
{
|
||||
return $this->province;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $province
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setProvince($province)
|
||||
{
|
||||
$this->province = $province;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCity()
|
||||
{
|
||||
return $this->city;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $city
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setCity($city)
|
||||
{
|
||||
$this->city = $city;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCountry()
|
||||
{
|
||||
return $this->country;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $country
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setCountry($country)
|
||||
{
|
||||
$this->country = $country;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getHeadimgurl()
|
||||
{
|
||||
return $this->headimgurl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $headimgurl
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setHeadimgurl($headimgurl)
|
||||
{
|
||||
$this->headimgurl = $headimgurl;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPrivilege()
|
||||
{
|
||||
return $this->privilege;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $privilege
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setPrivilege($privilege)
|
||||
{
|
||||
$this->privilege = $privilege;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getUnionid()
|
||||
{
|
||||
return $this->unionid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $unionid
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public function setUnionid($unionid)
|
||||
{
|
||||
$this->unionid = $unionid;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param $object
|
||||
* @return SnsInfo
|
||||
*/
|
||||
public static function instance($object)
|
||||
{
|
||||
$class = new SnsInfo();
|
||||
$class->setOpenid($object['openid']);
|
||||
$class->setNickname($object['nickname']);
|
||||
$class->setSex($object['sex']);
|
||||
$class->setCity($object['city']);
|
||||
$class->setProvince($object['province']);
|
||||
$class->setCountry($object['country']);
|
||||
$class->setHeadimgurl($object['headimgurl']);
|
||||
$class->setUnionid($object['unionid']);
|
||||
$class->setPrivilege($object['privilege']);
|
||||
return $class;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace officialaccount\dcaler;
|
||||
|
||||
|
||||
use officialaccount\NewsManager;
|
||||
|
||||
class XMLParse
|
||||
{
|
||||
/**
|
||||
* 提取出xml数据包中的加密消息
|
||||
* @param string $xmltext 待提取的xml字符串
|
||||
* @return array 提取出的加密消息字符串
|
||||
*/
|
||||
public function extract($xmltext)
|
||||
{
|
||||
try {
|
||||
$xml = new \DOMDocument();
|
||||
$xml->loadXML($xmltext);
|
||||
$array_e = $xml->getElementsByTagName('Encrypt');
|
||||
$array_a = $xml->getElementsByTagName('ToUserName');
|
||||
$encrypt = $array_e->item(0)->nodeValue;
|
||||
$tousername = $array_a->item(0)->nodeValue;
|
||||
return [0, $encrypt, $tousername];
|
||||
} catch (\Exception $e) {
|
||||
return [NewsManager::ParseXmlError, null, null];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $encrypt
|
||||
* @param $signature
|
||||
* @param $timestamp
|
||||
* @param $nonce
|
||||
* @return string
|
||||
*/
|
||||
public function generate($encrypt, $signature, $timestamp, $nonce)
|
||||
{
|
||||
$format = "<xml>
|
||||
<Encrypt><![CDATA[%s]]></Encrypt>
|
||||
<MsgSignature><![CDATA[%s]]></MsgSignature>
|
||||
<TimeStamp>%s</TimeStamp>
|
||||
<Nonce><![CDATA[%s]]></Nonce>
|
||||
</xml>";
|
||||
return sprintf($format, $encrypt, $signature, $timestamp, $nonce);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user