better-staridc-MNBT
1<?php
2/**
3 * SPA 面板数据接口(JSON 列表与初始化)
4 * 仅在 $egn 匹配时执行
5 */
6$panel_actions = [
7 'monitor_list', 'monitor_log_list', 'notice_list',
8 'backup_list', 'deploy_list', 'set_init', 'pass_list',
9];
10if (!in_array($egn, $panel_actions, true)) {
11 return;
12}
13
14include_once("../MPHX/monitor.function.php");
15if (function_exists('monitor_ensure_tables')) {
16 monitor_ensure_tables($DB);
17}
18
19if ($egn === 'monitor_list') {
20 $tasks = $DB->get_all_prepare(
21 "SELECT * FROM MN_monitor_task WHERE user=? ORDER BY id DESC",
22 [$yhc['user']]
23 ) ?: [];
24 exit(json_encode([
25 'qk' => 1,
26 'code' => '获取成功',
27 'msg' => [
28 'tasks' => $tasks,
29 'task_count' => count($tasks),
30 'max' => 5,
31 ],
32 ], JSON_UNESCAPED_UNICODE));
33}
34
35if ($egn === 'monitor_log_list') {
36 $id = intval($_POST['id'] ?? $_GET['id'] ?? 0);
37 $page = max(1, intval($_POST['page'] ?? 1));
38 $page_size = intval($_POST['page_size'] ?? 20);
39 if (!in_array($page_size, [10, 15, 20, 25, 50, 100], true)) {
40 $page_size = 20;
41 }
42 $where = " WHERE user=?";
43 $params = [$yhc['user']];
44 if ($id > 0) {
45 $where .= " AND task_id=?";
46 $params[] = $id;
47 }
48 $total = (int)$DB->count_prepare("SELECT count(*) FROM MN_monitor_log {$where}", $params);
49 $total_pages = max(1, (int)ceil($total / $page_size));
50 if ($page > $total_pages) $page = $total_pages;
51 $offset = ($page - 1) * $page_size;
52 $logs = $DB->get_all_prepare(
53 "SELECT * FROM MN_monitor_log {$where} ORDER BY id DESC LIMIT {$offset},{$page_size}",
54 $params
55 ) ?: [];
56 exit(json_encode([
57 'qk' => 1,
58 'code' => '获取成功',
59 'msg' => [
60 'logs' => $logs,
61 'total' => $total,
62 'page' => $page,
63 'page_size' => $page_size,
64 'total_pages' => $total_pages,
65 'task_id' => $id,
66 ],
67 ], JSON_UNESCAPED_UNICODE));
68}
69
70if ($egn === 'notice_list') {
71 $type = trim($_POST['type'] ?? '');
72 $level = trim($_POST['level'] ?? '');
73 $read = trim($_POST['read'] ?? '');
74 $keyword = trim($_POST['keyword'] ?? '');
75 $page = max(1, intval($_POST['page'] ?? 1));
76 $page_size = intval($_POST['page_size'] ?? 15);
77 if (!in_array($page_size, [10, 15, 25, 50, 100], true)) {
78 $page_size = 15;
79 }
80 $where = " WHERE user=?";
81 $params = [$yhc['user']];
82 if ($type !== '') {
83 $where .= " AND type=?";
84 $params[] = $type;
85 }
86 if ($level !== '') {
87 $where .= " AND level=?";
88 $params[] = $level;
89 }
90 if ($read === 'true' || $read === 'false') {
91 $where .= " AND is_read=?";
92 $params[] = $read;
93 }
94 if ($keyword !== '') {
95 $where .= " AND (title LIKE ? OR content LIKE ?)";
96 $like = '%' . $keyword . '%';
97 $params[] = $like;
98 $params[] = $like;
99 }
100 $total = (int)$DB->count_prepare("SELECT count(*) FROM MN_notice_log {$where}", $params);
101 $total_pages = max(1, (int)ceil($total / $page_size));
102 if ($page > $total_pages) $page = $total_pages;
103 $offset = ($page - 1) * $page_size;
104 $logs = $DB->get_all_prepare(
105 "SELECT * FROM MN_notice_log {$where} ORDER BY id DESC LIMIT {$offset},{$page_size}",
106 $params
107 ) ?: [];
108 exit(json_encode([
109 'qk' => 1,
110 'code' => '获取成功',
111 'msg' => [
112 'logs' => $logs,
113 'total' => $total,
114 'page' => $page,
115 'page_size' => $page_size,
116 'total_pages' => $total_pages,
117 ],
118 ], JSON_UNESCAPED_UNICODE));
119}
120
121if ($egn === 'backup_list') {
122 include("../class.php");
123 $api = new bt_api($btipe, $btkeye);
124 $hxd = $yhc['hxd'] ?? '';
125 $r_data = $api->Databasebackuplist($hxd) ?: [];
126 $pattern = '/共(\d+)条/';
127 $matches = [];
128 preg_match($pattern, $r_data['page'] ?? '', $matches);
129 $count = $matches[1] ?? 0;
130 $bf_data = $r_data['data'] ?? [];
131 if (!is_array($bf_data)) $bf_data = [];
132 exit(json_encode([
133 'qk' => 1,
134 'code' => '获取成功',
135 'msg' => [
136 'list' => $bf_data,
137 'count' => $count,
138 'db_id' => $hxd,
139 'user' => $yhc['user'] ?? '',
140 ],
141 ], JSON_UNESCAPED_UNICODE));
142}
143
144if ($egn === 'deploy_list') {
145 $rows = $DB->get_all_prepare("SELECT * FROM MN_bs WHERE qk='1' OR qk=1 ORDER BY id DESC") ?: [];
146 // 兼容无 qk 字段
147 if (!$rows) {
148 $rows = $DB->get_all_prepare("SELECT * FROM MN_bs ORDER BY id DESC") ?: [];
149 }
150 $list = [];
151 foreach ($rows as $r) {
152 $src = $r['src'] ?? '[]';
153 $sxpz = $r['sxpz'] ?? '{}';
154 $tj = $r['tj'] ?? '[]';
155 if (is_string($src)) {
156 $decoded = json_decode($src, true);
157 $src = is_array($decoded) ? $decoded : [];
158 }
159 if (is_string($sxpz)) {
160 $decoded = json_decode($sxpz, true);
161 $sxpz = is_array($decoded) ? $decoded : [];
162 }
163 if (is_string($tj)) {
164 $decoded = json_decode($tj, true);
165 $tj = is_array($decoded) ? $decoded : [];
166 }
167 $list[] = [
168 'id' => $r['id'] ?? 0,
169 'name' => $r['name'] ?? '',
170 'jc' => $r['jc'] ?? '',
171 'jg' => $r['jg'] ?? 0,
172 'src' => $src,
173 'sxpz' => $sxpz,
174 'tj' => $tj,
175 'qk' => $r['qk'] ?? '1',
176 ];
177 }
178 exit(json_encode([
179 'qk' => 1,
180 'code' => '获取成功',
181 'msg' => [
182 'list' => $list,
183 'web' => json_decode($yhc['hxa'] ?? '', true) ?: [],
184 'sql' => json_decode($yhc['hxb'] ?? '', true) ?: [],
185 ],
186 ], JSON_UNESCAPED_UNICODE));
187}
188
189if ($egn === 'pass_list') {
190 include("../class.php");
191 $api = new bt_api($btipe, $btkeye);
192 $r = $api->GetLogs($zjid) ?: [];
193 $list = [];
194 if (is_array($r)) {
195 // Linux 宝塔常按站点名分组;Windows 可能直接是列表
196 $site = $yhc['sqldz'] ?? '';
197 if ($site !== '' && isset($r[$site]) && is_array($r[$site])) {
198 $list = $r[$site];
199 } elseif (isset($r[0]) || array_keys($r) === range(0, count($r) - 1)) {
200 $list = $r;
201 } else {
202 foreach ($r as $v) {
203 if (is_array($v) && (isset($v[0]) || isset($v['name']))) {
204 if (isset($v['name'])) {
205 $list[] = $v;
206 } else {
207 $list = array_merge($list, $v);
208 }
209 }
210 }
211 }
212 }
213 if (!is_array($list)) $list = [];
214 exit(json_encode(['qk' => 1, 'code' => '获取成功', 'msg' => ['list' => array_values($list)]], JSON_UNESCAPED_UNICODE));
215}
216
217if ($egn === 'set_init') {
218 $section = trim($_POST['section'] ?? $_POST['gn_section'] ?? 'php');
219 include("../class.php");
220 $api = new bt_api($btipe, $btkeye);
221 $apist = new bt_api_set($btipe, $btkeye);
222 $msg = ['section' => $section];
223
224 if ($section === 'php') {
225 $list = $apist->btapi_listphp();
226 if (is_array($list)) {
227 unset($list[0], $list[1]);
228 $list = array_values($list);
229 } else {
230 $list = [];
231 }
232 $cur = $apist->btapi_phpnowz($yhc['sqldz'] ?? '');
233 $msg['php'] = $cur['phpversion'] ?? '';
234 $msg['list'] = $list;
235 } elseif ($section === 'mrwd') {
236 // 读取:GetIndex(GetLogseb);写入:SetIndex(GetLogsea)
237 // 旧代码误用 GetLogsea('', '') 会触发「默认文档不能为空」
238 $r = $api->GetLogseb($zjid);
239 $index = '';
240 if (is_string($r) && $r !== '') {
241 $index = $r;
242 } elseif (is_array($r)) {
243 // 数字索引列表: ["index.php","index.html"]
244 if (isset($r[0]) && (is_string($r[0]) || is_numeric($r[0]))) {
245 $flat = true;
246 foreach ($r as $item) {
247 if (is_array($item)) { $flat = false; break; }
248 }
249 if ($flat) {
250 $index = implode(',', $r);
251 }
252 }
253 if ($index === '') {
254 // 常见包装: msg / Index / index / data
255 foreach (['Index', 'index', 'msg', 'data', 'result'] as $k) {
256 if (!isset($r[$k])) continue;
257 $v = $r[$k];
258 if (is_string($v) && $v !== '' && strpos($v, '不能为空') === false) {
259 $index = $v;
260 break;
261 }
262 if (is_array($v) && isset($v[0]) && is_string($v[0])) {
263 $index = implode(',', $v);
264 break;
265 }
266 }
267 }
268 }
269 if ($index === '' || $index === null || $index === false
270 || (is_string($index) && (strpos($index, '不能为空') !== false || strpos($index, '失败') !== false))) {
271 $index = 'index.php,index.html,index.htm,default.php,default.htm,default.html';
272 }
273 $msg['index'] = $index;
274 } elseif ($section === 'yxml') {
275 $path = ($os_xt ?? '') . ($yhc['sqldz'] ?? '');
276 $r = $api->yxmlrhq($zjid, $path) ?: [];
277 // 宝塔 GetDirUserINI: runPath.runPath / dirs
278 $rp = $r['runPath'] ?? [];
279 if (is_array($rp)) {
280 $msg['current'] = $rp['runPath'] ?? $rp['path'] ?? '/';
281 $dirs = $rp['dirs'] ?? $r['dirs'] ?? [];
282 } else {
283 $msg['current'] = is_string($rp) ? $rp : ($r['path'] ?? '/');
284 $dirs = $r['dirs'] ?? $r['dir'] ?? [];
285 }
286 if (!is_array($dirs)) $dirs = [];
287 $msg['dirs'] = $dirs;
288 $msg['runPath'] = $r['runPath'] ?? null;
289 } elseif ($section === 'wjt') {
290 $templates = $api->GetLogswr($yhc['sqldz'] ?? '') ?: [];
291 // GetRewriteList 常见 { rewrite: ['wordpress', ...] }
292 if (is_array($templates) && isset($templates['rewrite']) && is_array($templates['rewrite'])) {
293 $templates = $templates['rewrite'];
294 } elseif (is_array($templates) && isset($templates['list'])) {
295 $templates = $templates['list'];
296 } elseif (!is_array($templates)) {
297 $templates = [];
298 }
299 $msg['templates'] = array_values($templates);
300 } elseif ($section === 'gzip') {
301 $r = $api->get_gzip_status($yhc['sqldz'] ?? '') ?: [];
302 // 兼容 data 包装
303 if (isset($r['data']) && is_array($r['data'])) {
304 $r = array_merge($r, $r['data']);
305 }
306 $msg['gzip'] = $r;
307 } elseif ($section === 'cache') {
308 $r = $api->get_static_cache($yhc['sqldz'] ?? '') ?: [];
309 $list = $r['data'] ?? $r['msg'] ?? $r['list'] ?? $r;
310 if (!is_array($list)) $list = [];
311 if (isset($list['status']) && isset($list['data'])) {
312 $list = $list['data'];
313 }
314 if (!is_array($list)) $list = [];
315 // 关联数组转列表
316 if ($list && array_keys($list) !== range(0, count($list) - 1)) {
317 $tmp = [];
318 foreach ($list as $k => $v) {
319 if (is_array($v)) {
320 $tmp[] = array_merge(['suffix' => $v['suffix'] ?? $k], $v);
321 } else {
322 $tmp[] = ['suffix' => $k, 'time_out' => $v];
323 }
324 }
325 $list = $tmp;
326 }
327 $msg['list'] = array_values($list);
328 } elseif ($section === 'xgpass') {
329 $msg['ftp_hint'] = '修改后控制面板登录密码同步为 FTP 密码';
330 } elseif ($section === 'mysqlcz') {
331 $r = $api->GetDatabaseAccess($yhc['sqluser'] ?? '') ?: [];
332 $msg['access'] = $r['msg'] ?? $r['dataAccess'] ?? $r['access'] ?? '127.0.0.1';
333 if (is_array($msg['access'])) {
334 $msg['access'] = $msg['access']['dataAccess'] ?? '127.0.0.1';
335 }
336 } elseif ($section === 'url') {
337 $msg['btip'] = $cert['btip'] ?? '';
338 $msg['als'] = $cert['als'] ?? 'false';
339 } elseif ($section === 'pass') {
340 // 仅元信息;列表走 pass_list
341 $msg['ok'] = true;
342 } else {
343 $msg['ok'] = true;
344 }
345
346 exit(json_encode(['qk' => 1, 'code' => '获取成功', 'msg' => $msg], JSON_UNESCAPED_UNICODE));
347}