<?php
// عرض أو تحميل ملف
$project_root = realpath(__DIR__ . '/../../..');
$file_path = $_GET['file'] ?? '';
$action = $_GET['action'] ?? 'view'; // view or download

if (empty($file_path)) {
    die('ملف غير محدد');
}

$full_path = realpath($project_root . '/' . $file_path);

// التحقق من الأمان
if ($full_path === false || strpos($full_path, $project_root) !== 0) {
    die('مسار غير صالح');
}

if (!file_exists($full_path) || !is_file($full_path)) {
    die('الملف غير موجود');
}

// الحصول على معلومات الملف
$file_name = basename($full_path);
$file_size = filesize($full_path);
$mime_type = mime_content_type($full_path);

// إعداد الهيدر
header('Content-Type: ' . $mime_type);
header('Content-Length: ' . $file_size);

if ($action === 'download') {
    header('Content-Disposition: attachment; filename="' . $file_name . '"');
} else {
    header('Content-Disposition: inline; filename="' . $file_name . '"');
}

// إرسال الملف
readfile($full_path);
exit;
?>
