Files
sistema_para_juego/gallery/api/edit.php

68 lines
2.1 KiB
PHP
Executable File

<?php
/**
* API de Galería - Editar nombre de 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('edit_gallery_images')) {
http_response_code(403);
echo json_encode(['success' => false, 'error' => 'No tienes permiso para editar imágenes de la galería.']);
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'PUT' && $_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']) || !isset($input['nombre_original'])) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => 'Faltan parámetros']);
exit;
}
try {
$db = getDB();
$stmt = $db->prepare("UPDATE gallery SET nombre_original = ? WHERE id = ?");
$stmt->execute([$input['nombre_original'], $input['id']]);
if ($stmt->rowCount() > 0) {
echo json_encode(['success' => true, 'message' => 'Nombre actualizado correctamente']);
} else {
http_response_code(404);
echo json_encode(['success' => false, 'error' => 'Imagen no encontrada']);
}
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['success' => false, 'error' => 'Error del servidor']);
error_log('Error en edit.php: ' . $e->getMessage());
}