BasicPushEvent.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/WeChatDeveloper
  12. // +----------------------------------------------------------------------
  13. namespace WeChat\Contracts;
  14. use WeChat\Exceptions\InvalidArgumentException;
  15. use WeChat\Exceptions\InvalidDecryptException;
  16. use WeChat\Exceptions\InvalidResponseException;
  17. /**
  18. * 微信通知处理基本类
  19. * Class BasicPushEvent
  20. * @package WeChat\Contracts
  21. */
  22. class BasicPushEvent
  23. {
  24. /**
  25. * 公众号APPID
  26. * @var string
  27. */
  28. protected $appid;
  29. /**
  30. * 公众号推送XML内容
  31. * @var string
  32. */
  33. protected $postxml;
  34. /**
  35. * 公众号推送加密类型
  36. * @var string
  37. */
  38. protected $encryptType;
  39. /**
  40. * 公众号的推送请求参数
  41. * @var DataArray
  42. */
  43. protected $input;
  44. /**
  45. * 当前公众号配置对象
  46. * @var DataArray
  47. */
  48. protected $config;
  49. /**
  50. * 公众号推送内容对象
  51. * @var DataArray
  52. */
  53. protected $receive;
  54. /**
  55. * 准备回复的消息内容
  56. * @var array
  57. */
  58. protected $message;
  59. /**
  60. * BasicPushEvent constructor.
  61. * @param array $options
  62. * @throws InvalidResponseException
  63. */
  64. public function __construct(array $options)
  65. {
  66. if (empty($options['appid'])) {
  67. throw new InvalidArgumentException("Missing Config -- [appid]");
  68. }
  69. if (empty($options['appsecret'])) {
  70. throw new InvalidArgumentException("Missing Config -- [appsecret]");
  71. }
  72. if (empty($options['token'])) {
  73. throw new InvalidArgumentException("Missing Config -- [token]");
  74. }
  75. // 参数初始化
  76. $this->config = new DataArray($options);
  77. $this->input = new DataArray($_REQUEST);
  78. $this->appid = $this->config->get('appid');
  79. // 推送消息处理
  80. if ($_SERVER['REQUEST_METHOD'] == "POST") {
  81. $this->postxml = file_get_contents("php://input");
  82. $this->encryptType = $this->input->get('encrypt_type');
  83. if ($this->isEncrypt()) {
  84. if (empty($options['encodingaeskey'])) {
  85. throw new InvalidArgumentException("Missing Config -- [encodingaeskey]");
  86. }
  87. if (!class_exists('Prpcrypt', false)) {
  88. require __DIR__ . '/Prpcrypt.php';
  89. }
  90. $prpcrypt = new \Prpcrypt($this->config->get('encodingaeskey'));
  91. $result = Tools::xml2arr($this->postxml);
  92. $array = $prpcrypt->decrypt($result['Encrypt']);
  93. if (intval($array[0]) > 0) {
  94. throw new InvalidResponseException($array[1], $array[0]);
  95. }
  96. list($this->postxml, $this->appid) = [$array[1], $array[2]];
  97. }
  98. $this->receive = new DataArray(Tools::xml2arr($this->postxml));
  99. } elseif ($_SERVER['REQUEST_METHOD'] == "GET" && $this->checkSignature()) {
  100. @ob_clean();
  101. exit($this->input->get('echostr'));
  102. } else {
  103. throw new InvalidResponseException('Invalid interface request.', '0');
  104. }
  105. }
  106. /**
  107. * 消息是否需要加密
  108. * @return boolean
  109. */
  110. public function isEncrypt()
  111. {
  112. return $this->encryptType === 'aes';
  113. }
  114. /**
  115. * 回复消息
  116. * @param array $data 消息内容
  117. * @param boolean $return 是否返回XML内容
  118. * @param boolean $isEncrypt 是否加密内容
  119. * @return string
  120. * @throws InvalidDecryptException
  121. */
  122. public function reply(array $data = [], $return = false, $isEncrypt = false)
  123. {
  124. $xml = Tools::arr2xml(empty($data) ? $this->message : $data);
  125. if ($this->isEncrypt() || $isEncrypt) {
  126. if (!class_exists('Prpcrypt', false)) {
  127. require __DIR__ . '/Prpcrypt.php';
  128. }
  129. $prpcrypt = new \Prpcrypt($this->config->get('encodingaeskey'));
  130. // 如果是第三方平台,加密得使用 component_appid
  131. $component_appid = $this->config->get('component_appid');
  132. $appid = empty($component_appid) ? $this->appid : $component_appid;
  133. $array = $prpcrypt->encrypt($xml, $appid);
  134. if ($array[0] > 0) throw new InvalidDecryptException('Encrypt Error.', '0');
  135. list($timestamp, $encrypt) = [time(), $array[1]];
  136. $nonce = rand(77, 999) * rand(605, 888) * rand(11, 99);
  137. $tmpArr = [$this->config->get('token'), $timestamp, $nonce, $encrypt];
  138. sort($tmpArr, SORT_STRING);
  139. $signature = sha1(implode($tmpArr));
  140. $format = "<xml><Encrypt><![CDATA[%s]]></Encrypt><MsgSignature><![CDATA[%s]]></MsgSignature><TimeStamp>%s</TimeStamp><Nonce><![CDATA[%s]]></Nonce></xml>";
  141. $xml = sprintf($format, $encrypt, $signature, $timestamp, $nonce);
  142. }
  143. if ($return) return $xml;
  144. @ob_clean();
  145. echo $xml;
  146. }
  147. /**
  148. * 验证来自微信服务器
  149. * @param string $str
  150. * @return bool
  151. */
  152. private function checkSignature($str = '')
  153. {
  154. $nonce = $this->input->get('nonce');
  155. $timestamp = $this->input->get('timestamp');
  156. $msg_signature = $this->input->get('msg_signature');
  157. $signature = empty($msg_signature) ? $this->input->get('signature') : $msg_signature;
  158. $tmpArr = [$this->config->get('token'), $timestamp, $nonce, $str];
  159. sort($tmpArr, SORT_STRING);
  160. return sha1(implode($tmpArr)) === $signature;
  161. }
  162. /**
  163. * 获取公众号推送对象
  164. * @param null|string $field 指定获取字段
  165. * @return array
  166. */
  167. public function getReceive($field = null)
  168. {
  169. return $this->receive->get($field);
  170. }
  171. /**
  172. * 获取当前微信OPENID
  173. * @return string
  174. */
  175. public function getOpenid()
  176. {
  177. return $this->receive->get('FromUserName');
  178. }
  179. /**
  180. * 获取当前推送消息类型
  181. * @return string
  182. */
  183. public function getMsgType()
  184. {
  185. return $this->receive->get('MsgType');
  186. }
  187. /**
  188. * 获取当前推送消息ID
  189. * @return string
  190. */
  191. public function getMsgId()
  192. {
  193. return $this->receive->get('MsgId');
  194. }
  195. /**
  196. * 获取当前推送时间
  197. * @return integer
  198. */
  199. public function getMsgTime()
  200. {
  201. return $this->receive->get('CreateTime');
  202. }
  203. /**
  204. * 获取当前推送公众号
  205. * @return string
  206. */
  207. public function getToOpenid()
  208. {
  209. return $this->receive->get('ToUserName');
  210. }
  211. }