feat: 添加历史公告页面和文件大小格式化功能

- 新增历史公告页面,显示所有公告并按发布时间倒序排列
- 在公告页面添加查看历史公告的链接
- 添加文件大小格式化函数,改进下载文件大小显示
- 修复文件路径处理逻辑,增强路径解析的可靠性
- 更新配置文件中的敏感信息
This commit is contained in:
2025-07-15 17:29:50 +08:00
parent 8bde83a8d3
commit 6fc5673e9a
3 changed files with 210 additions and 1 deletions

148
history_announcements.php Normal file
View File

@@ -0,0 +1,148 @@
<?php
// 包含配置文件
require_once 'config.php';
// 使用config.php中的数据库连接
if (!$conn) {
echo '<script>swal("错误", "数据库连接失败", "error");</script>';
exit;
}
// 查询所有公告,按发布时间倒序
$sql = "SELECT id, title, content, created_at FROM announcements ORDER BY created_at DESC";
$result = $conn->query($sql);
?>
<?php
session_start();
require_once 'config.php';
if (!isset($conn) || !$conn instanceof mysqli) {
die('数据库连接失败,请检查配置文件。');
}?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>历史公告 - <?php echo APP_STORE_NAME; ?></title>
<!-- Bootstrap CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- 自定义CSS -->
<link rel="stylesheet" href="styles.css">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<!-- Fluent Design 模糊效果 -->
<style>
.blur-bg {
backdrop-filter: blur(10px);
background-color: rgba(255, 255, 255, 0.5);
}
.page-transition {
animation: fadeIn 0.5s ease-in-out;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
</style>
<style>
.announcement-item {
margin-bottom: 2rem;
padding: 1rem;
border: 1px solid #e9ecef;
border-radius: 0.3rem;
}
.announcement-item h3 {
margin-bottom: 0.5rem;
color: #343a40;
}
.date {
color: #6c757d;
font-size: 0.9rem;
margin-bottom: 1rem;
}
.content {
color: #495057;
line-height: 1.6;
}
</style>
</head>
<body class="page-transition">
<!-- 导航栏 -->
<nav class="navbar navbar-expand-lg navbar-light blur-bg">
<div class="container">
<a href="index.php"><img src="/favicon.jpeg" alt="Logo" style="height: 30px; margin-right: 10px; border-radius: var(--border-radius);"></a>
<a class="navbar-brand" href="#"><?php echo APP_STORE_NAME; ?></a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="index.php">首页</a>
</li>
<?php if (isset($_SESSION['admin'])): ?>
<li class="nav-item">
<a class="nav-link" href="/admin/">管理</a>
</li>
<?php endif; ?>
<li class="nav-item">
<a class="nav-link" href="tags.php">标签</a>
</li>
<?php if (isset($_SESSION['developer_id'])): ?>
<li class="nav-item">
<a class="nav-link" href="developer/dashboard.php">进入面板</a>
</li>
<?php else: ?>
<li class="nav-item">
<a class="nav-link" href="developer/register.php">开发者注册</a>
</li>
<li class="nav-item">
<a class="nav-link" href="developer/login.php">开发者登录</a>
</li>
<?php endif; ?>
<li class="nav-item">
<a class="nav-link" href="http://leonmmcoset.jjmm.ink:3232/app.php?id=36">下载LeonAPP手机版</a>
</li>
<li class="nav-item">
<a class="nav-link" href="http://leonmmcoset.jjmm.ink:3232/app.php?id=41">下载LeonAPP电脑版</a>
</li>
<li class="nav-item">
<a class="nav-link" href="thanks.php">鸣谢</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-5">
<h1 class="mb-4">历史公告</h1>
<?php if ($result && $result->num_rows > 0): ?>
<div class="announcements-list">
<?php while($announcement = $result->fetch_assoc()): ?>
<div class="announcement-item">
<h3><?php echo htmlspecialchars($announcement['title'], ENT_QUOTES); ?></h3>
<p class="date"><?php echo htmlspecialchars($announcement['created_at'], ENT_QUOTES); ?></p>
<div class="content"><?php echo nl2br(htmlspecialchars($announcement['content'], ENT_QUOTES)); ?></div>
</div>
<?php endwhile; ?>
</div>
<?php else: ?>
<div class="alert alert-info">
暂无历史公告记录。
</div>
<?php endif; ?>
</div>
<script src="js/bootstrap.bundle.js"></script>
</body>
</html>

View File

@@ -101,6 +101,7 @@ $announcement = $announcementResult && $announcementResult->num_rows > 0 ? $anno
<div class="alert alert-info blur-bg"> <div class="alert alert-info blur-bg">
<h4 class="alert-heading"><?php echo htmlspecialchars($announcement['title']); ?></h4> <h4 class="alert-heading"><?php echo htmlspecialchars($announcement['title']); ?></h4>
<p><?php echo nl2br(htmlspecialchars($announcement['content'])); ?></p> <p><?php echo nl2br(htmlspecialchars($announcement['content'])); ?></p>
<p class="mt-2"><a href="history_announcements.php" class="alert-link">查看所有历史公告</a></p>
</div> </div>
</div> </div>
<?php endif; ?> <?php endif; ?>

View File

@@ -1,6 +1,23 @@
<?php <?php
require_once 'config.php'; require_once 'config.php';
// 格式化文件大小函数
function formatFileSize($bytes) {
if ($bytes === false) return '未知';
if ($bytes >= 1073741824) {
return number_format($bytes / 1073741824, 2) . ' GB';
} elseif ($bytes >= 1048576) {
return number_format($bytes / 1048576, 2) . ' MB';
} elseif ($bytes >= 1024) {
return number_format($bytes / 1024, 2) . ' KB';
} elseif ($bytes > 0) {
return $bytes . ' B';
} else {
return '0 B';
}
}
// 验证App ID // 验证App ID
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) { if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
header('Location: index.php?error=无效的App ID'); header('Location: index.php?error=无效的App ID');
@@ -125,7 +142,50 @@ while ($row = $result->fetch_assoc()) {
<button class="btn btn-outline-secondary mt-2" onclick="toggleFavorite(<?php echo $appId; ?>, '<?php echo addslashes(htmlspecialchars($app['name'])); ?>')">收藏</button> <button class="btn btn-outline-secondary mt-2" onclick="toggleFavorite(<?php echo $appId; ?>, '<?php echo addslashes(htmlspecialchars($app['name'])); ?>')">收藏</button>
</div> </div>
<div class="card-footer bg-transparent d-flex justify-content-between align-items-center"> <div class="card-footer bg-transparent d-flex justify-content-between align-items-center">
<a href="<?php echo htmlspecialchars($version['file_path']); ?>" class="btn btn-primary" download>下载(大小:<?php echo $fileSize; ?>)</a> <?php
// 处理绝对路径和相对路径
// 统一路径格式为正斜杠并处理转义问题
// 使用系统目录分隔符标准化路径
// 修剪路径并标准化分隔符
$filePath = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, trim($version['file_path']));
// 增强正则表达式以识别各种Windows绝对路径格式
// 识别带盘符和不带盘符的绝对路径格式
// 仅识别带盘符的Windows绝对路径格式
// 使用更可靠的绝对路径检测方法
// 仅将带盘符的路径视为绝对路径
// 识别带盘符或以目录分隔符开头的绝对路径
// 修正绝对路径识别,仅匹配带盘符和分隔符的格式
if (!preg_match('/^[A-Za-z]:[\\/]/i', $filePath) && strpos($filePath, DIRECTORY_SEPARATOR) !== 0) {
// 非绝对路径则拼接文档根目录
// 使用当前文件位置计算应用根目录,增强错误处理
$currentDir = realpath(dirname(__FILE__));
error_log('Version ID: ' . $version['id'] . ' - Current directory: ' . ($currentDir ?: 'Invalid path'));
// 根据实际应用结构调整目录层级 (向上两级)
$basePath = $currentDir ? realpath($currentDir . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..') : false;
error_log('Version ID: ' . $version['id'] . ' - Calculated base path: ' . ($basePath ?: 'Failed to resolve'));
// 确保基础路径有效
if (!$basePath) {
// 使用文档根目录作为备选
$basePath = $_SERVER['DOCUMENT_ROOT'] ?? '';
error_log('Version ID: ' . $version['id'] . ' - Fallback to DOCUMENT_ROOT: ' . $basePath);
}
error_log('Version ID: ' . $version['id'] . ' - Base path: ' . $basePath);
$filePath = $basePath . DIRECTORY_SEPARATOR . ltrim($filePath, DIRECTORY_SEPARATOR);
// 添加调试信息以跟踪路径问题
// 增强调试信息以全面跟踪路径问题
error_log('Version ID: ' . $version['id'] . ' - Original file path: ' . $version['file_path']);
error_log('Version ID: ' . $version['id'] . ' - Normalized path: ' . $filePath);
error_log('Version ID: ' . $version['id'] . ' - File exists: ' . (file_exists($filePath) ? 'Yes' : 'No'));
error_log('Version ID: ' . $version['id'] . ' - Real path: ' . realpath($filePath));
}
// 解析实际路径并处理可能的解析错误
$realPath = realpath($filePath);
$fileSize = $realPath !== false ? filesize($realPath) : false;
$sizeText = formatFileSize($fileSize);
?>
<a href="<?php echo htmlspecialchars($version['file_path']); ?>" class="btn btn-primary" download>下载(大小:<?php echo $sizeText; ?>)</a>
</div> </div>
</div> </div>
<?php endforeach; ?> <?php endforeach; ?>