V1.0正式版
This commit is contained in:
1 parent
d2fc7a83a8
commit
529ad70e11
13 files changed
+1052
-237
No files matched your search
@@ -3,63 +3,18 @@
|
||||
* SecHub - 网络安全工具导航页
|
||||
*/
|
||||
|
||||
// 定义JSON文件路径
|
||||
// 定义路径
|
||||
$jsonDir = __DIR__ . '/assets/json/';
|
||||
$dbDir = __DIR__ . '/assets/db/';
|
||||
$dbPath = $dbDir . 'sechub.db';
|
||||
|
||||
/**
|
||||
* 读取JSON文件并返回数据
|
||||
* @param string $filePath JSON文件路径
|
||||
* @return array 解析后的数据数组
|
||||
*/
|
||||
function loadJsonData($filePath) {
|
||||
if (!file_exists($filePath)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$content = file_get_contents($filePath);
|
||||
if ($content === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$data = json_decode($content, true);
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
error_log("JSON解析错误: " . json_last_error_msg());
|
||||
return [];
|
||||
}
|
||||
|
||||
return is_array($data) ? $data : [];
|
||||
// 确保数据库目录存在
|
||||
if (!is_dir($dbDir)) {
|
||||
mkdir($dbDir, 0755, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有JSON文件并自动识别栏目配置
|
||||
* @param string $jsonDir JSON目录路径
|
||||
* @return array 栏目配置数组
|
||||
*/
|
||||
function getSectionsConfig($jsonDir) {
|
||||
$sections = [];
|
||||
$jsonFiles = glob($jsonDir . '*.json');
|
||||
|
||||
foreach ($jsonFiles as $file) {
|
||||
$filename = basename($file);
|
||||
$data = loadJsonData($file);
|
||||
|
||||
if (!empty($data) && isset($data[0]['section'])) {
|
||||
$sections[$filename] = [
|
||||
'title' => $data[0]['section'],
|
||||
'file' => $filename,
|
||||
'items' => array_slice($data, 1)
|
||||
];
|
||||
} else {
|
||||
$sections[$filename] = [
|
||||
'title' => pathinfo($filename, PATHINFO_FILENAME),
|
||||
'file' => $filename,
|
||||
'items' => $data
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $sections;
|
||||
}
|
||||
// 引入数据库类
|
||||
require_once __DIR__ . '/db.php';
|
||||
|
||||
/**
|
||||
* 渲染卡片HTML
|
||||
@@ -81,8 +36,17 @@ function renderCard($item) {
|
||||
</div>";
|
||||
}
|
||||
|
||||
// 自动获取栏目配置
|
||||
$sections = getSectionsConfig($jsonDir);
|
||||
// 初始化数据库并同步数据
|
||||
try {
|
||||
$database = new SecHubDatabase($dbPath, $jsonDir);
|
||||
$database->syncJsonToDatabase();
|
||||
|
||||
// 获取栏目配置
|
||||
$sections = $database->getSectionsConfig();
|
||||
} catch (Exception $e) {
|
||||
error_log("数据库初始化失败: " . $e->getMessage());
|
||||
$sections = [];
|
||||
}
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
@@ -90,6 +54,7 @@ $sections = getSectionsConfig($jsonDir);
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="shortcut icon" href="./assets/imgs/favicon.ico" type="image/x-icon">
|
||||
<title>SecHub - 网络安全工具集</title>
|
||||
<link rel="stylesheet" href="assets/css/style.css">
|
||||
</head>
|
||||
@@ -98,28 +63,57 @@ $sections = getSectionsConfig($jsonDir);
|
||||
<header>
|
||||
<h1>SecHub 网安工具集</h1>
|
||||
<p>一站式网络安全工具与资源导航平台</p>
|
||||
|
||||
<!-- 全局搜索框 -->
|
||||
<div class="search-container">
|
||||
<input type="text"
|
||||
id="global-search"
|
||||
class="search-input"
|
||||
placeholder="🔍 搜索工具名称、描述或链接..."
|
||||
autocomplete="off">
|
||||
<div id="search-results" class="search-results"></div>
|
||||
</div>
|
||||
|
||||
<button id="theme-toggle" class="theme-toggle" title="点击切换白天/黑夜模式">
|
||||
<span class="theme-icon">🌙</span>
|
||||
<span class="theme-tooltip">切换主题</span>
|
||||
</button>
|
||||
</header>
|
||||
|
||||
<?php foreach ($sections as $key => $config): ?>
|
||||
<?php foreach ($sections as $key => $config): ?>
|
||||
<?php
|
||||
$items = $config['items'] ?? [];
|
||||
$items = $database->getItemsBySection($key);
|
||||
// 获取对应的JSON文件名
|
||||
$jsonFile = $key . '.json';
|
||||
?>
|
||||
|
||||
<section class="section">
|
||||
<h2 class="section-title">
|
||||
<?= htmlspecialchars($config['title']) ?>
|
||||
</h2>
|
||||
<section class="section" data-section="<?= htmlspecialchars($key) ?>">
|
||||
<div class="section-header">
|
||||
<div class="section-title-wrapper">
|
||||
<h2 class="section-title">
|
||||
<?= htmlspecialchars($config['title']) ?>
|
||||
</h2>
|
||||
<!-- 下载按钮 -->
|
||||
<button class="download-btn"
|
||||
onclick="downloadJson('<?= htmlspecialchars($jsonFile) ?>')"
|
||||
title="下载JSON文件">
|
||||
📥 下载数据源
|
||||
</button>
|
||||
</div>
|
||||
<!-- 单项搜索框 -->
|
||||
<input type="text"
|
||||
class="section-search"
|
||||
data-section="<?= htmlspecialchars($key) ?>"
|
||||
placeholder="🔍 在此栏目中搜索..."
|
||||
autocomplete="off">
|
||||
</div>
|
||||
|
||||
<?php if (empty($items)): ?>
|
||||
<div class="empty-state">
|
||||
<p>暂无数据,请在 <?= htmlspecialchars($config['file']) ?> 中添加项目</p>
|
||||
<p>暂无数据</p>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div class="cards-grid">
|
||||
<div class="cards-grid" data-section-items="<?= htmlspecialchars($key) ?>">
|
||||
<?php foreach ($items as $item): ?>
|
||||
<?= renderCard($item) ?>
|
||||
<?php endforeach; ?>
|
||||
@@ -145,6 +139,152 @@ $sections = getSectionsConfig($jsonDir);
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// HTML转义函数
|
||||
function escapeHtml(text) {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
// 渲染卡片函数(用于搜索结果)
|
||||
function renderCardJS(item) {
|
||||
const name = item.name || '未命名';
|
||||
const url = item.url || '#';
|
||||
const description = item.description || '暂无简介';
|
||||
|
||||
return `
|
||||
<div class="card" onclick="window.open('${url}', '_blank')">
|
||||
<div class="card-header">
|
||||
<h3 class="card-title">${escapeHtml(name)}</h3>
|
||||
<a href="${url}" target="_blank" class="card-link" onclick="event.stopPropagation()">${escapeHtml(url)}</a>
|
||||
</div>
|
||||
<p class="card-description">${escapeHtml(description)}</p>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
// 全局搜索功能
|
||||
const globalSearchInput = document.getElementById('global-search');
|
||||
const searchResults = document.getElementById('search-results');
|
||||
let searchTimeout;
|
||||
|
||||
globalSearchInput.addEventListener('input', function() {
|
||||
clearTimeout(searchTimeout);
|
||||
const keyword = this.value.trim();
|
||||
|
||||
if (keyword.length === 0) {
|
||||
searchResults.style.display = 'none';
|
||||
showAllSections();
|
||||
return;
|
||||
}
|
||||
|
||||
searchTimeout = setTimeout(() => {
|
||||
performGlobalSearch(keyword);
|
||||
}, 300);
|
||||
});
|
||||
|
||||
function performGlobalSearch(keyword) {
|
||||
fetch(`search.php?action=global&keyword=${encodeURIComponent(keyword)}`)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
displaySearchResults(data);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('搜索失败:', error);
|
||||
});
|
||||
}
|
||||
|
||||
function displaySearchResults(results) {
|
||||
searchResults.innerHTML = '';
|
||||
|
||||
if (Object.keys(results).length === 0) {
|
||||
searchResults.innerHTML = '<div class="no-results">未找到相关结果</div>';
|
||||
searchResults.style.display = 'block';
|
||||
return;
|
||||
}
|
||||
|
||||
Object.keys(results).forEach(tableName => {
|
||||
const section = results[tableName];
|
||||
const sectionDiv = document.createElement('div');
|
||||
sectionDiv.className = 'search-result-section';
|
||||
|
||||
const title = document.createElement('h3');
|
||||
title.className = 'search-result-title';
|
||||
title.textContent = section.section;
|
||||
sectionDiv.appendChild(title);
|
||||
|
||||
const cardsGrid = document.createElement('div');
|
||||
cardsGrid.className = 'cards-grid';
|
||||
|
||||
section.items.forEach(item => {
|
||||
const cardDiv = document.createElement('div');
|
||||
cardDiv.innerHTML = renderCardJS(item);
|
||||
cardsGrid.appendChild(cardDiv.firstElementChild);
|
||||
});
|
||||
|
||||
sectionDiv.appendChild(cardsGrid);
|
||||
searchResults.appendChild(sectionDiv);
|
||||
});
|
||||
|
||||
searchResults.style.display = 'block';
|
||||
}
|
||||
|
||||
function showAllSections() {
|
||||
// 不需要做任何事,因为栏目一直显示
|
||||
}
|
||||
|
||||
function hideAllSections() {
|
||||
// 不需要隐藏栏目
|
||||
}
|
||||
|
||||
// 下载JSON文件
|
||||
function downloadJson(filename) {
|
||||
const url = `assets/json/${filename}`;
|
||||
const link = document.createElement('a');
|
||||
link.href = url;
|
||||
link.download = filename;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
}
|
||||
|
||||
// 单项搜索功能
|
||||
document.querySelectorAll('.section-search').forEach(input => {
|
||||
input.addEventListener('input', function() {
|
||||
const section = this.dataset.section;
|
||||
const keyword = this.value.trim();
|
||||
const cardsGrid = document.querySelector(`[data-section-items="${section}"]`);
|
||||
|
||||
if (!cardsGrid) return;
|
||||
|
||||
const cards = cardsGrid.querySelectorAll('.card');
|
||||
|
||||
cards.forEach(card => {
|
||||
const title = card.querySelector('.card-title').textContent.toLowerCase();
|
||||
const description = card.querySelector('.card-description').textContent.toLowerCase();
|
||||
const link = card.querySelector('.card-link').textContent.toLowerCase();
|
||||
|
||||
if (keyword === '' ||
|
||||
title.includes(keyword.toLowerCase()) ||
|
||||
description.includes(keyword.toLowerCase()) ||
|
||||
link.includes(keyword.toLowerCase())) {
|
||||
card.style.display = 'block';
|
||||
} else {
|
||||
card.style.display = 'none';
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// 点击其他地方关闭搜索结果
|
||||
document.addEventListener('click', function(e) {
|
||||
if (!e.target.closest('.search-container')) {
|
||||
searchResults.style.display = 'none';
|
||||
if (globalSearchInput.value.trim() === '') {
|
||||
showAllSections();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 黑夜模式切换功能
|
||||
(function() {
|
||||
const themeToggle = document.getElementById('theme-toggle');
|
||||
@@ -194,4 +334,4 @@ $sections = getSectionsConfig($jsonDir);
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
Reference in new issue
Block a user