<?php
namespace WeChat\Contracts;
use WeChat\Exceptions\InvalidArgumentException;
use WeChat\Exceptions\InvalidDecryptException;
use WeChat\Exceptions\InvalidResponseException;
class BasicPushEvent
{
protected $appid;
protected $postxml;
protected $encryptType;
protected $config;
protected $params;
protected $receive;
protected $message;
public function __construct(array $options)
{
if (empty($options['appid'])) {
throw new InvalidArgumentException("Missing Config -- [appid]");
}
if (empty($options['appsecret'])) {
throw new InvalidArgumentException("Missing Config -- [appsecret]");
}
if (empty($options['token'])) {
throw new InvalidArgumentException("Missing Config -- [token]");
}
$this->config = new DataArray($options);
$this->params = new DataArray($_REQUEST);
$this->appid = $this->config->get('appid');
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$this->postxml = file_get_contents("php://input");
$this->encryptType = $this->params->get('encrypt_type');
if ($this->encryptType == 'aes') {
if (empty($options['encodingaeskey'])) {
throw new InvalidArgumentException("Missing Config -- [encodingaeskey]");
}
if (!class_exists('Prpcrypt', false)) {
require __DIR__ . '/Prpcrypt.php';
}
$prpcrypt = new \Prpcrypt($this->config->get('encodingaeskey'));
$result = Tools::xml2arr($this->postxml);
$array = $prpcrypt->decrypt($result['Encrypt']);
if (intval($array[0]) > 0) {
throw new InvalidResponseException($array[1], $array[0]);
}
list($this->postxml, $this->appid) = [$array[1], $array[2]];
}
$this->receive = new DataArray(Tools::xml2arr($this->postxml));
} elseif ($_SERVER['REQUEST_METHOD'] == "GET" && $this->checkSignature()) {
@ob_clean();
exit($this->params->get('echostr'));
} else {
throw new InvalidResponseException('Invalid interface request.', '0');
}
}
public function reply(array $data = [], $return = false)
{
$xml = Tools::arr2xml(empty($data) ? $this->message : $data);
if ($this->encryptType == 'aes') {
if (!class_exists('Prpcrypt', false)) {
require __DIR__ . '/Prpcrypt.php';
}
$prpcrypt = new \Prpcrypt($this->config->get('encodingaeskey'));
$component_appid = $this->config->get('component_appid');
$appid = empty($component_appid) ? $this->appid : $component_appid;
$array = $prpcrypt->encrypt($xml, $appid);
if ($array[0] > 0) {
throw new InvalidDecryptException('Encrypt Error.', '0');
}
list($timestamp, $encrypt) = [time(), $array[1]];
$nonce = rand(77, 999) * rand(605, 888) * rand(11, 99);
$tmpArr = [$this->config->get('token'), $timestamp, $nonce, $encrypt];
sort($tmpArr, SORT_STRING);
$signature = sha1(implode($tmpArr));
$format = "<xml><Encrypt><![CDATA[%s]]></Encrypt><MsgSignature><![CDATA[%s]]></MsgSignature><TimeStamp>%s</TimeStamp><Nonce><![CDATA[%s]]></Nonce></xml>";
$xml = sprintf($format, $encrypt, $signature, $timestamp, $nonce);
}
if ($return) {
return $xml;
}
@ob_clean();
echo $xml;
}
private function checkSignature($str = '')
{
$nonce = $this->params->get('nonce');
$timestamp = $this->params->get('timestamp');
$msg_signature = $this->params->get('msg_signature');
$signature = empty($msg_signature) ? $this->params->get('signature') : $msg_signature;
$tmpArr = [$this->config->get('token'), $timestamp, $nonce, $str];
sort($tmpArr, SORT_STRING);
if (sha1(implode($tmpArr)) == $signature) {
return true;
}
return false;
}
public function getReceive($field = null)
{
return $this->receive->get($field);
}
public function getOpenid()
{
return $this->receive->get('FromUserName');
}
public function getMsgType()
{
return $this->receive->get('MsgType');
}
public function getMsgId()
{
return $this->receive->get('MsgId');
}
public function getMsgTime()
{
return $this->receive->get('CreateTime');
}
public function getToOpenid()
{
return $this->receive->get('ToUserName');
}
}