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