| Current Path : /var/www/cesa.co.za/php/includes/ |
| Current File : /var/www/cesa.co.za/php/includes/cce_file_download_endpoint.php |
<?php
/**
* Shared tokenized file download handler for CCE portals.
*
* Define CCE_FILE_DOWNLOAD_PORTAL before including this script.
*
* @see documentation/specs/cce-file-upload-service.md
*/
/**
* @param int $statusCode
* @param string $message
* @return void
*/
function cceDownloadRespond($statusCode, $message)
{
while (ob_get_level() > 0) {
ob_end_clean();
}
if (!headers_sent()) {
http_response_code($statusCode);
header('Content-Type: text/plain; charset=utf-8');
header('Cache-Control: no-store');
}
echo $message;
exit;
}
if (!defined('CCE_FILE_DOWNLOAD_PORTAL')) {
cceDownloadRespond(500, 'Download portal not configured.');
}
if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
require_once $_SERVER['DOCUMENT_ROOT'] . '/php/includes/CceUploadedFileService.php';
$portal = (string) CCE_FILE_DOWNLOAD_PORTAL;
$service = new CceUploadedFileService();
$token = isset($_GET['t']) ? trim((string) $_GET['t']) : '';
if ($token === '') {
cceDownloadRespond(400, 'Invalid download request.');
}
$meta = $service->redeemDownloadToken($portal, $token);
if ($meta === null) {
cceDownloadRespond(404, 'Download link is invalid or has expired.');
}
if (!$service->authorizeDownload($portal, $meta)) {
cceDownloadRespond(403, 'You are not allowed to download this file.');
}
$documentRoot = isset($_SERVER['DOCUMENT_ROOT']) ? (string) $_SERVER['DOCUMENT_ROOT'] : '';
$resolvedPath = $service->resolveFilePath(
$documentRoot,
(string) $meta['storageLocation'],
(string) $meta['basename']
);
if ($resolvedPath === null) {
cceDownloadRespond(404, 'File not found.');
}
while (ob_get_level() > 0) {
ob_end_clean();
}
$service->streamFileResponse($resolvedPath);
exit;