<?php
$nosession = false;
include("./includes/common.php");

$share_key = isset($_GET['key']) ? trim($_GET['key']) : '';
$pwd = isset($_GET['pwd']) ? trim($_GET['pwd']) : '';

if (empty($share_key)) {
    exit('<!DOCTYPE html><html><head><meta charset="utf-8"><title>分享链接无效</title></head><body style="text-align:center;padding:50px;"><h2>分享链接无效</h2><p>请检查链接是否正确</p></body></html>');
}

// 查询文件信息
$row = $DB->getRow("SELECT * FROM pre_file WHERE share_key = :share_key LIMIT 1", [':share_key' => $share_key]);

if (!$row) {
    exit('<!DOCTYPE html><html><head><meta charset="utf-8"><title>分享文件不存在</title></head><body style="text-align:center;padding:50px;"><h2>分享文件不存在</h2><p>该分享链接已失效或文件已被删除</p></body></html>');
}

// 检查文件是否被屏蔽
if ($row['block'] >= 1) {
    exit('<!DOCTYPE html><html><head><meta charset="utf-8"><title>文件已被屏蔽</title></head><body style="text-align:center;padding:50px;"><h2>文件已被屏蔽</h2><p>该文件因违规已被管理员屏蔽</p></body></html>');
}

// 检查是否是所有者
$is_owner = false;
if ($islogin2 && $row['uid'] == $uid) {
    $is_owner = true;
} elseif (!$islogin2 && ((isset($_SESSION['fileids']) && in_array($row['id'], $_SESSION['fileids'])) || in_array($row['id'], get_guest_file_ids())) && strtotime($row['addtime']) > strtotime("-7 days")) {
    $is_owner = true;
}

// 密码验证 - 使用文件原有的密码
$need_pwd = !empty($row['pwd']) && !$is_owner;
$session_key = 'share_pwd_' . $share_key;

if ($need_pwd) {
    if (!empty($pwd)) {
        $info = password_get_info($row['pwd']);
        if ($info['algo'] != 0) {
            $pwd_verified = password_verify($pwd, $row['pwd']);
        } else {
            $pwd_verified = ($row['pwd'] === $pwd);
        }
        if ($pwd_verified) {
            $_SESSION[$session_key] = true;
        }
    } else {
        $pwd_verified = isset($_SESSION[$session_key]) && $_SESSION[$session_key] === true;
    }
} else {
    $pwd_verified = true;
}

// 如果密码验证通过且不是所有者，增加访问次数
if ($pwd_verified && !$is_owner) {
    $DB->exec("UPDATE pre_file SET share_count = share_count + 1 WHERE id = :id", [':id' => $row['id']]);
}

// 如果密码验证通过且URL中有密码参数，重定向到不带密码的URL（防止密码泄露）
if ($pwd_verified && !empty($pwd) && $need_pwd) {
    header("Location: " . $siteurl . "share.php?key=" . $share_key);
    exit;
}

$name = $row['name'];
$type = $row['type'];
$size = $row['size'];
$hash = $row['hash'];
$addtime = $row['addtime'];
$count = $row['count'];
$share_count = $row['share_count'];
$share_notice = !empty($row['share_notice']) ? $row['share_notice'] : '';

// 生成下载链接
$downurl = 'down.php?hash=' . $hash;
$viewurl = 'view.php?hash=' . $hash . '.' . $type;

// 构建带密码的链接
$pwd_param = $pwd_verified && !empty($pwd) ? '&pwd=' . rawurlencode($pwd) : '';
$downurl_full = $siteurl . $downurl . $pwd_param;
$viewurl_full = $siteurl . $viewurl . $pwd_param;

// 获取文件预览类型
$view_type = get_view_type($type);

// 获取文件图标
$file_icon = type_to_icon($type);

// 格式化文件大小
$size_formatted = size_format($size);

