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

View File

@@ -1,6 +1,23 @@
<?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
if (!isset($_GET['id']) || !is_numeric($_GET['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>
</div>
<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>
<?php endforeach; ?>