forked from Relief156/Minecraft-Server-Status
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
1253 lines (1089 loc) · 51.4 KB
/
api.php
File metadata and controls
1253 lines (1089 loc) · 51.4 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// API处理文件
// 确保PHP错误不会干扰JSON响应
ini_set('display_errors', 0);
ini_set('log_errors', 1);
// 启用输出缓冲以控制响应
ob_start();
// 检查是否已安装
if (!file_exists('installed.lock')) {
header('Content-Type: application/json');
echo json_encode(['error' => '系统尚未安装,请先完成安装设置']);
exit;
}
require_once 'config.php';
require_once 'db.php';
class MinecraftAPI {
private $java_api_urls; // Java版API URL列表(支持多个URL按优先级顺序)
private $bedrock_api_urls; // Bedrock版API URL列表(支持多个URL按优先级顺序)
private $log_file = 'api.log';
private $icon_cache_dir = 'cache/icons';
private $icon_cache_ttl = 3600; // 缓存过期时间,单位秒(1小时)
private $status_cache_ttl = 30; // 服务器状态缓存时间,单位秒(30秒)
private $status_cache = array(); // 内存缓存
private $request_interval = 5; // 最小请求间隔,单位秒(防止请求过快)
private $last_request_times = array(); // 记录每个服务器的最后请求时间
private $db; // 数据库连接
private $api_retry_count = 1; // API请求重试次数
// 构造函数
public function __construct() {
// 初始化API URL(从配置文件读取)
$this->java_api_urls = JAVA_API_URLS; // Java版API URL列表
$this->bedrock_api_urls = BEDROCK_API_URLS; // Bedrock版API URL列表
// 加载已选择的API
$this->loadSelectedApi();
// 初始化数据库连接
$this->db = new Database();
}
// 加载已选择的API
private function loadSelectedApi() {
$selection_file = 'api_selection.json';
if (file_exists($selection_file)) {
$selected_apis = json_decode(file_get_contents($selection_file), true);
if ($selected_apis) {
// 为Java版设置选择的API
if (isset($selected_apis['selected_java_api'])) {
// 确保选择的API存在于可用API列表中
if (in_array($selected_apis['selected_java_api'], $this->java_api_urls)) {
// 将选择的API移到列表首位
$key = array_search($selected_apis['selected_java_api'], $this->java_api_urls);
if ($key !== false) {
unset($this->java_api_urls[$key]);
array_unshift($this->java_api_urls, $selected_apis['selected_java_api']);
}
}
}
// 为Bedrock版设置选择的API
if (isset($selected_apis['selected_bedrock_api'])) {
// 确保选择的API存在于可用API列表中
if (in_array($selected_apis['selected_bedrock_api'], $this->bedrock_api_urls)) {
// 将选择的API移到列表首位
$key = array_search($selected_apis['selected_bedrock_api'], $this->bedrock_api_urls);
if ($key !== false) {
unset($this->bedrock_api_urls[$key]);
array_unshift($this->bedrock_api_urls, $selected_apis['selected_bedrock_api']);
}
}
}
}
}
}
// 日志记录方法
private function log($message) {
// 检查是否需要归档前一天的日志
$this->archiveLogIfNeeded();
$timestamp = date('Y-m-d H:i:s');
$log_entry = "[$timestamp] $message\n";
file_put_contents($this->log_file, $log_entry, FILE_APPEND);
}
// 检查并归档日志文件(如果日期已变更)
private function archiveLogIfNeeded() {
// 如果日志文件不存在,不需要归档
if (!file_exists($this->log_file)) {
return;
}
// 获取日志文件的最后修改日期和当前日期
$log_file_date = date('Y-m-d', filemtime($this->log_file));
$current_date = date('Y-m-d');
// 如果最后修改日期不是今天,说明是跨天了,需要归档
if ($log_file_date !== $current_date) {
// 确保logs目录存在
if (!file_exists('logs')) {
if (!mkdir('logs', 0755, true)) {
// 添加错误日志记录
$error_message = "[归档错误] 无法创建logs目录";
error_log($error_message);
return; // 无法创建目录,终止归档
}
}
// 归档文件名:logs/YYYY-MM-DD_api.log.gz
$archive_file = 'logs/' . $log_file_date . '_api.log.gz';
try {
// 读取当前日志内容
$log_content = file_get_contents($this->log_file);
if ($log_content === false) {
throw new Exception("无法读取日志文件内容");
}
// 尝试压缩归档 - 使用备用方法
if (function_exists('gzopen')) {
// 方法1:使用gzopen/gzwrite
$gz_handle = gzopen($archive_file, 'w9'); // 使用最高压缩级别
if ($gz_handle === false) {
throw new Exception("无法创建GZIP文件");
}
if (gzwrite($gz_handle, $log_content) === false) {
throw new Exception("写入GZIP文件失败");
}
gzclose($gz_handle);
} else {
// 方法2:备用方法,使用file_put_contents和base64_encode
// 如果服务器不支持gzip函数,使用这种方式保存非压缩版本
$archive_file = str_replace('.gz', '', $archive_file); // 移除.gz扩展名
if (file_put_contents($archive_file, $log_content) === false) {
throw new Exception("无法创建归档文件");
}
}
// 清空原日志文件
if (file_put_contents($this->log_file, '') === false) {
throw new Exception("清空原日志文件失败");
}
// 记录归档操作
$archive_message = "日志已归档到: $archive_file";
$timestamp = date('Y-m-d H:i:s');
file_put_contents($this->log_file, "[$timestamp] $archive_message\n", FILE_APPEND);
} catch (Exception $e) {
// 添加错误日志记录
$error_message = "[归档错误] " . $e->getMessage();
error_log($error_message);
// 也记录到当前日志文件(如果可能)
try {
$timestamp = date('Y-m-d H:i:s');
file_put_contents($this->log_file, "[$timestamp] $error_message\n", FILE_APPEND);
} catch (Exception $e2) {
// 如果无法写入日志文件,忽略
}
}
}
}
// 获取服务器状态
public function getServerStatus($server_address, $server_type = 'java') {
// 缓存键名
$cache_key = $server_address . ':' . $server_type;
$current_time = time();
// 记录请求开始
$this->log('开始请求服务器状态: ' . $server_address . ' (类型: ' . $server_type . ')');
// 检查是否有缓存并且缓存未过期
if (isset($this->status_cache[$cache_key]) &&
($current_time - $this->status_cache[$cache_key]['timestamp']) < $this->status_cache_ttl) {
$this->log('使用缓存的服务器状态数据');
return $this->status_cache[$cache_key]['data'];
}
// 检查请求间隔
if (isset($this->last_request_times[$cache_key])) {
$time_since_last_request = $current_time - $this->last_request_times[$cache_key];
if ($time_since_last_request < $this->request_interval) {
// 如果间隔太短,等待剩余时间
$wait_time = $this->request_interval - $time_since_last_request;
$this->log('请求间隔过短,等待 ' . $wait_time . ' 秒');
sleep($wait_time);
$current_time = time(); // 更新当前时间
}
}
// 更新最后请求时间
$this->last_request_times[$cache_key] = $current_time;
// 尝试使用主API和备用API的故障转移逻辑
// 首先尝试首选API,如果失败则尝试其他可用API
$api_urls = array();
// 根据服务器类型选择API列表
if ($server_type === 'java') {
$api_list = $this->java_api_urls;
} else {
$api_list = $this->bedrock_api_urls;
}
// 根据API配置构建优先顺序的URL数组
foreach ($api_list as $index => $api_url) {
$api_urls['api_' . ($index + 1)] = $api_url;
}
$response = null;
$http_code = 0;
$error_message = '';
$success = false;
foreach ($api_urls as $api_name => $api_url) {
// 只在主API失败时才尝试备用API
if ($api_name == 'secondary' && $success) {
$this->log('主API请求成功,跳过备用API');
break;
}
$this->log('尝试使用API (' . $api_name . '): ' . $api_url);
// 构建API请求URL
$url = $api_url . urlencode($server_address);
$this->log('请求URL: ' . $url);
// 初始化cURL
$ch = curl_init();
// 设置cURL选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 添加User-Agent头部以避免403错误
curl_setopt($ch, CURLOPT_USERAGENT, 'Minecraft-Server-Status-Monitor/1.0');
// 公益API不需要Authorization头
// 设置cURL超时时间
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 10秒超时
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // 5秒连接超时
// 执行请求
$this->log('开始执行cURL请求');
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->log('cURL请求完成,HTTP状态码: ' . $http_code);
// 检查错误
if (curl_errno($ch)) {
$error_message = 'API请求失败: ' . curl_error($ch);
$this->log($error_message);
curl_close($ch);
// 如果是主API失败,尝试备用API
if ($api_name == 'primary') {
$this->log('主API请求失败,尝试备用API...');
continue;
}
} else {
// 检查是否为429状态码(Too many requests)
if ($http_code == 429) {
$error_message = 'API请求频率过高,请稍后再试(Too many requests)';
$this->log($error_message);
curl_close($ch);
// 如果是主API遇到频率限制,尝试备用API
if ($api_name == 'primary') {
$this->log('主API请求频率过高,尝试备用API...');
continue;
} else {
// 备用API也遇到频率限制,则设置特殊错误信息
$error_message = '所有API都暂时无法处理请求,请稍后再试';
return ['error' => $error_message, 'http_code' => 429];
}
}
// 检查响应是否为空
if (empty($response)) {
$error_message = 'API返回空响应';
$this->log($error_message);
curl_close($ch);
// 如果是主API失败,尝试备用API
if ($api_name == 'primary') {
$this->log('主API返回空响应,尝试备用API...');
continue;
}
} else {
// 成功获取响应
$this->log($api_name . ' API请求成功,获取到有效响应');
$success = true;
curl_close($ch);
break;
}
}
}
if (!$success) {
$this->log('所有API请求均失败');
return ['error' => '无法连接到API服务器: ' . $error_message];
}
// 记录响应内容(限制长度,避免日志过大)
$response_preview = strlen($response) > 500 ? substr($response, 0, 500) . '...' : $response;
$this->log('响应内容: ' . $response_preview);
// 解析JSON响应
$this->log('开始解析JSON响应');
$data = json_decode($response, true);
// 检查响应是否有效
if (json_last_error() !== JSON_ERROR_NONE) {
// 获取JSON解析错误信息
$json_error = json_last_error_msg();
$error_message = '无效的API响应: ' . $json_error;
$this->log($error_message);
// 如果是429状态码,提供更友好的错误信息
if (isset($http_code) && $http_code == 429) {
$error_message = 'API请求过于频繁,请稍后再试';
}
// 如果是非JSON格式的错误消息,尝试提取原始消息
else if (!empty($response) && !is_array($data)) {
// 去除多余的空白字符和HTML标签
$clean_response = trim(strip_tags($response));
// 如果清理后的响应不为空,使用它作为错误消息的一部分
if (!empty($clean_response)) {
$error_message .= ',原始响应: ' . substr($clean_response, 0, 100);
}
}
return ['error' => $error_message, 'http_code' => $http_code];
}
// 记录解析后的响应数据结构
$this->log('响应数据解析成功,数据结构: ' . print_r(array_keys((array)$data), true));
// 根据返回的状态码判断服务器是否在线
if (isset($data['online'])) {
$status = $data['online'] ? '在线' : '离线';
$this->log('服务器状态: ' . $status);
}
// 转换API响应格式以匹配index.php的期望格式
$formatted_data = [];
// 复制基本字段 - 处理用户自建API格式
if (isset($data['online'])) {
$formatted_data['online'] = $data['online'];
}
// 转换玩家计数字段
if (isset($data['players']) && isset($data['players']['online'])) {
$formatted_data['players_online'] = $data['players']['online'];
}
if (isset($data['players']) && isset($data['players']['max'])) {
$formatted_data['players_max'] = $data['players']['max'];
}
// 处理玩家列表
if (isset($data['players']) && isset($data['players']['list']) && !empty($data['players']['list'])) {
$player_list = $data['players']['list'];
// 将逗号分隔的玩家列表转换为数组
$players = explode(', ', $player_list);
$formatted_data['player_list'] = $players;
}
// 复制版本信息
if (isset($data['version'])) {
$formatted_data['version'] = $data['version'];
}
// 处理MOTD - 支持mcsy.net返回的嵌套JSON格式的MOTD
if (isset($data['motd'])) {
// 尝试解析JSON格式的MOTD
$motd_json = json_decode($data['motd'], true);
if ($motd_json !== null) {
// 提取纯文本MOTD内容
$motd_text = $this->extractMOTDText($motd_json);
$formatted_data['motd'] = $motd_text;
// 生成HTML格式的MOTD(保留颜色和格式)
$motd_html = $this->convertMOTDToHTML($motd_json);
$formatted_data['motd_html'] = $motd_html;
} else {
// 如果解析失败,直接使用原始文本
$formatted_data['motd'] = $data['motd'];
$formatted_data['motd_html'] = $this->convertMinecraftColorsToHTML($data['motd']);
}
}
// 处理favicon - 用户自建API可能直接返回base64编码的图标
if (isset($data['favicon'])) {
$formatted_data['server_icon'] = $data['favicon'];
} else {
// 否则从icon接口获取图标(如果需要)
$formatted_data['server_icon'] = $this->getCachedServerIcon($server_address);
}
// 为离线服务器添加额外信息,提高用户体验
// 无论服务器是否在线,都添加基本连接信息
// 始终显示请求的服务器地址
$formatted_data['server_address'] = $server_address;
// 如果API返回了主机名,显示它
if (isset($data['hostname'])) {
$formatted_data['hostname'] = $data['hostname'];
}
// 如果API返回了IP地址,显示它
if (isset($data['ip'])) {
$formatted_data['ip_address'] = $data['ip'];
}
// 特别处理离线服务器
if (isset($data['online']) && !$data['online']) {
// 如果服务器离线,设置默认的MOTD信息
if (!isset($formatted_data['motd'])) {
$formatted_data['motd'] = '服务器当前离线';
$formatted_data['motd_html'] = '服务器当前离线';
}
}
// 记录格式化后的数据
$this->log('响应数据已格式化,包含字段: ' . print_r(array_keys($formatted_data), true));
// 将结果存入缓存
$cache_key = $server_address . ':' . $server_type;
$this->status_cache[$cache_key] = array(
'data' => $formatted_data,
'timestamp' => time()
);
return $formatted_data;
}
// 递归函数来解析嵌套的extra数组,提取纯文本
private function extractMOTDText($motd_data) {
$text = '';
if (is_array($motd_data)) {
// 如果有text字段,添加到结果中
if (isset($motd_data['text'])) {
$text .= $motd_data['text'];
}
// 如果有extra字段,递归处理
if (isset($motd_data['extra']) && is_array($motd_data['extra'])) {
foreach ($motd_data['extra'] as $extra) {
$text .= $this->extractMOTDText($extra);
}
}
} elseif (is_string($motd_data)) {
// 如果是字符串,直接返回
$text .= $motd_data;
}
return $text;
}
// 递归函数来解析嵌套的extra数组并转换为HTML格式(保留颜色和格式)
private function convertMOTDToHTML($motd_data) {
$html = '';
if (is_array($motd_data)) {
// 处理样式
$styles = [];
if (isset($motd_data['color'])) {
$color = $motd_data['color'];
// 检查是否是十六进制颜色值(以#开头并至少有6个字符)
if (strpos($color, '#') === 0 && strlen($color) >= 6) {
$styles[] = 'color: ' . $color;
} else {
// Minecraft颜色代码映射到CSS颜色
$color_map = [
'black' => '#000000',
'dark_blue' => '#0000AA',
'dark_green' => '#00AA00',
'dark_aqua' => '#00AAAA',
'dark_red' => '#AA0000',
'dark_purple' => '#AA00AA',
'gold' => '#FFAA00',
'gray' => '#AAAAAA',
'dark_gray' => '#555555',
'blue' => '#5555FF',
'green' => '#55FF55',
'aqua' => '#55FFFF',
'red' => '#FF5555',
'light_purple' => '#FF55FF',
'yellow' => '#FFFF55',
'white' => '#FFFFFF'
];
if (isset($color_map[$color])) {
$styles[] = 'color: ' . $color_map[$color];
}
}
}
if (isset($motd_data['bold']) && $motd_data['bold']) {
$styles[] = 'font-weight: bold';
}
if (isset($motd_data['italic']) && $motd_data['italic']) {
$styles[] = 'font-style: italic';
}
if (isset($motd_data['underlined']) && $motd_data['underlined']) {
$styles[] = 'text-decoration: underline';
}
// 开始样式
if (!empty($styles)) {
$html .= '<span style="' . implode('; ', $styles) . '">';
}
// 添加文本内容并处理换行符
if (isset($motd_data['text'])) {
$text = $motd_data['text'];
// 特殊处理单独的换行符对象
if ($text === "\n" || $text === "\\n") {
$html .= '<br>';
} else {
// 增强换行符处理,确保所有类型的换行符都能被识别
// 先转义所有可能的换行符表示
$text_with_breaks = str_replace(['\\n', "\n", "\r\n", "\r"], '<br>', $text);
// 再处理Minecraft颜色代码
$text_with_colors = $this->convertMinecraftColorsToHTML($text_with_breaks);
$html .= $text_with_colors;
}
}
// 处理嵌套的extra内容
if (isset($motd_data['extra']) && is_array($motd_data['extra'])) {
foreach ($motd_data['extra'] as $extra) {
$html .= $this->convertMOTDToHTML($extra);
}
}
// 结束样式
if (!empty($styles)) {
$html .= '</span>';
}
} elseif (is_string($motd_data)) {
// 如果是纯字符串,先处理换行符
$text_with_breaks = str_replace(['\\n', "\n", "\r\n", "\r"], '<br>', $motd_data);
// 再处理Minecraft颜色代码
$text_with_colors = $this->convertMinecraftColorsToHTML($text_with_breaks);
$html .= $text_with_colors;
}
return $html;
}
// 将Minecraft颜色代码转换为HTML
private function convertMinecraftColorsToHTML($text) {
// 检查是否包含Minecraft颜色代码
if (strpos($text, '§') === false) {
return htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8', false);
}
// Minecraft颜色代码映射
$color_map = [
'0' => '#000000', // 黑色
'1' => '#0000AA', // 深蓝色
'2' => '#00AA00', // 深绿色
'3' => '#00AAAA', // 深青色
'4' => '#AA0000', // 深红色
'5' => '#AA00AA', // 深紫色
'6' => '#FFAA00', // 金色
'7' => '#AAAAAA', // 灰色
'8' => '#555555', // 深灰色
'9' => '#5555FF', // 蓝色
'a' => '#55FF55', // 绿色
'b' => '#55FFFF', // 青色
'c' => '#FF5555', // 红色
'd' => '#FF55FF', // 紫色
'e' => '#FFFF55', // 黄色
'f' => '#FFFFFF', // 白色
];
// 样式代码映射
$style_map = [
'l' => 'font-weight: bold', // 粗体
'o' => 'font-style: italic', // 斜体
'n' => 'text-decoration: underline', // 下划线
'm' => 'text-decoration: line-through', // 删除线
'k' => 'text-shadow: 2px 2px 4px rgba(0,0,0,0.5);', // 闪烁(这里用阴影模拟)
'r' => 'reset' // 重置样式
];
$html = '';
$current_styles = [];
$length = mb_strlen($text, 'UTF-8');
for ($i = 0; $i < $length; $i++) {
// 使用mb_substr确保正确处理多字节字符
$char = mb_substr($text, $i, 1, 'UTF-8');
// 检查是否是颜色代码开始
if ($char === '§' && $i + 1 < $length) {
$code = mb_substr($text, $i + 1, 1, 'UTF-8');
$code = strtolower($code);
$i++;
// 重置样式
if ($code === 'r') {
if (!empty($current_styles)) {
$html .= '</span>';
$current_styles = [];
}
}
// 处理颜色代码
else if (isset($color_map[$code])) {
// 关闭当前样式
if (!empty($current_styles)) {
$html .= '</span>';
}
// 添加新颜色样式
$current_styles = ['color: ' . $color_map[$code]];
$html .= '<span style="' . implode('; ', $current_styles) . '">';
}
// 处理样式代码
else if (isset($style_map[$code])) {
if ($style_map[$code] === 'reset') {
if (!empty($current_styles)) {
$html .= '</span>';
$current_styles = [];
}
} else {
// 关闭当前样式
if (!empty($current_styles)) {
$html .= '</span>';
}
// 添加新样式
$current_styles = [$style_map[$code]];
$html .= '<span style="' . implode('; ', $current_styles) . '">';
}
}
} else {
// 普通字符,转义HTML特殊字符
$html .= htmlspecialchars($char, ENT_NOQUOTES, 'UTF-8', false);
}
}
// 关闭所有未闭合的样式标签
if (!empty($current_styles)) {
$html .= '</span>';
}
return $html;
}
// 获取缓存的服务器图标
private function getCachedServerIcon($server_address) {
// 确保缓存目录存在
if (!file_exists($this->icon_cache_dir)) {
mkdir($this->icon_cache_dir, 0755, true);
$this->log('创建图标缓存目录: ' . $this->icon_cache_dir);
}
// 生成缓存文件名(使用服务器地址的哈希值)
$cache_file = $this->icon_cache_dir . '/' . md5($server_address) . '.png';
$this->log('检查图标缓存文件: ' . $cache_file);
// 检查缓存文件是否存在且未过期
if (file_exists($cache_file) && (time() - filemtime($cache_file)) < $this->icon_cache_ttl) {
$this->log('使用缓存的图标文件');
return $cache_file;
}
// 如果缓存不存在或已过期,从用户自建API获取新图标并缓存
$icon_url = 'http://cow.mc6.cn:10709/icon/' . urlencode($server_address);
$this->log('获取新图标: ' . $icon_url);
// 使用cURL获取图标
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $icon_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Minecraft-Server-Status-Monitor/1.0');
curl_setopt($ch, CURLOPT_HEADER, false); // 不需要获取响应头
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 5秒超时
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// 检查响应
if ($http_code == 200 && !empty($response)) {
// 保存图标到缓存文件
file_put_contents($cache_file, $response);
$this->log('图标已保存到缓存: ' . $cache_file);
return $cache_file;
} else {
$this->log('无法获取图标,HTTP状态码: ' . $http_code);
// 如果缓存文件存在但已过期,仍然返回它
if (file_exists($cache_file)) {
$this->log('返回已过期的缓存图标');
return $cache_file;
}
// 获取失败,返回默认图标
$this->log('获取图标失败,返回默认图标');
return 'assets/default-icon.png';
}
}
// 获取服务器在线人数历史数据
public function getPlayerHistoryData($server_id, $days = 1) {
require_once 'db.php';
$db = new Database();
$result = $db->getPlayerHistory($server_id, $days);
// 初始化返回数据结构,确保即使没有数据也返回空数组
$labels = array();
$values = array();
if ($result === false) {
// 记录错误日志
$this->log('获取历史数据失败,数据库查询错误');
// 返回空的数据结构而不是false
return array(
'labels' => $labels,
'values' => $values
);
}
// 处理查询结果
while ($row = $result->fetch_assoc()) {
$labels[] = $row['time_label'];
$values[] = round($row['avg_players']);
}
$this->log('历史数据查询完成,数据点数量: ' . count($values));
return array(
'labels' => $labels,
'values' => $values
);
}
// 获取服务器原始玩家历史数据(包含玩家列表)
public function getRawPlayerHistoryData($server_id, $days = 1) {
require_once 'db.php';
$db = new Database();
$result = $db->getRawPlayerHistory($server_id, $days);
// 初始化返回数据结构,确保即使没有数据也返回空数组
$labels = array();
$values = array();
$playerLists = array();
if ($result === false) {
// 记录错误日志
$this->log('获取原始历史数据失败,数据库查询错误');
// 返回空的数据结构而不是false
return array(
'labels' => $labels,
'values' => $values,
'playerLists' => $playerLists
);
}
// 处理查询结果
while ($row = $result->fetch_assoc()) {
// 格式化时间标签
$time_label = date('Y-m-d H:i', strtotime($row['record_time']));
$labels[] = $time_label;
$values[] = $row['players_online'];
// 解析玩家列表JSON
$player_list = array();
if (!empty($row['player_list_json'])) {
try {
$player_list = json_decode($row['player_list_json'], true);
// 确保是数组格式
if (!is_array($player_list)) {
$player_list = array();
}
} catch (Exception $e) {
$this->log('解析玩家列表失败: ' . $e->getMessage());
}
}
$playerLists[] = $player_list;
}
$this->log('原始历史数据查询完成,数据点数量: ' . count($values));
return array(
'labels' => $labels,
'values' => $values,
'playerLists' => $playerLists
);
}
// 使用cURL多线程并行获取多个服务器状态
public function getServersStatusInParallel($servers) {
$current_time = time();
$cache_key_prefix = 'parallel_';
$mh = curl_multi_init();
$curl_handles = array();
$server_results = array();
// 为每个服务器创建cURL句柄
foreach ($servers as $server) {
$server_address = $server['address'];
$server_type = isset($server['type']) ? $server['type'] : 'java';
$cache_key = $cache_key_prefix . $server_address . ':' . $server_type;
// 检查是否有缓存并且缓存未过期
if (isset($this->status_cache[$cache_key]) &&
($current_time - $this->status_cache[$cache_key]['timestamp']) < $this->status_cache_ttl) {
$this->log('使用缓存的服务器状态数据: ' . $server_address);
$server_results[$server_address] = $this->status_cache[$cache_key]['data'];
continue;
}
// 根据服务器类型选择API列表
if ($server_type === 'java') {
$api_list = $this->java_api_urls;
} else {
$api_list = $this->bedrock_api_urls;
}
// 初始化API URL
$api_urls = array();
foreach ($api_list as $index => $api_url) {
$api_urls['api_' . ($index + 1)] = $api_url;
}
$primary_api_url = reset($api_urls);
// 构建API请求URL
$url = $primary_api_url . urlencode($server_address);
$this->log('创建并行请求: ' . $url);
// 初始化cURL
$ch = curl_init();
// 设置cURL选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Minecraft-Server-Status-Monitor/1.0');
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 10秒超时
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // 5秒连接超时
// 将cURL句柄添加到多线程批处理
curl_multi_add_handle($mh, $ch);
$curl_handles[$server_address] = $ch;
}
// 执行所有并行请求
$running = null;
do {
curl_multi_exec($mh, $running);
curl_multi_select($mh);
} while ($running > 0);
// 处理响应
foreach ($curl_handles as $server_address => $ch) {
$response = curl_multi_getcontent($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->log('并行请求完成: ' . $server_address . ', HTTP状态码: ' . $http_code);
// 检查错误
if (curl_errno($ch)) {
$error_message = 'API请求失败: ' . curl_error($ch);
$this->log($error_message);
$server_results[$server_address] = ['error' => $error_message];
} else {
// 解析JSON响应
$data = json_decode($response, true);
// 检查响应是否有效
if (json_last_error() !== JSON_ERROR_NONE) {
$json_error = json_last_error_msg();
$error_message = '无效的API响应: ' . $json_error;
$this->log($error_message);
$server_results[$server_address] = ['error' => $error_message];
} else {
// 根据服务器类型转换API响应格式
$formatted_data = [];
if (isset($data['online'])) {
$formatted_data['online'] = $data['online'];
}
// 处理玩家信息
if (isset($data['players'])) {
if (isset($data['players']['online'])) {
$formatted_data['players_online'] = $data['players']['online'];
}
if (isset($data['players']['max'])) {
$formatted_data['players_max'] = $data['players']['max'];
}
if (isset($data['players']['list']) && !empty($data['players']['list'])) {
$player_list = $data['players']['list'];
$players = is_array($player_list) ? $player_list : explode(', ', $player_list);
$formatted_data['player_list'] = $players;
}
}
// 处理版本信息
if (isset($data['version'])) {
$formatted_data['version'] = $data['version'];
}
// 基岩版特有: 协议版本信息
if ($server_type === 'bedrock' && isset($data['protocol']) && isset($data['protocol']['name'])) {
$formatted_data['protocol_version'] = $data['protocol']['name'];
}
// 处理MOTD
if (isset($data['motd'])) {
// 基岩版的motd通常是数组格式
if ($server_type === 'bedrock' && is_array($data['motd'])) {
if (isset($data['motd']['clean'])) {
$formatted_data['motd'] = $data['motd']['clean'];
} elseif (isset($data['motd']['raw'])) {
$formatted_data['motd'] = $data['motd']['raw'];
}
if (isset($data['motd']['html'])) {
$formatted_data['motd_html'] = $data['motd']['html'];
} else {
$formatted_data['motd_html'] = $this->convertMinecraftColorsToHTML(implode('', $data['motd']));
}
} else {
// Java版处理逻辑
$motd_json = json_decode($data['motd'], true);
if ($motd_json !== null) {
$formatted_data['motd'] = $this->extractMOTDText($motd_json);
$formatted_data['motd_html'] = $this->convertMOTDToHTML($motd_json);
} else {
$formatted_data['motd'] = $data['motd'];
$formatted_data['motd_html'] = $this->convertMinecraftColorsToHTML($data['motd']);
}
}
}
// 处理服务器图标
if (isset($data['favicon'])) {
$formatted_data['server_icon'] = $data['favicon'];
} else {
$formatted_data['server_icon'] = $this->getCachedServerIcon($server_address);
}
// 基本信息
$formatted_data['server_address'] = $server_address;
if (isset($data['hostname'])) {
$formatted_data['hostname'] = $data['hostname'];
}
if (isset($data['ip'])) {
$formatted_data['ip_address'] = $data['ip'];
}
// 基岩版特有: 地图和游戏模式
if ($server_type === 'bedrock') {
if (isset($data['map']) && isset($data['map']['clean'])) {
$formatted_data['map'] = $data['map']['clean'];
}
if (isset($data['gamemode'])) {
$formatted_data['gamemode'] = $data['gamemode'];
}
}
// 离线服务器处理
if (isset($data['online']) && !$data['online']) {
if (!isset($formatted_data['motd'])) {
$formatted_data['motd'] = '服务器当前离线';
$formatted_data['motd_html'] = '服务器当前离线';
}
}
// 根据服务器类型确定缓存键
$cache_key = $cache_key_prefix . $server_address . ':' . $server_type;
$this->status_cache[$cache_key] = array(
'data' => $formatted_data,
'timestamp' => time()
);
$server_results[$server_address] = $formatted_data;
}
}
// 关闭cURL句柄
curl_multi_remove_handle($mh, $ch);
curl_close($ch);
}
// 关闭多线程批处理
curl_multi_close($mh);
return $server_results;
}
// 主入口函数
public function handleRequest() {
if (!isset($_GET['action'])) {
$this->sendErrorResponse('缺少操作参数');