1
0
Fork 0
mirror of https://github.com/yunluo/gdk.git synced 2024-05-11 18:56:50 +02:00
gdk/class/wechat.php

105 lines
3 KiB
PHP
Raw Normal View History

2020-02-15 12:37:30 +01:00
<?php
2021-10-10 15:48:53 +02:00
2020-03-26 15:24:00 +01:00
//error_reporting(0);
2020-02-15 12:37:30 +01:00
2020-03-26 17:06:31 +01:00
class Wechat_Captcha
2020-02-15 12:37:30 +01:00
{
2020-03-26 17:06:31 +01:00
public function __construct($wx_token, $wx_captcha)
2020-02-15 12:37:30 +01:00
{
2021-10-10 15:48:53 +02:00
$this->token = $wx_token;
2020-03-26 17:06:31 +01:00
$this->captcha = $wx_captcha;
2020-02-15 12:37:30 +01:00
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
private function __checkSignature()
2020-02-15 12:37:30 +01:00
{
2021-10-10 15:48:53 +02:00
$signature = $_GET['signature'];
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$token = $this->token;
$tmpArr = [$token, $timestamp, $nonce];
2020-02-15 12:37:30 +01:00
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
2021-10-10 15:48:53 +02:00
2020-03-26 17:06:31 +01:00
return $tmpStr == $signature ? true : false;
2020-02-15 12:37:30 +01:00
}
2021-10-10 15:48:53 +02:00
2020-03-26 17:06:31 +01:00
public function responseMsg()
2020-02-15 12:37:30 +01:00
{
//如果是验证请求,则执行签名验证并退出
2021-10-10 15:48:53 +02:00
if (!empty($_GET['echostr'])) {
2020-03-26 17:06:31 +01:00
$this->valid();
//验证签名是否有效
return;
//返回退出
2020-02-15 12:37:30 +01:00
}
2021-10-10 15:48:53 +02:00
if ('POST' != $_SERVER['REQUEST_METHOD']) {
2020-02-15 12:37:30 +01:00
echo '';
2021-10-10 15:48:53 +02:00
2020-02-15 12:37:30 +01:00
return;
}
//如果不是验证请求,则
//首先取得POST原始数据(XML格式)
//$postData = $GLOBALS["HTTP_RAW_POST_DATA"];
$postData = file_get_contents('php://input');
if (empty($postData)) {
echo '';
2021-10-10 15:48:53 +02:00
2020-02-15 12:37:30 +01:00
return;
}
2020-03-26 17:06:31 +01:00
//如果没有POST数据则退出
if (!empty($postData)) {
//解析POST数据(XML格式)
2021-10-10 15:48:53 +02:00
$object = simplexml_load_string($postData, 'SimpleXMLElement', LIBXML_NOCDATA);
$messgeType = trim($object->MsgType); //取得消息类型
2020-04-06 04:24:38 +02:00
$this->fromUser = $object->FromUserName;
2021-10-10 15:48:53 +02:00
$this->toUser = $object->ToUserName;
$keyword = trim($object->Content);
if ('text' == $messgeType && '验证码' == $keyword) {
$response_content = '您的验证码为:【'.$this->captcha.'】验证码有效期为2分钟请抓紧使用过期需重新申请';
$xmlTemplate = '<xml>
2020-03-26 17:06:31 +01:00
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
2021-10-10 15:48:53 +02:00
</xml>';
2020-03-26 17:06:31 +01:00
$xmlText = sprintf($xmlTemplate, $this->fromUser, $this->toUser, time(), $response_content, 0);
echo $xmlText;
}
} else {
2021-10-10 15:48:53 +02:00
echo '';
exit;
}
}
protected function _valid()
{
$echoStr = $_GET['echostr'];
//valid signature , option
if ($this->checkSignature()) {
echo $echoStr;
2020-03-26 17:06:31 +01:00
exit;
2020-02-15 12:37:30 +01:00
}
2021-10-10 15:48:53 +02:00
echo 'error signature';
2020-02-15 12:37:30 +01:00
}
}
2021-10-10 15:48:53 +02:00
function wx_process()
{
if (isset($_GET['signature'])) {
global $wx_captchas;
if (!isset($wx_captchas)) {
$wx_token = gdk_option('gdk_wxmp_token');
$wx_captchas = new Wechat_Captcha($wx_token, wx_captcha());
$wx_captchas->responseMsg();
exit;
}
}
2020-03-15 14:29:06 +01:00
}
add_action('parse_request', 'wx_process', 4);