-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweixin-robot-class.php
309 lines (262 loc) · 8.23 KB
/
weixin-robot-class.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
class wechatCallback {
private $postObj = '';
private $fromUsername = '';
private $toUsername = '';
private $response = '';
public function valid(){
//file_put_contents(WP_CONTENT_DIR.'/uploads/weixin.log',var_export($_GET,true));
if(isset($_GET['debug'])){
$this->checkSignature();
$this->responseMsg();
}else{
if($this->checkSignature() || isset($_GET['yixin'])){
if(isset($_GET["echostr"])){
$echoStr = $_GET["echostr"];
echo $echoStr;
}
$this->responseMsg();
exit;
}
}
}
public function responseMsg(){
$postStr = (isset($GLOBALS["HTTP_RAW_POST_DATA"]))?$GLOBALS["HTTP_RAW_POST_DATA"]:'';
//file_put_contents(WP_CONTENT_DIR.'/uploads/test.html',var_export($postStr,true));
$keyword = '';
if (isset($_GET['debug']) || !empty($postStr)){
if(isset($_GET['debug'])){
$this->fromUsername = $this->toUsername = '';
$keyword = strtolower(trim($_GET['t']));
}else{
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->postObj = $postObj;
$this->fromUsername = (string)$postObj->FromUserName;
$this->toUsername = (string)$postObj->ToUserName;
$msgType = strtolower(trim($postObj->MsgType));
if($msgType == 'text'){ // 文本消息
$keyword = strtolower(trim($postObj->Content));
}elseif($msgType == 'event'){ // 事件消息
$event = strtolower(trim($postObj->Event));
if(in_array($event, array('subscribe', 'unsubscribe'))) { // 订阅和取消订阅事件
$keyword = $event;
}elseif($event == 'click'){ //点击事件
$keyword = strtolower(trim($postObj->EventKey));
}elseif($event == 'view'){ //查看网页事件,估计也进不来。
$keyword = '[view]';
}elseif($event == 'location'){ // 高级接口,用户自动提交地理位置事件。
$keyword = '[event-location]';
}
}else{
if(isset($postObj->Recognition) && trim($postObj->Recognition)){ // 如果已经识别了语言,识别之后的文字作为关键字
$keyword = strtolower(trim($postObj->Recognition));
}else{ // 其他消息,统一处理成关键字为 [{消息类}] ,后面再做处理。
$keyword = '['.$msgType.']';
}
}
}
$pre = apply_filters('weixin_custom_keyword', false, $keyword);
if($pre == false){ // 如果不是自定义的关键字,就直接搜索回复。
$this->query($keyword);
}
do_action('weixin_robot',$this); // 已经执行了一次完整的信息自动回复
}else {
echo "";
}
exit;
}
public function query($keyword=''){
// $weixin_count = weixin_robot_get_setting('weixin_count');
//
// // 获取除 page 和 attachmet 之外的所有日志类型
// $post_types = get_post_types( array('exclude_from_search' => false) );
// unset($post_types['page']);
// unset($post_types['attachment']);
//
// $weixin_query_array = array(
// 's' => $keyword,
// 'ignore_sticky_posts' => true,
// 'posts_per_page' => $weixin_count,
// 'post_status' => 'publish',
// 'post_type' => $post_types
// );
$weixin_query_array = $this->get_query_param($keyword);
$weixin_query_array = apply_filters('weixin_query',$weixin_query_array);
if(empty($this->response)){
if(isset($weixin_query_array['s'])){
$this->response = 'query';
}elseif(isset($weixin_query_array['cat'])){
$this->response = 'cat';
}elseif(isset($weixin_query_array['tag_id'])){
$this->response = 'tag';
}
}
global $wp_the_query;
$wp_the_query->query($weixin_query_array);
$items = '';
$counter = 0;
if($wp_the_query->have_posts()){
while ($wp_the_query->have_posts()) {
$wp_the_query->the_post();
$title = apply_filters('weixin_title', get_the_title());
$excerpt= apply_filters('weixin_description', get_post_excerpt( '',apply_filters( 'weixin_description_length', 150 ) ) );
$url = apply_filters('weixin_url', get_permalink());
if($counter == 0){
$thumb = get_post_weixin_thumb('', array(640,320));
}else{
$thumb = get_post_weixin_thumb('', array(80,80));
}
$items = $items . $this->get_item($title, $excerpt, $thumb, $url);
$counter ++;
}
}
$articleCount = count($wp_the_query->posts);
if($articleCount > $weixin_query_array['posts_per_page']) $articleCount = $weixin_query_array['posts_per_page'];
if($articleCount){
echo sprintf($this->get_picTpl(),$articleCount,$items);
}else{
weixin_robot_not_found_reply($keyword);
}
}
public function get_query_param($keyword) {
$weixin_count = weixin_robot_get_setting('weixin_count');
// 获取除 page 和 attachmet 之外的所有日志类y
$post_types = get_post_types( array('exclude_from_search' => false) );
unset($post_types['page']);
unset($post_types['attachment']);
$weixin_query_array = array(
's' => $keyword,
'ignore_sticky_posts' => true,
'posts_per_page' => $weixin_count,
'post_status' => 'publish',
'post_type' => $post_types
);
return $weixin_query_array;
}
public function get_item($title, $description, $picUrl, $url){
if(!$description) $description = $title;
return
'
<item>
<Title><![CDATA['.html_entity_decode($title, ENT_QUOTES, "utf-8" ).']]></Title>
<Description><![CDATA['.html_entity_decode($description, ENT_QUOTES, "utf-8" ).']]></Description>
<PicUrl><![CDATA['.$picUrl.']]></PicUrl>
<Url><![CDATA['.$url.']]></Url>
</item>
';
}
public function get_fromUsername(){ // 微信的 USER OpenID
return $this->fromUsername;
}
public function get_response(){
return $this->response;
}
private function get_basicTpl(){
return "
<ToUserName><![CDATA[".$this->fromUsername."]]></ToUserName>
<FromUserName><![CDATA[".$this->toUsername."]]></FromUserName>
<CreateTime>".time()."</CreateTime>
";
}
public function get_textTpl(){
return "
<xml>".$this->get_basicTpl()."
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>
";
}
public function get_picTpl(){
return "
<xml>".$this->get_basicTpl()."
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%d</ArticleCount>
<Articles>
%s
</Articles>
</xml>
";
}
public function get_imageTpl(){
return "
<xml>".$this->get_basicTpl()."
<MsgType><![CDATA[image]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
</xml>
";
}
public function get_voiceTpl(){
return "
<xml>".$this->get_basicTpl()."
<MsgType><![CDATA[voice]]></MsgType>
<Voice>
<MediaId><![CDATA[%s]]></MediaId>
</Voice>
</xml>
";
}
public function get_videoTpl(){
return "
<xml>".$this->get_basicTpl()."
<MsgType><![CDATA[video]]></MsgType>
<Video>
<MediaId><![CDATA[%s]]></MediaId>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
</Video>
</xml>
";
}
public function get_musicTpl(){
return "
<xml>".$this->get_basicTpl()."
<MsgType><![CDATA[music]]></MsgType>
<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
</Music>
</xml>
";
}
public function get_transfer_customer_serviceTpl(){
return "
<xml>".$this->get_basicTpl()."
<MsgType><![CDATA[transfer_customer_service]]></MsgType>
</xml>
";
}
public function get_msgType(){
return $this->msgType;
}
public function get_postObj(){
return $this->postObj;
}
public function set_response($response){
$this->response = $response;
}
private function checkSignature(){
$signature = isset($_GET["signature"])?$_GET["signature"]:'';
$timestamp = isset($_GET["timestamp"])?$_GET["timestamp"]:'';
$nonce = isset($_GET["nonce"])?$_GET["nonce"]:'';
$weixin_token = weixin_robot_get_setting('weixin_token');
if(isset($_GET['debug'])){
echo 'WEIXIN_TOKEN:'.$weixin_token."\n";
}
$tmpArr = array($weixin_token, $timestamp, $nonce);
//sort($tmpArr);
sort($tmpArr,SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}