更新UI和管理后台

This commit is contained in:
MasonLiu 2026-07-01 00:47:09 +08:00
parent 9949072ae0
commit de78d7de07
9 changed files with 2020 additions and 432 deletions

0
.htaccess Normal file
View File

1371
admin.php

File diff suppressed because it is too large Load Diff

545
assets/css/admin.css Normal file
View File

@ -0,0 +1,545 @@
/* =========================================
SecHub 管理后台样式表
========================================= */
/* CSS 变量 + 暗黑模式变量 */
:root {
--bg: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
--card-bg: rgba(255, 255, 255, 0.92);
--card-border: rgba(255, 255, 255, 0.3);
--text: #2c3e50;
--text-sec: #7f8c8d;
--border: #e0e6ed;
--primary: #2b6cb0;
--primary-h: #1a4f8b;
--primary-light: #ebf4ff;
--success: #27ae60;
--success-bg: #d4edda;
--success-text: #155724;
--danger: #e74c3c;
--danger-bg: #f8d7da;
--danger-text: #721c24;
--warning: #f39c12;
--warning-bg: #fff3cd;
--warning-text: #856404;
--shadow: 0 4px 16px rgba(0,0,0,0.07);
--shadow-lg: 0 20px 60px rgba(0,0,0,0.15);
--radius: 16px;
--radius-sm: 8px;
--transition: 0.25s ease;
}
body.dark-mode {
--bg: linear-gradient(135deg, #1a202c 0%, #2d3748 100%);
--card-bg: rgba(26, 32, 44, 0.92);
--card-border: rgba(255, 255, 255, 0.08);
--text: #e2e8f0;
--text-sec: #a0aec0;
--border: #4a5568;
--primary: #63b3ed;
--primary-h: #4299e1;
--primary-light: #2d3748;
--success-bg: #22543d;
--success-text: #68d391;
--danger-bg: #742a2a;
--danger-text: #fc8181;
--warning-bg: #744210;
--warning-text: #f6e05e;
--shadow: 0 4px 16px rgba(0,0,0,0.3);
--shadow-lg: 0 20px 60px rgba(0,0,0,0.5);
}
/* Reset & Base */
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Microsoft YaHei', sans-serif;
background: var(--bg);
color: var(--text);
line-height: 1.6;
min-height: 100vh;
transition: background var(--transition), color var(--transition);
}
.container { max-width: 1300px; margin: 0 auto; padding: 20px; }
/* ======= Header ======= */
.header {
background: var(--card-bg);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border: 1px solid var(--card-border);
padding: 22px 30px;
border-radius: var(--radius);
box-shadow: var(--shadow);
margin-bottom: 22px;
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 15px;
transition: background var(--transition), box-shadow var(--transition);
}
.header h1 {
font-size: 1.4em;
color: var(--primary);
transition: color var(--transition);
}
.header-sub {
color: var(--text-sec);
font-size: 0.85em;
margin-top: 4px;
}
.header-actions { display: flex; gap: 10px; align-items: center; }
/* ======= Theme Toggle ======= */
.theme-btn {
background: none;
border: 1px solid var(--border);
border-radius: var(--radius-sm);
padding: 8px 14px;
cursor: pointer;
font-size: 1em;
color: var(--text-sec);
transition: all var(--transition);
display: flex;
align-items: center;
gap: 5px;
}
.theme-btn:hover {
background: var(--primary-light);
color: var(--primary);
border-color: var(--primary);
}
/* ======= Buttons ======= */
.btn {
border: none;
cursor: pointer;
font-weight: 500;
transition: all var(--transition);
display: inline-flex;
align-items: center;
gap: 5px;
text-decoration: none;
}
.btn:hover { transform: translateY(-1px); box-shadow: 0 2px 8px rgba(0,0,0,0.12); }
.btn-primary { background: var(--primary); color: white; padding: 8px 18px; border-radius: 6px; }
.btn-primary:hover { background: var(--primary-h); }
.btn-success { background: var(--success); color: white; padding: 8px 18px; border-radius: 6px; }
.btn-success:hover { background: #229954; }
.btn-danger { background: var(--danger); color: white; padding: 8px 18px; border-radius: 6px; }
.btn-danger:hover { background: #c0392b; }
.btn-warning { background: var(--warning); color: white; padding: 8px 18px; border-radius: 6px; }
.btn-warning:hover { background: #d68910; }
.btn-small { padding: 5px 12px; font-size: 0.85em; }
.btn-outline {
background: transparent;
border: 1px solid var(--border);
color: var(--text);
padding: 8px 18px;
border-radius: 6px;
transition: all var(--transition);
}
.btn-outline:hover {
background: var(--card-bg);
border-color: var(--primary);
color: var(--primary);
}
.btn-text { background: none; border: none; cursor: pointer; color: var(--primary); padding: 4px 8px; font-size: 0.9em; }
.btn-text:hover { text-decoration: underline; }
.btn-text.danger { color: var(--danger); }
.btn-lg { font-size: 1.1em; padding: 12px 50px; }
/* ======= Tabs ======= */
.tabs {
background: var(--card-bg);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border: 1px solid var(--card-border);
border-radius: var(--radius) var(--radius) 0 0;
box-shadow: var(--shadow);
display: flex;
border-bottom: 2px solid var(--border);
overflow-x: auto;
transition: background var(--transition);
}
.tab-btn {
padding: 14px 24px;
border: none;
background: none;
cursor: pointer;
font-size: 0.95em;
font-weight: 500;
color: var(--text-sec);
transition: all var(--transition);
white-space: nowrap;
position: relative;
}
.tab-btn:hover { color: var(--text); background: var(--primary-light); }
.tab-btn.active {
color: var(--primary);
background: var(--primary-light);
font-weight: 600;
}
.tab-btn.active::after {
content: '';
position: absolute;
bottom: -2px;
left: 0;
right: 0;
height: 2px;
background: var(--primary);
animation: tabSlideIn 0.2s ease;
}
@keyframes tabSlideIn {
from { transform: scaleX(0); }
to { transform: scaleX(1); }
}
/* Tab Content */
.tab-content { display: none; }
.tab-content.active { display: block; animation: fadeIn 0.2s ease; }
@keyframes fadeIn {
from { opacity: 0; transform: translateY(4px); }
to { opacity: 1; transform: translateY(0); }
}
/* ======= Panel ======= */
.panel {
background: var(--card-bg);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border: 1px solid var(--card-border);
border-radius: 0 0 var(--radius) var(--radius);
box-shadow: var(--shadow);
padding: 25px;
margin-bottom: 22px;
transition: background var(--transition);
}
/* ======= Alert ======= */
.alert {
padding: 14px 20px;
border-radius: var(--radius-sm);
margin-bottom: 20px;
font-weight: 500;
animation: slideDown 0.3s ease;
}
@keyframes slideDown {
from { opacity: 0; transform: translateY(-10px); }
to { opacity: 1; transform: translateY(0); }
}
.alert-success { background: var(--success-bg); color: var(--success-text); border: 1px solid; }
.alert-error { background: var(--danger-bg); color: var(--danger-text); border: 1px solid; }
.alert-warning { background: var(--warning-bg); color: var(--warning-text); border: 1px solid; }
/* ======= Toast ======= */
.toast-container {
position: fixed;
top: 20px;
right: 20px;
z-index: 9999;
display: flex;
flex-direction: column;
gap: 10px;
pointer-events: none;
}
.toast {
pointer-events: auto;
padding: 14px 20px;
border-radius: var(--radius-sm);
font-weight: 500;
font-size: 0.95em;
box-shadow: var(--shadow-lg);
animation: toastIn 0.35s ease;
max-width: 400px;
word-break: break-word;
display: flex;
align-items: center;
gap: 10px;
}
.toast.toast-out { animation: toastOut 0.3s ease forwards; }
@keyframes toastIn {
from { opacity: 0; transform: translateX(50px); }
to { opacity: 1; transform: translateX(0); }
}
@keyframes toastOut {
from { opacity: 1; transform: translateX(0); }
to { opacity: 0; transform: translateX(50px); }
}
.toast-success { background: var(--success-bg); color: var(--success-text); border-left: 4px solid var(--success); }
.toast-error { background: var(--danger-bg); color: var(--danger-text); border-left: 4px solid var(--danger); }
.toast-warning { background: var(--warning-bg); color: var(--warning-text); border-left: 4px solid var(--warning); }
/* ======= Table ======= */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 12px 15px; text-align: left; border-bottom: 1px solid var(--border); transition: background 0.15s; }
th {
background: var(--primary-light);
font-weight: 600;
color: var(--text-sec);
font-size: 0.9em;
transition: background var(--transition), color var(--transition);
}
tr:hover { background: var(--primary-light); }
.draggable-row { cursor: move; }
.draggable-row.dragging { opacity: 0.5; background: rgba(43, 108, 176, 0.1); }
.drag-handle { color: var(--text-sec); cursor: move; font-size: 1.2em; user-select: none; }
.order-num { font-weight: 600; color: var(--primary); }
.mono { font-family: 'Courier New', monospace; font-size: 0.9em; color: var(--text-sec); }
.count-badge {
display: inline-block;
background: var(--primary);
color: white;
padding: 2px 10px;
border-radius: 12px;
font-size: 0.85em;
}
/* ======= Two column layout ======= */
.two-col { display: flex; gap: 22px; align-items: flex-start; }
.two-col .side { flex: 0 0 280px; }
.two-col .main { flex: 1; min-width: 0; }
/* ======= Section list ======= */
.section-list { border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; }
.section-list-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px 16px;
border-bottom: 1px solid var(--border);
cursor: pointer;
transition: background 0.15s;
}
.section-list-item:last-child { border-bottom: none; }
.section-list-item:hover { background: var(--primary-light); }
.section-list-item.active { background: var(--primary-light); border-left: 3px solid var(--primary); }
.section-list-item .sname { font-weight: 500; }
.section-list-item .scount { font-size: 0.85em; color: var(--text-sec); }
.section-list-item .sactions { display: flex; gap: 4px; opacity: 0.3; transition: opacity 0.15s; }
.section-list-item:hover .sactions { opacity: 1; }
/* ======= Modal ======= */
.modal-overlay {
display: none;
position: fixed;
inset: 0;
background: rgba(0,0,0,0.5);
z-index: 1000;
align-items: center;
justify-content: center;
opacity: 0;
transition: opacity 0.2s ease;
}
.modal-overlay.show {
display: flex;
opacity: 1;
}
.modal {
background: var(--card-bg);
backdrop-filter: blur(12px);
border-radius: var(--radius);
padding: 30px;
width: 500px;
max-width: 90vw;
max-height: 85vh;
overflow-y: auto;
box-shadow: var(--shadow-lg);
transform: scale(0.92) translateY(10px);
transition: transform 0.25s ease;
color: var(--text);
}
.modal-overlay.show .modal {
transform: scale(1) translateY(0);
}
.modal h2 { margin-bottom: 20px; font-size: 1.2em; }
.modal .form-group { margin-bottom: 16px; }
.modal .form-group label { display: block; margin-bottom: 6px; font-weight: 500; font-size: 0.9em; color: var(--text-sec); }
.modal .form-group input,
.modal .form-group textarea {
width: 100%;
padding: 10px 14px;
border: 2px solid var(--border);
border-radius: 6px;
font-size: 0.95em;
font-family: inherit;
background: var(--card-bg);
color: var(--text);
transition: border-color 0.2s, background var(--transition), color var(--transition);
}
.modal .form-group input:focus,
.modal .form-group textarea:focus {
border-color: var(--primary);
outline: none;
box-shadow: 0 0 0 3px rgba(43, 108, 176, 0.1);
}
.modal .form-group textarea { min-height: 80px; resize: vertical; }
.modal .modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }
.modal .modal-actions .btn { padding: 10px 24px; }
.modal .required-star { color: var(--danger); }
/* ======= Import ======= */
.import-area textarea {
width: 100%;
min-height: 200px;
padding: 14px;
border: 2px solid var(--border);
border-radius: var(--radius-sm);
font-family: 'Courier New', monospace;
font-size: 0.9em;
resize: vertical;
transition: border-color 0.2s;
background: var(--card-bg);
color: var(--text);
}
.import-area textarea:focus { border-color: var(--primary); outline: none; }
.import-preview { margin-top: 15px; animation: fadeIn 0.2s ease; }
.import-preview table { font-size: 0.9em; }
.import-preview table th { background: var(--primary-light); }
.preview-empty { color: var(--text-sec); text-align: center; padding: 20px; }
/* ======= Settings ======= */
.settings-group {
border: 1px solid var(--border);
border-radius: var(--radius-sm);
padding: 20px;
margin-bottom: 20px;
transition: border-color var(--transition);
}
.settings-group h3 { margin-bottom: 15px; font-size: 1.05em; }
.settings-group .form-group { margin-bottom: 12px; }
.settings-group .form-group label { display: block; margin-bottom: 4px; font-weight: 500; font-size: 0.9em; color: var(--text-sec); }
.settings-group .form-group input {
width: 100%;
max-width: 300px;
padding: 10px 14px;
border: 2px solid var(--border);
border-radius: 6px;
font-size: 0.95em;
background: var(--card-bg);
color: var(--text);
transition: border-color 0.2s, background var(--transition);
}
.settings-group .form-group input:focus { border-color: var(--primary); outline: none; }
.settings-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.settings-info {
color: var(--text-sec);
font-size: 0.9em;
margin-bottom: 12px;
line-height: 1.8;
}
/* ======= Tip ======= */
.tip {
background: var(--primary-light);
border-left: 4px solid var(--primary);
padding: 12px 16px;
border-radius: 4px;
margin-bottom: 16px;
font-size: 0.9em;
}
/* ======= Empty state ======= */
.empty-state { text-align: center; color: var(--text-sec); padding: 40px 20px; }
/* ======= Warning box ======= */
.warning-box {
margin-bottom: 16px;
padding: 12px 16px;
background: var(--warning-bg);
color: var(--warning-text);
border-radius: 6px;
border: 1px solid;
font-size: 0.95em;
line-height: 1.6;
}
/* ======= Action bar ======= */
.action-bar {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 12px;
flex-wrap: wrap;
gap: 10px;
}
.action-bar h3 { font-size: 1em; }
.section-header-row {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
flex-wrap: wrap;
gap: 10px;
}
/* ======= Sort save bar ======= */
.sort-save-bar {
text-align: center;
padding-top: 20px;
border-top: 2px solid var(--border);
margin-top: 20px;
}
/* ======= Separator ======= */
.separator { border: none; border-top: 2px solid var(--border); margin: 20px 0; }
/* ======= Tool list cells ======= */
.cell-url { word-break: break-all; font-size: 0.9em; }
.cell-desc { color: var(--text-sec); font-size: 0.9em; }
.cell-url a { color: var(--primary); text-decoration: none; }
.cell-url a:hover { text-decoration: underline; }
.col-section-title { color: var(--primary); }
/* ======= Form Select ======= */
.form-select {
width: 100%;
max-width: 300px;
padding: 10px;
border: 2px solid var(--border);
border-radius: 6px;
font-size: 0.95em;
background: var(--card-bg);
color: var(--text);
transition: border-color 0.2s, background var(--transition), color var(--transition);
}
.form-select:focus { border-color: var(--primary); outline: none; }
.form-select-sm {
padding: 8px;
border: 2px solid var(--border);
border-radius: 6px;
font-size: 0.9em;
background: var(--card-bg);
color: var(--text);
}
/* ======= Export box ======= */
.export-box {
border: 1px solid var(--border);
padding: 16px;
border-radius: var(--radius-sm);
transition: border-color var(--transition);
}
/* ======= Footer ======= */
.admin-footer {
text-align: center;
padding: 20px;
color: var(--text-sec);
font-size: 0.85em;
}
/* ======= Responsive ======= */
@media (max-width: 768px) {
.container { padding: 10px; }
.header { flex-direction: column; text-align: center; }
.two-col { flex-direction: column; }
.two-col .side { flex: 0 0 auto; }
.tab-btn { padding: 10px 14px; font-size: 0.85em; }
.panel { padding: 15px; }
.modal { padding: 20px; }
}

