You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.7 KiB

2 months ago
<?php
namespace LaneWeChat\Core;
class Wechat{
private $debug;
private $request;
public function __construct($token, $debug = FALSE) {
if ($this->isValid() && $this->validateSignature($token)) {
return $_GET['echostr'];
}
$this->debug = $debug;
$xml = (array) simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'], 'SimpleXMLElement', LIBXML_NOCDATA);
$this->request = array_change_key_case($xml, CASE_LOWER);
}
private function isValid() {
return isset($_GET['echostr']);
}
private function validateSignature($token) {
$signature = $_GET['signature'];
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$signatureArray = array($token, $timestamp, $nonce);
sort($signatureArray, SORT_STRING);
return sha1(implode($signatureArray)) == $signature;
}
protected function getRequest($param = FALSE) {
if ($param === FALSE) {
return $this->request;
}
$param = strtolower($param);
if (isset($this->request[$param])) {
return $this->request[$param];
}
return NULL;
}
public function run() {
return WechatRequest::switchType($this->request);
}
public function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = WECHAT_TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
echo $_GET['echostr'];
return true;
}else{
return false;
}
}
}