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