58 lines
1.8 KiB
PHP
58 lines
1.8 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
echo json_encode(['error' => 'Método no permitido']);
|
|
exit;
|
|
}
|
|
|
|
if (!isset($_FILES['image'])) {
|
|
echo json_encode(['error' => 'No se recibió ninguna imagen']);
|
|
exit;
|
|
}
|
|
|
|
$file = $_FILES['image'];
|
|
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
|
$maxSize = 5 * 1024 * 1024; // 5MB
|
|
|
|
// Límite de imágenes por usuario (basado en IP)
|
|
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'];
|
|
$ip = explode(',', $ip)[0];
|
|
|
|
try {
|
|
$stmt = db()->prepare("SELECT COUNT(*) FROM messages WHERE ip_address = ? AND type = 'image' AND created_at > DATE_SUB(NOW(), INTERVAL 6 HOUR)");
|
|
$stmt->execute([$ip]);
|
|
$count = $stmt->fetchColumn();
|
|
|
|
if ($count >= 5) {
|
|
echo json_encode(['error' => 'Has alcanzado el límite de 5 imágenes cada 6 horas para evitar abusos']);
|
|
exit;
|
|
}
|
|
} catch (Exception $e) {
|
|
// Si falla la verificación, continuamos pero logueamos
|
|
error_log("Error checking upload limit: " . $e->getMessage());
|
|
}
|
|
|
|
if (!in_array($file['type'], $allowedTypes)) {
|
|
echo json_encode(['error' => 'Formato de imagen no permitido (solo JPG, PNG, GIF, WEBP)']);
|
|
exit;
|
|
}
|
|
|
|
if ($file['size'] > $maxSize) {
|
|
echo json_encode(['error' => 'La imagen es demasiado grande (máximo 5MB)']);
|
|
exit;
|
|
}
|
|
|
|
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
|
|
$filename = uniqid('img_', true) . '.' . $ext;
|
|
$targetPath = __DIR__ . '/../uploads/' . $filename;
|
|
|
|
if (move_uploaded_file($file['tmp_name'], $targetPath)) {
|
|
$url = 'uploads/' . $filename;
|
|
echo json_encode(['success' => true, 'url' => $url]);
|
|
} else {
|
|
echo json_encode(['error' => 'Error al guardar la imagen']);
|
|
}
|