<?php
include_once "../config/database.php";
include_once '../config/properties.php';
$new = "";
if ($_GET['path'] != null) {
    $f = trim($_GET['path']);
}

$file = ADMIN_HTTP_PATH . $properties['jdbc.reference_folder'] . '/' . $f;
$file = str_replace(' ', '%20', $file); // Ensure spaces are URL encoded
$ch = curl_init($file);

// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the file content as a string
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow any "Location:" header
curl_setopt($ch, CURLOPT_TIMEOUT, 60);          // Set the timeout for the request
curl_setopt($ch, CURLOPT_FAILONERROR, true);    // Fail on HTTP error
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); // Force HTTP/1.1
// Execute the cURL session
$fileContent = curl_exec($ch);

// Check if the download was successful
if ($fileContent === FALSE) {
    die('Failed to download file: ' . curl_error($ch));
}

// Get the content type and size
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$contentLength = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

// Close the cURL session
curl_close($ch);

// Set headers to serve the file to the client
header("Content-Description: File Transfer");
header("Content-Type: " . $contentType);
header("Content-Disposition: attachment; filename=\"" . basename($file) . "\"");
header("Content-Length: " . $contentLength);
header("Expires: 0");
header("Cache-Control: must-revalidate");
header("Pragma: public");

// Output the file content
echo $fileContent;
exit;

?>
