<?php
// 下载限流脚本
// 同一个IP同一个文件最多下载5次

$download_dir = '/usr/share/nginx/html/tgers.com.cn/downloads/';
$limit_file = '/var/www/downloads/download_log.txt';
$max_downloads = 5;

// 获取客户端IP
function getClientIP() {
    $ip = '';
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    // 取第一个IP（如果有多级代理）
    if (strpos($ip, ',') !== false) {
        $ip = trim(explode(',', $ip)[0]);
    }
    return $ip;
}

// 获取文件列表
function getFileList($dir) {
    $files = [];
    if (is_dir($dir)) {
        $items = scandir($dir);
        foreach ($items as $item) {
            if ($item != '.' && $item != '..' && is_file($dir . $item)) {
                $files[] = $item;
            }
        }
    }
    return $files;
}

// 检查下载次数
function checkDownloadLimit($ip, $filename, $log_file, $max) {
    if (!file_exists($log_file)) {
        return true; // 首次下载
    }
    
    $logs = json_decode(file_get_contents($log_file), true);
    if (!$logs) $logs = [];
    
    $key = $ip . '|' . $filename;
    $count = isset($logs[$key]) ? $logs[$key] : 0;
    
    return $count < $max;
}

// 记录下载
function recordDownload($ip, $filename, $log_file) {
    $logs = [];
    if (file_exists($log_file)) {
        $logs = json_decode(file_get_contents($log_file), true);
    }
    if (!$logs) $logs = [];
    
    $key = $ip . '|' . $filename;
    if (!isset($logs[$key])) {
        $logs[$key] = 0;
    }
    $logs[$key]++;
    
    // 清理超过7天的记录
    $seven_days_ago = time() - 7*24*3600;
    foreach ($logs as $k => $v) {
        // 简化处理：保留所有记录，由定时任务清理
    }
    
    file_put_contents($log_file, json_encode($logs));
}

// 处理下载请求
if (isset($_GET['file'])) {
    $filename = basename($_GET['file']); // 防止路径遍历
    $filepath = $download_dir . $filename;
    
    // 检查文件是否存在
    if (!file_exists($filepath)) {
        die('文件不存在');
    }
    
    $ip = getClientIP();
    
    // 检查下载限制
    if (!checkDownloadLimit($ip, $filename, $limit_file, $max_downloads)) {
        die('下载次数已达上限（' . $max_downloads . '次），请明天再试');
    }
    
    // 记录下载
    recordDownload($ip, $filename, $limit_file);
    
    // 设置下载头
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    header('Content-Length: ' . filesize($filepath));
    header('Pragma: public');
    
    readfile($filepath);
    exit;
}

// 显示下载页面
$files = getFileList($download_dir);
$ip = getClientIP();

