-
Notifications
You must be signed in to change notification settings - Fork 9
/
weixin.module
176 lines (161 loc) · 4.14 KB
/
weixin.module
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
<?php
/**
* @file 微信公众平台开发者模式
*/
/**
* Implements hook_permission().
*/
function weixin_permission() {
return array(
'administer weixin' => array(
'title' => '管理微信',
'description' => '微信公众平台配置权限',
),
);
}
/**
* Implements hook_menu().
*/
function weixin_menu() {
$items = array();
$items['weixin'] = array(
'title' => '微信API',
'page callback' => 'weixin_api_callback',
'access callback' => TRUE,
);
$items['admin/config/services/weixin'] = array(
'title' => '微信',
'description' => '配置微信公众平台',
'page callback' => 'drupal_get_form',
'page arguments' => array('weixin_info_setting'),
'access arguments' => array('administer weixin'),
'file' => 'weixin.inc',
);
return $items;
}
/**
* 微信回调页面
*/
function weixin_api_callback() {
$query = drupal_get_query_parameters();
if (checkSignature($query)) {
//首次调用验证成为开发者
if (isset($query['echostr'])) {
echo $query['echostr'];
exit;
}
//回应信息
responseMsg();
}
}
/**
* 验证信息来自微信
*/
function checkSignature($query) {
$signature = $query['signature'];
$timestamp = $query['timestamp'];
$nonce = $query['nonce'];
$token = variable_get('weixin_token');
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if ($tmpStr == $signature) {
return true;
}
else {
return false;
}
}
/**
* 回应信息
*/
function responseMsg() {
//获取post数据,xml
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//处理数据
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$getMsgType = $postObj->MsgType;
$time = time();
$msgType = 'text';
$textTpl = '<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>';
switch ($getMsgType) {
case 'text':
$keyword = $postObj->Content;
$contentStr = weixin_get_content($keyword, $msgType, $textTpl);
break;
case 'voice':
$keyword = $postObj->Recognition;
$contentStr = weixin_get_content($keyword, $msgType, $textTpl);
break;
case 'event':
if ($postObj->Event == 'subscribe') {
$contentStr = variable_get('weixin_greeting');
}
else {
print '';
exit;
}
break;
default:
$contentStr = variable_get('weixin_noindentify');
break;
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
print $resultStr;
}
else {
print '';
exit;
}
}
/**
* 根据关键字搜索内容
*/
function weixin_get_content($keyword, &$msgType, &$textTpl) {
//默认搜索node
$search_type = 'node';
$select_types = variable_get('weixin_node_types', array());
foreach ($select_types as $key => $value) {
if ($value == '0') {
unset($select_types[$key]);
}
}
$types_string = count($select_types) > 0 ? ' type:' . implode(',', $select_types) : '';
$results = module_invoke('search', 'data', $keyword . $types_string, $search_type);
$count_results = count($results['#results']);
if ($count_results > 0) {
$msgType = 'news';
$textTpl = '<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
%s
</xml>';
$output = '<ArticleCount>' . $count_results . '</ArticleCount>';
$output .= '<Articles>';
//只放标题和链接 最多10条记录
$itemTpl = '<item>
<Title><![CDATA[%s]]></Title>
<Url><![CDATA[%s]]></Url>
</item>';
foreach ($results['#results'] as $result) {
$output .= sprintf($itemTpl, $result['title'], $result['link']);
}
$output .= '</Articles>';
}
else {
$output = variable_get('weixin_noresult');
}
return $output;
}