-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAction.php
More file actions
196 lines (173 loc) · 5.27 KB
/
Action.php
File metadata and controls
196 lines (173 loc) · 5.27 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
<?php
namespace TypechoPlugin\Access;
use RuntimeException;
use Typecho\Widget;
use Widget\ActionInterface;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
class Action extends Widget implements ActionInterface
{
private ?Core $access = null;
private function getAccess(): Core
{
if ($this->access === null) {
$this->access = new Core();
}
return $this->access;
}
public function execute()
{
}
public function action()
{
}
public function writeLogs(): void
{
$image = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAQUAP8ALAAAAAABAAEAAAICRAEAOw==');
$this->response->setContentType('image/gif');
if ($this->getAccess()->config->writeType == 1) {
$this->getAccess()->writeLogs(null, $this->request->get('u'), $this->request->get('cid'), $this->request->get('mid'));
}
echo $image;
}
/**
* ISO 3166-1 alpha-2 国家码转国家名(简体中文)
*
* @param string $code 两位国家码,如 "AU"
* @return string 国家或地区中文名,如 "澳大利亚"
*/
public static function iso2zh(string $code): string
{
if (!preg_match('/^[A-Za-z]{2}$/', $code)) {
return '未知';
}
$zhName = \Locale::getDisplayRegion('-' . strtoupper($code), 'zh_CN');
// 超过 10 个字符时截断
if (mb_strlen($zhName, 'UTF-8') > 10) {
$zhName = mb_substr($zhName, 0, 10, 'UTF-8');
}
return $zhName;
}
public function ipGeo(): void
{
$this->checkAuth();
$ip = $this->request->get('ip');
try {
$result = Ip::find($ip);
if ($result['status'] === 'success') {
$response = [
'code' => 0,
'data' => $result,
'msg' => $result['error'] ?? null,
'i18n' => [
'country' => null,
'region' => null,
'city' => null,
],
];
if (!empty($result['country'])) {
$response['i18n']['country'] = self::iso2zh($result['countryCode']);
}
} else {
$response = [
'code' => 500,
'data' => $result['error'] ?? null,
'msg' => 'ERROR',
'i18n' => null,
];
}
} catch (\Exception $e) {
$response = [
'code' => 500,
'data' => $e->getMessage(),
'msg' => 'ERROR',
'i18n' => null,
];
}
$this->response->throwJson($response);
}
public function deleteLogs(): void
{
$this->checkAuth();
try {
$data = @file_get_contents('php://input');
$data = json_decode($data, true);
if (!is_array($data)) {
throw new RuntimeException('params invalid');
}
$this->getAccess()->deleteLogs($data);
$response = [
'code' => 0,
];
} catch (\Exception $e) {
$response = [
'code' => 100,
'data' => $e->getMessage(),
];
}
$this->response->throwJson($response);
}
/**
* 概览页懒加载数据接口
*/
public function overview(): void
{
$this->checkAuth();
try {
$data = $this->getAccess()->getOverviewData();
$response = [
'code' => 0,
'data' => $data,
];
} catch (\Exception $e) {
$response = [
'code' => 500,
'data' => $e->getMessage(),
];
}
$this->response->throwJson($response);
}
/**
* 日志页懒加载数据接口
*/
public function logsParse(): void
{
$this->checkAuth();
try {
$page = (int)$this->request->get('page', 1);
$type = (int)$this->request->get('type', 1);
$filter = $this->request->get('filter', 'all');
$filterValue = '';
switch ($filter) {
case 'ip': $filterValue = $this->request->get('ip', ''); break;
case 'post': $filterValue = $this->request->get('cid', ''); break;
case 'path': $filterValue = $this->request->get('path', ''); break;
}
$data = $this->getAccess()->getLogsData($page, $type, $filter, $filterValue);
$response = [
'code' => 0,
'data' => $data,
];
} catch (\Exception $e) {
$response = [
'code' => 500,
'data' => $e->getMessage(),
];
}
$this->response->throwJson($response);
}
/**
* 鉴权:非管理员直接返回 403 并终止
*/
protected function checkAuth(): void
{
if (!$this->getAccess()->isAdmin()) {
$this->response->setStatus(403);
$this->response->throwJson([
'code' => 403,
'data' => 'Access Denied',
]);
}
}
}