仰望星辰工作室

better-staridc-MNBT

better-staridc-MNBT/ MPHX/bt_api.php 54.0 KB · 1,503 行 原始文件
Z zfhsh first commit 1 天前
1<?php
2if (!defined('IN_CRONLITE')) exit();
3
4/**
5 * 统一宝塔面板API操作类
6 * 合并自: class.php, admin/class.php, user/class.php, api/api.class.php
7 */
8
9class bt_api
10{
11 public $BT_PANEL;
12 public $BT_KEY;
13
14 public function __construct($bt_panel = null, $bt_key = null)
15 {
16 $this->BT_PANEL = $bt_panel;
17 $this->BT_KEY = $bt_key;
18 }
19
20 // ========================================================================
21 // 站点管理
22 // ========================================================================
23
24 public function webkt($userq, $passq, $btserw, $cptypr, $ftpr, $sqlr, $mrbb, $mrml)
25 {
26 $url = $this->BT_PANEL . '/site?action=AddSite';
27 $p_data = $this->GetKeyData();
28 $p_data['webname'] = '{"domain":"' . $btserw . '","domainlist":[],"count":0}';
29 $p_data['path'] = $mrml;
30 $p_data['type_id'] = '0';
31 $p_data['type'] = 'PHP';
32 $p_data['version'] = $mrbb;
33 $p_data['port'] = '80';
34 $p_data['ps'] = 'MNBT开通的' . $cptypr;
35 $p_data['ftp'] = $ftpr;
36 $p_data['ftp_username'] = $userq;
37 $p_data['ftp_password'] = $passq;
38 $p_data['sql'] = $sqlr;
39 $p_data['codeing'] = 'utf8';
40 $p_data['datauser'] = $userq;
41 $p_data['datapassword'] = $passq;
42 return json_decode($this->HttpPostCookie($url, $p_data), true);
43 }
44
45 public function delsite($id, $webname)
46 {
47 $url = $this->BT_PANEL . '/site?action=DeleteSite';
48 $p_data = $this->GetKeyData();
49 $p_data['id'] = $id;
50 $p_data['webname'] = $webname;
51 $p_data['ftp'] = '1';
52 $p_data['database'] = '1';
53 $p_data['path'] = '1';
54 return json_decode($this->HttpPostCookie($url, $p_data), true);
55 }
56
57 public function stopjq($id, $name)
58 {
59 return $this->siteqt($id, $name, false);
60 }
61
62 public function ztweb($id, $name)
63 {
64 return $this->siteqt($id, $name, false);
65 }
66
67 public function qdweb($id, $name)
68 {
69 return $this->siteqt($id, $name, true);
70 }
71
72 public function siteqt($id, $name, $start)
73 {
74 $url = $this->BT_PANEL . ($start ? '/site?action=SiteStart' : '/site?action=SiteStop');
75 $p_data = $this->GetKeyData();
76 $p_data['id'] = $id;
77 $p_data['name'] = $name;
78 return json_decode($this->HttpPostCookie($url, $p_data), true);
79 }
80
81 public function setdqsj($id, $edate)
82 {
83 $url = $this->BT_PANEL . '/site?action=SetEdate';
84 $p_data = $this->GetKeyData();
85 $p_data['id'] = $id;
86 $p_data['edate'] = $edate;
87 return json_decode($this->HttpPostCookie($url, $p_data), true);
88 }
89
90 public function get_site_domains($siteid)
91 {
92 $url = $this->BT_PANEL . '/site?action=GetSiteDomains';
93 $p_data = $this->GetKeyData();
94 $p_data['id'] = $siteid;
95 return json_decode($this->HttpPostCookie($url, $p_data), true);
96 }
97
98 public function sitemsg($sitename)
99 {
100 $url = $this->BT_PANEL . '/data?action=getData';
101 $p_data = $this->GetKeyData();
102 $p_data['table'] = 'sites';
103 $p_data['limit'] = 200;
104 $p_data['p'] = 1;
105 $p_data['search'] = $sitename;
106 $p_data['type'] = -1;
107 $result = json_decode($this->HttpPostCookie($url, $p_data), true);
108 $data = $result['data'] ?? [];
109 if (empty($data)) return ['code' => false, 'msg' => '无主机信息!'];
110 foreach ($data as $val) {
111 if ($val['name'] == $sitename) return ['code' => true, 'msg' => $val];
112 }
113 return ['code' => false, 'msg' => '无主机信息!'];
114 }
115
116 // ========================================================================
117 // 域名管理
118 // ========================================================================
119
120 public function btapi_ym($zdid)
121 {
122 return $this->get_domain_list($zdid);
123 }
124
125 public function GetLogsy($zdid)
126 {
127 $url = $this->BT_PANEL . '/data?action=getData&table=domain';
128 $p_data = $this->GetKeyData();
129 $p_data['search'] = $zdid;
130 $p_data['list'] = 'true';
131 return json_decode($this->HttpPostCookie($url, $p_data), true);
132 }
133
134 public function get_domain_list($search)
135 {
136 $url = $this->BT_PANEL . '/data?action=getData';
137 $p_data = $this->GetKeyData();
138 $p_data['search'] = $search;
139 $p_data['list'] = 'True';
140 $p_data['table'] = 'domain';
141 return json_decode($this->HttpPostCookie($url, $p_data), true);
142 }
143
144 public function Getymlist($id)
145 {
146 return $this->get_domain_list($id);
147 }
148
149 public function btapi_addym($siteid, $webname, $domain)
150 {
151 if (strpos($domain, "\n") !== false) {
152 return ['status' => false, 'msg' => '不能使用换行符!'];
153 }
154 $url = $this->BT_PANEL . '/site?action=AddDomain';
155 $p_data = $this->GetKeyData();
156 $p_data['id'] = $siteid;
157 $p_data['webname'] = $webname;
158 $p_data['domain'] = $domain;
159 $result = json_decode($this->HttpPostCookie($url, $p_data), true);
160 return $result['domains'][0] ?? $result;
161 }
162
163 public function btapi_delym($siteid, $webname, $domain, $port)
164 {
165 $url = $this->BT_PANEL . '/site?action=DelDomain';
166 $p_data = $this->GetKeyData();
167 $p_data['id'] = $siteid;
168 $p_data['webname'] = $webname;
169 $p_data['domain'] = $domain;
170 $p_data['port'] = $port;
171 return json_decode($this->HttpPostCookie($url, $p_data), true);
172 }
173
174 public function urlzmlls($siteid)
175 {
176 $url = $this->BT_PANEL . '/site?action=GetDirBinding';
177 $p_data = $this->GetKeyData();
178 $p_data['id'] = $siteid;
179 return json_decode($this->HttpPostCookie($url, $p_data), true);
180 }
181
182 public function addzml($siteid, $domain, $dir, $sitepath)
183 {
184 if (strpos($domain, "\n") !== false) {
185 return ['status' => false, 'msg' => '不能使用换行符!'];
186 }
187 $url = $this->BT_PANEL . '/site?action=AddDirBinding';
188 $p_data = $this->GetKeyData();
189 $p_data['id'] = $siteid;
190 $p_data['domain'] = $domain;
191 $p_data['dirName'] = $dir;
192 $data = json_decode($this->HttpPostCookie($url, $p_data), true);
193 if ($data['status']) {
194 $getyxml = $this->yxmlrhq($siteid, $sitepath);
195 if ($getyxml['runPath']['runPath'] != '/') {
196 $this->filecopy($sitepath . $getyxml['runPath']['runPath'] . '/.user.ini', $sitepath . $getyxml['runPath']['runPath'] . '/' . $dir . '/.user.ini');
197 } else {
198 $this->filecopy($sitepath . $getyxml['runPath']['runPath'] . '.user.ini', $sitepath . $getyxml['runPath']['runPath'] . $dir . '/.user.ini');
199 }
200 }
201 return $data;
202 }
203
204 public function delzml($siteid, $domain, $sitepath)
205 {
206 $zmllist = $this->urlzmlls($siteid);
207 $getyxml = $this->yxmlrhq($siteid, $sitepath);
208 $urlid = false;
209 $urlpath = null;
210 foreach ($zmllist['binding'] as $vals) {
211 if ($vals['domain'] == $domain) {
212 $urlid = $vals['id'];
213 $urlpath = $vals['path'];
214 break;
215 }
216 }
217 if ($urlid === false) return ["status" => false, "msg" => '域名不存在!'];
218 if (substr($urlpath, 0, 3) == '../') $urlpaths = substr($urlpath, 3);
219 if ($urlpath != false) {
220 if ($getyxml['runPath']['runPath'] != '/' . $urlpaths) {
221 if ($getyxml['runPath']['runPath'] != '/') {
222 if (substr($urlpath, 0, 3) == '../') {
223 $urlpath = substr($urlpath, 3);
224 $this->delwj($sitepath . '/' . $urlpath . '/.user.ini');
225 } else {
226 $this->delwj($sitepath . $getyxml['runPath']['runPath'] . '/' . $urlpath . '/.user.ini');
227 }
228 } else {
229 $this->delwj($sitepath . '/' . $urlpath . '/.user.ini');
230 }
231 }
232 }
233 $url = $this->BT_PANEL . '/site?action=DelDirBinding';
234 $p_data = $this->GetKeyData();
235 $p_data['id'] = $urlid;
236 return json_decode($this->HttpPostCookie($url, $p_data), true);
237 }
238
239 // ========================================================================
240 // PHP版本管理
241 // ========================================================================
242
243 public function btapi_listphp()
244 {
245 $url = $this->BT_PANEL . '/site?action=GetPHPVersion';
246 $p_data = $this->GetKeyData();
247 $p_data['s_type'] = '1';
248 return json_decode($this->HttpPostCookie($url, $p_data), true);
249 }
250
251 public function btapi_phpnowz($siteName)
252 {
253 $url = $this->BT_PANEL . '/site?action=GetSitePHPVersion';
254 $p_data = $this->GetKeyData();
255 $p_data['siteName'] = $siteName;
256 return json_decode($this->HttpPostCookie($url, $p_data), true);
257 }
258
259 public function btapi_setphp($siteName, $version)
260 {
261 $url = $this->BT_PANEL . '/site?action=SetPHPVersion';
262 $p_data = $this->GetKeyData();
263 $p_data['siteName'] = $siteName;
264 $p_data['version'] = $version;
265 return json_decode($this->HttpPostCookie($url, $p_data), true);
266 }
267
268 /**
269 * 一键部署专用:设置站点 PHP 版本
270 * @param array $dat [站点名, PHP版本号]
271 */
272 public function setphp($dat)
273 {
274 $siteName = (string)($dat[0] ?? '');
275 $version = (string)($dat[1] ?? '');
276 if ($siteName === '' || $version === '') {
277 return ['status' => false, 'msg' => '站点名或PHP版本号不能为空'];
278 }
279 return $this->btapi_setphp($siteName, $version);
280 }
281
282 // ========================================================================
283 // FTP管理
284 // ========================================================================
285
286 public function ftpcx($user)
287 {
288 $url = $this->BT_PANEL . '/data?action=getData';
289 $p_data = $this->GetKeyData();
290 $p_data['table'] = 'ftps';
291 $p_data['search'] = $user;
292 $p_data['limit'] = 100;
293 $p_data['p'] = 1;
294 return json_decode($this->HttpPostCookie($url, $p_data), true);
295 }
296
297 public function ftpxg($id, $username, $status)
298 {
299 return $this->setftpzt($id, $username, $status);
300 }
301
302 public function setftpzt($id, $username, $status)
303 {
304 $url = $this->BT_PANEL . '/ftp?action=SetStatus';
305 $p_data = $this->GetKeyData();
306 $p_data['id'] = $id;
307 $p_data['username'] = $username;
308 $p_data['status'] = $status;
309 return json_decode($this->HttpPostCookie($url, $p_data), true);
310 }
311
312 public function setftppass($id, $username, $password)
313 {
314 $url = $this->BT_PANEL . '/ftp?action=SetUserPassword';
315 $p_data = $this->GetKeyData();
316 $p_data['id'] = $id;
317 $p_data['ftp_username'] = $username;
318 $p_data['new_password'] = $password;
319 return json_decode($this->HttpPostCookie($url, $p_data), true);
320 }
321
322 // ========================================================================
323 // 数据库管理
324 // ========================================================================
325
326 public function databascx($user)
327 {
328 $url = $this->BT_PANEL . '/data?action=getData';
329 $p_data = $this->GetKeyData();
330 $p_data['table'] = 'databases';
331 $p_data['search'] = $user;
332 $p_data['limit'] = 100;
333 $p_data['p'] = 1;
334 return json_decode($this->HttpPostCookie($url, $p_data), true);
335 }
336
337 public function sqlkjhq($db_name)
338 {
339 $url = $this->BT_PANEL . '/database?action=GetInfo';
340 $p_data = $this->GetKeyData();
341 $p_data['db_name'] = $db_name;
342 return json_decode($this->HttpPostCookie($url, $p_data), true);
343 }
344
345 public function setmysqlpass($id, $name, $password)
346 {
347 $url = $this->BT_PANEL . '/database?action=ResDatabasePassword';
348 $p_data = $this->GetKeyData();
349 $p_data['id'] = $id;
350 $p_data['name'] = $name;
351 $p_data['password'] = $password;
352 return json_decode($this->HttpPostCookie($url, $p_data), true);
353 }
354
355 public function mysqlqx($name, $access)
356 {
357 $url = $this->BT_PANEL . '/database?action=SetDatabaseAccess';
358 $p_data = $this->GetKeyData();
359 $p_data['name'] = $name;
360 $p_data['dataAccess'] = '%';
361 $p_data['access'] = $access;
362 return json_decode($this->HttpPostCookie($url, $p_data), true);
363 }
364
365 public function GetDatabaseAccess($name)
366 {
367 $url = $this->BT_PANEL . '/database?action=GetDatabaseAccess';
368 $p_data = $this->GetKeyData();
369 $p_data['name'] = $name;
370 return json_decode($this->HttpPostCookie($url, $p_data), true);
371 }
372
373 public function SetDatabaseAccess($name, $dataAccess)
374 {
375 $url = $this->BT_PANEL . '/database?action=SetDatabaseAccess';
376 $p_data = $this->GetKeyData();
377 $p_data['name'] = $name;
378 $p_data['dataAccess'] = $dataAccess;
379 $p_data['access'] = $dataAccess;
380 return json_decode($this->HttpPostCookie($url, $p_data), true);
381 }
382
383 public function Databasebackuplist($id)
384 {
385 $url = $this->BT_PANEL . '/data?action=getData';
386 $p_data = $this->GetKeyData();
387 $p_data['table'] = "backup";
388 $p_data['search'] = $id;
389 $p_data['type'] = "1";
390 $p_data['limit'] = "200";
391 $p_data['p'] = "1";
392 return json_decode($this->HttpPostCookie($url, $p_data), true);
393 }
394
395 public function Databaseadd($id)
396 {
397 $url = $this->BT_PANEL . '/database?action=ToBackup';
398 $p_data = $this->GetKeyData();
399 $p_data['id'] = $id;
400 return json_decode($this->HttpPostCookie($url, $p_data), true);
401 }
402
403 public function DatabaseDelete($id)
404 {
405 $url = $this->BT_PANEL . '/database?action=DelBackup';
406 $p_data = $this->GetKeyData();
407 $p_data['id'] = $id;
408 return json_decode($this->HttpPostCookie($url, $p_data), true);
409 }
410
411 public function Databaserestore($file, $user)
412 {
413 $url = $this->BT_PANEL . '/database?action=InputSql';
414 $p_data = $this->GetKeyData();
415 $p_data['file'] = $file;
416 $p_data['name'] = $user;
417 return json_decode($this->HttpPostCookie($url, $p_data), true);
418 }
419
420 public function drsql($wj)
421 {
422 $url = $this->BT_PANEL . '/database?action=InputSql';
423 $p_data = $this->GetKeyData();
424 $p_data['file'] = $wj[0];
425 $p_data['name'] = strtolower($wj[1]);
426 return json_decode($this->HttpPostCookie($url, $p_data), true);
427 }
428
429 // ========================================================================
430 // SSL/HTTPS
431 // ========================================================================
432
433 public function httpsfcz()
434 {
435 $url = $this->BT_PANEL . '/site?action=get_https_mode';
436 $p_data = $this->GetKeyData();
437 $result = $this->HttpPostCookie($url, $p_data);
438 if ($result == 'false') {
439 $url = $this->BT_PANEL . '/site?action=set_https_mode';
440 return json_decode($this->HttpPostCookie($url, $p_data), true);
441 }
442 return ["status" => true, "msg" => 'https窜站未开启'];
443 }
444
445 public function sslsq($domains, $siteid, $sitename, $renew = false)
446 {
447 $this->httpsfcz();
448 $url = $this->BT_PANEL . '/acme?action=apply_cert_api';
449 $p_data = $this->GetKeyData();
450 $p_data['domains'] = $domains;
451 $p_data['auth_type'] = 'http';
452 $p_data['auth_to'] = $siteid;
453 $p_data['auto_wildcard'] = 0;
454 $p_data['id'] = $siteid;
455 if ($renew) {
456 $p_data['siteName'] = $sitename;
457 }
458 $data = json_decode($this->HttpPostCookie($url, $p_data), true);
459 if ($data['status']) {
460 $zsps = $this->setsslpem($sitename, $data['private_key'], $data['cert'] . '\n' . $data['root']);
461 return ["status" => $zsps['status'], "msg" => [$zsps['msg']]];
462 }
463 return $data;
464 }
465
466 public function setsslpem($sitename, $key, $csr)
467 {
468 $this->httpsfcz();
469 $url = $this->BT_PANEL . '/site?action=SetSSL';
470 $p_data = $this->GetKeyData();
471 $p_data['type'] = 1;
472 $p_data['siteName'] = $sitename;
473 $p_data['key'] = $key;
474 $p_data['csr'] = $csr;
475 return json_decode($this->HttpPostCookie($url, $p_data), true);
476 }
477
478 public function getsslpem($sitename)
479 {
480 $url = $this->BT_PANEL . '/site?action=GetSSL';
481 $p_data = $this->GetKeyData();
482 $p_data['siteName'] = $sitename;
483 return json_decode($this->HttpPostCookie($url, $p_data), true);
484 }
485
486 public function closessl($sitename)
487 {
488 $url = $this->BT_PANEL . '/site?action=CloseSSLConf';
489 $p_data = $this->GetKeyData();
490 $p_data['updateOf'] = 1;
491 $p_data['siteName'] = $sitename;
492 return json_decode($this->HttpPostCookie($url, $p_data), true);
493 }
494
495 public function httpsqzf($sitename, $enable)
496 {
497 $url = $this->BT_PANEL . ($enable == 'true' ? '/site?action=HttpToHttps' : '/site?action=CloseToHttps');
498 $p_data = $this->GetKeyData();
499 $p_data['siteName'] = $sitename;
500 return json_decode($this->HttpPostCookie($url, $p_data), true);
501 }
502
503 // ========================================================================
504 // 文件管理
505 // ========================================================================
506
507 public function hqwjnr($path)
508 {
509 $url = $this->BT_PANEL . '/files?action=GetFileBody';
510 $p_data = $this->GetKeyData();
511 $p_data['path'] = $path;
512 return json_decode($this->HttpPostCookie($url, $p_data), true);
513 }
514
515 public function setwj($wjxx)
516 {
517 $url = $this->BT_PANEL . '/files?action=SaveFileBody';
518 $p_data = $this->GetKeyData();
519 $p_data['data'] = $wjxx[0];
520 $p_data['encoding'] = 'utf-8';
521 $p_data['path'] = $wjxx[1];
522 return json_decode($this->HttpPostCookie($url, $p_data), true);
523 }
524
525 public function setwjt($wjxx)
526 {
527 return $this->setwj($wjxx);
528 }
529
530 public function xjwj($path)
531 {
532 $url = $this->BT_PANEL . '/files?action=CreateFile';
533 $p_data = $this->GetKeyData();
534 $p_data['path'] = $path;
535 return json_decode($this->HttpPostCookie($url, $p_data), true);
536 }
537
538 public function xjwjj($path)
539 {
540 $url = $this->BT_PANEL . '/files?action=CreateDir';
541 $p_data = $this->GetKeyData();
542 $p_data['path'] = $path;
543 return json_decode($this->HttpPostCookie($url, $p_data), true);
544 }
545
546 public function delwj($path)
547 {
548 $url = $this->BT_PANEL . '/files?action=DeleteFile';
549 $p_data = $this->GetKeyData();
550 $p_data['path'] = $path;
551 return json_decode($this->HttpPostCookie($url, $p_data), true);
552 }
553
554 public function delwjj($path, $name, $arridpath)
555 {
556 $name = trim($name);
557 if ($name === '/' || $name == '') return ["status" => false, "msg" => '无法删除站点目录!'];
558 if (strpos($name, '/')) return ["status" => false, "msg" => '目录名不规范!'];
559 $getfkz = $this->yxmlrhq($arridpath[0], $arridpath[1]);
560 if ($getfkz['runPath']['runPath'] == $path . $name) return ["status" => false, "msg" => '禁止删除运行目录!'];
561 $zmllist = $this->urlzmlls($arridpath[0])['binding'] ?? [];
562 $yxml = $getfkz['runPath']['runPath'];
563 if ($yxml != '/') $yxml .= '/';
564 foreach ($zmllist as $val) {
565 if (substr($val['path'], 0, 3) == '../') {
566 $val['path'] = substr($val['path'], 3);
567 $yxml = '/';
568 }
569 if ($yxml . $val['path'] == $path . $name) return ["status" => false, "msg" => '错误!您正在尝试删除的目录已被域名' . $val['domain'] . '绑定为子目录,禁止删除子目录!'];
570 $yxml = $getfkz['runPath']['runPath'];
571 if ($yxml != '/') $yxml .= '/';
572 }
573 $url = $this->BT_PANEL . '/files?action=DeleteDir';
574 $p_data = $this->GetKeyData();
575 $p_data['path'] = $arridpath[1] . $path . $name;
576 return json_decode($this->HttpPostCookie($url, $p_data), true);
577 }
578
579 public function xzdelwj($path, $file, $arridpath)
580 {
581 $getfkz = $this->yxmlrhq($arridpath[0], $arridpath[1]);
582 $arrfile = json_decode($file, true);
583 $file = $this->delval_array($arrfile, $getfkz['runPath']['runPath'], $path);
584 if (!$file) return ["status" => false, "msg" => '禁止删除运行目录!无法删除名称不规范的目录/文件!'];
585 $zmllist = $this->urlzmlls($arridpath[0])['binding'] ?? [];
586 $yxml = $getfkz['runPath']['runPath'];
587 if ($yxml != '/') $yxml .= '/';
588 foreach ($zmllist as $val) {
589 if (substr($val['path'], 0, 3) == '../') {
590 $val['path'] = substr($val['path'], 3);
591 $yxml = '/';
592 }
593 $file = $this->delval_array($file, $yxml . $val['path'], $path);
594 $yxml = $getfkz['runPath']['runPath'];
595 if ($yxml != '/') $yxml .= '/';
596 }
597 if (!$file) return ["status" => false, "msg" => '禁止删除子目录!'];
598 $url = $this->BT_PANEL . '/files?action=SetBatchData';
599 $p_data = $this->GetKeyData();
600 $p_data['data'] = json_encode($file, JSON_UNESCAPED_UNICODE);
601 $p_data['type'] = '4';
602 $p_data['path'] = $arridpath[1] . $path;
603 return json_decode($this->HttpPostCookie($url, $p_data), true);
604 }
605
606 public function GetHostDirectory($path, $page = '1', $showRow = '500')
607 {
608 $url = $this->BT_PANEL . '/files?action=GetDir';
609 $p_data = $this->GetKeyData();
610 $p_data['p'] = $page;
611 $p_data['showRow'] = $showRow;
612 $p_data['path'] = $path;
613 $p_data['is_operating'] = "true";
614 $p_data['search'] = "";
615 return json_decode($this->HttpPostCookie($url, $p_data), true);
616 }
617
618 public function GetFileContent($path)
619 {
620 return $this->hqwjnr($path);
621 }
622
623 public function GetLogswt($path)
624 {
625 return $this->hqwjnr($path);
626 }
627
628 public function GetLogswh($data, $path)
629 {
630 return $this->setwj([$data, $path]);
631 }
632
633 public function GetLogshqwjlo($stera, $sorting = 'False', $sort = 'name', $datasize = '2000', $page = '1')
634 {
635 $url = $this->BT_PANEL . '/files?action=GetDir';
636 $p_data = $this->GetKeyData();
637 $p_data['p'] = $page;
638 $p_data['showRow'] = $datasize;
639 $p_data['path'] = $stera;
640 $p_data['reverse'] = $sorting;
641 $p_data['sort'] = $sort;
642 return json_decode($this->HttpPostCookie($url, $p_data), true);
643 }
644
645 public function GetLogsworld($id, $username, $password)
646 {
647 return $this->setmysqlpass($id, $username, $password);
648 }
649
650 public function GetLogsftp($id, $username, $password)
651 {
652 return $this->setftppass($id, $username, $password);
653 }
654
655 public function webkjjs($wj)
656 {
657 return $this->hqsize($wj);
658 }
659
660 public function hqsize($path)
661 {
662 $url = $this->BT_PANEL . '/files?action=get_path_size';
663 $p_data = $this->GetKeyData();
664 $p_data['path'] = $path;
665 return json_decode($this->HttpPostCookie($url, $p_data), true);
666 }
667
668 public function fileupa($filename)
669 {
670 $url = $this->BT_PANEL . '/files?action=upload_file_exists';
671 $p_data = $this->GetKeyData();
672 $p_data['filename'] = $filename;
673 return json_decode($this->HttpPostCookie($url, $p_data), true);
674 }
675
676 public function fileups($uplj, $file, $f_start, $f_name, $f_size)
677 {
678 $url = $this->BT_PANEL . '/files?action=upload';
679 $p_data = $this->GetKeyData();
680 $p_data['f_path'] = $uplj;
681 $p_data['f_name'] = $f_name;
682 $p_data['f_size'] = $f_size;
683 $p_data['f_start'] = $f_start;
684 $p_data['blob'] = new CURLFile($file['tmp_name'], $file['type'], $file['name']);
685 return json_decode($this->HttpPostCookie($url, $p_data), true);
686 }
687
688 public function filecopy($sfile, $dfile)
689 {
690 $url = $this->BT_PANEL . '/files?action=CopyFile';
691 $p_data = $this->GetKeyData();
692 $p_data['sfile'] = $sfile;
693 $p_data['dfile'] = $dfile;
694 return json_decode($this->HttpPostCookie($url, $p_data), true);
695 }
696
697 public function fileysr($sfile, $dfile, $z_type, $path)
698 {
699 $url = $this->BT_PANEL . '/files?action=Zip';
700 $p_data = $this->GetKeyData();
701 $p_data['sfile'] = $sfile;
702 $p_data['dfile'] = $dfile;
703 $p_data['z_type'] = $z_type;
704 $p_data['path'] = $path;
705 return json_decode($this->HttpPostCookie($url, $p_data), true);
706 }
707
708 public function GetLogsjywj($sfile, $dfile, $coding, $password)
709 {
710 $url = $this->BT_PANEL . '/files?action=UnZip';
711 $p_data = $this->GetKeyData();
712 $p_data['sfile'] = $sfile;
713 $p_data['dfile'] = $dfile;
714 $p_data['coding'] = $coding;
715 $p_data['password'] = $password;
716 $p_data['type'] = 'zip';
717 return json_decode($this->HttpPostCookie($url, $p_data), true);
718 }
719
720 public function cxname($jwz)
721 {
722 global $yhc;
723 if (strpos($jwz[2], '/')) return ["status" => false, "msg" => '被重命名的文件不存在!'];
724 if (strpos($jwz[3], '/')) return ["status" => false, "msg" => '新文件名不规范!'];
725 $getfkz = $this->yxmlrhq($yhc['btid'], $jwz[0]);
726 if ($getfkz['runPath']['runPath'] == $jwz[1] . $jwz[2]) return ["status" => false, "msg" => '错误!您正在尝试重命名运行目录,这是不被允许的!'];
727 if ($getfkz['runPath']['runPath'] == $jwz[1] . $jwz[3]) return ["status" => false, "msg" => '错误!此文件名已存在!'];
728 $zmllist = $this->urlzmlls($yhc['btid'])['binding'] ?? [];
729 $yxml = $getfkz['runPath']['runPath'];
730 if ($yxml != '/') $yxml .= '/';
731 foreach ($zmllist as $val) {
732 if (substr($val['path'], 0, 3) == '../') {
733 $val['path'] = substr($val['path'], 3);
734 $yxml = '/';
735 }
736 if ($yxml . $val['path'] == $jwz[1] . $jwz[2]) return ["status" => false, "msg" => '错误!您正在尝试重命名的目录已被域名' . $val['domain'] . '绑定为子目录,禁止重命名子目录!'];
737 if ($yxml . $val['path'] == $jwz[1] . $jwz[3]) return ["status" => false, "msg" => '错误!此文件名已存在!'];
738 $yxml = $getfkz['runPath']['runPath'];
739 if ($yxml != '/') $yxml .= '/';
740 }
741 $url = $this->BT_PANEL . '/files?action=MvFile';
742 $p_data = $this->GetKeyData();
743 $p_data['sfile'] = $jwz[0] . $jwz[1] . $jwz[2];
744 $p_data['dfile'] = $jwz[0] . $jwz[1] . $jwz[3];
745 $p_data['rename'] = true;
746 return json_decode($this->HttpPostCookie($url, $p_data), true);
747 }
748
749 public function zswjsc($file, $wz)
750 {
751 $url = $this->BT_PANEL . '/files?action=UploadFile&path=' . $wz . '&codeing=utf-8';
752 $p_data = $this->GetKeyData();
753 $p_data['zunfile'] = new CURLFile($file['tmp_name'], $file['type'], $file['name']);
754 return json_decode($this->HttpPostCookie($url, $p_data), true);
755 }
756
757 public function wailkq($path, $name)
758 {
759 $url = $this->BT_PANEL . '/files?action=create_download_url';
760 $p_data = $this->GetKeyData();
761 $p_data['filename'] = $path . $name;
762 $p_data['ps'] = $name;
763 $p_data['password'] = '';
764 $p_data['expire'] = 24;
765 return json_decode($this->HttpPostCookie($url, $p_data), true);
766 }
767
768 public function wailhq($id)
769 {
770 $url = $this->BT_PANEL . '/files?action=get_download_url_find';
771 $p_data = $this->GetKeyData();
772 $p_data['id'] = $id;
773 return json_decode($this->HttpPostCookie($url, $p_data), true);
774 }
775
776 public function wailgb($id)
777 {
778 $url = $this->BT_PANEL . '/files?action=remove_download_url';
779 $p_data = $this->GetKeyData();
780 $p_data['id'] = $id;
781 return json_decode($this->HttpPostCookie($url, $p_data), true);
782 }
783
784 // ========================================================================
785 // 反向代理
786 // ========================================================================
787
788 public function fxdl_add($urlp, $site)
789 {
790 $url = $this->BT_PANEL . '/site?action=CreateProxy';
791 $p_data = $this->GetKeyData();
792 $p_data['type'] = '1';
793 $p_data['proxyname'] = md5($urlp);
794 $p_data['cachetime'] = '1';
795 $p_data['proxydir'] = '/';
796 $p_data['proxysite'] = 'http://' . $urlp;
797 $p_data['todomain'] = $urlp;
798 $p_data['cache'] = '0';
799 $p_data['advanced'] = '0';
800 $p_data['sitename'] = $site;
801 $p_data['subfilter'] = '[{"sub1":"","sub2":""}]';
802 return json_decode($this->HttpPostCookie($url, $p_data), true);
803 }
804
805 public function fxdl_del($urlp, $site)
806 {
807 $url = $this->BT_PANEL . '/site?action=RemoveProxy';
808 $p_data = $this->GetKeyData();
809 $p_data['sitename'] = $site;
810 $p_data['proxyname'] = md5($urlp);
811 return json_decode($this->HttpPostCookie($url, $p_data), true);
812 }
813
814 // ========================================================================
815 // 防盗链
816 // ========================================================================
817
818 public function getfdlkg($id, $name)
819 {
820 $url = $this->BT_PANEL . '/site?action=GetSecurity';
821 $p_data = $this->GetKeyData();
822 $p_data["id"] = $id;
823 $p_data["name"] = $name;
824 return json_decode($this->HttpPostCookie($url, $p_data), true);
825 }
826
827 public function Setfdlkg($id, $name, $fix, $domains, $status, $return_rule, $httpsta)
828 {
829 $url = $this->BT_PANEL . '/site?action=SetSecurity';
830 $p_data = $this->GetKeyData();
831 $p_data['id'] = $id;
832 $p_data['name'] = $name;
833 $p_data['fix'] = $fix;
834 $p_data['domains'] = $domains;
835 $p_data['status'] = $status;
836 $p_data['return_rule'] = $return_rule;
837 $p_data['http_status'] = $httpsta;
838 return json_decode($this->HttpPostCookie($url, $p_data), true);
839 }
840
841 // ========================================================================
842 // 目录密码访问 / 默认文档 / 运行目录 / 伪静态
843 // ========================================================================
844
845 public function GetLogs($id)
846 {
847 $url = $this->BT_PANEL . '/site?action=get_dir_auth';
848 $p_data = $this->GetKeyData();
849 $p_data['id'] = $id;
850 return json_decode($this->HttpPostCookie($url, $p_data), true);
851 }
852
853 public function GetLogsr($id, $name)
854 {
855 $url = $this->BT_PANEL . '/site?action=delete_dir_auth';
856 $p_data = $this->GetKeyData();
857 $p_data['id'] = $id;
858 $p_data['name'] = $name;
859 return json_decode($this->HttpPostCookie($url, $p_data), true);
860 }
861
862 public function GetLogst($id, $name, $dir, $username, $password)
863 {
864 $url = $this->BT_PANEL . '/site?action=set_dir_auth';
865 $p_data = $this->GetKeyData();
866 $p_data['id'] = $id;
867 $p_data['name'] = $name;
868 $p_data['site_dir'] = $dir;
869 $p_data['username'] = $username;
870 $p_data['password'] = $password;
871 return json_decode($this->HttpPostCookie($url, $p_data), true);
872 }
873
874 public function GetLogseb($id)
875 {
876 $url = $this->BT_PANEL . '/site?action=GetIndex';
877 $p_data = $this->GetKeyData();
878 $p_data['id'] = $id;
879 return json_decode($this->HttpPostCookie($url, $p_data), true);
880 }
881
882 public function GetLogsea($id, $index)
883 {
884 $url = $this->BT_PANEL . '/site?action=SetIndex';
885 $p_data = $this->GetKeyData();
886 $p_data['id'] = $id;
887 $p_data['Index'] = $index;
888 return json_decode($this->HttpPostCookie($url, $p_data), true);
889 }
890
891 public function GetLogswr($siteName)
892 {
893 $url = $this->BT_PANEL . '/site?action=GetRewriteList';
894 $p_data = $this->GetKeyData();
895 $p_data['siteName'] = $siteName;
896 return json_decode($this->HttpPostCookie($url, $p_data), true);
897 }
898
899 public function yxmlrhq($id, $path)
900 {
901 $url = $this->BT_PANEL . '/site?action=GetDirUserINI';
902 $p_data = $this->GetKeyData();
903 $p_data['id'] = $id;
904 $p_data['path'] = $path;
905 return json_decode($this->HttpPostCookie($url, $p_data), true);
906 }
907
908 public function fkzup($siteid, $path)
909 {
910 $fkzqk = $this->yxmlrhq($siteid, $path);
911 if (!$fkzqk['userini']) {
912 $url = $this->BT_PANEL . '/site?action=SetDirUserINI';
913 $p_data = $this->GetKeyData();
914 $p_data['path'] = $path;
915 return json_decode($this->HttpPostCookie($url, $p_data), true);
916 }
917 return ["status" => true, "msg" => "防跨站设置情况为开启"];
918 }
919
920 public function setyxml($dat)
921 {
922 $siteid = $dat[0];
923 $runPath = $dat[1];
924 $sitePath = $dat[2];
925 $getfkz = $this->yxmlrhq($siteid, $sitePath);
926 $oldRunPath = $getfkz['runPath']['runPath'];
927 $zmllist = $this->urlzmlls($siteid)['binding'] ?? [];
928 if ($oldRunPath != '/') {
929 foreach ($zmllist as $vals) {
930 if (substr($vals['path'], 0, 3) != '../') {
931 $this->delzml($siteid, $vals['domain'], $sitePath);
932 }
933 }
934 }
935 $url = $this->BT_PANEL . '/site?action=SetSiteRunPath';
936 $p_data = $this->GetKeyData();
937 $p_data['id'] = $siteid;
938 $p_data['runPath'] = $runPath;
939 $result = $this->HttpPostCookie($url, $p_data);
940 $this->fkzup($siteid, $sitePath);
941 $getfkz = $this->yxmlrhq($siteid, $sitePath);
942 $zmllist = $this->urlzmlls($siteid)['binding'] ?? [];
943 $yxml = $getfkz['runPath']['runPath'];
944 if ($yxml != '/') $yxml .= '/';
945 foreach ($zmllist as $val) {
946 if (substr($val['path'], 0, 3) == '../') {
947 $val['path'] = substr($val['path'], 3);
948 $yxml = '/';
949 }
950 if ($getfkz['runPath']['runPath'] == '/') {
951 $this->filecopy($sitePath . $getfkz['runPath']['runPath'] . '.user.ini', $sitePath . $yxml . $val['path'] . '/.user.ini');
952 } else {
953 $this->filecopy($sitePath . $getfkz['runPath']['runPath'] . '/.user.ini', $sitePath . $yxml . $val['path'] . '/.user.ini');
954 }
955 $yxml = $getfkz['runPath']['runPath'];
956 if ($yxml != '/') $yxml .= '/';
957 }
958 return json_decode($result, true);
959 }
960
961 // ========================================================================
962 // Nginx / 日志
963 // ========================================================================
964
965 public function Getnginx($name)
966 {
967 $url = $this->BT_PANEL . '/files?action=GetFileBody';
968 $p_data = $this->GetKeyData();
969 $p_data['path'] = "/www/server/panel/vhost/nginx/" . $name . ".conf";
970 return json_decode($this->HttpPostCookie($url, $p_data), true);
971 }
972
973 public function getlog($siteName)
974 {
975 $url = $this->BT_PANEL . '/site?action=GetSiteLogs';
976 $p_data = $this->GetKeyData();
977 $p_data['siteName'] = $siteName;
978 return json_decode($this->HttpPostCookie($url, $p_data), true);
979 }
980
981 // ========================================================================
982 // 网站 GZIP 压缩配置(宝塔网站 PHP/ServiceConf API)
983 // ========================================================================
984
985 public function get_gzip_status($siteName)
986 {
987 $url = $this->BT_PANEL . '/mod/php/serviceconf/get_nginx_gzip';
988 $p_data = $this->GetKeyData();
989 $p_data['site_name'] = $siteName;
990 return json_decode($this->HttpPostCookie($url, $p_data), true);
991 }
992
993 public function remove_gzip_status($siteName)
994 {
995 $url = $this->BT_PANEL . '/mod/php/serviceconf/remove_nginx_gzip';
996 $p_data = $this->GetKeyData();
997 $p_data['site_name'] = $siteName;
998 return json_decode($this->HttpPostCookie($url, $p_data), true);
999 }
1000
1001 public function set_gzip($siteName, $gzip_types, $comp_level, $min_length)
1002 {
1003 $url = $this->BT_PANEL . '/mod/php/serviceconf/set_nginx_gzip';
1004 $p_data = $this->GetKeyData();
1005 $p_data['site_name'] = $siteName;
1006 $p_data['gzip_types'] = $gzip_types;
1007 $p_data['comp_level'] = $comp_level;
1008 $p_data['min_length'] = $min_length;
1009 return json_decode($this->HttpPostCookie($url, $p_data), true);
1010 }
1011
1012 public function get_static_cache($siteName)
1013 {
1014 $url = $this->BT_PANEL . '/mod/php/serviceconf/get_nginx_static_cache';
1015 $p_data = $this->GetKeyData();
1016 $p_data['site_name'] = $siteName;
1017 return json_decode($this->HttpPostCookie($url, $p_data), true);
1018 }
1019
1020 public function set_static_cache($siteName, $suffix, $time_out, $old_suffix = null)
1021 {
1022 $url = $this->BT_PANEL . '/mod/php/serviceconf/set_nginx_static_cache';
1023 $p_data = $this->GetKeyData();
1024 $p_data['site_name'] = $siteName;
1025 $p_data['suffix'] = $suffix;
1026 $p_data['time_out'] = $time_out;
1027 if ($old_suffix !== null && $old_suffix !== '') {
1028 $p_data['old_suffix'] = $old_suffix;
1029 }
1030 return json_decode($this->HttpPostCookie($url, $p_data), true);
1031 }
1032
1033 public function remove_static_cache($siteName, $suffix)
1034 {
1035 $url = $this->BT_PANEL . '/mod/php/serviceconf/remove_nginx_static_cache';
1036 $p_data = $this->GetKeyData();
1037 $p_data['site_name'] = $siteName;
1038 $p_data['suffix'] = $suffix;
1039 return json_decode($this->HttpPostCookie($url, $p_data), true);
1040 }
1041
1042 // ========================================================================
1043 // PHPMyAdmin
1044 // ========================================================================
1045
1046 public function api_sql_cf()
1047 {
1048 $url = $this->BT_PANEL . '/plugin?action=get_soft_find';
1049 $p_data = $this->GetKeyData();
1050 $p_data['sName'] = 'phpmyadmin';
1051 return json_decode($this->HttpPostCookie($url, $p_data), true);
1052 }
1053
1054 public function api_sql_set($type)
1055 {
1056 $url = $this->BT_PANEL . '/system?action=ServiceAdmin';
1057 $p_data = $this->GetKeyData();
1058 $p_data['name'] = 'phpmyadmin';
1059 $p_data['type'] = $type;
1060 return json_decode($this->HttpPostCookie($url, $p_data), true);
1061 }
1062
1063 // ========================================================================
1064 // 一键部署
1065 // ========================================================================
1066
1067 public function 获取部署程序的列表()
1068 {
1069 $url = $this->BT_PANEL . '/deployment?action=GetList';
1070 $p_data = $this->GetKeyData();
1071 $p_data['type'] = 0;
1072 return json_decode($this->HttpPostCookie($url, $p_data), true);
1073 }
1074
1075 /**
1076 * 获取可部署的软件包列表
1077 * @return array
1078 */
1079 public function deployment_get_list($timeout = 25)
1080 {
1081 $url = $this->BT_PANEL . '/deployment?action=GetList';
1082 $p_data = $this->GetKeyData();
1083 $p_data['action'] = 'GetList';
1084 $p_data['type'] = 0;
1085 return json_decode($this->HttpPostCookie($url, $p_data, $timeout), true);
1086 }
1087
1088 /**
1089 * 获取可用于一键部署的网站和框架列表
1090 * @return array
1091 */
1092 public function deployment_get_site_list($timeout = 25)
1093 {
1094 $url = $this->BT_PANEL . '/deployment?action=GetSiteList';
1095 $p_data = $this->GetKeyData();
1096 $p_data['action'] = 'GetSiteList';
1097 return json_decode($this->HttpPostCookie($url, $p_data, $timeout), true);
1098 }
1099
1100 /**
1101 * 部署软件包到指定网站
1102 * @param string $dname 软件包名称
1103 * @param string $site_name 目标网站域名
1104 * @param string $project_type 项目类型(php/java),默认 php
1105 * @return array
1106 */
1107 public function deployment_setup_package($dname, $site_name, $project_type = 'php', $timeout = 25)
1108 {
1109 $url = $this->BT_PANEL . '/deployment?action=SetupPackage';
1110 $p_data = $this->GetKeyData();
1111 $p_data['action'] = 'SetupPackage';
1112 $p_data['dname'] = $dname;
1113 $p_data['site_name'] = $site_name;
1114 if ($project_type !== 'php') {
1115 $p_data['project_type'] = $project_type;
1116 }
1117 $raw = $this->HttpPostCookie($url, $p_data, $timeout);
1118 $decoded = json_decode($raw, true);
1119 if (!is_array($decoded)) {
1120 return ['status' => false, 'msg' => '面板响应异常:' . mb_substr((string)$raw, 0, 200)];
1121 }
1122 return $decoded;
1123 }
1124
1125 /**
1126 * 检测服务器环境
1127 * @return array
1128 */
1129 public function deployment_check_project_env($timeout = 25)
1130 {
1131 $url = $this->BT_PANEL . '/deployment?action=check_project_env';
1132 $p_data = $this->GetKeyData();
1133 $p_data['action'] = 'check_project_env';
1134 return json_decode($this->HttpPostCookie($url, $p_data, $timeout), true);
1135 }
1136
1137 /**
1138 * 获取部署进度
1139 * @return array|null
1140 */
1141 public function deployment_get_speed($timeout = 25)
1142 {
1143 $url = $this->BT_PANEL . '/deployment?action=GetSpeed';
1144 $p_data = $this->GetKeyData();
1145 $p_data['action'] = 'GetSpeed';
1146 return json_decode($this->HttpPostCookie($url, $p_data, $timeout), true);
1147 }
1148
1149 /**
1150 * 获取部署日志
1151 * @return array
1152 */
1153 public function deployment_get_in_log($timeout = 25)
1154 {
1155 $url = $this->BT_PANEL . '/deployment?action=GetInLog';
1156 $p_data = $this->GetKeyData();
1157 $p_data['action'] = 'GetInLog';
1158 return json_decode($this->HttpPostCookie($url, $p_data, $timeout), true);
1159 }
1160
1161 /**
1162 * 添加自定义软件包(PHP 项目)
1163 * @param string $name 英文名称
1164 * @param string $title 显示标题
1165 * @param string $version 版本号
1166 * @param string $php PHP 版本,如 '74'
1167 * @param string $enable_functions 需要解禁的 PHP 函数
1168 * @param string $project_type 项目类型(php/java),默认 php
1169 * @param string $java_version JDK 版本(仅 Java)
1170 * @param string $mysql_version MySQL 版本(仅 Java)
1171 * @return array
1172 */
1173 public function deployment_add_package($name, $title, $version, $php = '', $enable_functions = '', $project_type = 'php', $java_version = '', $mysql_version = '', $timeout = 25)
1174 {
1175 $url = $this->BT_PANEL . '/deployment?action=AddPackage';
1176 $p_data = $this->GetKeyData();
1177 $p_data['action'] = 'AddPackage';
1178 $p_data['name'] = $name;
1179 $p_data['title'] = $title;
1180 $p_data['version'] = $version;
1181 $p_data['project_type'] = $project_type;
1182 if ($project_type === 'java') {
1183 $p_data['java_version'] = $java_version;
1184 $p_data['mysql_version'] = $mysql_version;
1185 } else {
1186 if ($php !== '') {
1187 $p_data['php'] = $php;
1188 }
1189 if ($enable_functions !== '') {
1190 $p_data['enable_functions'] = $enable_functions;
1191 }
1192 }
1193 return json_decode($this->HttpPostCookie($url, $p_data, $timeout), true);
1194 }
1195
1196 /**
1197 * 获取自定义软件包详情
1198 * @param string $p_name 软件包英文名称
1199 * @return array
1200 */
1201 public function deployment_get_package_other($p_name, $timeout = 25)
1202 {
1203 $url = $this->BT_PANEL . '/deployment?action=GetPackageOther';
1204 $p_data = $this->GetKeyData();
1205 $p_data['action'] = 'GetPackageOther';
1206 $p_data['p_name'] = $p_name;
1207 return json_decode($this->HttpPostCookie($url, $p_data, $timeout), true);
1208 }
1209
1210 /**
1211 * 删除通过一键部署安装的项目
1212 * @param string $dname 部署名称
1213 * @param string $site_name 网站名称
1214 * @return array
1215 */
1216 public function deployment_del_package($dname, $site_name, $timeout = 25)
1217 {
1218 $url = $this->BT_PANEL . '/deployment?action=DelPackage';
1219 $p_data = $this->GetKeyData();
1220 $p_data['action'] = 'DelPackage';
1221 $p_data['dname'] = $dname;
1222 $p_data['site_name'] = $site_name;
1223 return json_decode($this->HttpPostCookie($url, $p_data, $timeout), true);
1224 }
1225
1226 // ========================================================================
1227 // 统计/列表
1228 // ========================================================================
1229
1230 public function sjlist($table)
1231 {
1232 $url = $this->BT_PANEL . '/data?action=getData';
1233 $p_data = $this->GetKeyData();
1234 $p_data['table'] = $table;
1235 $p_data['limit'] = '9999';
1236 $p_data['p'] = '1';
1237 $p_data['search'] = '';
1238 return json_decode($this->HttpPostCookie($url, $p_data), true);
1239 }
1240
1241 /**
1242 * 按账号名精确查询 FTP/数据库记录 ID(开通主机后回填 MN_zj.ftpid / MN_zj.hxd 用)。
1243 *
1244 * 面板 /data?action=getData 对每一行都要做额外统计(数据库需算占用大小),
1245 * 实测每返回一行约 53ms:537 行要 28 秒,而按 search 精确过滤后仅 0.1 秒。
1246 * 因此这里一律带 search 查询,绝不再全表拉取。
1247 *
1248 * @param string $table 'ftps' 或 'databases'
1249 * @param string $name 面板账号名
1250 * @return string 记录 ID;未找到返回 '0'(与原先全表扫描的兜底值一致)
1251 */
1252 public function sjid($table, $name)
1253 {
1254 $name = (string)$name;
1255 if ($name === '') {
1256 return '0';
1257 }
1258 foreach (['100', '2000'] as $limit) {
1259 $url = $this->BT_PANEL . '/data?action=getData';
1260 $p_data = $this->GetKeyData();
1261 $p_data['table'] = $table;
1262 $p_data['search'] = $name;
1263 $p_data['limit'] = $limit;
1264 $p_data['p'] = '1';
1265 $result = json_decode($this->HttpPostCookie($url, $p_data), true);
1266 if (isset($result['data']) && is_array($result['data'])) {
1267 foreach ($result['data'] as $row) {
1268 if (($row['name'] ?? '') === $name) {
1269 return (string)$row['id'];
1270 }
1271 }
1272 // 首轮结果被截断(返回条数已达上限)时放大 limit 再查一次,
1273 // 仍然带 search,避免退化为全表扫描
1274 if (count($result['data']) < (int)$limit) {
1275 break;
1276 }
1277 } else {
1278 break;
1279 }
1280 }
1281 return '0';
1282 }
1283
1284 // ========================================================================
1285 // Shell执行(通过宝塔计划任务)
1286 // ========================================================================
1287
1288 private function shell_zx($id)
1289 {
1290 $url = $this->BT_PANEL . '/crontab?action=StartTask';
1291 $p_data = $this->GetKeyData();
1292 $p_data['id'] = $id;
1293 return json_decode($this->HttpPostCookie($url, $p_data), true);
1294 }
1295
1296 private function shell_del($id)
1297 {
1298 $url = $this->BT_PANEL . '/crontab?action=DelCrontab';
1299 $p_data = $this->GetKeyData();
1300 $p_data['id'] = $id;
1301 return json_decode($this->HttpPostCookie($url, $p_data), true);
1302 }
1303
1304 private function shell_list($name)
1305 {
1306 $url = $this->BT_PANEL . '/crontab?action=GetCrontab';
1307 $p_data = $this->GetKeyData();
1308 $result = $this->HttpPostCookie($url, $p_data);
1309 $data = json_decode($result, true);
1310 if (is_array($data)) {
1311 foreach ($data as $v) {
1312 if (is_array($v) && $v['name'] == $name) return ['code' => 1, 'msg' => '此任务存在', 'id' => $v['id']];
1313 }
1314 }
1315 return ['code' => 0, 'msg' => '此任务不存在'];
1316 }
1317
1318 private function shell_log($id)
1319 {
1320 $url = $this->BT_PANEL . '/crontab?action=GetLogs';
1321 $p_data = $this->GetKeyData();
1322 $p_data['id'] = $id;
1323 $data = json_decode($this->HttpPostCookie($url, $p_data), true);
1324 if (!$data['status']) return ['code' => 0, 'msg' => $data['msg']];
1325 $jg = explode("\n", $data['msg']);
1326 foreach ($jg as $k => $v) {
1327 if (strpos($v, '★') !== false && isset($jg[$k + 1]) && isset($jg[$k - 1]) && strpos($jg[$k + 1], '-------') !== false && strpos($jg[$k - 1], '-------') !== false) {
1328 unset($jg[$k]);
1329 unset($jg[$k + 1]);
1330 unset($jg[$k - 1]);
1331 $jg[$k] = trim($jg[$k]);
1332 }
1333 }
1334 $jg = array_values($jg);
1335 $dat = count($jg);
1336 return ['code' => 1, 'msg' => $jg[$dat - 2] ?? ''];
1337 }
1338
1339 private function shell_add($shell)
1340 {
1341 $url = $this->BT_PANEL . '/crontab?action=AddCrontab';
1342 $p_data = $this->GetKeyData();
1343 $p_data['name'] = 'MNBT的shell脚本';
1344 $p_data['type'] = 'day-n';
1345 $p_data['where1'] = 30;
1346 $p_data['hour'] = 1;
1347 $p_data['minute'] = 30;
1348 $p_data['week'] = '';
1349 $p_data['sType'] = 'toShell';
1350 $p_data['sBody'] = $shell;
1351 $p_data['sName'] = '';
1352 $p_data['backupTo'] = '';
1353 $p_data['save'] = '';
1354 $p_data['urladdress'] = '';
1355 $p_data['save_local'] = 1;
1356 $p_data['notice'] = '';
1357 $p_data['notice_channel'] = '';
1358 $p_data['datab_name'] = '';
1359 $p_data['tables_name'] = '';
1360 return json_decode($this->HttpPostCookie($url, $p_data), true);
1361 }
1362
1363 private function shell_yx($shell, $cljgty = true)
1364 {
1365 if (!$shell) return ['code' => 0, 'msg' => '脚本错误!'];
1366 $re_rw = $this->shell_list('MNBT的shell脚本');
1367 if ($re_rw['code']) {
1368 $rs_del = $this->shell_del($re_rw['id']);
1369 if (!$rs_del['status']) return ['code' => 0, 'msg' => '原任务删除失败,返回:' . ($rs_del['msg'] ?? '')];
1370 }
1371 $re_add = $this->shell_add($shell);
1372 if (!$re_add['status']) return ['code' => 0, 'msg' => '任务添加失败,返回:' . ($re_add['msg'] ?? '')];
1373 $re_zx = $this->shell_zx($re_add['id']);
1374 if (!$re_zx['status']) {
1375 $this->shell_del($re_add['id']);
1376 return ['code' => 0, 'msg' => '任务执行失败,返回:' . ($re_zx['msg'] ?? '')];
1377 }
1378 $re_jg = [];
1379 if ($cljgty) {
1380 sleep(1);
1381 $re_jg = $this->shell_log($re_add['id']);
1382 if (!$re_jg['code']) return $re_jg;
1383 }
1384 $this->shell_del($re_add['id']);
1385 return ['code' => 1, 'msg' => $re_jg['msg'] ?? ''];
1386 }
1387
1388 public function GetAllFile()
1389 {
1390 include(ROOT . 'bash.conf.php');
1391 return $this->shell_yx($shell_get_file);
1392 }
1393
1394 public function GetFileCentent($name)
1395 {
1396 $shell_cat_file = '
1397 #!/bin/bash
1398 content=$(cat ' . $name . ')
1399 echo "$content"
1400 ';
1401 return $this->shell_yx($shell_cat_file);
1402 }
1403
1404 // ========================================================================
1405 // 内部工具
1406 // ========================================================================
1407
1408 /**
1409 * 调用宝塔插件 API
1410 * @param string $name 插件名称
1411 * @param string $action 操作名(对应插件的 s 参数)
1412 * @param array $args 业务参数
1413 * @return array 解码后的响应
1414 */
1415 public function pluginRequest($name, $action, $args = [])
1416 {
1417 $url = $this->BT_PANEL . '/plugin?action=a&name=' . urlencode($name) . '&s=' . urlencode($action);
1418 $p_data = $this->GetKeyData();
1419 foreach ($args as $k => $v) {
1420 $p_data[$k] = $v;
1421 }
1422 return json_decode($this->HttpPostCookie($url, $p_data), true) ?: [];
1423 }
1424
1425 private function delval_array($val_array, $del_val, $path)
1426 {
1427 if (!is_array($val_array)) return $val_array;
1428 $result = [];
1429 foreach ($val_array as $v) {
1430 if ($path . $v != $del_val) {
1431 $result[] = $v;
1432 }
1433 }
1434 return $result;
1435 }
1436
1437 private function GetKeyData()
1438 {
1439 $now_time = time();
1440 return [
1441 'request_token' => md5($now_time . '' . md5($this->BT_KEY)),
1442 'request_time' => $now_time
1443 ];
1444 }
1445
1446 private function HttpPostCookie($url, $data, $timeout = 60, $connectTimeout = 10)
1447 {
1448 $cookie_file = ROOT . 'api/cookie/' . md5($this->BT_PANEL) . '.cookie';
1449 if (!is_dir(dirname($cookie_file))) {
1450 @mkdir(dirname($cookie_file), 0755, true);
1451 }
1452 if (!file_exists($cookie_file)) {
1453 $fp = fopen($cookie_file, 'w+');
1454 if (is_resource($fp)) {
1455 fclose($fp);
1456 }
1457 }
1458
1459 $output = $this->curlRequest($url, $data, $cookie_file, $timeout, $connectTimeout, $last_error);
1460 if ($output === false) {
1461 return '{"status":false,"msg":"连接面板失败:' . addslashes((string)$last_error) . '"}';
1462 }
1463 return $output;
1464 }
1465
1466 /**
1467 * 执行一次 cURL POST 请求。
1468 * 跟随面板 http->https 跳转并保持 POST 方法,避免面板开启 SSL 后 http 方式无法对接。
1469 * @return string|false 响应体;请求失败返回 false 并写入 $last_error
1470 */
1471 private function curlRequest($url, $data, $cookie_file, $timeout, $connectTimeout, &$last_error)
1472 {
1473 $ch = curl_init();
1474 curl_setopt($ch, CURLOPT_URL, $url);
1475 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
1476 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connectTimeout);
1477 curl_setopt($ch, CURLOPT_POST, 1);
1478 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
1479 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
1480 curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
1481 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1482 curl_setopt($ch, CURLOPT_HEADER, 0);
1483 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
1484 curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
1485 curl_setopt($ch, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
1486 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
1487 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
1488 $output = curl_exec($ch);
1489 if ($output === false) {
1490 $last_error = (string)curl_error($ch);
1491 }
1492 curl_close($ch);
1493 return $output;
1494 }
1495}
1496
1497// ========================================================================
1498// 向后兼容类
1499// ========================================================================
1500
1501class bt_api_set extends bt_api {}
1502class win_bt_api extends bt_api {}
1503class bt_api_rj extends bt_api {}