This repository has been archived by the owner on Oct 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwechat.php
292 lines (245 loc) · 7.22 KB
/
wechat.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
<?php
if (!defined('ABSPATH')) {
exit();
}
require_once 'sharing-config.php';
// 将数据放到wp-content目录下,免得控制台升级造成配置数据被删除
// WeChat数据目录
define('BOSIMA_WECHAT_DATAPATH', WP_CONTENT_DIR.'/bps-wechat/config/');
/**
* 微信帮助类.
*/
class Bosima_WeChat
{
// 微信访问配置
private $config = null;
//静态变量保存全局实例
private static $instance = null;
/**
* 私有构造函数,防止外界实例化对象
*/
private function __construct()
{
if (!is_dir(BOSIMA_WECHAT_DATAPATH)) {
mkdir(BOSIMA_WECHAT_DATAPATH, 0700, $recursive = true);
}
$this->initConfig();
}
/**
* 单例入口.
*/
public static function getInstance()
{
if (is_null(self::$instance) || !isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* 获取微信配置.
*/
public function getWeChatConfig()
{
return $this->config;
}
/**
* 设置微信AppId.
*/
public function updateStaticConfig($appId, $appSecrect)
{
$this->config->appId = $appId;
$this->config->appSecrect = $appSecrect;
$this->updateConfigFile();
}
/**
* 设置微信AppId.
*/
public function updateAllConfig()
{
$this->updateConfigFile();
}
/**
* 获取JS用签名等相关信息.
*/
public function getSign($url)
{
$jsapiTicket = $this->getJsTicket();
$timestamp = time();
$nonceStr = $this->getNonceString();
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$signStr = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
$signature = sha1($signStr);
$signPackage = array(
'appId' => $this->config->appId,
'nonceStr' => $nonceStr,
'timestamp' => $timestamp,
//'url' => $url,
'signature' => $signature,
//'rawString' => $signStr,
);
return $signPackage;
}
/**
* 获取服务器出口IP.
*/
public static function getOutIp(){
$response = wp_remote_get("http://myip.fireflysoft.net/");
return wp_remote_retrieve_body($response);
}
/**
* 从缓存获取配置信息.
*/
private function initConfig()
{
$this->config = new Bosima_WeChat_AccessConfig();
if (empty($this->config->appId)) {
$this->initConfigFormFile();
}
}
/**
* 从配置文件初始化配置信息.
*/
private function initConfigFormFile()
{
$configPath = BOSIMA_WECHAT_DATAPATH.'data.php';
if (file_exists($configPath)) {
$configContent = trim(substr(file_get_contents($configPath), 15));
if ($configContent) {
$this->config = json_decode($configContent);
}
if ($this->config->appId && $this->config->appSecrect) {
$this->checkAccessToken();
$this->checkJsTicket();
}
}
}
/**
* 更新微信配置文件.
*/
private function updateConfigFile()
{
// 对于写文件进行加锁
$lock = BOSIMA_WECHAT_DATAPATH.'config.lck';
try {
while (true) {
if (file_exists($lock)) {
usleep(100);
} else {
touch($lock);
$configPath = BOSIMA_WECHAT_DATAPATH.'data.php';
$content = json_encode($this->config);
file_put_contents($configPath, '<?php exit();?>'.$content);
if (file_exists($lock)) {
unlink($lock);
}
break;
}
}
} catch (Exception $ex) {
if (file_exists($lock)) {
unlink($lock);
}
}
}
/**
* 获取随机数.
*/
private function getNonceString($length = 16)
{
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$str = '';
$len = strlen($chars);
for ($i = 0; $i < $length; ++$i) {
$str .= substr($chars, mt_rand(0, $len - 1), 1);
}
return $str;
}
/**
* 获取微信接口AccessToken.
*/
private function getAccessToken()
{
$this->checkAccessToken();
return $this->config->accessToken;
}
/**
* 获取微信接口JS Ticket.
*/
private function getJsTicket()
{
$this->checkJsTicket();
return $this->config->jsTicket;
}
/**
* 检查Access Token,如果无效,则获取.
*/
private function checkAccessToken()
{
if (empty($this->config->accessToken) || $this->config->accessTokenDeadline < time()) {
$this->setAccessTokenFromRemote();
}
}
/**
* 检查JS Ticket,如果无效,则获取.
*/
private function checkJsTicket()
{
if (empty($this->config->jsTicket) || $this->config->jsTicketDeadline < time()) {
$this->setJSTicketFromRemote();
}
}
/**
* 通过远程接口设置Access Token.
*/
private function setAccessTokenFromRemote()
{
// 从远程接口获取
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&'.
'appid='.$this->config->appId.
'&secret='.$this->config->appSecrect;
$result = $this->doCurl($url);
if ($result) {
$j = json_decode($result);
if (isset($j->access_token)) {
// 设置到缓存
$this->config->accessToken = $j->access_token;
$this->config->accessTokenDeadline = time() + 7000;
// 保存文件
$this->updateConfigFile();
}
}
}
/**
* 通过远程接口设置JS Ticket.
*/
private function setJSTicketFromRemote()
{
$tmpToken = $this->getAccessToken();
// 从远程接口获取
$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$tmpToken.'&type=jsapi';
$result = $this->doCurl($url);
if ($result) {
$j = json_decode($result);
if (isset($j->errcode) && $j->errcode == '0') {
// 设置到缓存
$this->config->jsTicket = $j->ticket;
$this->config->jsTicketDeadline = time() + 7000;
// 保存文件
$this->updateConfigFile();
}
}
}
/**
* curl get请求
*
* @param url 请求地址
*
* @return 请求响应数据
*/
private function doCurl($url)
{
$response = wp_remote_get($url);
$body = wp_remote_retrieve_body($response);
return $body;
}
}