Receive.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php namespace Wechat\Api;
  2. use Dever;
  3. use Api\Lib\Platform\Request;
  4. class Receive
  5. {
  6. # 获取消息
  7. public function callback()
  8. {
  9. $input = Dever::input();
  10. $input['body'] = file_get_contents('php://input', 'r');
  11. $this->log($input);
  12. $test = Dever::input('test');
  13. if ($test == 1) {
  14. $input = '{"l":"receive.callback","m":"auth","signature":"60e9631eb2b17edbbe1805dc20733ddd9c29d238","timestamp":"1724457958","nonce":"942288454","encrypt_type":"aes","msg_signature":"fbaf4080e53d54253cd898644f56535b3e81e64f","body":"<xml>\n <AppId><![CDATA[wxa787f5f39aa0598c]]></AppId>\n <Encrypt><![CDATA[289f2UFxfqOMRCkY6dF4L7b0RuTcVxcKGVEZU3nhJHuTlgA2uvDrTV5YhppDyoJYGIFf2KenpUB4Xg+1wYefxLnYGV8UyJMVZIzlLyPFWMgV8Jw+ElwN2vogPvJ+ceXwDdpzPcLuf7h9MPoedlQqh9Kv0wjjJ6wX8M9i4GZQjYhKWWCcj6hgyymt4eTPJmJ4cYh+X4a2v883Srt581uQSOa+lAv8jbvtYRVk/23qRn5FsOSeIzzGgoj0o4VGZx/muOl0kMQI8B5lZDKCtCeSljWj14c6UQ/5OQh2M22LYLi3PcE3zcoDa5cOni5adUPQQX5sWoAsRBj9U6K+idvG88XLLutA/kM4F+MaX1lcBMhq49DTN2I3qZRzddPdutZi70E/+A7ou9zzZUArgDuuCz35P40/BFGoS8dh9rGXvDfnYlS549XJ0J4fJ14X12q7LImBvr61ugNKFTMGJapgJg==]]></Encrypt>\n</xml>\n"}';
  15. $input = Dever::json_decode($input);
  16. }
  17. $input['body'] = (array) simplexml_load_string($input['body'], null, LIBXML_NOCDATA);
  18. $input = array_merge($input, $input['body']);
  19. $api = Dever::load('account', 'api')->run('wechat_open', 'msg', $input, 1, 'setting');
  20. $request = new Request($api->field, $api->platform['id'], $api->type, $api->info['id']);
  21. $body = $request->body();
  22. if ($body['sign'] != $input['msg_signature']) {
  23. Dever::error('签名验证失败');
  24. }
  25. #$input['Encrypt'] = base64_decode($input['Encrypt']);
  26. $api->field->key = base64_decode($api->field->key . '=');
  27. $iv = substr($api->field->key, 0, 16);
  28. $input['Encrypt'] = str_replace(' ', '+', $input['Encrypt']);
  29. $body = openssl_decrypt($input['Encrypt'], 'AES-256-CBC', substr($api->field->key, 0, 32), OPENSSL_ZERO_PADDING, $iv);
  30. $pkc_encoder = new PKCS7Encoder;
  31. $body = $pkc_encoder->decode($body);
  32. if (strlen($body) < 16)
  33. Dever::error('解密失败');
  34. $body = substr($body, 16, strlen($body));
  35. $len_list = unpack("N", substr($body, 0, 4));
  36. $xml_len = $len_list[1];
  37. $xml_content = substr($body, 4, $xml_len);
  38. $appid = substr($body, $xml_len + 4);
  39. if ($appid != $api->field->appid) {
  40. Dever::error('第三方平台配置错误');
  41. }
  42. $body = (array) simplexml_load_string($xml_content, null, LIBXML_NOCDATA);
  43. $m = $input['m'];
  44. if ($m == 'auth') {
  45. # 权限
  46. $this->auth($body);
  47. } else {
  48. $this->msg($m, $body);
  49. }
  50. echo 'success';die;
  51. }
  52. # 获取权限信息
  53. private function auth($body)
  54. {
  55. if (isset($body['ComponentVerifyTicket'])) {
  56. Dever::load('info', 'wechat')->up(false, 'component_verify_ticket', $body['ComponentVerifyTicket'], 12*3600);
  57. }
  58. }
  59. # 获取消息
  60. private function msg($m, $body)
  61. {
  62. list($method, $appid) = explode('/', $m);
  63. }
  64. # 记录日志
  65. private function log($log)
  66. {
  67. return Dever::log($log, 'wechat');
  68. }
  69. }
  70. /**
  71. * PKCS7Encoder class
  72. *
  73. * 提供基于PKCS7算法的加解密接口.
  74. */
  75. class PKCS7Encoder
  76. {
  77. public static $block_size = 32;
  78. /**
  79. * 对需要加密的明文进行填充补位
  80. * @param $text 需要进行填充补位操作的明文
  81. * @return 补齐明文字符串
  82. */
  83. function encode($text)
  84. {
  85. $block_size = PKCS7Encoder::$block_size;
  86. $text_length = strlen($text);
  87. //计算需要填充的位数
  88. $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
  89. if ($amount_to_pad == 0) {
  90. $amount_to_pad = PKCS7Encoder::block_size;
  91. }
  92. //获得补位所用的字符
  93. $pad_chr = chr($amount_to_pad);
  94. $tmp = "";
  95. for ($index = 0; $index < $amount_to_pad; $index++) {
  96. $tmp .= $pad_chr;
  97. }
  98. return $text . $tmp;
  99. }
  100. /**
  101. * 对解密后的明文进行补位删除
  102. * @param decrypted 解密后的明文
  103. * @return 删除填充补位后的明文
  104. */
  105. function decode($text)
  106. {
  107. $pad = ord(substr($text, -1));
  108. if ($pad < 1 || $pad > 32) {
  109. $pad = 0;
  110. }
  111. return substr($text, 0, (strlen($text) - $pad));
  112. }
  113. }