76 lines
2.5 KiB
PHP
76 lines
2.5 KiB
PHP
<?php
|
|
/**
|
|
* Funciones de sanitización y validación reutilizables.
|
|
*/
|
|
|
|
function limpiar_texto($valor, $max = 500) {
|
|
$valor = trim((string)$valor);
|
|
$valor = strip_tags($valor);
|
|
$valor = preg_replace('/[\r\n\t]+/', ' ', $valor);
|
|
if (mb_strlen($valor) > $max) {
|
|
$valor = mb_substr($valor, 0, $max);
|
|
}
|
|
return $valor;
|
|
}
|
|
|
|
function limpiar_mensaje($valor, $max = 5000) {
|
|
$valor = trim((string)$valor);
|
|
$valor = strip_tags($valor);
|
|
if (mb_strlen($valor) > $max) {
|
|
$valor = mb_substr($valor, 0, $max);
|
|
}
|
|
return $valor;
|
|
}
|
|
|
|
function limpiar_email($valor) {
|
|
return filter_var(trim((string)$valor), FILTER_SANITIZE_EMAIL);
|
|
}
|
|
|
|
function email_valido($valor) {
|
|
return (bool) filter_var($valor, FILTER_VALIDATE_EMAIL);
|
|
}
|
|
|
|
function en_lista($valor, array $permitidos) {
|
|
return in_array($valor, $permitidos, true) ? $valor : '';
|
|
}
|
|
|
|
/**
|
|
* Valida los datos del formulario y devuelve
|
|
* ['ok' => bool, 'errores' => [], 'datos' => []]
|
|
*/
|
|
function validar_contacto(array $post) {
|
|
$errores = [];
|
|
|
|
$datos = [
|
|
'nombre' => limpiar_texto($post['nombre'] ?? '', 120),
|
|
'empresa' => limpiar_texto($post['empresa'] ?? '', 120),
|
|
'email' => limpiar_email($post['email'] ?? ''),
|
|
'telefono' => limpiar_texto($post['telefono'] ?? '', 40),
|
|
'servicio' => en_lista($post['servicio'] ?? '', [
|
|
'branding','web','rrss','automatizaciones','paid','ecosistema','otro'
|
|
]),
|
|
'etapa' => en_lista($post['etapa'] ?? '', [
|
|
'idea','inicio','marcha','redisenio','escalar'
|
|
]),
|
|
'presupuesto' => en_lista($post['presupuesto'] ?? '', [
|
|
'esencial','impulso','premium','elite','proyecto','no-se'
|
|
]),
|
|
'mensaje' => limpiar_mensaje($post['mensaje'] ?? '', 5000),
|
|
'origen' => en_lista($post['origen'] ?? '', [
|
|
'instagram','linkedin','referido','google','otro'
|
|
]),
|
|
'consentimiento'=> !empty($post['consentimiento']),
|
|
];
|
|
|
|
if ($datos['nombre'] === '') $errores[] = 'El nombre es obligatorio.';
|
|
if (!email_valido($datos['email'])) $errores[] = 'El email no es válido.';
|
|
if ($datos['servicio'] === '') $errores[] = 'Seleccioná un servicio de interés.';
|
|
if ($datos['mensaje'] === '') $errores[] = 'El mensaje es obligatorio.';
|
|
if (!$datos['consentimiento']) $errores[] = 'Debés aceptar el uso de tus datos.';
|
|
|
|
return [
|
|
'ok' => empty($errores),
|
|
'errores' => $errores,
|
|
'datos' => $datos,
|
|
];
|
|
} |