- Add PublicCampaignsController for public campaign analytics
- Add public routes for /public/analytics/{slug} and /api/campaigns/{slug}
- Add public campaign analytics view with charts and KPIs
- Support for campaign sharing via custom slugs
- Real-time analytics with ApexCharts integration
324 lines
7.9 KiB
PHP
324 lines
7.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Core;
|
|
|
|
/**
|
|
* Validate - Sistema de validação de input
|
|
* Implementa validação defensiva para todos os tipos de input
|
|
*/
|
|
class Validate
|
|
{
|
|
/**
|
|
* Validar se não está vazio
|
|
*/
|
|
public static function required($value): bool
|
|
{
|
|
if (is_string($value)) {
|
|
return trim($value) !== '';
|
|
}
|
|
|
|
if (is_array($value)) {
|
|
return !empty($value);
|
|
}
|
|
|
|
return $value !== null && $value !== '';
|
|
}
|
|
|
|
/**
|
|
* Validar email
|
|
*/
|
|
public static function email(string $value): bool
|
|
{
|
|
return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
|
|
}
|
|
|
|
/**
|
|
* Validar número inteiro
|
|
*/
|
|
public static function integer($value, ?int $min = null, ?int $max = null): bool
|
|
{
|
|
if (!is_numeric($value)) return false;
|
|
|
|
$value = (int)$value;
|
|
|
|
if ($min !== null && $value < $min) return false;
|
|
if ($max !== null && $value > $max) return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Validar número decimal
|
|
*/
|
|
public static function decimal($value, ?float $min = null, ?float $max = null): bool
|
|
{
|
|
if (!is_numeric($value)) return false;
|
|
|
|
$value = (float)$value;
|
|
|
|
if ($min !== null && $value < $min) return false;
|
|
if ($max !== null && $value > $max) return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Validar URL
|
|
*/
|
|
public static function url(string $value): bool
|
|
{
|
|
return filter_var($value, FILTER_VALIDATE_URL) !== false;
|
|
}
|
|
|
|
/**
|
|
* Validar telefone
|
|
*/
|
|
public static function phone(string $value): bool
|
|
{
|
|
$value = preg_replace('/[^0-9+\-\(\)\s]/', '', $value);
|
|
return strlen($value) >= 8 && strlen($value) <= 20;
|
|
}
|
|
|
|
/**
|
|
* Validar data
|
|
*/
|
|
public static function date(string $value, string $format = 'Y-m-d'): bool
|
|
{
|
|
$date = \DateTime::createFromFormat($format, $value);
|
|
return $date && $date->format($format) === $value;
|
|
}
|
|
|
|
/**
|
|
* Validar comprimento mínimo
|
|
*/
|
|
public static function minLength(string $value, int $min): bool
|
|
{
|
|
return strlen($value) >= $min;
|
|
}
|
|
|
|
/**
|
|
* Validar comprimento máximo
|
|
*/
|
|
public static function maxLength(string $value, int $max): bool
|
|
{
|
|
return strlen($value) <= $max;
|
|
}
|
|
|
|
/**
|
|
* Validar comprimento exato
|
|
*/
|
|
public static function exactLength(string $value, int $length): bool
|
|
{
|
|
return strlen($value) === $length;
|
|
}
|
|
|
|
/**
|
|
* Validar regex pattern
|
|
*/
|
|
public static function pattern(string $value, string $pattern): bool
|
|
{
|
|
return preg_match($pattern, $value) === 1;
|
|
}
|
|
|
|
/**
|
|
* Validar se é alfanumérico
|
|
*/
|
|
public static function alphanumeric(string $value): bool
|
|
{
|
|
return preg_match('/^[a-zA-Z0-9]+$/', $value) === 1;
|
|
}
|
|
|
|
/**
|
|
* Validar se é alfabético
|
|
*/
|
|
public static function alpha(string $value): bool
|
|
{
|
|
return preg_match('/^[a-zA-Z\s]+$/', $value) === 1;
|
|
}
|
|
|
|
/**
|
|
* Validar se é numérico
|
|
*/
|
|
public static function numeric(string $value): bool
|
|
{
|
|
return is_numeric($value);
|
|
}
|
|
|
|
/**
|
|
* Validar se é um ficheiro válido
|
|
*/
|
|
public static function file(array $file): bool
|
|
{
|
|
return isset($file['tmp_name']) &&
|
|
is_uploaded_file($file['tmp_name']) &&
|
|
$file['error'] === UPLOAD_ERR_OK;
|
|
}
|
|
|
|
/**
|
|
* Validar tipo de ficheiro
|
|
*/
|
|
public static function fileType(array $file, array $allowedTypes): bool
|
|
{
|
|
if (!self::file($file)) return false;
|
|
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
|
$mimeType = finfo_file($finfo, $file['tmp_name']);
|
|
finfo_close($finfo);
|
|
|
|
return in_array($mimeType, $allowedTypes);
|
|
}
|
|
|
|
/**
|
|
* Validar tamanho de ficheiro
|
|
*/
|
|
public static function fileSize(array $file, int $maxSize): bool
|
|
{
|
|
if (!self::file($file)) return false;
|
|
|
|
return $file['size'] <= $maxSize;
|
|
}
|
|
|
|
/**
|
|
* Validar se valor está em lista
|
|
*/
|
|
public static function in($value, array $allowedValues): bool
|
|
{
|
|
return in_array($value, $allowedValues, true);
|
|
}
|
|
|
|
/**
|
|
* Validar se valor não está em lista
|
|
*/
|
|
public static function notIn($value, array $forbiddenValues): bool
|
|
{
|
|
return !in_array($value, $forbiddenValues, true);
|
|
}
|
|
|
|
/**
|
|
* Validar se é um array
|
|
*/
|
|
public static function array($value): bool
|
|
{
|
|
return is_array($value);
|
|
}
|
|
|
|
/**
|
|
* Validar se é um boolean
|
|
*/
|
|
public static function boolean($value): bool
|
|
{
|
|
// Permitir nulo/vazio quando usado com 'nullable'
|
|
if ($value === null || $value === '') return true;
|
|
if (is_bool($value)) return true;
|
|
// Aceitar valores comuns de formulários web
|
|
if (is_string($value)) {
|
|
$normalized = strtolower(trim($value));
|
|
if (in_array($normalized, ['1','0','true','false','on','off','yes','no'], true)) {
|
|
return true;
|
|
}
|
|
}
|
|
if (is_int($value)) {
|
|
return in_array($value, [0,1], true);
|
|
}
|
|
return in_array($value, ['0','1', 0, 1, true, false], true);
|
|
}
|
|
|
|
/**
|
|
* Validar se é um JSON válido
|
|
*/
|
|
public static function json(string $value): bool
|
|
{
|
|
json_decode($value);
|
|
return json_last_error() === JSON_ERROR_NONE;
|
|
}
|
|
|
|
/**
|
|
* Validar se é um IP válido
|
|
*/
|
|
public static function ip(string $value): bool
|
|
{
|
|
return filter_var($value, FILTER_VALIDATE_IP) !== false;
|
|
}
|
|
|
|
/**
|
|
* Validar se é um IPv4 válido
|
|
*/
|
|
public static function ipv4(string $value): bool
|
|
{
|
|
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false;
|
|
}
|
|
|
|
/**
|
|
* Validar se é um IPv6 válido
|
|
*/
|
|
public static function ipv6(string $value): bool
|
|
{
|
|
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false;
|
|
}
|
|
|
|
/**
|
|
* Validar se é um MAC address válido
|
|
*/
|
|
public static function mac(string $value): bool
|
|
{
|
|
return filter_var($value, FILTER_VALIDATE_MAC) !== false;
|
|
}
|
|
|
|
/**
|
|
* Validar se é um slug válido
|
|
*/
|
|
public static function slug(string $value): bool
|
|
{
|
|
return preg_match('/^[a-z0-9\-]+$/', $value) === 1;
|
|
}
|
|
|
|
/**
|
|
* Validar se é um username válido
|
|
*/
|
|
public static function username(string $value): bool
|
|
{
|
|
return preg_match('/^[a-zA-Z0-9_]{3,20}$/', $value) === 1;
|
|
}
|
|
|
|
/**
|
|
* Validar se é uma password forte
|
|
*/
|
|
public static function strongPassword(string $value): bool
|
|
{
|
|
// Mínimo 8 caracteres, pelo menos 1 maiúscula, 1 minúscula, 1 número
|
|
return preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d@$!%*?&]{8,}$/', $value) === 1;
|
|
}
|
|
|
|
/**
|
|
* Validar múltiplas regras
|
|
*/
|
|
public static function rules($value, array $rules): array
|
|
{
|
|
$errors = [];
|
|
|
|
foreach ($rules as $rule) {
|
|
$ruleName = $rule;
|
|
$ruleParams = [];
|
|
|
|
if (is_array($rule)) {
|
|
$ruleName = $rule[0];
|
|
$ruleParams = array_slice($rule, 1);
|
|
}
|
|
|
|
$method = $ruleName;
|
|
if (!method_exists(self::class, $method)) {
|
|
$errors[] = "Regra de validação '{$ruleName}' não existe";
|
|
continue;
|
|
}
|
|
|
|
$result = call_user_func_array([self::class, $method], array_merge([$value], $ruleParams));
|
|
|
|
if (!$result) {
|
|
$errors[] = "Falha na validação: {$ruleName}";
|
|
}
|
|
}
|
|
|
|
return $errors;
|
|
}
|
|
}
|