Callback.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace WeWork;
  3. use SimpleXMLElement;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use WeWork\Crypt\WXBizMsgCrypt;
  6. use WeWork\Message\ReplyMessageInterface;
  7. class Callback
  8. {
  9. /**
  10. * @var Request
  11. */
  12. private $request;
  13. /**
  14. * @var WXBizMsgCrypt
  15. */
  16. private $crypt;
  17. /**
  18. * @param Request $request
  19. * @param WXBizMsgCrypt $crypt
  20. */
  21. public function __construct(Request $request, WXBizMsgCrypt $crypt)
  22. {
  23. $this->request = $request;
  24. $this->crypt = $crypt;
  25. }
  26. /**
  27. * @param string $key
  28. * @return mixed
  29. */
  30. public function get(string $key)
  31. {
  32. if (!$this->request->getContent()) {
  33. return null;
  34. }
  35. if (!$this->request->attributes->has($key)) {
  36. $this->initializeAttribute();
  37. }
  38. return $this->request->attributes->get($key);
  39. }
  40. /**
  41. * @return void
  42. */
  43. private function initializeAttribute(): void
  44. {
  45. $data = '';
  46. $this->crypt->DecryptMsg(
  47. $this->request->query->get('msg_signature'),
  48. $this->request->query->get('timestamp'),
  49. $this->request->query->get('nonce'),
  50. $this->request->getContent(),
  51. $data
  52. );
  53. if ($data) {
  54. $xml = new SimpleXMLElement($data);
  55. foreach ($xml as $key => $value) {
  56. $this->request->attributes->set("$key", "$value");
  57. }
  58. }
  59. }
  60. /**
  61. * @return string
  62. */
  63. private function decryptEchoStr(): string
  64. {
  65. $plainText = '';
  66. $this->crypt->VerifyURL(
  67. $this->request->query->get('msg_signature'),
  68. $this->request->query->get('timestamp'),
  69. $this->request->query->get('nonce'),
  70. $this->request->query->get('echostr'),
  71. $plainText
  72. );
  73. return $plainText;
  74. }
  75. /**
  76. * @param ReplyMessageInterface $replyMessage
  77. * @return string
  78. */
  79. public function reply(ReplyMessageInterface $replyMessage): string
  80. {
  81. if ($this->request->query->has('echostr')) {
  82. return $this->decryptEchoStr();
  83. } else {
  84. return $this->encryptReply($this->buildReply($replyMessage));
  85. }
  86. }
  87. /**
  88. * @param string $reply
  89. * @return string
  90. */
  91. private function encryptReply(string $reply): string
  92. {
  93. $cipherText = '';
  94. $this->crypt->EncryptMsg(
  95. $reply,
  96. $this->request->query->get('timestamp'),
  97. $this->request->query->get('nonce'),
  98. $cipherText
  99. );
  100. return $cipherText;
  101. }
  102. /**
  103. * @param ReplyMessageInterface $replyMessage
  104. * @return string
  105. */
  106. private function buildReply(ReplyMessageInterface $replyMessage): string
  107. {
  108. $reply = $replyMessage->formatForReply();
  109. $reply['ToUserName'] = $this->request->attributes->get('FromUserName');
  110. $reply['FromUserName'] = $this->request->attributes->get('ToUserName');
  111. $reply['CreateTime'] = (int)$this->request->attributes->get('CreateTime');
  112. $element = new SimpleXMLElement('<xml/>');
  113. $this->arrayToXml($reply, $element);
  114. $dom = dom_import_simplexml($element);
  115. return $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement);
  116. }
  117. /**
  118. * @param array $data
  119. * @param SimpleXMLElement $element
  120. * @return void
  121. */
  122. private function arrayToXml(array $data, SimpleXMLElement &$element): void
  123. {
  124. foreach ($data as $key => $value) {
  125. if (is_numeric($key)) {
  126. $key = 'item';
  127. }
  128. if (is_array($value)) {
  129. $subNode = $element->addChild($key);
  130. $this->arrayToXml($value, $subNode);
  131. } elseif (is_string($value)) {
  132. $node = dom_import_simplexml($element->addChild($key));
  133. $no = $node->ownerDocument;
  134. $node->appendChild($no->createCDATASection($value));
  135. } else {
  136. $element->addChild($key, $value);
  137. }
  138. }
  139. }
  140. }