Files
MoneyFlix/crm/Core/Sanitize.php
T
1sonhador 9d43854277 feat: Initial MoneyFlix project setup
- Created separate MoneyFlix project directory
- Copied Node.js redirector from production server
- Added CRM PHP code (Redirects module, Core classes, Config, Routes, Views)
- Created comprehensive documentation (README_MONEYFLIX.md)
- Added sync script to keep CRM code updated (sync-from-crm.sh)
- Configured .gitignore for Node.js and PHP
- Set up proper project structure for multi-client isolation

This separates MoneyFlix from the main CRM to avoid confusion
between different client systems and provides a dedicated
environment for MoneyFlix-specific development and maintenance.
2025-10-03 12:09:41 +01:00

217 lines
5.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Core;
/**
* Sanitize - Sistema de sanitização de input
* Implementa sanitização defensiva para todos os tipos de input
*/
class Sanitize
{
/**
* Sanitizar string básica
*/
public static function string(?string $value, int $maxLength = 255): string
{
if ($value === null) return '';
$value = trim($value);
$value = strip_tags($value);
$value = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
return substr($value, 0, $maxLength);
}
/**
* Sanitizar email
*/
public static function email(?string $value): string
{
if ($value === null) return '';
$value = trim($value);
$value = filter_var($value, FILTER_SANITIZE_EMAIL);
return $value ?: '';
}
/**
* Sanitizar número inteiro
*/
public static function integer($value, ?int $min = null, ?int $max = null): int
{
$value = filter_var($value, FILTER_SANITIZE_NUMBER_INT);
$value = (int)$value;
if ($min !== null && $value < $min) $value = $min;
if ($max !== null && $value > $max) $value = $max;
return $value;
}
/**
* Sanitizar número decimal
*/
public static function decimal($value, int $decimals = 2): float
{
$value = filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
return round((float)$value, $decimals);
}
/**
* Sanitizar boolean
*/
public static function boolean($value): bool
{
if (is_bool($value)) {
return $value;
}
if (is_string($value)) {
$value = strtolower(trim($value));
// TRUE values
$trueValues = [
'1', 'true', 'yes', 'y', 'sim', 's', 'ok', 'on', 'active', 'enabled', 'enable',
'verdadeiro', 'v', 'positivo', 'checked'
];
return in_array($value, $trueValues, true);
}
if (is_numeric($value)) {
return $value > 0;
}
return false;
}
/**
* Sanitizar URL
*/
public static function url(?string $value): string
{
if ($value === null) return '';
$value = trim($value);
$value = filter_var($value, FILTER_SANITIZE_URL);
return $value ?: '';
}
/**
* Sanitizar telefone
*/
public static function phone(?string $value): string
{
if ($value === null) return '';
$value = trim($value);
$value = preg_replace('/[^0-9+\-\(\)\s]/', '', $value);
return substr($value, 0, 20);
}
/**
* Sanitizar data
*/
public static function date(?string $value): string
{
if ($value === null || empty(trim($value))) {
return date('Y-m-d');
}
$value = trim($value);
// Validar formato Y-m-d
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
// Verificar se a data é válida
if (checkdate((int)substr($value, 5, 2), (int)substr($value, 8, 2), (int)substr($value, 0, 4))) {
return $value;
}
}
// Tentar converter outros formatos
$timestamp = strtotime($value);
if ($timestamp !== false && $timestamp > 0) {
$formatted = date('Y-m-d', $timestamp);
// Verificar se a data resultante é válida
if ($formatted !== '1970-01-01') {
return $formatted;
}
}
// Se tudo falhar, retornar data atual
return date('Y-m-d');
}
/**
* Sanitizar array
*/
public static function array(array $data, ?callable $sanitizer = null): array
{
if ($sanitizer === null) {
$sanitizer = [self::class, 'string'];
}
return array_map($sanitizer, $data);
}
/**
* Sanitizar para SQL (escapar)
*/
public static function sql(string $value): string
{
return addslashes($value);
}
/**
* Sanitizar para HTML (escapar)
*/
public static function html(string $value): string
{
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
/**
* Sanitizar para JavaScript (escapar)
*/
public static function js(string $value): string
{
return json_encode($value, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
}
/**
* Sanitizar para atributos HTML
*/
public static function attribute(string $value): string
{
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
/**
* Sanitizar para CSS
*/
public static function css(string $value): string
{
return preg_replace('/[^a-zA-Z0-9\s\-_\.#]/', '', $value);
}
/**
* Sanitizar nome de ficheiro
*/
public static function filename(string $value): string
{
$value = preg_replace('/[^a-zA-Z0-9\-_\.]/', '', $value);
return substr($value, 0, 255);
}
/**
* Sanitizar slug/URL amigável
*/
public static function slug(string $value): string
{
$value = strtolower($value);
$value = preg_replace('/[^a-z0-9\-]/', '-', $value);
$value = preg_replace('/-+/', '-', $value);
$value = trim($value, '-');
return substr($value, 0, 100);
}
}