-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlugin.php
More file actions
402 lines (377 loc) · 15.7 KB
/
Plugin.php
File metadata and controls
402 lines (377 loc) · 15.7 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?php
namespace TypechoPlugin\Access;
use Typecho\Db;
use Typecho\Db\Exception as DbException;
use Typecho\Plugin as TypechoPlugin;
use Typecho\Plugin\Exception as PluginException;
use Typecho\Plugin\PluginInterface;
use Typecho\Request;
use Typecho\Response;
use Typecho\Widget\Helper\Form;
use Typecho\Widget\Helper\Form\Element\Text;
use Typecho\Widget\Helper\Form\Element\Radio;
use Utils\Helper;
use Widget\Notice;
use Widget\Options;
use Widget\Plugins\Edit;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* 图表式访问统计插件 for Typecho
*
* @package Access
* @author Vex
* @version 3.0.1
* @link https://github.com/vndroid/Access
*/
class Plugin implements PluginInterface
{
public static string $panel = 'Access/page/console.php';
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @return string
* @throws DbException
* @throws PluginException
*/
public static function activate(): string
{
if (!extension_loaded('curl')) {
throw new PluginException(_t('检测到当前 PHP 环境缺失 cURL 扩展'));
}
if (!extension_loaded('intl')) {
throw new PluginException(_t('检测到当前 PHP 环境缺失 intl 扩展'));
}
$msg = self::install();
Helper::addPanel(1, self::$panel, _t('访问统计'), _t('统计控制台'), 'subscriber');
Helper::addRoute('access_ip_geo', '/access/geo.json', '\TypechoPlugin\Access\Action', 'ipGeo');
Helper::addRoute('access_track_flag', '/access/track/flag.gif', '\TypechoPlugin\Access\Action', 'writeLogs');
Helper::addRoute('access_logs_delete', '/access/logs/delete.json', '\TypechoPlugin\Access\Action', 'deleteLogs');
Helper::addRoute('access_logs_overview', '/access/overview.json', '\TypechoPlugin\Access\Action', 'overview');
Helper::addRoute('access_logs_details', '/access/logs/get.json', '\TypechoPlugin\Access\Action', 'logsParse');
TypechoPlugin::factory('\Widget\Archive')->beforeRender = [__CLASS__, 'backend'];
TypechoPlugin::factory('\Widget\Archive')->footer = [__CLASS__, 'frontend'];
TypechoPlugin::factory('admin/footer.php')->end = [__CLASS__, 'adminFooter'];
return _t($msg);
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @return string
* @throws DbException
* @throws PluginException
*/
public static function deactivate(): string
{
$cleanFlag = false;
$config = Options::alloc()->plugin(basename(__DIR__));
// 如果 Redis 缓存为启用状态,删除所有缓存键
if (isset($config->redisCache) && $config->redisCache == '1' && extension_loaded('redis')) {
self::clearRedisCache($config);
}
if ($config->isDrop == 1) {
$db = Db::get();
$db->query("DROP TABLE `{$db->getPrefix()}access`", Db::WRITE);
$cleanFlag = true;
}
Helper::removePanel(1, self::$panel);
Helper::removeRoute('access_ip_geo');
Helper::removeRoute('access_track_flag');
Helper::removeRoute('access_logs_delete');
Helper::removeRoute('access_logs_overview');
Helper::removeRoute('access_logs_details');
return _t($cleanFlag ? '插件已禁用,数据表已清除' : '插件已禁用,数据表已保留');
}
/**
* 清除 Redis 中所有 Access 插件的缓存键
*
* @param mixed $config 插件配置
* @return void
*/
private static function clearRedisCache($config): void
{
try {
$redis = new \Redis();
$host = $config->redisHost ?: '127.0.0.1';
$port = (int)($config->redisPort ?: 6379);
if (!$redis->connect($host, $port, 3)) {
return;
}
$password = $config->redisAuth ?? '';
if ($password !== '') {
$redis->auth($password);
}
$redis->ping();
// 使用 SCAN 迭代删除所有匹配前缀的键,避免 KEYS 阻塞
$prefix = 'typecho_access:*';
$iterator = null;
while (($keys = $redis->scan($iterator, $prefix, 100)) !== false) {
if (!empty($keys)) {
$redis->del($keys);
}
}
$redis->close();
} catch (\Exception $e) {
// 清除失败不影响禁用流程
}
}
/**
* 获取插件配置面板
*
* @param Form $form 配置面板
* @return void
*/
public static function config(Form $form): void
{
$pageSize = new Text(
'pageSize', null, '20',
'分页数量', '每页显示的日志数量'
);
$isDrop = new Radio(
'isDrop', [
'0' => '否',
'1' => '是',
], '0', '数据清理', '在禁用插件时,同时删除数据库中历史数据(无法恢复)谨慎修改。'
);
$writeType = new Radio(
'writeType', [
'0' => '前端',
'1' => '后端',
], '1', '统计类型', '日志写入类型(若选择为前端方式,如果使用了 PJAX,请在 PJAX 相关事件中调用 window.Access.track() 方法),若写入速度较慢可选择前端写入日志。'
);
$isPaid = new Radio(
'isPaid', [
'0' => 'Lite',
'1' => 'Core',
], '0', 'IPinfo 接口类型', '默认使用 Lite(免费版),字段相比 Core(付费版)少'
);
$isToken = new Text(
'isToken', null, '',
'IPinfo 接口令牌', '接口调用令牌,请前往 <a href="https://ipinfo.io/dashboard" target="_blank">IPinfo</a> 面板获取'
);
$socks5Host = new Text(
'socks5Host', null, '',
'SOCKS5 代理地址', '格式为[主机:端口],留空则不使用代理'
);
$socks5Auth = new Text(
'socks5Auth', null, '',
'SOCKS5 代理认证', '格式为 [用户名:密码],留空则不使用认证'
);
$redisCache = new Radio(
'redisCache', [
'0' => '禁用',
'1' => '启用',
], '0', '缓存加速',
'启用后来源统计等慢查询结果会缓存至 Redis,提高访问速度'
);
$redisHost = new Text(
'redisHost', null, '127.0.0.1',
'Redis 地址', 'Redis 服务地址,默认为 127.0.0.1'
);
$redisPort = new Text(
'redisPort', null, '6379',
'Redis 端口', 'Redis 服务端口,默认为 6379'
);
$redisAuth = new Text(
'redisAuth', null, '',
'Redis 认证', 'Redis 服务密码,默认留空无密码'
);
$form->addInput($pageSize);
$form->addInput($isDrop);
$form->addInput($writeType);
$form->addInput($isPaid);
$form->addInput($isToken);
$form->addInput($socks5Host);
$form->addInput($socks5Auth);
$form->addInput($redisCache);
$form->addInput($redisHost);
$form->addInput($redisPort->addRule('isInteger', _t('端口必须为纯数字')));
$form->addInput($redisAuth);
}
/**
* 个人用户的配置面板
*
* @param Form $form
* @return void
*/
public static function personalConfig(Form $form)
{
}
/**
* 自定义配置处理,保存前校验 Redis 扩展
*
* @param array $settings 配置值
* @param bool $isInit 是否为初始化
* @return void
* @throws DbException
*/
public static function configHandle(array $settings, bool $isInit): void
{
if (!$isInit && isset($settings['redisCache']) && $settings['redisCache'] == '1') {
if (!extension_loaded('redis')) {
Notice::alloc()->set(_t('启用 Redis 缓存失败:PHP 未安装 redis 扩展,请先安装扩展后再启用'), 'error');
$referer = Request::getInstance()->getReferer();
Response::getInstance()
->setStatus(302)
->setHeader('Location', $referer ?: '/')
->respond();
}
}
Edit::configPlugin('Access', $settings);
}
/**
* 初始化以及升级插件数据库,如初始化失败,直接抛出异常
*
* @return string
* @throws DbException
* @throws PluginException
*/
public static function install(): string
{
if (!str_ends_with(trim(__DIR__, '/\\'), 'Access')) {
throw new PluginException(_t('插件目录名必须为 Access,且首字母大写,请检查插件目录名是否正确'));
}
$db = Db::get();
$adapterName = $db->getAdapterName();
$msg = '';
if (str_contains($adapterName, 'Mysql')) {
$prefix = $db->getPrefix();
$scripts = file_get_contents(__TYPECHO_ROOT_DIR__ . __TYPECHO_PLUGIN_DIR__ . '/Access/sql/MySQL.sql');
$scripts = str_replace('typecho_', $prefix, $scripts);
$scripts = str_replace('%charset%', 'utf8mb4', $scripts);
$scripts = explode(';', $scripts);
try {
$configLink = '<a href="' . Helper::options()->adminUrl('options-plugin.php?config=Access', true) . '">' . _t('前往设置') . '</a>';
# 初始化数据库如果不存在
if (!$db->fetchRow($db->query("SHOW TABLES LIKE '{$prefix}access';", Db::READ))) {
foreach ($scripts as $script) {
$script = trim($script);
if ($script) {
$db->query($script, Db::WRITE);
}
}
$msg = _t('成功创建数据表,插件启用成功,') . $configLink;
}
# 处理旧版本数据
if ($db->fetchRow($db->query("SHOW TABLES LIKE '{$prefix}access_log';", Db::READ))) {
$rows = $db->fetchAll($db->select()->from('table.access_log'));
set_time_limit(1800);
foreach ($rows as $row) {
$ua = new UA($row['ua']);
$row['browser_id'] = $ua->getBrowserID();
$row['browser_version'] = $ua->getBrowserVersion();
$row['os_id'] = $ua->getOSID();
$row['os_version'] = $ua->getOSVersion();
$row['path'] = parse_url($row['url'], PHP_URL_PATH);
$row['query_string'] = parse_url($row['url'], PHP_URL_QUERY);
$row['ip'] = bindec(decbin(ip2long($row['ip'])));
$row['entrypoint'] = $row['referer'];
$row['entrypoint_domain'] = $row['referer_domain'];
$row['time'] = $row['date'];
$row['robot'] = $ua->isRobot() ? 1 : 0;
$row['robot_id'] = $ua->getRobotID();
$row['robot_version'] = $ua->getRobotVersion();
unset($row['date']);
try {
$db->query($db->insert('table.access')->rows($row));
} catch (DbException $e) {
if ($e->getCode() != 23000) {
throw new PluginException(_t('导入旧版数据失败,插件启用失败,错误信息:%s。', $e->getMessage()));
}
}
}
$db->query("DROP TABLE `{$prefix}access_log`;", Db::WRITE);
$msg = _t('检测到旧版数据残留,已更新数据表,插件启用成功,') . $configLink;
}
# 如果已经存在新版数据则跳过
if ($db->fetchRow($db->query("SHOW TABLES LIKE '{$prefix}access';", Db::READ))) {
$msg = _t('数据表已存在,插件启用成功,') . $configLink;
}
return $msg;
} catch (DbException $e) {
throw new PluginException(_t('数据表建立失败,插件启用失败,错误信息:%s。', $e->getMessage()));
} catch (\Exception $e) {
throw new PluginException($e->getMessage());
}
} elseif (str_contains($adapterName, 'SQLite')) {
$prefix = $db->getPrefix();
$scripts = file_get_contents(__TYPECHO_ROOT_DIR__ . __TYPECHO_PLUGIN_DIR__ . '/Access/sql/SQLite.sql');
$scripts = str_replace('typecho_', $prefix, $scripts);
$scripts = explode(';', $scripts);
try {
$configLink = '<a href="' . Helper::options()->adminUrl('options-plugin.php?config=Access', true) . '">' . _t('前往设置') . '</a>';
# 初始化数据库如果不存在
if (!$db->fetchRow($db->query("SELECT name FROM sqlite_master WHERE TYPE='table' AND name='{$prefix}access';", Db::READ))) {
foreach ($scripts as $script) {
$script = trim($script);
if ($script) {
$db->query($script, Db::WRITE);
}
}
$msg = _t('成功创建数据表,插件启用成功,') . $configLink;
} else {
$msg = _t('数据表已经存在,插件启用成功,') . $configLink;
}
return $msg;
} catch (DbException $e) {
throw new PluginException(_t('数据表建立失败,插件启用失败,错误信息:%s。', $e->getMessage()));
} catch (\Exception $e) {
throw new PluginException($e->getMessage());
}
} else {
throw new PluginException(_t('当前适配器为%s,目前只支持 MySQL 和 SQLite', $adapterName));
}
}
/**
* 获取后端统计,该统计方法可以统计到一切访问
*
* @param $archive
* @return void
* @throws PluginException
*/
public static function backend($archive): void
{
$config = Options::alloc()->plugin('Access');
if ($config->writeType == 1) {
$access = new Core();
$access->writeLogs($archive);
}
}
/**
* 获取前端统计,该方法要求客户端必须渲染网页,所以不能统计 RSS 等直接抓取页面的方式
*
* @param $archive
* @return void
* @throws PluginException
*/
public static function frontend($archive): void
{
$config = Options::alloc()->plugin('Access');
if ($config->writeType == 0) {
$index = rtrim(Helper::options()->index, '/');
$access = new Core();
$parsedArchive = $access->parseArchive($archive);
echo "<script type=\"text/javascript\">(function(w){var t=function(){var i=new Image();i.src='{$index}/access/track/flag.gif?u='+location.pathname+location.search+location.hash+'&cid={$parsedArchive['content_id']}&mid={$parsedArchive['meta_id']}&rand='+new Date().getTime()};t();var a={};a.track=t;w.Access=a})(this);</script>";
}
}
/**
* 后台页脚
*
* @return void
*/
public static function adminFooter(): void
{
$url = $_SERVER['PHP_SELF'];
$filename = substr($url, strrpos($url, '/') + 1);
if ($filename === 'index.php') {
echo '<script>
$(document).ready(function() {
$("#start-link").append("<li><a href=\"';
Helper::options()->adminUrl('extending.php?panel=' . self::$panel);
echo '\">' . _t('访问统计') . '</a></li>");
});
</script>';
}
}
}