- 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
173 lines
4.8 KiB
PHP
173 lines
4.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Core;
|
|
|
|
/**
|
|
* InputSanitizer - Sanitização automática de todos os inputs
|
|
* Implementa sanitização defensiva universal para $_POST, $_GET, $_REQUEST
|
|
*/
|
|
class InputSanitizer
|
|
{
|
|
private static array $sanitizedInputs = [];
|
|
private static bool $initialized = false;
|
|
|
|
/**
|
|
* Inicializar sanitização automática
|
|
*/
|
|
public static function initialize(): void
|
|
{
|
|
if (self::$initialized) {
|
|
return;
|
|
}
|
|
|
|
// Process JSON input for API requests
|
|
$contentType = $_SERVER['CONTENT_TYPE'] ?? '';
|
|
if (strpos($contentType, 'application/json') !== false) {
|
|
$jsonInput = file_get_contents('php://input');
|
|
$jsonData = json_decode($jsonInput, true);
|
|
if ($jsonData && is_array($jsonData)) {
|
|
foreach ($jsonData as $key => $value) {
|
|
self::$sanitizedInputs['POST'][$key] = self::sanitizeValue($value);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Sanitizar $_POST
|
|
foreach ($_POST as $key => $value) {
|
|
self::$sanitizedInputs['POST'][$key] = self::sanitizeValue($value);
|
|
}
|
|
|
|
// Sanitizar $_GET
|
|
foreach ($_GET as $key => $value) {
|
|
self::$sanitizedInputs['GET'][$key] = self::sanitizeValue($value);
|
|
}
|
|
|
|
// Sanitizar $_REQUEST
|
|
foreach ($_REQUEST as $key => $value) {
|
|
self::$sanitizedInputs['REQUEST'][$key] = self::sanitizeValue($value);
|
|
}
|
|
|
|
self::$initialized = true;
|
|
}
|
|
|
|
/**
|
|
* Obter input sanitizado
|
|
*/
|
|
public static function get(string $key, string $method = 'POST', $default = ''): mixed
|
|
{
|
|
self::initialize();
|
|
|
|
return self::$sanitizedInputs[$method][$key] ?? $default;
|
|
}
|
|
|
|
/**
|
|
* Obter todos os inputs sanitizados de um método
|
|
*/
|
|
public static function all(string $method = 'POST'): array
|
|
{
|
|
self::initialize();
|
|
|
|
return self::$sanitizedInputs[$method] ?? [];
|
|
}
|
|
|
|
/**
|
|
* Verificar se input existe
|
|
*/
|
|
public static function has(string $key, string $method = 'POST'): bool
|
|
{
|
|
self::initialize();
|
|
|
|
return isset(self::$sanitizedInputs[$method][$key]);
|
|
}
|
|
|
|
/**
|
|
* Sanitizar valor baseado no tipo
|
|
*/
|
|
private static function sanitizeValue($value): mixed
|
|
{
|
|
if (is_array($value)) {
|
|
return array_map([self::class, 'sanitizeValue'], $value);
|
|
}
|
|
|
|
if (is_string($value)) {
|
|
return Sanitize::string($value);
|
|
}
|
|
|
|
if (is_numeric($value)) {
|
|
return Sanitize::integer($value);
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
/**
|
|
* Sanitizar input específico com tipo
|
|
*/
|
|
public static function email(string $key, string $method = 'POST'): string
|
|
{
|
|
$value = self::get($key, $method);
|
|
return Sanitize::email($value);
|
|
}
|
|
|
|
public static function integer(string $key, string $method = 'POST', ?int $min = null, ?int $max = null): int
|
|
{
|
|
$value = self::get($key, $method);
|
|
return Sanitize::integer($value, $min, $max);
|
|
}
|
|
|
|
public static function decimal(string $key, string $method = 'POST', int $decimals = 2): float
|
|
{
|
|
$value = self::get($key, $method);
|
|
return Sanitize::decimal($value, $decimals);
|
|
}
|
|
|
|
public static function url(string $key, string $method = 'POST'): string
|
|
{
|
|
$value = self::get($key, $method);
|
|
return Sanitize::url($value);
|
|
}
|
|
|
|
public static function phone(string $key, string $method = 'POST'): string
|
|
{
|
|
$value = self::get($key, $method);
|
|
return Sanitize::phone($value);
|
|
}
|
|
|
|
public static function date(string $key, string $method = 'POST'): string
|
|
{
|
|
$value = self::get($key, $method);
|
|
return Sanitize::date($value);
|
|
}
|
|
|
|
/**
|
|
* Validar input com regras
|
|
*/
|
|
public static function validate(string $key, array $rules, string $method = 'POST'): array
|
|
{
|
|
$value = self::get($key, $method);
|
|
return Validate::rules($value, $rules);
|
|
}
|
|
|
|
/**
|
|
* Limpar dados sanitizados
|
|
*/
|
|
public static function clear(): void
|
|
{
|
|
self::$sanitizedInputs = [];
|
|
self::$initialized = false;
|
|
}
|
|
/**
|
|
* Converte vários formatos para boolean seguro
|
|
*/
|
|
public static function toBoolean($value, bool $default = false): bool
|
|
{
|
|
if ($value === null || $value === '') return $default;
|
|
if (is_bool($value)) return $value;
|
|
if (is_int($value)) return $value === 1;
|
|
$normalized = strtolower(trim((string)$value));
|
|
if (in_array($normalized, ['1','true','on','yes'], true)) return true;
|
|
if (in_array($normalized, ['0','false','off','no'], true)) return false;
|
|
return $default;
|
|
}
|
|
}
|