?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件下载 - 安势科技</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%); color: white; min-height: 100vh; display: flex; flex-direction: column; }
        .header { display: flex; justify-content: space-between; align-items: center; padding: 15px 20px; background: rgba(0, 0, 0, 0.3); flex-wrap: wrap; gap: 10px; }
        .nav { display: flex; gap: 20px; align-items: center; flex-wrap: wrap; justify-content: center; }
        .nav a { color: white; text-decoration: none; font-size: 1rem; padding: 8px 12px; border-radius: 4px; transition: background 0.3s; white-space: nowrap; }
        .nav a:hover { background: rgba(255, 255, 255, 0.2); }
        .dropdown { position: relative; display: inline-block; }
        .dropdown-content { display: none; position: absolute; background: rgba(0, 0, 0, 0.95); min-width: 140px; border-radius: 4px; z-index: 100; margin-top: 8px; }
        .dropdown-content a { display: block; padding: 10px 12px; font-size: 0.9rem; }
        .dropdown-content a:hover { background: rgba(255, 255, 255, 0.2); }
        .dropdown:hover .dropdown-content { display: block; }
        
        .container { flex: 1; padding: 80px 20px 40px; max-width: 1200px; margin: 0 auto; }
        h1 { text-align: center; font-size: 2rem; margin-bottom: 40px; }
        
        .grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 30px; margin-bottom: 60px; }
        .card { 
            background: linear-gradient(135deg, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0.05) 100%);
            border-radius: 16px; 
            padding: 40px 30px;
            text-align: center;
            text-decoration: none;
            color: inherit;
            transition: transform 0.3s, box-shadow 0.3s;
            border: 1px solid rgba(255,255,255,0.1);
        }
        .card:hover { transform: translateY(-5px); box-shadow: 0 20px 40px rgba(0,0,0,0.3); }
        .card-icon { font-size: 3rem; margin-bottom: 15px; }
        .card h2 { font-size: 1.8rem; margin-bottom: 10px; }
        .card p { opacity: 0.8; line-height: 1.6; }
        
        .card-yushi { background: linear-gradient(135deg, #1e3a5f 0%, #0d2137 100%); border-color: #4fc3f7; }
        .card-yushi:hover { box-shadow: 0 20px 40px rgba(79, 195, 247, 0.3); }
        .card-huicheng { background: linear-gradient(135deg, #2d1f3d 0%, #1a1525 100%); border-color: #b388ff; }
        .card-huicheng:hover { box-shadow: 0 20px 40px rgba(179, 136, 255, 0.3); }
        .card-anjie { background: linear-gradient(135deg, #1f3d2d 0%, #0f251a 100%); border-color: #69f0ae; }
        .card-anjie:hover { box-shadow: 0 20px 40px rgba(105, 240, 174, 0.3); }
        .card-anshi { background: linear-gradient(135deg, #3d2d1f 0%, #251a0f 100%); border-color: #ffb74d; }
        .card-anshi:hover { box-shadow: 0 20px 40px rgba(255, 183, 77, 0.3); }
        
        .download-section h2 { text-align: center; font-size: 1.8rem; margin-bottom: 30px; }
        .file-list { 
            background: rgba(255,255,255,0.05);
            border-radius: 12px;
            padding: 20px;
            border: 1px solid rgba(255,255,255,0.1);
        }
        .file-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 15px 20px;
            border-bottom: 1px solid rgba(255,255,255,0.1);
            transition: background 0.3s;
        }
        .file-item:last-child { border-bottom: none; }
        .file-item:hover { background: rgba(255,255,255,0.05); }
        .file-info { display: flex; align-items: center; gap: 15px; flex: 1; }
        .file-icon { font-size: 1.5rem; }
        .file-name { font-size: 1rem; }
        .file-size { font-size: 0.85rem; opacity: 0.6; margin-left: 10px; }
        .download-btn {
            background: linear-gradient(135deg, #4fc3f7 0%, #2196f3 100%);
            color: white;
            padding: 8px 20px;
            border-radius: 6px;
            text-decoration: none;
            font-size: 0.9rem;
            transition: transform 0.3s, box-shadow 0.3s;
        }
        .download-btn:hover { transform: translateY(-2px); box-shadow: 0 5px 15px rgba(79, 195, 247, 0.4); }
        .download-btn.disabled {
            background: #666;
            cursor: not-allowed;
        }
        
        .footer { padding: 15px; text-align: center; font-size: 0.8rem; opacity: 0.7; }
        .footer a { color: white; text-decoration: none; }
        
        @media (max-width: 768px) {
            .grid { grid-template-columns: 1fr; }
            .card { padding: 30px 20px; }
            .file-item { flex-direction: column; gap: 10px; text-align: center; }
            .file-info { flex-direction: column; }
        }
    </style>
</head>
<body>
    <header class="header">
        <nav class="nav">
            <a href="/">首页</a>
            <a href="/xinnengyuan">新能源</a>
            <a href="/anfang">安防</a>
            <a href="/shijuejiance">视觉检测</a>
            <a href="/lianxiwomen">联系我们</a>
            <a href="/downloads/">下载</a>
            <div class="dropdown">
                <a href="#">友情▾</a>
                <div class="dropdown-content">
                    <a href="https://cn.uniview.com/" target="_blank">宇视科技</a>
                    <a href="https://www.h-visions.com/" target="_blank">上海慧程</a>
                    <a href="http://www.anjpower.com" target="_blank">安耐杰电力设备</a>
                </div>
            </div>
        </nav>
    </header>
    
    <div class="container">
        <h1>合作伙伴</h1>
        <div class="grid">
            <a href="https://cn.uniview.com/" target="_blank" class="card card-yushi">
                <div class="card-icon">📹</div>
                <h2>宇视科技</h2>
                <p>全球AIoT产品、解决方案与全栈式能力提供商<br>视频监控行业全球前4</p>
            </a>
            <a href="https://www.h-visions.com/" target="_blank" class="card card-huicheng">
                <div class="card-icon">💻</div>
                <h2>上海慧程</h2>
                <p>工业互联网整体解决方案供应商<br>智能制造数字化服务商</p>
            </a>
            <a href="http://www.anjpower.com" target="_blank" class="card card-anjie">
                <div class="card-icon">⚡</div>
                <h2>安耐杰电力</h2>
                <p>电力设备制造与服务商<br>高低压配电解决方案</p>
            </a>
            <a href="/" class="card card-anshi">
                <div class="card-icon">🏢</div>
                <h2>安势科技</h2>
                <p>智能科技解决方案提供商<br>新能源、安防、视觉检测</p>
            </a>
        </div>
        
        <div class="download-section">
            <h2>📥 文件下载</h2>
            <div class="file-list">
                <?php foreach($files as $file): ?>
                <?php 
                    $filesize = filesize($download_dir . $file);
                    $size_str = '';
                    if ($filesize > 1024*1024) {
                        $size_str = round($filesize/1024/1024, 1) . 'MB';
                    } else {
                        $size_str = round($filesize/1024, 1) . 'KB';
                    }
                ?>
                <div class="file-item">
                    <div class="file-info">
                        <span class="file-icon">📄</span>
                        <span class="file-name"><?php echo htmlspecialchars($file); ?></span>
                        <span class="file-size"><?php echo $size_str; ?></span>
                    </div>
                    <a href="?file=<?php echo urlencode($file); ?>" class="download-btn" download>下载</a>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
    
    <div class="footer">
        <a href="https://beian.miit.gov.cn/" target="_blank">浙ICP备2026034678号</a> | <a href="https://beian.mps.gov.cn" target="_blank">浙公网安备33048302001153号</a>
    </div>
</body>
</html>
