<?php
session_start();
 if($_SESSION['mhafuz']=='Active')
{
// Sanitize input
$filename = htmlspecialchars($_GET["name"]);
$folder = htmlspecialchars($_GET["folder"]);
$original_name = htmlspecialchars($_GET["original_name"]);

// Build file path
$file = '../../../public/uploads/' . ($folder ? $folder . '/' : '') . $filename;

// Check file existence
if (!file_exists($file)) {
    http_response_code(404);
    echo "File not found.";
    exit;
}

// Determine content type
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
$content_disposition = 'Content-Disposition: inline; filename="' . ($original_name ?: $filename) . '"';
$content_type = '';

switch ($extension) {
    case 'pdf':
        $content_type = 'application/pdf';
        break;
    case 'jpg':
    case 'jpeg':
        $content_type = 'image/jpeg';
        break;
    case 'png':
        $content_type = 'image/png';
        break;
    case 'xlsx':
        $content_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
        break;
    case 'xls':
        $content_type = 'application/vnd.ms-excel';
        break;
    case 'xlsm':
        $content_type = 'application/vnd.ms-excel.sheet.macroEnabled.12';
        break;
    case 'xml':
    case 'xlm':
        $content_type = 'application/xml';
        break;
    case 'docx':
        $content_type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
        break;
    case 'doc':
        $content_type = 'application/msword';
        break;
    case 'csv':
        $content_type = 'text/csv';
        break;
    case 'zip':
        $content_type = 'application/zip';
        break;
    case 'rar':
        $content_type = 'application/x-rar-compressed';
        break;
    case 'ppt':
        $content_type = 'application/vnd.ms-powerpoint';
        break;
    case 'pptx':
        $content_type = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
        break;
	case 'txt':
        $content_type = 'text/plain';
        break;
    default:
        http_response_code(400);
        echo "Unsupported file type.";
        exit;
}

// Clear any previous output
if (ob_get_length()) {
    ob_end_clean();
}

// Set headers
header('Content-Type: ' . $content_type);
header($content_disposition);
header('Content-Length: ' . filesize($file));

// Read file
readfile($file);

exit;
}else{
    http_response_code(403);
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>403 Forbidden</title>
        <style>
            body {
                font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
                background-color: #f4f6f9;
                display: flex;
                justify-content: center;
                align-items: center;
                height: 100vh;
                margin: 0;
            }
            .container {
                text-align: center;
                background: white;
                padding: 40px;
                border-radius: 8px;
                box-shadow: 0 4px 6px rgba(0,0,0,0.1);
                max-width: 400px;
                width: 90%;
            }
            h1 {
                font-size: 80px;
                margin: 0;
                color: #e74c3c;
                font-weight: 300;
            }
            h2 {
                color: #2c3e50;
                margin-top: 10px;
                margin-bottom: 20px;
            }
            p {
                color: #7f8c8d;
                line-height: 1.6;
                margin-bottom: 30px;
            }
            .btn {
                display: inline-block;
                padding: 10px 20px;
                background-color: #3498db;
                color: white;
                text-decoration: none;
                border-radius: 4px;
                transition: background-color 0.3s;
            }
            .btn:hover {
                background-color: #2980b9;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>403</h1>
            <h2>Access Denied</h2>
            <p>You do not have permission to view this resource.</p>
            <a href="javascript:history.back()" class="btn">Go Back</a>
        </div>
    </body>
    </html>
    <?php
    exit;
}
?>