Binary file not shown.

View File

@ -27,5 +27,10 @@
"name": "蓝队工具箱", "name": "蓝队工具箱",
"url": "http://github.com/abc123info/BlueTeamTools", "url": "http://github.com/abc123info/BlueTeamTools",
"description": "蓝队分析研判工具箱(小工具集)" "description": "蓝队分析研判工具箱(小工具集)"
},
{
"name": "国密加解密工具",
"url": "https://github.com/milu001/sm234_decrypt_gui",
"description": "国密可视化加解密工具"
} }
] ]

View File

@ -42,5 +42,10 @@
"name": "LinkFinder", "name": "LinkFinder",
"url": "https://github.com/GerbenJavado/LinkFinder", "url": "https://github.com/GerbenJavado/LinkFinder",
"description": "提取网站文件中的路径信息" "description": "提取网站文件中的路径信息"
},
{
"name": "FOFA Viewer",
"url": "https://github.com/wgpsec/fofa_viewer",
"description": "FOFA快速信息收集可视化工具"
} }
] ]

12
config.php Normal file
View File

@ -0,0 +1,12 @@
<?php
/**
* SecHub 全局配置文件
*/
// 管理后台访问密码(请修改为你自己的密码)
define('ADMIN_PASSWORD', 'sechub2024');
// 路径配置(通常无需修改)
define('JSON_DIR', __DIR__ . '/assets/json/');
define('DB_DIR', __DIR__ . '/assets/db/');
define('DB_PATH', DB_DIR . 'sechub.db');

