Files
whatsapp/api/lab/descargar_orden.php
T
2026-03-16 20:14:22 -05:00

69 lines
2.2 KiB
PHP

<?php
/**
* GET /api/lab/descargar_orden.php?file=nombre_archivo.pdf
* Sirve archivos de órdenes médicas con el Content-Type correcto.
*/
require_once __DIR__ . '/_helpers.php';
requireMethod('GET');
$filename = $_GET['file'] ?? '';
// Validar: solo nombre de archivo sin rutas (evitar path traversal)
if (!$filename || $filename !== basename($filename) || strpos($filename, '..') !== false) {
http_response_code(400);
echo json_encode(['error' => 'Archivo inválido']);
exit;
}
// Solo archivos con prefijo de orden (seguridad adicional)
if (!preg_match('/^orden_[a-zA-Z0-9_]+\.[a-zA-Z0-9]+$/', $filename)) {
http_response_code(403);
echo json_encode(['error' => 'Acceso denegado']);
exit;
}
$path = realpath(__DIR__ . '/../../uploads/media/' . $filename);
$base = realpath(__DIR__ . '/../../uploads/media');
// Verificar que el archivo esté dentro del directorio permitido
if (!$path || !$base || strpos($path, $base) !== 0 || !is_file($path)) {
http_response_code(404);
echo json_encode(['error' => 'Archivo no encontrado']);
exit;
}
// Detectar MIME real
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $path);
finfo_close($finfo);
// Mapa de extensiones como fallback
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
$mimeMap = [
'pdf' => 'application/pdf',
'doc' => 'application/msword',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp',
];
if (!$mime || $mime === 'application/octet-stream') {
$mime = $mimeMap[$ext] ?? 'application/octet-stream';
}
// Tipos que el navegador puede mostrar inline
$inlineTypes = ['application/pdf', 'image/jpeg', 'image/png', 'image/gif', 'image/webp'];
$disposition = in_array($mime, $inlineTypes) ? 'inline' : 'attachment';
header('Content-Type: ' . $mime);
header('Content-Disposition: ' . $disposition . '; filename="' . rawurlencode($filename) . '"');
header('Content-Length: ' . filesize($path));
header('Cache-Control: private, max-age=3600');
header('X-Content-Type-Options: nosniff');
readfile($path);
exit;