-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.php
More file actions
executable file
·1243 lines (1135 loc) · 72.4 KB
/
admin.php
File metadata and controls
executable file
·1243 lines (1135 loc) · 72.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
/**
* 域名管理后台
* 提供域名配置的增删改查功能
*/
// 读取JSON文件辅助函数
function readJsonFile($file, $default = [])
{
if (file_exists($file)) {
return json_decode(file_get_contents($file), true) ?: $default;
}
return $default;
}
// 写入JSON文件辅助函数
function writeJsonFile($file, $data)
{
$dir = dirname($file);
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
return file_put_contents($file, json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
}
function getSiteSettingsFile()
{
return __DIR__ . '/data/site_settings.json';
}
function getSiteSettings()
{
return readJsonFile(getSiteSettingsFile());
}
function verifyAdminPassword($inputPassword, &$shouldMigrate = false)
{
$settings = getSiteSettings();
$passwordHash = $settings['admin_password_hash'] ?? '';
$legacyPassword = $settings['admin_password'] ?? '12345678';
if (!empty($passwordHash)) {
return password_verify($inputPassword, $passwordHash);
}
$isValid = hash_equals((string)$legacyPassword, (string)$inputPassword);
$shouldMigrate = $isValid;
return $isValid;
}
function migrateLegacyPasswordToHash($plainPassword)
{
$settings = getSiteSettings();
$settings['admin_password_hash'] = password_hash($plainPassword, PASSWORD_DEFAULT);
unset($settings['admin_password']);
writeJsonFile(getSiteSettingsFile(), $settings);
}
function ensureCsrfToken()
{
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
return $_SESSION['csrf_token'];
}
function validateCsrfToken($token)
{
return !empty($_SESSION['csrf_token']) && is_string($token) && hash_equals($_SESSION['csrf_token'], $token);
}
function isValidDomainName($domain)
{
return (bool)preg_match('/^(?!-)(?:[a-z0-9-]{1,63}\.)+[a-z]{2,63}$/i', $domain);
}
function isValidHttpUrl($url)
{
if ($url === '') {
return true;
}
if (!filter_var($url, FILTER_VALIDATE_URL)) {
return false;
}
$scheme = strtolower((string)parse_url($url, PHP_URL_SCHEME));
return in_array($scheme, ['http', 'https'], true);
}
function getDefaultFooterLinks()
{
return [
['name' => 'WHOIS查询', 'url' => 'https://bluewhois.com/{domain}', 'icon_class' => 'fa-solid fa-magnifying-glass'],
['name' => '西风', 'url' => 'https://xifeng.net', 'icon_class' => 'fa-solid fa-wind'],
['name' => '更多域名', 'url' => 'https://domain.ls', 'icon_class' => 'fa-solid fa-globe'],
];
}
function normalizeFooterLinks($rawLinks)
{
$defaultLinks = getDefaultFooterLinks();
if (!is_array($rawLinks) || empty($rawLinks)) {
return $defaultLinks;
}
$normalized = [];
foreach ($rawLinks as $item) {
if (!is_array($item)) {
continue;
}
$name = trim((string)($item['name'] ?? ''));
$url = trim((string)($item['url'] ?? ''));
$iconClass = trim((string)($item['icon_class'] ?? 'fa-solid fa-link'));
if ($name === '' || $url === '') {
continue;
}
$normalized[] = [
'name' => $name,
'url' => $url,
'icon_class' => $iconClass,
];
}
if (empty($normalized)) {
return $defaultLinks;
}
return array_slice($normalized, 0, 3);
}
function getFooterLinksFromSettings($data)
{
if (!empty($data['footer_links']) && is_array($data['footer_links'])) {
return normalizeFooterLinks($data['footer_links']);
}
return normalizeFooterLinks([
[
'name' => 'WHOIS查询',
'url' => $data['footer_whois_url'] ?? 'https://bluewhois.com/{domain}',
'icon_class' => 'fa-solid fa-magnifying-glass',
],
[
'name' => '西风',
'url' => $data['footer_xifeng_url'] ?? 'https://xifeng.net',
'icon_class' => 'fa-solid fa-wind',
],
[
'name' => '更多域名',
'url' => $data['footer_more_domains_url'] ?? 'https://domain.ls',
'icon_class' => 'fa-solid fa-globe',
],
]);
}
function isValidFooterLinkUrl($url)
{
$normalizedUrl = str_replace('{domain}', 'example.com', (string)$url);
return isValidHttpUrl($normalizedUrl);
}
function isValidFontAwesomeIconClass($iconClass)
{
if ($iconClass === '') {
return false;
}
// 支持 FontAwesome 类名
if (preg_match('/^(fa-(solid|regular|brands|duotone|thin|light)\s+)?fa-[a-z0-9-]+(?:\s+fa-[a-z0-9-]+)*$/i', $iconClass)) {
return true;
}
// 支持 SVG 代码(必须以 <svg 开头,以 </svg> 结尾)
if (isValidSvgIcon($iconClass)) {
return true;
}
return false;
}
function isValidSvgIcon($str)
{
$str = trim($str);
if (stripos($str, '<svg') !== 0 || stripos($str, '</svg>') === false) {
return false;
}
// 禁止脚本注入
$lower = strtolower($str);
if (preg_match('/<script|on\w+\s*=|javascript:/i', $lower)) {
return false;
}
return true;
}
if (PHP_VERSION_ID >= 70300) {
session_set_cookie_params([
'lifetime' => 0,
'path' => '/',
'httponly' => true,
'secure' => (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'),
'samesite' => 'Lax',
]);
}
session_start();
$csrfToken = ensureCsrfToken();
$isLoggedIn = isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true;
$adminEntryUrl = '/admin';
// 处理登录
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'login') {
if (!validateCsrfToken($_POST['csrf_token'] ?? '')) {
$loginError = '请求无效,请刷新页面后重试';
} elseif (!isset($_POST['password']) || $_POST['password'] === '') {
$loginError = '请输入密码';
} else {
$shouldMigratePassword = false;
if (verifyAdminPassword($_POST['password'], $shouldMigratePassword)) {
session_regenerate_id(true);
$_SESSION['admin_logged_in'] = true;
if ($shouldMigratePassword) {
migrateLegacyPasswordToHash($_POST['password']);
}
header('Location: ' . $adminEntryUrl);
exit;
}
$loginError = '密码错误';
}
}
// 处理登出
if (isset($_GET['logout'])) {
session_destroy();
header('Location: ' . $adminEntryUrl);
exit;
}
// 如果未登录,显示登录表单
if (!$isLoggedIn) {
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>域名管理后台 - 登录</title>
<link rel="stylesheet" href="/assets/css/admin.css">
</head>
<body class="login-page">
<div class="login-box">
<h1>
<svg class="login-icon" viewBox="0 0 24 24" fill="currentColor" width="24" height="24" style="vertical-align: middle; margin-right: 8px;">
<path d="M18 8h-1V6c0-2.76-2.24-5-5-5S7 3.24 7 6v2H6c-1.1 0-2 .9-2 2v10c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V10c0-1.1-.9-2-2-2zm-6 9c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2zm3.1-9H8.9V6c0-1.71 1.39-3.1 3.1-3.1 1.71 0 3.1 1.39 3.1 3.1v2z" />
</svg>
域名管理后台
</h1>
<?php if (isset($loginError)): ?>
<div class="error"><?php echo htmlspecialchars($loginError, ENT_QUOTES, 'UTF-8'); ?></div>
<?php endif; ?>
<form method="post">
<input type="hidden" name="action" value="login">
<input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8'); ?>">
<div class="form-group">
<label for="password">管理员密码</label>
<input type="password" id="password" name="password" required autofocus>
</div>
<button type="submit">登录</button>
</form>
</div>
</body>
</html>
<?php
exit;
}
// 已登录,处理AJAX请求
require_once __DIR__ . '/core/DomainManager.php';
require_once __DIR__ . '/core/StatsTracker.php';
require_once __DIR__ . '/core/DomainConfig.php';
require_once __DIR__ . '/core/EmailHandler.php';
$domainManager = new DomainManager();
// 处理AJAX请求
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
header('Content-Type: application/json; charset=UTF-8');
if (!validateCsrfToken($_POST['csrf_token'] ?? '')) {
echo json_encode(['success' => false, 'message' => '请求无效,请刷新页面后重试'], JSON_UNESCAPED_UNICODE);
exit;
}
switch ($_POST['action']) {
case 'get_email_settings':
$settingsFile = __DIR__ . '/data/email_settings.json';
$data = readJsonFile($settingsFile);
echo json_encode(['success' => true, 'data' => [
'from_name' => $data['from_name'] ?? '',
'from_email' => $data['from_email'] ?? '',
'default_to_email' => $data['default_to_email'] ?? '',
'smtp_enabled' => true,
'smtp_host' => $data['smtp_host'] ?? '',
'smtp_port' => (int)($data['smtp_port'] ?? 587),
'smtp_encryption' => $data['smtp_encryption'] ?? 'tls',
'smtp_username' => $data['smtp_username'] ?? '',
'smtp_password' => $data['smtp_password'] ?? ''
]]);
exit;
case 'save_email_settings':
$fromName = trim($_POST['from_name'] ?? '');
$fromEmail = trim($_POST['from_email'] ?? '');
$defaultToEmail = trim($_POST['default_to_email'] ?? '');
$smtpEnabled = true;
$smtpHost = trim($_POST['smtp_host'] ?? '');
$smtpPort = (int)($_POST['smtp_port'] ?? 587);
$smtpEncryption = in_array($_POST['smtp_encryption'] ?? 'tls', ['none', 'ssl', 'tls']) ? ($_POST['smtp_encryption']) : 'tls';
$smtpUsername = trim($_POST['smtp_username'] ?? '');
$smtpPassword = trim($_POST['smtp_password'] ?? '');
if ($fromEmail && !filter_var($fromEmail, FILTER_VALIDATE_EMAIL)) {
echo json_encode(['success' => false, 'message' => '发件人邮箱格式不正确']);
exit;
}
if ($defaultToEmail && !filter_var($defaultToEmail, FILTER_VALIDATE_EMAIL)) {
echo json_encode(['success' => false, 'message' => '默认收件邮箱格式不正确']);
exit;
}
$settingsFile = __DIR__ . '/data/email_settings.json';
writeJsonFile($settingsFile, [
'from_name' => $fromName,
'from_email' => $fromEmail,
'default_to_email' => $defaultToEmail,
'smtp_enabled' => true,
'smtp_host' => $smtpHost,
'smtp_port' => $smtpPort,
'smtp_encryption' => $smtpEncryption,
'smtp_username' => $smtpUsername,
'smtp_password' => $smtpPassword
]);
echo json_encode(['success' => true, 'message' => '邮件设置已保存']);
exit;
case 'get_site_settings':
$settingsFile = __DIR__ . '/data/site_settings.json';
$data = readJsonFile($settingsFile);
// 不返回密码,只返回是否存在密码的标记
echo json_encode(['success' => true, 'data' => [
'admin_password_set' => !empty($data['admin_password_hash']) || !empty($data['admin_password']),
'site_name' => $data['site_name'] ?? '',
]]);
exit;
case 'save_site_settings':
$oldPassword = trim($_POST['old_password'] ?? '');
$adminPassword = trim($_POST['admin_password'] ?? '');
$confirmPassword = trim($_POST['confirm_password'] ?? '');
$siteName = trim($_POST['site_name'] ?? '');
$settingsFile = __DIR__ . '/data/site_settings.json';
// 读取现有设置
$existingData = readJsonFile($settingsFile);
// 更新设置(先复制现有数据)
$newData = $existingData;
$newData['site_name'] = $siteName;
// 如果要修改密码,必须先验证原密码
if (!empty($adminPassword)) {
if (empty($oldPassword)) {
echo json_encode(['success' => false, 'message' => '修改密码需要输入原密码']);
exit;
}
$dummy = false;
if (!verifyAdminPassword($oldPassword, $dummy)) {
echo json_encode(['success' => false, 'message' => '原密码不正确']);
exit;
}
// 验证新密码
if (strlen($adminPassword) < 8) {
echo json_encode(['success' => false, 'message' => '新密码长度至少8位']);
exit;
}
if ($adminPassword !== $confirmPassword) {
echo json_encode(['success' => false, 'message' => '两次输入的新密码不一致']);
exit;
}
$newData['admin_password_hash'] = password_hash($adminPassword, PASSWORD_DEFAULT);
unset($newData['admin_password']);
}
writeJsonFile($settingsFile, $newData);
echo json_encode(['success' => true, 'message' => '站点设置已保存']);
exit;
case 'get_footer_settings':
$settingsFile = __DIR__ . '/data/site_settings.json';
$data = readJsonFile($settingsFile);
echo json_encode(['success' => true, 'data' => [
'footer_links' => getFooterLinksFromSettings($data),
'footer_analytics_code' => $data['footer_analytics_code'] ?? '',
]], JSON_UNESCAPED_UNICODE);
exit;
case 'save_footer_settings':
$settingsFile = __DIR__ . '/data/site_settings.json';
$existingData = readJsonFile($settingsFile);
$footerAnalyticsCode = trim((string)($_POST['footer_analytics_code'] ?? ''));
$footerLinksRaw = (string)($_POST['footer_links_json'] ?? '[]');
$decoded = json_decode($footerLinksRaw, true);
if (!is_array($decoded)) {
echo json_encode(['success' => false, 'message' => '页脚链接数据格式无效'], JSON_UNESCAPED_UNICODE);
exit;
}
if (count($decoded) > 3) {
echo json_encode(['success' => false, 'message' => '最多只允许 3 个页脚链接'], JSON_UNESCAPED_UNICODE);
exit;
}
$normalizedLinks = [];
foreach ($decoded as $index => $item) {
if (!is_array($item)) {
echo json_encode(['success' => false, 'message' => '页脚链接格式无效'], JSON_UNESCAPED_UNICODE);
exit;
}
$name = trim((string)($item['name'] ?? ''));
$url = trim((string)($item['url'] ?? ''));
$iconClass = trim((string)($item['icon_class'] ?? 'fa-solid fa-link'));
if ($name === '' || $url === '') {
echo json_encode(['success' => false, 'message' => '页脚链接名称和 URL 不能为空'], JSON_UNESCAPED_UNICODE);
exit;
}
if (!isValidFooterLinkUrl($url)) {
echo json_encode(['success' => false, 'message' => '第 ' . ($index + 1) . ' 个链接 URL 格式不正确'], JSON_UNESCAPED_UNICODE);
exit;
}
if (!isValidFontAwesomeIconClass($iconClass)) {
echo json_encode(['success' => false, 'message' => '第 ' . ($index + 1) . ' 个图标类名无效'], JSON_UNESCAPED_UNICODE);
exit;
}
$normalizedLinks[] = [
'name' => $name,
'url' => $url,
'icon_class' => $iconClass,
];
}
if (empty($normalizedLinks)) {
$normalizedLinks = getDefaultFooterLinks();
}
$newData = $existingData;
$newData['footer_links'] = $normalizedLinks;
$newData['footer_analytics_code'] = $footerAnalyticsCode;
writeJsonFile($settingsFile, $newData);
echo json_encode(['success' => true, 'message' => '页脚设置已保存'], JSON_UNESCAPED_UNICODE);
exit;
case 'test_email':
$to = trim($_POST['to'] ?? '');
if (!$to || !filter_var($to, FILTER_VALIDATE_EMAIL)) {
echo json_encode(['success' => false, 'message' => '测试收件邮箱格式不正确']);
exit;
}
// 读取全局发件设置
$settingsFile = __DIR__ . '/data/email_settings.json';
$data = readJsonFile($settingsFile);
$fromName = $data['from_name'] ?? '';
$fromEmail = $data['from_email'] ?? '';
// 使用当前域名配置构造 EmailHandler
$domainConfig = new DomainConfig();
$emailHandler = new EmailHandler($domainConfig);
$subject = '测试邮件 - 域名后台';
$content = '<p>这是一封测试邮件,用于验证邮件发送配置是否可用。</p>';
$result = $emailHandler->sendEmail($subject, $content, $fromEmail ?: null, $fromName ?: null, null, $to);
echo json_encode($result);
exit;
case 'add':
// 添加域名
$domain = DomainConfig::normalizeDomain(trim($_POST['domain'] ?? ''));
if (empty($domain)) {
echo json_encode(['success' => false, 'message' => '域名不能为空']);
exit;
}
if (!isValidDomainName($domain)) {
echo json_encode(['success' => false, 'message' => '域名格式不正确']);
exit;
}
if ($domainManager->domainExists($domain)) {
echo json_encode(['success' => false, 'message' => '该域名已存在']);
exit;
}
$result = $domainManager->addDomain([
'domain' => $domain,
'title' => trim($_POST['title'] ?? $domain),
'description' => trim($_POST['description'] ?? ''),
'theme_color' => trim($_POST['theme_color'] ?? '#0065F3'),
'domain_intro' => trim($_POST['domain_intro'] ?? ''),
'domain_price' => trim($_POST['domain_price'] ?? '')
]);
if ($result) {
echo json_encode(['success' => true, 'message' => '添加成功']);
} else {
echo json_encode(['success' => false, 'message' => '添加失败']);
}
exit;
case 'update':
// 更新域名
if (empty($_POST['id'])) {
echo json_encode(['success' => false, 'message' => 'ID不能为空']);
exit;
}
$result = $domainManager->updateDomain($_POST['id'], [
'title' => trim($_POST['title'] ?? ''),
'description' => trim($_POST['description'] ?? ''),
'theme_color' => trim($_POST['theme_color'] ?? '#0065F3'),
'domain_intro' => trim($_POST['domain_intro'] ?? ''),
'domain_price' => trim($_POST['domain_price'] ?? '')
]);
if ($result) {
echo json_encode(['success' => true, 'message' => '更新成功']);
} else {
echo json_encode(['success' => false, 'message' => '更新失败']);
}
exit;
case 'delete':
// 删除域名
if (empty($_POST['id'])) {
echo json_encode(['success' => false, 'message' => 'ID不能为空']);
exit;
}
$result = $domainManager->deleteDomain($_POST['id']);
if ($result) {
echo json_encode(['success' => true, 'message' => '删除成功']);
} else {
echo json_encode(['success' => false, 'message' => '删除失败']);
}
exit;
case 'get':
// 获取单个域名配置
if (empty($_POST['id'])) {
echo json_encode(['success' => false, 'message' => 'ID不能为空']);
exit;
}
$domain = $domainManager->getDomainById($_POST['id']);
if ($domain) {
echo json_encode(['success' => true, 'data' => $domain]);
} else {
echo json_encode(['success' => false, 'message' => '域名不存在']);
}
exit;
}
}
$allowedSections = ['domains', 'stats', 'email', 'site', 'footer'];
// section 切换(支持 ?section= 和 /admin/{section} 两种形式)
$requestPath = parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH) ?: '';
$sectionFromPath = '';
if (preg_match('#^/admin(?:/([a-z]+))?/?$#i', $requestPath, $m)) {
$sectionFromPath = strtolower((string)($m[1] ?? 'domains'));
}
$section = isset($_GET['section']) ? (string)$_GET['section'] : ($sectionFromPath ?: 'domains');
if (!in_array($section, $allowedSections, true)) {
$section = 'domains';
}
$usePrettyAdminRoutes = true;
$adminAjaxEndpoint = $usePrettyAdminRoutes ? '/admin.php' : $_SERVER['PHP_SELF'];
function adminSectionUrl($section = 'domains', $params = [])
{
global $usePrettyAdminRoutes, $allowedSections;
$targetSection = in_array($section, $allowedSections, true) ? $section : 'domains';
$query = http_build_query($params);
if ($usePrettyAdminRoutes) {
$path = '/admin' . ($targetSection === 'domains' ? '' : '/' . $targetSection);
return $query !== '' ? ($path . '?' . $query) : $path;
}
$base = $_SERVER['PHP_SELF'] . '?section=' . rawurlencode($targetSection);
return $query !== '' ? ($base . '&' . $query) : $base;
}
function adminLogoutUrl()
{
global $usePrettyAdminRoutes;
if ($usePrettyAdminRoutes) {
return '/admin?logout=1';
}
return $_SERVER['PHP_SELF'] . '?logout=1';
}
// 分页设置
$perPage = isset($_GET['per_page']) ? max(10, min(50, (int)$_GET['per_page'])) : 10;
$currentPage = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1;
// 分页计算辅助函数
function getPaginationData($total, $perPage, $currentPage) {
$totalPages = ceil($total / $perPage);
$offset = ($currentPage - 1) * $perPage;
return ['total_pages' => $totalPages, 'offset' => $offset];
}
// 获取所有域名(用于域名管理 & 邮件设置)
$allDomains = $domainManager->getAllDomains();
$domainsCount = count($allDomains);
// 分页处理
if ($section === 'domains') {
$pagination = getPaginationData($domainsCount, $perPage, $currentPage);
$totalPages = $pagination['total_pages'];
$domains = array_slice($allDomains, $pagination['offset'], $perPage);
} else {
$domains = $allDomains;
$totalPages = 1;
}
// 若统计页,准备统计数据
$days = 30;
$allStats = [];
$statsTotalPages = 1;
if ($section === 'stats') {
$days = isset($_GET['days']) ? max(1, (int)$_GET['days']) : 30;
$statsTracker = new StatsTracker();
$allStatsRaw = $statsTracker->getAllDomainsStats($days);
$statsCount = count($allStatsRaw);
// 统计页分页
$statsPagination = getPaginationData($statsCount, $perPage, $currentPage);
$statsTotalPages = $statsPagination['total_pages'];
$allStats = array_slice($allStatsRaw, $statsPagination['offset'], $perPage);
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>域名管理后台</title>
<link rel="stylesheet" href="/assets/css/admin.css">
</head>
<body data-admin-endpoint="<?php echo htmlspecialchars($adminAjaxEndpoint, ENT_QUOTES, 'UTF-8'); ?>" data-csrf-token="<?php echo htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8'); ?>">
<!-- 顶部导航栏 -->
<div class="topbar">
<div class="topbar-inner admin-container">
<div class="brand">
<svg class="header-icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" width="28" height="28" fill="currentColor">
<path d="M797.866667 768H226.133333c-10.666667 0-17.066667 12.8-8.533333 21.333333 74.666667 78.933333 179.2 128 294.4 128s221.866667-49.066667 294.4-128c8.533333-8.533333 2.133333-21.333333-8.533333-21.333333zM192 298.666667h640c10.666667 0 17.066667-12.8 10.666667-21.333334-72.533333-102.4-194.133333-170.666667-330.666667-170.666666S253.866667 174.933333 181.333333 277.333333c-6.4 8.533333 0 21.333333 10.666667 21.333334z m640 64H192c-46.933333 0-85.333333 38.4-85.333333 85.333333v170.666667c0 46.933333 38.4 85.333333 85.333333 85.333333h640c46.933333 0 85.333333-38.4 85.333333-85.333333v-170.666667c0-46.933333-38.4-85.333333-85.333333-85.333333z m-437.333333 115.2L362.666667 618.666667c-4.266667 12.8-12.8 17.066667-25.6 17.066666s-21.333333-6.4-25.6-17.066666l-29.866667-98.133334-29.866667 98.133334c-4.266667 10.666667-12.8 17.066667-25.6 17.066666s-21.333333-6.4-25.6-17.066666l-32-140.8V469.333333c2.133333-10.666667 6.4-17.066667 17.066667-17.066666s19.2 6.4 21.333333 19.2l21.333334 108.8 32-110.933334c4.266667-8.533333 10.666667-14.933333 19.2-14.933333 10.666667 0 17.066667 4.266667 19.2 14.933333l32 110.933334 21.333333-108.8c2.133333-12.8 8.533333-19.2 19.2-19.2s17.066667 6.4 17.066667 17.066666c6.4 2.133333 6.4 6.4 6.4 8.533334z m230.4 0L593.066667 618.666667c-4.266667 12.8-12.8 17.066667-25.6 17.066666s-21.333333-6.4-25.6-17.066666L512 520.533333 482.133333 618.666667c-4.266667 10.666667-12.8 17.066667-25.6 17.066666s-21.333333-6.4-25.6-17.066666l-32-140.8V469.333333c2.133333-10.666667 6.4-17.066667 17.066667-17.066666s19.2 6.4 21.333333 19.2l21.333334 108.8L490.666667 469.333333c4.266667-8.533333 10.666667-14.933333 19.2-14.933333 10.666667 0 17.066667 4.266667 19.2 14.933333l32 110.933334 21.333333-108.8c2.133333-12.8 8.533333-19.2 19.2-19.2s17.066667 6.4 17.066667 17.066666c8.533333 2.133333 8.533333 6.4 6.4 8.533334z m232.533333 0L825.6 618.666667c-4.266667 12.8-12.8 17.066667-25.6 17.066666s-21.333333-6.4-25.6-17.066666l-29.866667-98.133334-29.866666 98.133334c-4.266667 10.666667-12.8 17.066667-25.6 17.066666s-21.333333-6.4-25.6-17.066666l-32-140.8V469.333333c2.133333-10.666667 6.4-17.066667 17.066666-17.066666s19.2 6.4 21.333334 19.2l21.333333 108.8 32-110.933334c4.266667-8.533333 10.666667-14.933333 19.2-14.933333 10.666667 0 17.066667 4.266667 19.2 14.933333l32 110.933334 21.333333-108.8c2.133333-12.8 8.533333-19.2 19.2-19.2s17.066667 6.4 17.066667 17.066666c8.533333 2.133333 6.4 6.4 6.4 8.533334z"></path>
</svg>
<span class="title">域名管理后台</span>
</div>
<div class="topbar-actions">
<a href="<?php echo htmlspecialchars(adminLogoutUrl(), ENT_QUOTES, 'UTF-8'); ?>" class="logout-btn">
<div class="logout-sign">
<svg viewBox="0 0 24 24" fill="currentColor" width="17" height="17">
<path d="M17 7l-1.41 1.41L18.17 11H8v2h10.17l-2.58 2.59L17 17l5-5zM4 5h8V3H4c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h8v-2H4V5z" />
</svg>
</div>
<span class="logout-text">退出登录</span>
</a>
</div>
</div>
</div>
<div class="admin-layout admin-container">
<!-- 侧边菜单 -->
<aside class="sidebar">
<nav class="menu">
<a class="menu-item <?php echo $section === 'domains' ? 'active' : ''; ?>" href="<?php echo htmlspecialchars(adminSectionUrl('domains'), ENT_QUOTES, 'UTF-8'); ?>">域名管理</a>
<a class="menu-item <?php echo $section === 'stats' ? 'active' : ''; ?>" href="<?php echo htmlspecialchars(adminSectionUrl('stats'), ENT_QUOTES, 'UTF-8'); ?>">访问统计</a>
<a class="menu-item <?php echo $section === 'email' ? 'active' : ''; ?>" href="<?php echo htmlspecialchars(adminSectionUrl('email'), ENT_QUOTES, 'UTF-8'); ?>">邮件设置</a>
<a class="menu-item <?php echo $section === 'site' ? 'active' : ''; ?>" href="<?php echo htmlspecialchars(adminSectionUrl('site'), ENT_QUOTES, 'UTF-8'); ?>">站点设置</a>
<a class="menu-item <?php echo $section === 'footer' ? 'active' : ''; ?>" href="<?php echo htmlspecialchars(adminSectionUrl('footer'), ENT_QUOTES, 'UTF-8'); ?>">页脚设置</a>
</nav>
</aside>
<!-- 主内容区域 -->
<main class="content">
<?php if ($section === 'domains'): ?>
<div class="admin-header">
<h1>
<svg class="header-icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" width="28" height="28" fill="currentColor">
<path d="M797.866667 768H226.133333c-10.666667 0-17.066667 12.8-8.533333 21.333333 74.666667 78.933333 179.2 128 294.4 128s221.866667-49.066667 294.4-128c8.533333-8.533333 2.133333-21.333333-8.533333-21.333333zM192 298.666667h640c10.666667 0 17.066667-12.8 10.666667-21.333334-72.533333-102.4-194.133333-170.666667-330.666667-170.666666S253.866667 174.933333 181.333333 277.333333c-6.4 8.533333 0 21.333333 10.666667 21.333334z m640 64H192c-46.933333 0-85.333333 38.4-85.333333 85.333333v170.666667c0 46.933333 38.4 85.333333 85.333333 85.333333h640c46.933333 0 85.333333-38.4 85.333333-85.333333v-170.666667c0-46.933333-38.4-85.333333-85.333333-85.333333z m-437.333333 115.2L362.666667 618.666667c-4.266667 12.8-12.8 17.066667-25.6 17.066666s-21.333333-6.4-25.6-17.066666l-29.866667-98.133334-29.866667 98.133334c-4.266667 10.666667-12.8 17.066667-25.6 17.066666s-21.333333-6.4-25.6-17.066666l-32-140.8V469.333333c2.133333-10.666667 6.4-17.066667 17.066667-17.066666s19.2 6.4 21.333333 19.2l21.333334 108.8 32-110.933334c4.266667-8.533333 10.666667-14.933333 19.2-14.933333 10.666667 0 17.066667 4.266667 19.2 14.933333l32 110.933334 21.333333-108.8c2.133333-12.8 8.533333-19.2 19.2-19.2s17.066667 6.4 17.066667 17.066666c6.4 2.133333 6.4 6.4 6.4 8.533334z m230.4 0L593.066667 618.666667c-4.266667 12.8-12.8 17.066667-25.6 17.066666s-21.333333-6.4-25.6-17.066666L512 520.533333 482.133333 618.666667c-4.266667 10.666667-12.8 17.066667-25.6 17.066666s-21.333333-6.4-25.6-17.066666l-32-140.8V469.333333c2.133333-10.666667 6.4-17.066667 17.066667-17.066666s19.2 6.4 21.333333 19.2l21.333334 108.8L490.666667 469.333333c4.266667-8.533333 10.666667-14.933333 19.2-14.933333 10.666667 0 17.066667 4.266667 19.2 14.933333l32 110.933334 21.333333-108.8c2.133333-12.8 8.533333-19.2 19.2-19.2s17.066667 6.4 17.066667 17.066666c8.533333 2.133333 8.533333 6.4 6.4 8.533334z m232.533333 0L825.6 618.666667c-4.266667 12.8-12.8 17.066667-25.6 17.066666s-21.333333-6.4-25.6-17.066666l-29.866667-98.133334-29.866666 98.133334c-4.266667 10.666667-12.8 17.066667-25.6 17.066666s-21.333333-6.4-25.6-17.066666l-32-140.8V469.333333c2.133333-10.666667 6.4-17.066667 17.066666-17.066666s19.2 6.4 21.333334 19.2l21.333333 108.8 32-110.933334c4.266667-8.533333 10.666667-14.933333 19.2-14.933333 10.666667 0 17.066667 4.266667 19.2 14.933333l32 110.933334 21.333333-108.8c2.133333-12.8 8.533333-19.2 19.2-19.2s17.066667 6.4 17.066667 17.066666c8.533333 2.133333 6.4 6.4 6.4 8.533334z" />
</svg>
域名管理
</h1>
<span class="domain-count" id="domainCountDisplay">共 <?php echo $domainsCount; ?> 个域名</span>
<div class="domain-search-wrapper">
<svg class="search-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" width="18" height="18">
<circle cx="11" cy="11" r="8"></circle>
<path d="m21 21-4.35-4.35"></path>
</svg>
<input type="text" id="domainSearch" class="domain-search-input" placeholder="搜索域名..." autocomplete="off">
</div>
<div class="header-actions">
<div class="pagination-controls">
<label for="perPageSelect" style="font-size: 13px; color: var(--text-secondary); margin-right: 8px;">每页:</label>
<select id="perPageSelect" class="per-page-select" onchange="changePerPage(this.value)">
<option value="10" <?php echo $perPage == 10 ? 'selected' : ''; ?>>10</option>
<option value="20" <?php echo $perPage == 20 ? 'selected' : ''; ?>>20</option>
<option value="30" <?php echo $perPage == 30 ? 'selected' : ''; ?>>30</option>
<option value="50" <?php echo $perPage == 50 ? 'selected' : ''; ?>>50</option>
</select>
</div>
<button class="btn" onclick="showAddModal()">添加</button>
</div>
</div>
<div class="domains-list" id="domainsList">
<?php foreach ($domains as $domain): ?>
<div class="domain-row" data-id="<?php echo $domain['id']; ?>" data-domain="<?php echo htmlspecialchars(strtolower($domain['domain'])); ?>" data-title="<?php echo htmlspecialchars(strtolower($domain['title'])); ?>">
<div class="domain-row-main">
<div class="domain-row-name">
<span class="domain-name-text">
<?php
$domainName = htmlspecialchars($domain['domain']);
$parts = explode('.', $domainName, 2);
if (count($parts) == 2) {
echo '<span class="domain-name-part">' . $parts[0] . '</span>';
echo '<span class="domain-name-dot">.</span>';
echo '<span class="domain-name-part">' . $parts[1] . '</span>';
} else {
echo '<span class="domain-name-part">' . $domainName . '</span>';
}
?>
</span>
</div>
<div class="domain-row-info">
<span class="row-info-item row-info-title"><?php echo htmlspecialchars($domain['title']); ?></span>
<span class="row-info-item row-info-color">
<span class="color-preview" style="background-color: <?php echo $domain['theme_color']; ?>"></span>
<?php echo $domain['theme_color']; ?>
</span>
<?php if ($domain['domain_intro']): ?>
<span class="row-info-item row-info-intro" title="<?php echo htmlspecialchars($domain['domain_intro']); ?>">
<?php echo mb_strlen($domain['domain_intro']) > 30 ? mb_substr($domain['domain_intro'], 0, 30) . '...' : htmlspecialchars($domain['domain_intro']); ?>
</span>
<?php endif; ?>
<?php if ($domain['domain_price']): ?>
<span class="row-info-item row-info-price">¥<?php echo htmlspecialchars($domain['domain_price']); ?></span>
<?php endif; ?>
</div>
<div class="domain-row-actions">
<button class="btn-icon btn-whois-toggle" type="button" data-domain="<?php echo htmlspecialchars($domain['domain'], ENT_QUOTES, 'UTF-8'); ?>" title="WHOIS 一键查询" aria-expanded="false">
<svg class="whois-icon" viewBox="0 0 1024 1024" aria-hidden="true" focusable="false">
<path d="M707.621926 350.549333l-45.037037 6.637037C636.416 179.465481 567.580444 60.681481 498.839704 60.681481c-68.077037 0-136.343704 116.508444-163.081482 291.802075l-44.980148-6.864593C320.587852 150.243556 399.701333 15.17037 498.839704 15.17037c99.972741 0 179.617185 137.462519 208.782222 335.378963zM290.664296 677.641481l44.999111-6.826666c26.661926 175.653926 95.004444 292.503704 163.176297 292.503704 68.266667 0 136.722963-117.229037 163.271111-293.281186l44.999111 6.788741C677.546667 872.997926 598.224593 1008.82963 498.839704 1008.82963c-99.252148 0-178.460444-135.433481-208.175408-331.188149z" fill="currentColor"></path>
<path d="M512 1008.82963C237.605926 1008.82963 15.17037 786.394074 15.17037 512 15.17037 237.605926 237.605926 15.17037 512 15.17037 786.394074 15.17037 1008.82963 237.605926 1008.82963 512c0 274.394074-222.435556 496.82963-496.82963 496.82963z m0-45.511111c249.249185 0 451.318519-202.069333 451.318519-451.318519S761.249185 60.681481 512 60.681481 60.681481 262.750815 60.681481 512 262.750815 963.318519 512 963.318519z" fill="currentColor"></path>
<path d="M64.265481 376.737185v-45.511111H959.715556v45.511111H64.284444zM959.715556 647.262815v45.511111H64.284444v-45.511111H959.715556z" fill="currentColor"></path>
<path d="M118.139259 429.131852h31.288889l31.744 128.720592h0.948148l33.431704-128.701629h28.672l33.431704 128.701629h0.948148l31.762963-128.701629h31.288889l-48.82963 169.244444h-29.392593l-32.957629-127.29837h-0.948148l-33.185185 127.29837H166.72237L118.120296 429.131852z m241.284741 0h27.742815v70.656h85.807407V429.131852h27.723852v169.244444h-27.723852v-74.903703h-85.807407v74.903703h-27.742815V429.131852z m249.609481-3.299556c25.903407 0 46.288593 8.362667 61.155556 25.125926 14.222222 15.796148 21.333333 36.807111 21.333333 63.051852 0 25.903407-7.111111 46.838519-21.333333 62.805333-14.866963 16.592593-35.252148 24.89837-61.155556 24.898371-25.92237 0-46.307556-8.38163-61.155555-25.125926-14.070519-15.966815-21.105778-36.826074-21.105778-62.577778 0-25.92237 7.035259-46.857481 21.105778-62.824296 14.52563-16.914963 34.910815-25.353481 61.155555-25.353482z m0 24.405334c-17.389037 0-30.985481 5.935407-40.77037 17.787259-9.178074 11.207111-13.748148 26.548148-13.748148 45.985185 0 19.26637 4.570074 34.512593 13.748148 45.738667 9.481481 11.700148 23.058963 17.540741 40.77037 17.54074 17.540741 0 31.04237-5.613037 40.523852-16.820148 9.329778-11.226074 13.994667-26.718815 13.994667-46.459259 0-19.759407-4.664889-35.403852-13.994667-46.933333-9.329778-11.226074-22.831407-16.839111-40.523852-16.839111z m108.562963-21.086815h27.723852v169.244444h-27.723852V429.131852z m120.642371-3.318519c20.081778 0 35.65037 4.096 46.705778 12.325926 11.851852 8.836741 18.640593 22.509037 20.385185 40.997926h-27.496297c-2.37037-10.42963-6.712889-17.938963-13.046518-22.509037-6.011259-4.589037-15.322074-6.883556-27.97037-6.883555-10.752 0-18.887111 1.517037-24.405334 4.513185-6.959407 3.470222-10.42963 9.310815-10.429629 17.54074 0 7.414519 3.944296 13.179259 11.851851 17.294223 3.792593 2.048 13.425778 5.537185 28.918519 10.429629 22.281481 6.788741 37.05363 12.325926 44.316444 16.592593 14.696296 8.685037 22.053926 20.859259 22.053926 36.503704 0 15.17037-5.935407 27.173926-17.787259 36.029629-12.003556 8.685037-28.747852 13.046519-50.251852 13.046519-20.859259 0-37.129481-4.039111-48.829629-12.098371-14.373926-9.955556-22.110815-25.675852-23.22963-47.160888h27.496296c1.896296 12.951704 6.485333 22.110815 13.748148 27.496296 6.807704 4.892444 17.066667 7.338667 30.814815 7.338666 12.325926 0 22.129778-2.048 29.392593-6.162962 7.281778-4.41837 10.903704-10.05037 10.903704-16.820149 0-9.007407-5.290667-16.118519-15.872-21.333333-3.792593-1.896296-14.791111-5.613037-32.95763-11.150222-21.010963-6.637037-33.962667-11.377778-38.874074-14.222222-12.951704-7.736889-19.437037-19.114667-19.437037-34.133334 0-15.17037 6.314667-26.927407 18.962963-35.309037 11.700148-8.229926 26.699852-12.325926 45.037037-12.325926z" fill="currentColor"></path>
</svg>
<span class="whois-btn-spinner" aria-hidden="true"></span>
</button>
<button class="btn-icon" onclick="editDomain(<?php echo $domain['id']; ?>)" title="编辑">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" width="18" height="18">
<path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7" stroke-linejoin="round" stroke-linecap="round"></path>
<path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z" stroke-linejoin="round" stroke-linecap="round"></path>
</svg>
</button>
<button class="btn-icon btn-delete" onclick="deleteDomain(<?php echo $domain['id']; ?>, '<?php echo htmlspecialchars($domain['domain'], ENT_QUOTES); ?>')" title="删除">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" width="18" height="18">
<path d="M3 6h18M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" stroke-linejoin="round" stroke-linecap="round"></path>
<line x1="10" y1="11" x2="10" y2="17" stroke-linejoin="round" stroke-linecap="round"></line>
<line x1="14" y1="11" x2="14" y2="17" stroke-linejoin="round" stroke-linecap="round"></line>
</svg>
</button>
</div>
</div>
</div>
<?php endforeach; ?>
<?php if (empty($domains)): ?>
<div class="empty-state">
<div class="empty-icon">
<svg viewBox="0 0 24 24" fill="currentColor" width="64" height="64">
<path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 4l-8 5-8-5V6l8 5 8-5v2z" />
</svg>
</div>
<p>还没有添加任何域名</p>
<button class="btn btn-primary" onclick="showAddModal()">添加第一个域名</button>
</div>
<?php endif; ?>
<?php if ($domainsCount > 0): ?>
<div class="pagination-wrapper">
<div class="pagination">
<?php if ($currentPage > 1): ?>
<a href="<?php echo htmlspecialchars(adminSectionUrl('domains', ['page' => $currentPage - 1, 'per_page' => $perPage]), ENT_QUOTES, 'UTF-8'); ?>" class="pagination-btn">上一页</a>
<?php else: ?>
<span class="pagination-btn disabled">上一页</span>
<?php endif; ?>
<span class="pagination-info">第 <?php echo $currentPage; ?> / <?php echo $totalPages; ?> 页</span>
<?php if ($currentPage < $totalPages): ?>
<a href="<?php echo htmlspecialchars(adminSectionUrl('domains', ['page' => $currentPage + 1, 'per_page' => $perPage]), ENT_QUOTES, 'UTF-8'); ?>" class="pagination-btn">下一页</a>
<?php else: ?>
<span class="pagination-btn disabled">下一页</span>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
</div>
<?php elseif ($section === 'stats'): ?>
<div class="admin-header">
<h1>
<svg class="stats-icon header-icon" viewBox="0 0 24 24" fill="currentColor" width="28" height="28">
<path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z" />
</svg>
访问统计
</h1>
<div class="domain-search-wrapper">
<svg class="search-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" width="18" height="18">
<circle cx="11" cy="11" r="8"></circle>
<path d="m21 21-4.35-4.35"></path>
</svg>
<input type="text" id="statsDomainSearch" class="domain-search-input" placeholder="搜索域名..." autocomplete="off">
</div>
<div class="header-actions">
<div class="pagination-controls">
<label for="statsPerPageSelect" style="font-size: 13px; color: var(--text-secondary); margin-right: 8px;">每页:</label>
<select id="statsPerPageSelect" class="per-page-select" onchange="changeStatsPerPage(this.value)">
<option value="10" <?php echo $perPage == 10 ? 'selected' : ''; ?>>10</option>
<option value="20" <?php echo $perPage == 20 ? 'selected' : ''; ?>>20</option>
<option value="30" <?php echo $perPage == 30 ? 'selected' : ''; ?>>30</option>
<option value="50" <?php echo $perPage == 50 ? 'selected' : ''; ?>>50</option>
</select>
</div>
<form method="get" class="stats-filter">
<input type="hidden" name="section" value="stats">
<input type="hidden" name="per_page" value="<?php echo $perPage; ?>">
<input type="hidden" name="page" value="1">
<select name="days" onchange="this.form.submit()">
<option value="7" <?php echo $days == 7 ? 'selected' : ''; ?>>最近7天</option>
<option value="30" <?php echo $days == 30 ? 'selected' : ''; ?>>最近30天</option>
<option value="90" <?php echo $days == 90 ? 'selected' : ''; ?>>最近90天</option>
<option value="365" <?php echo $days == 365 ? 'selected' : ''; ?>>最近1年</option>
</select>
</form>
</div>
</div>
<?php if (empty($allStats)): ?>
<div class="empty-state">
<div class="empty-icon">
<svg viewBox="0 0 24 24" fill="currentColor" width="64" height="64">
<path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 4l-8 5-8-5V6l8 5 8-5v2z" />
</svg>
</div>
<p>暂无访问记录</p>
</div>
<?php else: ?>
<div class="stats-list" id="statsList">
<?php foreach ($allStats as $stat): ?>
<div class="stats-row" data-domain="<?php echo htmlspecialchars(strtolower($stat['domain'])); ?>">
<div class="stats-row-main">
<div class="stats-row-domain">
<svg class="domain-icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" width="18" height="18">
<path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64z m193.5 301.7l-210.6 292a31.8 31.8 0 0 1-51.7 0L318.5 365.7c-3.8-5.3 0-12.7 6.5-12.7h46.9c10.3 0 19.9 5 25.9 13.3l71.2 98.8 157.2-218c6-8.4 15.7-13.3 25.9-13.3H699c6.5 0 10.3 7.4 6.5 12.7z" fill="#2867CE"></path>
</svg>
<span class="stats-domain-text"><?php echo htmlspecialchars($stat['domain']); ?></span>
</div>
<div class="stats-row-data">
<span class="stats-data-item">
<span class="stats-data-label">访问量:</span>
<span class="stats-data-value"><?php echo number_format($stat['total_visits']); ?></span>
</span>
<span class="stats-data-item">
<span class="stats-data-label">访客:</span>
<span class="stats-data-value"><?php echo number_format($stat['unique_ips']); ?></span>
</span>
<span class="stats-data-item">
<span class="stats-data-label">最后访问:</span>
<span class="stats-data-value"><?php echo htmlspecialchars($stat['last_visit']); ?></span>
</span>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php if (count($allStatsRaw ?? []) > 0): ?>
<div class="pagination-wrapper">
<div class="pagination">
<?php if ($currentPage > 1): ?>
<a href="<?php echo htmlspecialchars(adminSectionUrl('stats', ['page' => $currentPage - 1, 'per_page' => $perPage, 'days' => $days]), ENT_QUOTES, 'UTF-8'); ?>" class="pagination-btn">上一页</a>
<?php else: ?>
<span class="pagination-btn disabled">上一页</span>
<?php endif; ?>
<span class="pagination-info">第 <?php echo $currentPage; ?> / <?php echo $statsTotalPages; ?> 页</span>
<?php if ($currentPage < $statsTotalPages): ?>
<a href="<?php echo htmlspecialchars(adminSectionUrl('stats', ['page' => $currentPage + 1, 'per_page' => $perPage, 'days' => $days]), ENT_QUOTES, 'UTF-8'); ?>" class="pagination-btn">下一页</a>
<?php else: ?>
<span class="pagination-btn disabled">下一页</span>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php endif; ?>
<?php elseif ($section === 'email'): ?>
<div class="admin-header">
<h1>
<svg class="header-icon" viewBox="0 0 24 24" fill="currentColor" width="28" height="28">
<path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 4l-8 5-8-5V6l8 5 8-5v2z" />
</svg>
邮件设置
</h1>
</div>
<div class="card">
<form id="emailSettingsForm" class="email-settings-form" onsubmit="saveEmailSettings(event)">
<section class="email-settings-section">
<div class="email-settings-section-header">
<h3>收件人设置</h3>
<p>测试邮件默认会使用这个邮箱,可按需手动修改。</p>
</div>
<div class="form-group">
<label for="default_to_email">默认收件人邮箱</label>
<input type="email" id="default_to_email" name="default_to_email" placeholder="admin@example.com">
</div>
</section>
<section class="email-settings-section">
<div class="email-settings-section-header">
<h3>发件人与 SMTP</h3>
<p>用于系统发送询价通知、测试邮件和自动通知。</p>
</div>
<div class="form-row form-row-two">
<div class="form-group">
<label for="from_name">默认发件人名称</label>
<input type="text" id="from_name" name="from_name" placeholder="例如:域名停放系统">
</div>
<div class="form-group">
<label for="from_email">默认发件人邮箱</label>
<input type="email" id="from_email" name="from_email" placeholder="noreply@example.com">
</div>
</div>
<div class="form-row form-row-three">
<div class="form-group">
<label for="smtp_encryption">加密方式</label>
<select id="smtp_encryption" name="smtp_encryption">
<option value="none">不加密</option>
<option value="tls">STARTTLS</option>
<option value="ssl">SSL/TLS</option>
</select>
</div>
<div class="form-group">
<label for="smtp_host">SMTP 服务器</label>
<input type="text" id="smtp_host" name="smtp_host" placeholder="smtp.example.com">
</div>
<div class="form-group">
<label for="smtp_port">端口</label>
<input type="number" id="smtp_port" name="smtp_port" placeholder="587">
</div>
</div>
<div class="form-row form-row-two">
<div class="form-group">
<label for="smtp_username">用户名</label>
<input type="text" id="smtp_username" name="smtp_username" placeholder="user@example.com">
</div>
<div class="form-group">
<label for="smtp_password">密码</label>
<input type="password" id="smtp_password" name="smtp_password" placeholder="••••••••">
</div>
</div>
</section>
<section class="email-settings-section">
<div class="email-settings-section-header">
<h3>测试邮件</h3>
<p>发送前可临时修改收件邮箱,不会覆盖默认配置。</p>
</div>
<div class="form-group">
<label for="test_email_to">测试邮件收件邮箱</label>
<input type="email" id="test_email_to" name="test_email_to" placeholder="test@example.com">
</div>
</section>
<div class="email-settings-actions">
<button type="button" class="btn btn-secondary" id="testEmailBtn" onclick="sendTestEmail()">发送测试邮件</button>
<button type="submit" class="btn btn-primary">保存设置</button>
</div>