Commit 1c0a09fa by yeran

update 201804518

1 parent 84e36693
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
......@@ -13,18 +13,24 @@ class Config{
'key_file'=>null,
'token'=>null,
'encodingAesKey'=>null,
'accessToken' => '',
'open'=>[
'appid'=>'',
'secret'=>'',
'token'=>null,
'encodingAesKey'=>null,
'componentAccessToken'=>''
],
'webLogin'=>[
'appid'=>'',
'secret'=>''
]
];
static public $type = 'O';
static public $config = [
'cachePath' => APP_ROOT.'/cache/file/%s', //%s 为APPID替代 此公众号下所有凭证都会缓存在此目录下
'cacheTime' => '7000', //缓存有效时间
];
/**
*
* 第三方授权维护服务的链接
......@@ -43,15 +49,9 @@ class Config{
'openHandler'=>'O', //开放平台接入
'mpHandler'=>'A' //普通接入
];
static public $type;
static public $config = [
'cachePath' => APP_ROOT.'/cache/file/%s', //%s 为APPID替代 此公众号下所有凭证都会缓存在此目录下
'cacheTime' => '7000', //缓存有效时间
];
static public $accessToken;
static public $jsApiTicket;
static public $oAuthAccessToken;
static public $componentAccessToken;
static public $CARDJSTICKET;
}
\ No newline at end of file
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
......@@ -24,18 +24,22 @@ class CryptMsg
private $encodingAesKey;
private $appId;
/**
* 构造函数
* @param $token string 公众平台上,开发者设置的token
* @param $encodingAesKey string 公众平台上,开发者设置的EncodingAESKey
* @param $appId string 公众平台的appId
*/
public function WXBizMsgCrypt($token, $encodingAesKey, $appId)
{
$this->token = $token;
$this->encodingAesKey = $encodingAesKey;
$this->appId = $appId;
}
/**
* 构造函数
* @param $token string 公众平台上,开发者设置的token
* @param $encodingAesKey string 公众平台上,开发者设置的EncodingAESKey
* @param $appId string 公众平台的appId
*/
public function WXBizMsgCrypt($token, $encodingAesKey, $appId)
{
$this->token = $token;
$this->encodingAesKey = $encodingAesKey;
$this->appId = $appId;
}
/**
* 将公众平台回复用户的消息加密打包.
......
File mode changed
......@@ -60,90 +60,150 @@ class Prpcrypt
{
public $key;
function Prpcrypt($k)
function __construct($k)
{
$this->key = base64_decode($k . "=");
}
/**
* 对明文进行加密
* @param string $text 需要加密的明文
* @return string 加密后的密文
*/
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(ErrorCode::$OK, base64_encode($encrypted));
} catch (Exception $e) {
//print $e;
return array(ErrorCode::$EncryptAESError, null);
}
}
/**
* 对密文进行解密
* @param string $encrypted 需要解密的密文
* @return 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(ErrorCode::$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(ErrorCode::$IllegalBuffer, null);
}
if ($from_appid != $appid)
return array(ErrorCode::$ValidateAppidError, null);
return array(0, $xml_content);
}
/**
* 加密方法
* @param string $text
* @param $appid
* @return string
*/
public function encrypt($text,$appid) {
try {
//获得16位随机字符串,填充到明文之前
$random = $this->getRandomStr();//"aaaabbbbccccdddd";
$text = $random . pack("N", strlen($text)) . $text . $appid;
$iv = substr($this->key, 0, 16);
$pkc_encoder = new PKCS7Encoder;
$text = $pkc_encoder->encode($text);
$encrypted = openssl_encrypt($text,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv);
return array(ErrorCode::$OK, $encrypted);
} catch (Exception $e) {
//print $e;
return array(ErrorCode::$EncryptAESError, null);
}
}
/**
* 解密方法
* @param string $encrypted
* @param $appid
* @return string
*/
public function decrypt($encrypted,$appid) {
try {
$iv = substr($this->key, 0, 16);
$decrypted = openssl_decrypt($encrypted,'AES-256-CBC',substr($this->key, 0, 32),OPENSSL_ZERO_PADDING,$iv);
} catch (Exception $e) {
return array(ErrorCode::$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);
if (!$appid)
$appid = $from_appid;
//如果传入的appid是空的,则认为是订阅号,使用数据中提取出来的appid
} catch (Exception $e) {
//print $e;
return array(ErrorCode::$IllegalBuffer, null);
}
if ($from_appid != $appid)
return array(ErrorCode::$ValidateAppidError, null);
//不注释上边两行,避免传入appid是错误的情况
return array(0, $xml_content, $from_appid);
}
// /**
// * 对明文进行加密
// * @param string $text 需要加密的明文
// * @return string 加密后的密文
// */
// 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(ErrorCode::$OK, base64_encode($encrypted));
// } catch (Exception $e) {
// //print $e;
// return array(ErrorCode::$EncryptAESError, null);
// }
// }
// /**
// * 对密文进行解密
// * @param string $encrypted 需要解密的密文
// * @return 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(ErrorCode::$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(ErrorCode::$IllegalBuffer, null);
// }
// if ($from_appid != $appid)
// return array(ErrorCode::$ValidateAppidError, null);
// return array(0, $xml_content);
//
// }
/**
......
File mode changed
......@@ -17,16 +17,23 @@ class XMLParse
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;
$tousername = null;
if(!empty($array_a) && $array_a->item(0)){
$tousername = $array_a->item(0)->nodeValue;
}
return array(0, $encrypt, $tousername);
} catch (Exception $e) {
//debug($e);
//print $e . "\n";
debug($e->getMessage());
return array(ErrorCode::$ParseXmlError, null, null);
}
}
......
File mode changed
File mode changed
File mode changed
......@@ -16,12 +16,12 @@ class WXBizDataCrypt
private $appid;
private $sessionKey;
/**
/**
* 构造函数
* @param $sessionKey string 用户在小程序登录后获取的会话密钥
* @param $appid string 小程序的appid
*/
public function WXBizDataCrypt( $appid, $sessionKey)
public function __construct( $appid, $sessionKey)
{
$this->sessionKey = $sessionKey;
$this->appid = $appid;
......
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
File mode changed
......@@ -8,6 +8,10 @@
namespace wechatkit\Mini;
use Lestore\Wxauthcenter\Services\AccessTokenService;
use wechatkit\HttpFul\HttpFul;
use wechatkit\Run\BaseRun;
/**
*
* 微信授权登陆
......@@ -15,6 +19,68 @@ namespace wechatkit\Mini;
* Class Login
* @package wechatkit\Mini
*/
class Login{
class Login extends BaseRun{
public function mpAuthorize($jsCode){
return $this->jscode2session($jsCode);
if(!$result){
return null;
}
$openid = $result['openid'];
$session_key = $result['session_key'];
$unionid = $result['unionid'];
//执行用户信息获取
}
/**
*
* 微信小程序登陆
*
* @param $jsCode
* @return bool|mixed|null
* @internal param $appid
* @internal param $secret
*/
public function jscode2session($jsCode){
if ($jsCode){
$appid = $this->options['appid'];
$componentAppid = $this->options['open']['appid'];
$componentAccessToken = AccessTokenService::componentAccessToken($this->options);
$result = null;
if($componentAccessToken) {
$result = $this->accessTokenFromJsCode($jsCode, $appid, $componentAppid, $componentAccessToken);
}
return $result;
}
return null;
}
/**
*
* 通过微信小程序code获取用户openid
*
* @param $jsCode
* @param $appid
* @param $componentAppid
* @param $componentAccessToken
* @return bool|mixed|null
* @internal param $code
* @internal param HttpFul $httpFul
*
*/
protected function accessTokenFromJsCode($jsCode,$appid,$componentAppid,$componentAccessToken){
if(!$jsCode)
return null;
$result = HttpFul::init()->handler('thirdOauth2JsAccessToken', [$appid,$jsCode,$componentAppid,$componentAccessToken]);
return $result;
}
}
\ No newline at end of file
File mode changed
File mode changed
File mode changed
File mode changed
......@@ -2,10 +2,12 @@
namespace wechatkit\Oauth2;
use wechatkit\Config\Config;
use Lestore\Wxauthcenter\Services\AccessTokenService;
use wechatkit\Config\Config;
use wechatkit\HttpFul\HttpFul;
use wechatkit\Run\BaseRun;
class Oauth2
class Oauth2 extends BaseRun
{
/**
......@@ -42,158 +44,90 @@
return null;
$result = $httpFul->handler('openOauth2JSAccessToken', [$appid,$secret,$code]);
$this->result($result);
return $result;
}
/**
* 公众号授权
*
* @param $redirect_uri
* @param string $scope
* @param string $state
* @param bool $auto
* @return bool|mixed
*/
public function mpAuthorize_v2($redirect_uri =null, $scope = 'snsapi_userinfo', $state = '', $auto = TRUE)
public function mpAuthorize($redirect_uri =null, $scope = 'snsapi_userinfo', $state = '', $auto = TRUE)
{
if(null == $redirect_uri){
$redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$redirect_uri = httpType().$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
if (Config::$type == 'O') { //开放平台授权
if ($this->authType == 'O') { //开放平台授权
if (!$_GET['code']) {
header('location:' . sprintf(
'https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s&component_appid=%s#wechat_redirect',
Config::$options['appid'],
urlencode($redirect_uri),
$scope,
$state,
Config::$options['open']['appid']
));
$url = sprintf(
'https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s&component_appid=%s#wechat_redirect',
$this->options['appid'],
urlencode($redirect_uri),
$scope,
$state,
$this->options['open']['appid']
);
header('location:' .$url);
exit(255);
} else {
if ($auto) {
$result = $this->accessTokenFormCode($_GET['code'], new HttpFul());
$result = $this->accessTokenFormCode($_GET['code']);
if(empty($result)){
return null;
}
if ('snsapi_base' == $scope) {
return $result['openid'];
}
// asyncLog('---openid--'.json_encode($result));
$info = $this->oAuth2AccessTokenUserInfo($result['openid'], new HttpFul());
$info = $this->oAuth2AccessTokenUserInfo($result['openid'],$result['access_token']);
return $info;
}
}
}
}
/**
* @param $redirect_uri
* @param string $scope
* @param string $state
* @param bool $auto
* @return bool|mixed
*/
public function mpAuthorize($redirect_uri =null, $scope = 'snsapi_userinfo', $state = '', $auto = TRUE)
{
if(null == $redirect_uri){
$redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
if (Config::$type == 'A') { //普通授权
if (!$_GET['code']) {
header('location:' . sprintf(
'https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect',
Config::$options['appid'],
urlencode($redirect_uri),
$scope,
$state
));
exit(255);
} else {
if ($auto) {
$result = $this->accessTokenFormCode($_GET['code'], new HttpFul());
if ('snsapi_base' == $scope) {
return $result['openid'];
}
return $this->oAuth2AccessTokenUserInfo($result['openid'], new HttpFul());
}
}
} else if (Config::$type == 'O') { //开放平台授权
if (!$_GET['code']) {
header('location:' . sprintf(
'https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s&component_appid=%s#wechat_redirect',
Config::$options['appid'],
urlencode($redirect_uri),
$scope,
$state,
Config::$options['open']['appid']
));
exit(255);
} else {
if ($auto) {
$result = $this->accessTokenFormCode($_GET['code'], new HttpFul());
if ('snsapi_base' == $scope) {
return $result['openid'];
}
return $this->oAuth2AccessTokenUserInfo($result['openid'], new HttpFul());
}
}
}
return null;
}
/**
* @param $code
* @param HttpFul $httpFul
* @return bool|mixed
*/
protected function accessTokenFormCode($code, HttpFul $httpFul)
/**
*
* 通过code获取accessToken
*
* @param $code
* @return bool|mixed
*/
protected function accessTokenFormCode($code)
{
if(Config::$type == 'A'){
$result = $httpFul->handler('oauth2AccessToken', [$code]);
}else{
$result = $httpFul->handler('openOauth2AccessToken', [Config::$options['appid'],$code,Config::$options['open']['appid'],Config::$componentAccessToken]);
}
$this->result($result);
$result = null;
if($this->authType == 'O') {
$componentAccessToken = AccessTokenService::componentAccessToken($this->options);
if (!empty($componentAccessToken)) {
$result = HttpFul::init()->handler('openOauth2AccessToken'
, [$this->options['appid'], $code, $this->options['open']['appid'], $componentAccessToken]);
}
}
return $result;
}
/**
* @param $openid
* @param HttpFul $httpFul
* @return bool|mixed
*/
protected function oAuth2AccessTokenUserInfo($openid, HttpFul $httpFul)
/**
* @param $openid
* @param $accessToken //登录使用的accesstoken
* @return bool|mixed
*/
protected function oAuth2AccessTokenUserInfo($openid,$accessToken)
{
$result = $httpFul->handler('oAuth2AccessTokenUserInfo', [$this->access_token, $openid]);
$this->result($result);
$result = HttpFul::init()->handler('oAuth2AccessTokenUserInfo', [$accessToken, $openid]);
return $result;
}
/**
* @param array $result
*/
public function result($result)
{
if(!$result && !is_array($result)){
return false;
}
foreach ($result as $key => $value) {
$this->$key = $value;
}
}
}
......@@ -2,17 +2,14 @@
namespace wechatkit\Open;
use wechatkit\Config\Config;
use Lestore\Wxauthcenter\Services\AccessTokenService;
use wechatkit\Config\Config;
use wechatkit\HttpFul\HttpFul;
use wechatkit\Run\BaseRun;
class Open
class Open extends BaseRun
{
public function __construct()
{
if (Config::$type != 'O') {
throw new \Exception('this api must open type');
}
}
//网页登入
public function webOAuth2($url = NULL, $type = 'URL', $state = NULL, $wxLoginConfig = ['style' => 'black',
'href' => NULL])
......@@ -63,33 +60,62 @@
}
//微信公众号授权e
public function authorizer($url=null){
if(!$_GET['auth_code']){
$preAuthCode = HttpFul::init()->handler('preAuthCode',[Config::$componentAccessToken],['component_appid'=>Config::$options['open']['appid']],'JSON','POST');
if($preAuthCode){
if(null == $url){
/**
* 微信公众号/小程序授权
* @param $authType 1则商户扫码后,手机端仅展示公众号、2表示仅展示小程序,3表示公众号和小程序都展示,如果为未指定,则默认小程序和公众号都展示
* @param null $url
* @return bool|mixed
*/
public function authorizer($authType=3,$url=null){
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
if(!array_key_exists('auth_code',$_GET) || !$_GET['auth_code']){
$url = $http_type.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$componentAppid = $this->options['open']['appid'];
$componentToken = AccessTokenService::componentAccessToken($this->options);
if(empty($componentToken)){
return false;
}
$preAuthCode = HttpFul::init()->handler('preAuthCode'
,[$componentToken]
,['component_appid'=>$componentAppid],'JSON','POST');
if($preAuthCode){
if(null == $url){
$url = httpType().$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
header(sprintf('Location:https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s',Config::$options['open']['appid'],$preAuthCode['pre_auth_code'],urlencode($url)));
exit(255);
$url = sprintf('Location:https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s'
,$componentAppid,$preAuthCode['pre_auth_code'],urlencode($url));
$url .= '&auth_type='.$authType;
header($url);
exit(255);
}
return false;
}else{
return $this->authinfo($_GET['auth_code']);
}
}
//拉公众号信息
/**
* 拉公众号详细信息
*
* @param $authorizer_appid
* @return bool|mixed
*/
public function mpInfo($authorizer_appid){
//$apiGetAuthorizerInfo
return HttpFul::init()->handler('apiGetAuthorizerInfo',[Config::$componentAccessToken],['component_appid'=>Config::$options['open']['appid'],'authorizer_appid'=>$authorizer_appid],'JSON','POST');
return HttpFul::init()->handler('apiGetAuthorizerInfo',[AccessTokenService::componentAccessToken($this->options)]
,['component_appid'=>$this->options['open']['appid'],'authorizer_appid'=>$authorizer_appid],'JSON','POST');
}
/**
* 获取公众号授权信息
* @param $auth_code
* @return bool|mixed
*/
public function authinfo($auth_code){
return HttpFul::init()->handler('apiQueryAuth',[Config::$componentAccessToken],['component_appid'=>Config::$options['open']['appid'],'authorization_code'=>$auth_code],'JSON','POST');
return HttpFul::init()->handler('apiQueryAuth',[AccessTokenService::componentAccessToken($this->options)]
,['component_appid'=>$this->options['open']['appid'],'authorization_code'=>$auth_code],'JSON','POST');
}
}
\ No newline at end of file
File mode changed
<?php
/**
* Created by IntelliJ IDEA.
* User: yeran
* Date: 2018/5/14
* Time: 下午4:37
*/
namespace wechatkit\Run;
class BaseRun {
public $options;
public $authType = 'O';
public $authorizerAccessToken;
public $componentAccessToken;
public function __construct($options)
{
$this->options = $options;
}
/**
* @return mixed
*/
public function getOptions()
{
return $this->options;
}
/**
* @param mixed $options
*/
public function setOptions($options)
{
$this->options = $options;
}
/**
* @return mixed
*/
public function getAuthorizerAccessToken()
{
return $this->authorizerAccessToken;
}
/**
* @param mixed $authorizerAccessToken
*/
public function setAuthorizerAccessToken($authorizerAccessToken)
{
$this->authorizerAccessToken = $authorizerAccessToken;
}
/**
* @return mixed
*/
public function getComponentAccessToken()
{
return $this->componentAccessToken;
}
/**
* @param mixed $componentAccessToken
*/
public function setComponentAccessToken($componentAccessToken)
{
$this->componentAccessToken = $componentAccessToken;
}
}
\ No newline at end of file
<?php
namespace wechatkit\Run;
use wechatkit\Config, wechatkit\AccessToken;
class Run implements \wechatkit\Core\Run{
static $self = NULL;
/**
* @param $type
* @param array $options [ appid secret mch_id key cert_file key_file ]
* @param array $config
* @return bool
*/
public function App($type, array $options = [], array $config = [])
{
if (!in_array($type, Config\Config::$typeList)) {
return FALSE;
} else {
Config\Config::$type = $type;
}
Config\Config::$options = array_merge(Config\Config::$options, $options);
Config\Config::$config = array_merge(Config\Config::$config, $config);
$this->AccessToken()->certificate(false);
return $this;
}
static public function go()
{
if (NULL == self::$self) {
return new \wechatkit\Run\Run();
} else {
return self::$self;
}
}
public function getAccessToken($appid){
return $this->AccessToken()->getAccessToken($appid);
}
/**
* @param array $arr
* @param $value
* @return array
*/
protected function key(array $arr, $value)
{
return array_keys($arr, $value);
}
/**
* @param $name
* @param $arguments
* @return mixed
* @throws \Exception
*/
public function __call($name, $arguments)
{
if (is_dir(__DIR__ . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . $name)) {
$className = '\wechatkit\\' . $name . '\\' . $name;
return new $className();
} else {
throw new \Exception('class ' . $name . ' not found!');
}
}
/*
* O save
* */
public function componentVerifyTicketSave($component_verify_ticket)
{
$componentDir = sprintf(Config\Config::$config['cachePath'], Config\Config::$options['open']['appid']);
if (!is_dir($componentDir)) {
if (!mkdir($componentDir, 0777, TRUE)) {
throw new \Exception('DIR "' . $componentDir . '" no write! place create this');
}
}
$ret = file_put_contents($componentDir . DIRECTORY_SEPARATOR . 'component_verify_ticket', $component_verify_ticket);
if ($ret) {
return TRUE;
}
}
public function certificatesSave($wxappid,$name,$value)
{
$componentDir = sprintf(Config\Config::$config['cachePath'], $wxappid);
if (!is_dir($componentDir)) {
if (!mkdir($componentDir, 0777, TRUE)) {
throw new \Exception('DIR "' . $componentDir . '" no write! place create this');
}
}
$ret = file_put_contents($componentDir . DIRECTORY_SEPARATOR . $name, $value);
if ($ret) {
return TRUE;
}
}
}
\ No newline at end of file
namespace wechatkit\Run;
use wechatkit\Config, wechatkit\AccessToken;
class Run implements \wechatkit\Core\Run{
static $self = NULL;
static $namespace;
public $options;
public $cacheConfig;
/**
* @param $type string
* @param array $options [ appid secret mch_id key cert_file key_file ]
* @param array $config
* @return $this
*/
public function App($type, array $options = [], array $config = [],array $authThird = [])
{
if (!in_array($type, Config\Config::$typeList)) {
return false;
} else {
Config\Config::$type = $type;
}
$this->options = array_merge(Config\Config::$options, $options);
$this->cacheConfig = array_merge(Config\Config::$config, $config);
// $this->AccessToken()->certificate(false);
return $this;
}
static public function go($class=null)
{
if (NULL == self::$self) {
$instance = new \wechatkit\Run\Run();
self::$self = $instance;
} else {
$instance = self::$self;
}
if($class)
self::$namespace = $class;
else self::$namespace = null;
return $instance;
}
public function getAccessToken($appid){
return $this->AccessToken()->getAccessToken($appid);
}
/**
* @param array $arr
* @param $value
* @return array
*/
protected function key(array $arr, $value)
{
return array_keys($arr, $value);
}
/**
* @param $name
* @param $arguments
* @return mixed
* @throws \Exception
*/
public function __call($name = null, $arguments)
{
$namespace = self::$namespace?:$name;
if (is_dir(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . $namespace)) {
$className = '\wechatkit\\' . $namespace . '\\' . $name;
return new $className($this->options);
} else {
throw new \Exception('class ' . $name . ' not found!');
}
}
/*
* O save
* */
public function componentVerifyTicketSave($component_verify_ticket)
{
$componentDir = sprintf(Config\Config::$config['cachePath'], Config\Config::$options['open']['appid']);
if (!is_dir($componentDir)) {
if (!mkdir($componentDir, 0777, TRUE)) {
throw new \Exception('DIR "' . $componentDir . '" no write! place create this');
}
}
$ret = file_put_contents($componentDir . DIRECTORY_SEPARATOR . 'component_verify_ticket', $component_verify_ticket);
if ($ret) {
return TRUE;
}
}
public function certificatesSave($wxappid,$name,$value)
{
$componentDir = sprintf(Config\Config::$config['cachePath'], $wxappid);
if (!is_dir($componentDir)) {
if (!mkdir($componentDir, 0777, TRUE)) {
throw new \Exception('DIR "' . $componentDir . '" no write! place create this');
}
}
$ret = file_put_contents($componentDir . DIRECTORY_SEPARATOR . $name, $value);
if ($ret) {
return TRUE;
}
}
}
......@@ -2,57 +2,62 @@
namespace wechatkit\Service;
use wechatkit\Config\Config;
use wechatkit\CryptMsg\CryptMsg;
use wechatkit\Run\BaseRun;
class Service {
class Service extends BaseRun {
public $postData;
public $xml;
public $xmlArray;
public function __construct()
{
$this->postData = file_get_contents('php://input');
if(!$this->postData){
exit(255);
}
$cryptmsg = new CryptMsg();
debug('=====================进入service.php');
if(Config::$type == 'A'){
$this->xml = simplexml_load_string($this->postData, 'SimpleXMLElement', LIBXML_NOCDATA);
if($this->xml->Encrypt){
$cryptmsg->WXBizMsgCrypt(Config::$options['token'],Config::$options['encodingAesKey'], Config::$options['appid']);
$ret = $cryptmsg->decryptMsg($_GET['msg_signature'], $_GET['timestamp'], $_GET['nonce'], $this->postData, $this->xml);
$this->xml = simplexml_load_string($this->xml, 'SimpleXMLElement', LIBXML_NOCDATA);
}
$this->xmlArray = json_decode(json_encode($this->xml),1);
}elseif(Config::$type == 'O'){
$cryptmsg->WXBizMsgCrypt(Config::$options['open']['token'],Config::$options['open']['encodingAesKey'], Config::$options['open']['appid']);
$result = $cryptmsg->decryptMsg($_GET['msg_signature'], $_GET['timestamp'], $_GET['nonce'], $this->postData, $this->xml);
if(0 != $result){
#解密失败
if(function_exists('debug')){
debug('微信数据解密失败'.$result);
}
}else{
debug('=====================进入service.php');
$this->xml = simplexml_load_string($this->xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->xmlArray = json_decode(json_encode($this->xml),1);
}
}else{
$this->xml = simplexml_load_string($this->postData, 'SimpleXMLElement', LIBXML_NOCDATA);
$xmlArray = $this->xmlArray = json_decode(json_encode($this->xml),1);
debug('================$xml'.$xml.'------------$xmlArray'.$xmlArray);
}
}
public function handle($postData){
$this->postData = $postData;//file_get_contents('php://input');
if(!$this->postData){
return $this;
}
debug('=====================执行微信消息解密流程===================');
$cryptmsg = new CryptMsg();
if($this->authType == 'A'){
$this->xml = simplexml_load_string($this->postData, 'SimpleXMLElement', LIBXML_NOCDATA);
if($this->xml->Encrypt){
$cryptmsg->WXBizMsgCrypt($this->options['token'],$this->options['encodingAesKey'], $this->options['appid']);
$ret = $cryptmsg->decryptMsg($_GET['msg_signature'], $_GET['timestamp'], $_GET['nonce'], $this->postData, $this->xml);
$this->xml = simplexml_load_string($this->xml, 'SimpleXMLElement', LIBXML_NOCDATA);
}
$this->xmlArray = json_decode(json_encode($this->xml),1);
}elseif($this->authType == 'O'){
$cryptmsg->WXBizMsgCrypt($this->options['open']['token'],$this->options['open']['encodingAesKey'], $this->options['open']['appid']);
$result = $cryptmsg->decryptMsg($_GET['msg_signature'], $_GET['timestamp'], $_GET['nonce'], $this->postData, $this->xml);
if(0 != $result){
#解密失败
if(function_exists('debug')){
debug('微信数据解密失败'.$result);
}
}else{
$this->xml = simplexml_load_string($this->xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->xmlArray = json_decode(json_encode($this->xml),1);
debug("===========微信信息解密成功==========".PHP_EOL.json_encode($this->xmlArray));
}
}else{
$this->xml = simplexml_load_string($this->postData, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->xmlArray = json_decode(json_encode($this->xml),1);
}
return $this;
}
public function run(){
public function decryptData(){
return $this->xml;
}
public function runArray(){
public function decryptArray(){
return $this->xmlArray;
}
......
File mode changed
File mode changed
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!