Primer commit del sistema separado falta mejorar mucho
This commit is contained in:
105
gallery/api/delete.php
Executable file
105
gallery/api/delete.php
Executable file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/**
|
||||
* API de Galería - Eliminar imagen
|
||||
*/
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Cargar variables de entorno
|
||||
if (file_exists(__DIR__ . '/../../.env')) {
|
||||
$lines = file(__DIR__ . '/../../.env', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
foreach ($lines as $line) {
|
||||
if (strpos(trim($line), '#') === 0) continue;
|
||||
if (strpos($line, '=') === false) continue;
|
||||
list($key, $value) = explode('=', $line, 2);
|
||||
$_ENV[trim($key)] = trim($value);
|
||||
}
|
||||
}
|
||||
|
||||
require_once __DIR__ . '/../../shared/database/connection.php';
|
||||
require_once __DIR__ . '/../../shared/auth/jwt.php';
|
||||
|
||||
// Verificar autenticación
|
||||
if (!isAuthenticated()) {
|
||||
http_response_code(401);
|
||||
echo json_encode(['success' => false, 'error' => 'No autenticado']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Verificar permiso
|
||||
if (!hasPermission('delete_gallery_images')) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['success' => false, 'error' => 'No tienes permiso para eliminar imágenes de la galería.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Solo admins pueden eliminar
|
||||
if ($userData->rol !== 'Admin' && !JWTAuth::hasPermission($userData, 'gestionar_galeria')) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['success' => false, 'error' => 'No tiene permisos para eliminar imágenes']);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'DELETE' && $_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['success' => false, 'error' => 'Método no permitido']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
if (!isset($input['id'])) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'error' => 'Falta el ID de la imagen']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$db = getDB();
|
||||
|
||||
// Buscar la imagen
|
||||
$stmt = $db->prepare("SELECT * FROM gallery WHERE id = ?");
|
||||
$stmt->execute([$input['id']]);
|
||||
$image = $stmt->fetch();
|
||||
|
||||
if (!$image) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['success' => false, 'error' => 'Imagen no encontrada']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Verificar que no esté siendo usada en mensajes de bienvenida
|
||||
$stmt = $db->query("SELECT COUNT(*) as total FROM bienvenida_discord WHERE imagen_id = " . $input['id']);
|
||||
$usedInDiscord = $stmt->fetch()['total'];
|
||||
|
||||
$stmt = $db->query("SELECT COUNT(*) as total FROM bienvenida_telegram WHERE imagen_id = " . $input['id']);
|
||||
$usedInTelegram = $stmt->fetch()['total'];
|
||||
|
||||
if ($usedInDiscord > 0 || $usedInTelegram > 0) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'error' => 'No se puede eliminar. La imagen está siendo usada en mensajes de bienvenida']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Eliminar archivos físicos
|
||||
$uploadPath = __DIR__ . '/../uploads/' . $image['nombre'];
|
||||
$thumbnailPath = __DIR__ . '/../thumbnails/' . $image['nombre'];
|
||||
|
||||
if (file_exists($uploadPath)) {
|
||||
unlink($uploadPath);
|
||||
}
|
||||
if (file_exists($thumbnailPath)) {
|
||||
unlink($thumbnailPath);
|
||||
}
|
||||
|
||||
// Eliminar de la base de datos
|
||||
$stmt = $db->prepare("DELETE FROM gallery WHERE id = ?");
|
||||
$stmt->execute([$input['id']]);
|
||||
|
||||
echo json_encode(['success' => true, 'message' => 'Imagen eliminada correctamente']);
|
||||
|
||||
} catch (Exception $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'error' => 'Error del servidor']);
|
||||
error_log('Error en delete.php: ' . $e->getMessage());
|
||||
}
|
||||
Reference in New Issue
Block a user