108 lines
3.8 KiB
PHP
Executable File
108 lines
3.8 KiB
PHP
Executable File
<?php
|
|
require_once __DIR__ . '/includes/session_check.php';
|
|
require_once __DIR__ . '/includes/activity_logger.php';
|
|
|
|
// Verificar que sea admin
|
|
if ($_SESSION['role'] !== 'admin') {
|
|
http_response_code(403);
|
|
echo json_encode(['error' => 'Acceso denegado. Solo administradores pueden subir imágenes.']);
|
|
exit;
|
|
}
|
|
|
|
// Configuración
|
|
$uploadDir = __DIR__ . '/';
|
|
$maxFileSize = 5 * 1024 * 1024; // 5MB
|
|
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
|
|
$allowedMimes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
|
|
|
$response = ['success' => false, 'message' => ''];
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
|
|
$imageType = $_POST['type'] ?? '';
|
|
$file = $_FILES['image'];
|
|
|
|
// Validar tipo de imagen
|
|
if (!in_array($imageType, ['login', 'logo'])) {
|
|
$response['message'] = 'Tipo de imagen inválido.';
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
// Validar que el archivo exista
|
|
if (!isset($file['tmp_name']) || !is_uploaded_file($file['tmp_name'])) {
|
|
$response['message'] = 'No se recibió ningún archivo o hubo un error en la subida.';
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
// Validar tamaño
|
|
if ($file['size'] > $maxFileSize) {
|
|
$response['message'] = 'El archivo es demasiado grande. Máximo 5MB.';
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
// Validar extensión
|
|
$fileExtension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
|
|
if (!in_array($fileExtension, $allowedExtensions)) {
|
|
$response['message'] = 'Formato de archivo no permitido. Usa: ' . implode(', ', $allowedExtensions);
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
// Validar MIME type
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
|
$fileMime = finfo_file($finfo, $file['tmp_name']);
|
|
finfo_close($finfo);
|
|
|
|
if (!in_array($fileMime, $allowedMimes)) {
|
|
$response['message'] = 'El archivo no es una imagen válida.';
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
// Determinar la ruta destino
|
|
if ($imageType === 'login') {
|
|
$filename = 'login.png';
|
|
$destinationPath = $uploadDir . 'galeria/' . $filename;
|
|
$relativeUrl = '/galeria/' . $filename;
|
|
} else { // logo
|
|
$filename = 'logo.png';
|
|
$destinationPath = $uploadDir . 'assets/images/' . $filename;
|
|
$relativeUrl = '/assets/images/' . $filename;
|
|
}
|
|
|
|
// Crear directorio si no existe
|
|
$destDir = dirname($destinationPath);
|
|
if (!is_dir($destDir)) {
|
|
mkdir($destDir, 0755, true);
|
|
}
|
|
|
|
// Mover el archivo
|
|
if (move_uploaded_file($file['tmp_name'], $destinationPath)) {
|
|
// Registrar actividad
|
|
$actionDesc = $imageType === 'login' ? 'Updated login background image' : 'Updated logo image';
|
|
log_activity($_SESSION['user_id'], 'Image Upload', 'Admin ' . $_SESSION['username'] . ' ' . $actionDesc);
|
|
|
|
// Log del archivo guardado
|
|
error_log('Image uploaded successfully: ' . $destinationPath . ' (Size: ' . filesize($destinationPath) . ' bytes)');
|
|
|
|
$response['success'] = true;
|
|
$response['message'] = ucfirst($imageType) . ' actualizado correctamente.';
|
|
$response['url'] = $relativeUrl . '?t=' . time(); // Cache buster
|
|
} else {
|
|
$response['message'] = 'Error al mover el archivo. Verifica los permisos de la carpeta.';
|
|
error_log('Failed to move uploaded file to: ' . $destinationPath);
|
|
}
|
|
} catch (Exception $e) {
|
|
error_log('Error uploading admin image: ' . $e->getMessage());
|
|
$response['message'] = 'Error al procesar la imagen.';
|
|
}
|
|
}
|
|
|
|
header('Content-Type: application/json');
|
|
echo json_encode($response);
|
|
exit;
|
|
?>
|