$title = '文件分享 - ' . $conf['title'];
?>
<!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 htmlspecialchars($title); ?></title>
    <link rel="stylesheet" href="https://s4.zstatic.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://s4.zstatic.net/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="assets/css/style.css">
    <style>
        .share-page {
            min-height: 100vh;
            background: linear-gradient(135deg, #f5f7fa 0%, #e8f5f3 100%);
            padding: 40px 0;
        }
        .share-container {
            max-width: 800px;
            margin: 0 auto;
            padding: 0 15px;
        }
        .share-header {
            text-align: center;
            margin-bottom: 30px;
        }
        .share-header h1 {
            color: #0f766e;
            font-size: 28px;
            font-weight: 600;
            margin-bottom: 10px;
        }
        .share-header p {
            color: #666;
            font-size: 14px;
        }
        .share-card {
            background: #fff;
            border-radius: 20px;
            box-shadow: 0 10px 40px rgba(15, 118, 110, 0.1);
            overflow: hidden;
            margin-bottom: 25px;
        }
        .share-card-header {
            background: linear-gradient(135deg, #0f766e 0%, #14b8a6 100%);
            color: #fff;
            padding: 25px 30px;
            position: relative;
        }
        .share-card-header::before {
            content: '';
            position: absolute;
            right: -30px;
            top: -30px;
            width: 120px;
            height: 120px;
            background: rgba(255,255,255,0.1);
            border-radius: 50%;
        }
        .share-card-header h2 {
            margin: 0;
            font-size: 20px;
            font-weight: 600;
            display: flex;
            align-items: center;
            gap: 12px;
            position: relative;
            z-index: 1;
        }
        .share-card-header h2 i {
            font-size: 24px;
        }
        .share-card-body {
            padding: 30px;
        }
        .file-info-section {
            display: flex;
            align-items: center;
            gap: 20px;
            padding: 20px;
            background: #fafafa;
            border-radius: 16px;
            margin-bottom: 25px;
        }
        .file-icon-wrapper {
            width: 80px;
            height: 80px;
            background: linear-gradient(135deg, #0f766e 0%, #14b8a6 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-shrink: 0;
        }
        .file-icon-wrapper i {
            font-size: 36px;
            color: #fff;
        }
        .file-info-content {
            flex: 1;
            min-width: 0;
        }
        .file-info-content h3 {
            margin: 0 0 8px;
            font-size: 18px;
            font-weight: 600;
            color: #333;
            word-break: break-all;
        }
        .file-info-content .file-meta {
            display: flex;
            gap: 20px;
            flex-wrap: wrap;
            color: #666;
            font-size: 14px;
        }
        .file-meta span {
            display: flex;
            align-items: center;
            gap: 5px;
        }
        .file-meta span i {
            color: #14b8a6;
        }
        .notice-section {
            background: linear-gradient(135deg, #fff9e6 0%, #fff5d6 100%);
            border-left: 4px solid #f59e0b;
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 25px;
        }
        .notice-section .notice-header {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 12px;
            color: #b45309;
            font-weight: 600;
        }
        .notice-section .notice-header i {
            font-size: 18px;
        }
        .notice-section .notice-content {
            color: #92400e;
            line-height: 1.6;
        }
        .notice-section .notice-content img {
            max-width: 100%;
            height: auto;
            border-radius: 8px;
        }
        .action-section {
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
        }
        .btn-share-action {
            flex: 1;
            min-width: 140px;
            padding: 15px 25px;
            border-radius: 12px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            text-decoration: none;
            border: none;
        }
        .btn-share-primary {
            background: linear-gradient(135deg, #0f766e 0%, #14b8a6 100%);
            color: #fff;
        }
        .btn-share-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(15, 118, 110, 0.35);
            color: #fff;
            text-decoration: none;
        }
        .btn-share-secondary {
            background: #e6f9f7;
            color: #0f766e;
            border: 1px solid #14b8a6;
        }
        .btn-share-secondary:hover {
            background: #14b8a6;
            color: #fff;
            text-decoration: none;
        }
        .password-section {
            text-align: center;
            padding: 40px 20px;
        }
        .password-section .lock-icon {
            width: 100px;
            height: 100px;
            background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 25px;
        }
        .password-section .lock-icon i {
            font-size: 45px;
            color: #fff;
        }
        .password-section h3 {
            font-size: 22px;
            font-weight: 600;
            color: #333;
            margin: 0 0 10px;
        }
        .password-section p {
            color: #666;
            font-size: 14px;
            margin-bottom: 25px;
        }
        .password-form {
            max-width: 300px;
            margin: 0 auto;
        }
        .password-form .form-control {
            height: 50px;
            border-radius: 10px;
            border: 2px solid #e8e8e8;
            font-size: 16px;
            text-align: center;
            margin-bottom: 15px;
        }
        .password-form .form-control:focus {
            border-color: #14b8a6;
            box-shadow: 0 0 0 3px rgba(20,184,166,0.1);
        }
        .password-form .btn {
            width: 100%;
            height: 50px;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            background: linear-gradient(135deg, #0f766e 0%, #14b8a6 100%);
            border: none;
        }
        .password-form .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(15, 118, 110, 0.35);
        }
        .password-error {
            color: #dc2626;
            font-size: 14px;
            margin-top: 10px;
        }
        .preview-section {
            background: #fafafa;
            border-radius: 16px;
            padding: 20px;
            margin-bottom: 25px;
            text-align: center;
        }
        .preview-section img {
            max-width: 100%;
            max-height: 400px;
            border-radius: 12px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.1);
        }
        .preview-section audio,
        .preview-section video {
            width: 100%;
            border-radius: 12px;
        }
        .stats-section {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 15px;
            margin-top: 20px;
        }
        .stats-item {
            background: #fafafa;
            padding: 15px;
            border-radius: 12px;
            text-align: center;
        }
        .stats-item .stats-value {
            font-size: 20px;
            font-weight: 700;
            color: #0f766e;
            margin-bottom: 5px;
        }
        .stats-item .stats-label {
            font-size: 12px;
            color: #666;
        }
        .footer-section {
            text-align: center;
            padding: 20px;
            color: #888;
            font-size: 13px;
        }
        .footer-section a {
            color: #14b8a6;
        }
        @media (max-width: 768px) {
            .share-page {
                padding: 20px 0;
            }
            .share-header h1 {
                font-size: 22px;
            }
            .share-card-header {
                padding: 20px;
            }
            .share-card-header h2 {
                font-size: 16px;
            }
            .share-card-body {
                padding: 20px;
            }
            .file-info-section {
                flex-direction: column;
                text-align: center;
                padding: 15px;
            }
            .file-icon-wrapper {
                width: 60px;
                height: 60px;
            }
            .file-icon-wrapper i {
                font-size: 28px;
            }
            .file-info-content h3 {
                font-size: 16px;
            }
            .file-meta {
                justify-content: center;
            }
            .action-section {
                flex-direction: column;
            }
            .btn-share-action {
                width: 100%;
            }
            .stats-section {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div class="share-page">
        <div class="share-container">
            <div class="share-header">
                <h1><i class="fa fa-cloud"></i> <?php echo htmlspecialchars($conf['title']); ?></h1>
                <p>安全、快速的文件分享服务</p>
            </div>

            <div class="share-card">
                <div class="share-card-header">
                    <h2><i class="fa fa-share-alt"></i> 文件分享</h2>
                </div>
                <div class="share-card-body">
                    <?php if ($need_pwd && !$pwd_verified): ?>
                        <!-- 密码验证界面 -->
                        <div class="password-section">
                            <div class="lock-icon">
                                <i class="fa fa-lock"></i>
                            </div>
                            <h3>文件受密码保护</h3>
                            <p>请输入访问密码以查看文件</p>
                            <form class="password-form" method="get" action="">
                                <input type="hidden" name="key" value="<?php echo htmlspecialchars($share_key); ?>">
                                <input type="password" name="pwd" class="form-control" placeholder="请输入密码" required autofocus>
                                <button type="submit" class="btn btn-primary">
                                    <i class="fa fa-unlock"></i> 验证密码
                                </button>
                                <?php if (!empty($pwd)): ?>
                                    <div class="password-error">
                                        <i class="fa fa-exclamation-circle"></i> 密码错误，请重试
                                    </div>
                                <?php endif; ?>
                            </form>
                        </div>
                    <?php else: ?>
                        <!-- 公告栏 -->
                        <?php if (!empty($share_notice)): ?>
                            <div class="notice-section">
                                <div class="notice-header">
                                    <i class="fa fa-bullhorn"></i>
                                    <span>分享者公告</span>
                                </div>
                                <div class="notice-content">
                                    <?php echo $share_notice; ?>
                                </div>
                            </div>
                        <?php endif; ?>

                        <!-- 文件信息 -->
                        <div class="file-info-section">
                            <div class="file-icon-wrapper">
                                <i class="fa <?php echo $file_icon; ?>"></i>
                            </div>
                            <div class="file-info-content">
                                <h3><?php echo htmlspecialchars($name); ?></h3>
                                <div class="file-meta">
                                    <span><i class="fa fa-hdd-o"></i> <?php echo $size_formatted; ?></span>
                                    <span><i class="fa fa-calendar"></i> <?php echo $addtime; ?></span>
                                    <span><i class="fa fa-file-o"></i> <?php echo strtoupper($type); ?></span>
                                </div>
                            </div>
                        </div>

                        <!-- 文件预览 -->
                        <?php if ($view_type == 'image'): ?>
                            <div class="preview-section">
                                <img src="<?php echo $viewurl_full; ?>" alt="<?php echo htmlspecialchars($name); ?>">
                            </div>
                        <?php elseif ($view_type == 'audio'): ?>
                            <div class="preview-section">
                                <audio controls preload="metadata">
                                    <source src="<?php echo $viewurl_full; ?>" type="audio/<?php echo $type; ?>">
                                    您的浏览器不支持音频播放
                                </audio>
                            </div>
                        <?php elseif ($view_type == 'video' && $row['block'] == 0): ?>
                            <div class="preview-section">
                                <video controls preload="metadata" style="max-height: 400px;">
                                    <source src="<?php echo $viewurl_full; ?>" type="video/<?php echo $type; ?>">
                                    您的浏览器不支持视频播放
                                </video>
                            </div>
                        <?php endif; ?>

                        <!-- 操作按钮 -->
                        <div class="action-section">
                            <a href="<?php echo $downurl_full; ?>" class="btn-share-action btn-share-primary" target="_blank">
                                <i class="fa fa-download"></i> 下载文件
                            </a>
                            <?php if ($view_type == 'image'): ?>
                                <a href="<?php echo $viewurl_full; ?>" class="btn-share-action btn-share-secondary" target="_blank">
                                    <i class="fa fa-eye"></i> 查看原图
                                </a>
                            <?php elseif ($view_type == 'office'): ?>
                                <a href="https://view.officeapps.live.com/op/view.aspx?src=<?php echo rawurlencode($downurl_full); ?>" class="btn-share-action btn-share-secondary" target="_blank">
                                    <i class="fa fa-eye"></i> 在线预览
                                </a>
                            <?php endif; ?>
                        </div>

                        <!-- 统计信息 -->
                        <div class="stats-section">
                            <div class="stats-item">
                                <div class="stats-value"><?php echo $count; ?></div>
                                <div class="stats-label">下载次数</div>
                            </div>
                            <div class="stats-item">
                                <div class="stats-value"><?php echo $share_count; ?></div>
                                <div class="stats-label">页面访问</div>
                            </div>
                            <div class="stats-item">
                                <div class="stats-value"><?php echo $size_formatted; ?></div>
                                <div class="stats-label">文件大小</div>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>

            <div class="footer-section">
                <p>该文件由用户分享，本站不对文件内容负责</p>
                <p><a href="./"><?php echo htmlspecialchars($conf['title']); ?></a> &copy; <?php echo date('Y'); ?></p>
            </div>
        </div>
    </div>

    <script src="https://s4.zstatic.net/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://s4.zstatic.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
</body>
</html>
