更新UI和管理后台

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

No files matched your search

+398
View File
@@ -384,6 +384,9 @@ class SecHubDatabase {
* 获取指定栏目的所有项目
*/
public function getItemsBySection($tableName) {
if (!$this->validateTableName($tableName)) {
return [];
}
$tableName = preg_replace('/[^a-zA-Z0-9_]/', '_', $tableName);
$sql = "SELECT * FROM {$tableName} ORDER BY name";
$stmt = $this->db->query($sql);
@@ -409,4 +412,399 @@ class SecHubDatabase {
public function close() {
$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);
}
}