398
db.php
View File

@ -384,6 +384,9 @@ class SecHubDatabase {
* 获取指定栏目的所有项目 * 获取指定栏目的所有项目
*/ */
public function getItemsBySection($tableName) { public function getItemsBySection($tableName) {
if (!$this->validateTableName($tableName)) {
return [];
}
$tableName = preg_replace('/[^a-zA-Z0-9_]/', '_', $tableName); $tableName = preg_replace('/[^a-zA-Z0-9_]/', '_', $tableName);
$sql = "SELECT * FROM {$tableName} ORDER BY name"; $sql = "SELECT * FROM {$tableName} ORDER BY name";
$stmt = $this->db->query($sql); $stmt = $this->db->query($sql);
@ -409,4 +412,399 @@ class SecHubDatabase {
public function close() { public function close() {
$this->db = null; $this->db = null;
} }
// ======================== 表名安全验证 ========================
/**
* 验证表名是否存在于数据库中防SQL注入
*/
private function validateTableName($tableName) {
$tables = $this->getAllTables();
$validNames = array_column($tables, 'name');
return in_array($tableName, $validNames, true);
}
// ======================== JSON 文件操作 ========================
/**
* 读取JSON文件并解析
* @return array|null
*/
private function readJsonArray($filePath) {
if (!file_exists($filePath)) return null;
$content = file_get_contents($filePath);
if ($content === false) return null;
$data = json_decode($content, true);
if (json_last_error() !== JSON_ERROR_NONE) return null;
return is_array($data) ? $data : null;
}
/**
* 写入JSON文件保持格式
*/
private function writeJsonArray($filePath, $data) {
$json = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
return file_put_contents($filePath, $json . "\n") !== false;
}
/**
* 从表名获取JSON文件路径
*/
private function getJsonFilePath($tableName) {
return $this->jsonDir . $tableName . '.json';
}
/**
* 获取所有栏目带详细信息
* @return array 每个元素包含 name, title, no, item_count, filename
*/
public function getAllSectionsWithInfo() {
$tables = $this->getAllTables();
$sections = [];
foreach ($tables as $table) {
$info = $this->getSectionInfo($table['name']);
if ($info) {
$sections[] = $info;
}
}
// 按 no 排序
usort($sections, function($a, $b) {
return ($a['no'] ?? 0) - ($b['no'] ?? 0);
});
return $sections;
}
/**
* 获取单个栏目配置信息
* @return array|null
*/
public function getSectionInfo($tableName) {
if (!$this->validateTableName($tableName)) return null;
$filePath = $this->getJsonFilePath($tableName);
$data = $this->readJsonArray($filePath);
if (!$data || empty($data)) return null;
$meta = $data[0];
return [
'name' => $tableName,
'title' => $meta['section'] ?? $tableName,
'no' => $meta['no'] ?? 0,
'item_count' => count($data) - 1,
'filename' => $tableName . '.json'
];
}
// ======================== 工具条目 CRUD ========================
/**
* 在栏目中添加工具条目
*/
public function addItemToSection($tableName, $name, $url, $description) {
if (!$this->validateTableName($tableName)) {
return ['success' => false, 'error' => '无效的栏目'];
}
$filePath = $this->getJsonFilePath($tableName);
$data = $this->readJsonArray($filePath);
if (!$data) {
return ['success' => false, 'error' => '读取JSON文件失败'];
}
$data[] = [
'name' => $name,
'url' => $url,
'description' => $description
];
if (!$this->writeJsonArray($filePath, $data)) {
return ['success' => false, 'error' => '写入JSON文件失败'];
}
$this->syncSingleFile($filePath);
return ['success' => true];
}
/**
* 更新栏目中的工具条目
* @param string $tableName 表名
* @param int $itemIndex 工具在JSON中的索引0表示第一个工具项
*/
public function updateItemInSection($tableName, $itemIndex, $name, $url, $description) {
if (!$this->validateTableName($tableName)) {
return ['success' => false, 'error' => '无效的栏目'];
}
$filePath = $this->getJsonFilePath($tableName);
$data = $this->readJsonArray($filePath);
if (!$data) {
return ['success' => false, 'error' => '读取JSON文件失败'];
}
// itemIndex 从 0 开始,对应 JSON 数组中的 data[1], data[2], ...
$arrayIndex = $itemIndex + 1;
if (!isset($data[$arrayIndex])) {
return ['success' => false, 'error' => '工具条目不存在'];
}
$data[$arrayIndex]['name'] = $name;
$data[$arrayIndex]['url'] = $url;
$data[$arrayIndex]['description'] = $description;
if (!$this->writeJsonArray($filePath, $data)) {
return ['success' => false, 'error' => '写入JSON文件失败'];
}
$this->syncSingleFile($filePath);
return ['success' => true];
}
/**
* 删除栏目中的工具条目
*/
public function deleteItemFromSection($tableName, $itemIndex) {
if (!$this->validateTableName($tableName)) {
return ['success' => false, 'error' => '无效的栏目'];
}
$filePath = $this->getJsonFilePath($tableName);
$data = $this->readJsonArray($filePath);
if (!$data) {
return ['success' => false, 'error' => '读取JSON文件失败'];
}
$arrayIndex = $itemIndex + 1;
if (!isset($data[$arrayIndex])) {
return ['success' => false, 'error' => '工具条目不存在'];
}
array_splice($data, $arrayIndex, 1);
if (!$this->writeJsonArray($filePath, $data)) {
return ['success' => false, 'error' => '写入JSON文件失败'];
}
$this->syncSingleFile($filePath);
return ['success' => true];
}
/**
* 批量添加工具条目
* @param string $tableName 表名
* @param array $items 条目数组,每个元素包含 [name, url, description]
*/
public function batchAddItems($tableName, $items) {
if (!$this->validateTableName($tableName)) {
return ['success' => false, 'error' => '无效的栏目', 'count' => 0, 'total' => count($items)];
}
$filePath = $this->getJsonFilePath($tableName);
$data = $this->readJsonArray($filePath);
if (!$data) {
return ['success' => false, 'error' => '读取JSON文件失败', 'count' => 0, 'total' => count($items)];
}
$added = 0;
foreach ($items as $item) {
$name = trim($item[0] ?? '');
$url = trim($item[1] ?? '');
$description = trim($item[2] ?? '');
if (empty($name) && empty($url)) continue;
$data[] = [
'name' => $name,
'url' => $url,
'description' => $description
];
$added++;
}
if ($added === 0) {
return ['success' => false, 'error' => '没有有效的条目可添加', 'count' => 0, 'total' => count($items)];
}
if (!$this->writeJsonArray($filePath, $data)) {
return ['success' => false, 'error' => '写入JSON文件失败', 'count' => $added, 'total' => count($items)];
}
$this->syncSingleFile($filePath);
return ['success' => true, 'count' => $added, 'total' => count($items)];
}
// ======================== 栏目管理 ========================
/**
* 创建新栏目
* @param string $sectionName 栏目显示名称
* @return array
*/
public function createSection($sectionName) {
if (empty(trim($sectionName))) {
return ['success' => false, 'error' => '栏目名称不能为空'];
}
$tableName = $this->generateTableName($sectionName);
// 检查JSON文件是否已存在
if (file_exists($this->getJsonFilePath($tableName))) {
return ['success' => false, 'error' => '栏目文件已存在: ' . $tableName . '.json'];
}
// 获取最大排序号
$maxNo = $this->getMaxSectionNo();
// 创建JSON数据
$data = [
['no' => $maxNo + 1, 'section' => $sectionName]
];
if (!$this->writeJsonArray($this->getJsonFilePath($tableName), $data)) {
return ['success' => false, 'error' => '创建JSON文件失败'];
}
// 同步到数据库
try {
$this->syncSingleFile($this->getJsonFilePath($tableName));
} catch (Exception $e) {
// 清理已创建的文件
unlink($this->getJsonFilePath($tableName));
return ['success' => false, 'error' => '数据库同步失败: ' . $e->getMessage()];
}
return ['success' => true, 'table_name' => $tableName];
}
/**
* 删除栏目删除JSON文件 + 数据库表 + 同步日志)
*/
public function removeSection($tableName) {
if (!$this->validateTableName($tableName)) {
return ['success' => false, 'error' => '栏目不存在'];
}
// 删除JSON文件
$filePath = $this->getJsonFilePath($tableName);
if (file_exists($filePath)) {
if (!unlink($filePath)) {
return ['success' => false, 'error' => '删除JSON文件失败'];
}
}
// 删除数据库表
$safeName = preg_replace('/[^a-zA-Z0-9_]/', '_', $tableName);
$this->db->exec("DROP TABLE IF EXISTS {$safeName}");
// 删除同步日志
$stmt = $this->db->prepare("DELETE FROM json_sync_log WHERE table_name = :tn");
$stmt->execute([':tn' => $tableName]);
return ['success' => true];
}
/**
* 重命名栏目显示名称
*/
public function renameSection($tableName, $newSectionName) {
if (empty(trim($newSectionName))) {
return ['success' => false, 'error' => '栏目名称不能为空'];
}
if (!$this->validateTableName($tableName)) {
return ['success' => false, 'error' => '栏目不存在'];
}
$filePath = $this->getJsonFilePath($tableName);
$data = $this->readJsonArray($filePath);
if (!$data) {
return ['success' => false, 'error' => '读取JSON文件失败'];
}
// 更新元数据中的section名称
$data[0]['section'] = $newSectionName;
if (!$this->writeJsonArray($filePath, $data)) {
return ['success' => false, 'error' => '写入JSON文件失败'];
}
$this->syncSingleFile($filePath);
return ['success' => true];
}
/**
* 获取全部栏目数据用于导出
* @return array
*/
public function exportAllSections() {
$tables = $this->getAllTables();
$export = [];
foreach ($tables as $table) {
$filePath = $this->getJsonFilePath($table['name']);
$data = $this->readJsonArray($filePath);
if ($data) {
$export[$table['name']] = $data;
}
}
return $export;
}
/**
* 获取栏目CSV导出内容
*/
public function exportSectionToCsv($tableName) {
if (!$this->validateTableName($tableName)) return '';
$items = $this->getItemsBySection($tableName);
$info = $this->getSectionInfo($tableName);
$csv = "名称,URL,描述\r\n";
foreach ($items as $item) {
$name = str_replace('"', '""', $item['name'] ?? '');
$url = str_replace('"', '""', $item['url'] ?? '');
$desc = str_replace('"', '""', $item['description'] ?? '');
$csv .= "\"{$name}\",\"{$url}\",\"{$desc}\"\r\n";
}
return $csv;
}
// ======================== 辅助方法 ========================
/**
* 获取最大排序号
*/
private function getMaxSectionNo() {
$sql = "SELECT MAX(section_no) as max_no FROM json_sync_log";
$stmt = $this->db->query($sql);
$row = $stmt->fetch();
return $row['max_no'] ?? 0;
}
/**
* 从栏目名称生成表名/文件名
*/
private function generateTableName($name) {
// 先做基本清理
$name = preg_replace('/[^a-zA-Z0-9_\x{4e00}-\x{9fff}]/u', '_', $name);
$name = preg_replace('/_+/', '_', $name);
$name = trim($name, '_');
if (empty($name)) $name = 'section';
// 如果包含中文,提取英文部分+hash
if (preg_match('/[\x{4e00}-\x{9fff}]/u', $name)) {
$eng = preg_replace('/[\x{4e00}-\x{9fff}]/u', '', $name);
$eng = trim(preg_replace('/_+/', '_', $eng), '_');
if (empty($eng)) $eng = 'section';
$hash = substr(md5($name), 0, 4);
$name = $eng . '_' . $hash;
}
return strtolower($name);
}
} }

0
nginx.htaccess Normal file
View File