Files
MoneyFlix/crm/Core/OutputSanitize.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

191 lines
4.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Core;
/**
* Classe utilitária para sanitização de output
* Centraliza todas as funções de escape e formatação
*/
class OutputSanitize
{
/**
* Escapar HTML para prevenir XSS
*/
public static function html(string $value): string
{
return htmlspecialchars($value, ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
/**
* Escapar atributos HTML
*/
public static function attr(string $value): string
{
return htmlspecialchars($value, ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
/**
* Escapar para JavaScript
*/
public static function js(string $value): string
{
return json_encode($value, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
}
/**
* Escapar para CSS
*/
public static function css(string $value): string
{
return preg_replace('/[^a-zA-Z0-9\-_]/', '', $value);
}
/**
* Escapar para URL
*/
public static function url(string $value): string
{
return urlencode($value);
}
/**
* Formatar data para exibição
*/
public static function date(string $date, string $format = 'd/m/Y'): string
{
if (empty($date)) {
return '';
}
try {
$dateObj = new \DateTime($date);
return $dateObj->format($format);
} catch (\Exception $e) {
return '';
}
}
/**
* Formatar data e hora para exibição
*/
public static function datetime(string $datetime, string $format = 'd/m/Y H:i'): string
{
if (empty($datetime)) {
return '';
}
try {
$dateObj = new \DateTime($datetime);
return $dateObj->format($format);
} catch (\Exception $e) {
return '';
}
}
/**
* Formatar número para exibição
*/
public static function number(float|int $number, int $decimals = 2): string
{
return number_format($number, $decimals, ',', '.');
}
/**
* Formatar moeda para exibição
*/
public static function currency(float|int $amount, string $currency = '€'): string
{
return $currency . ' ' . self::number($amount, 2);
}
/**
* Truncar texto com ellipsis
*/
public static function truncate(string $text, int $length = 100, string $ellipsis = '...'): string
{
if (mb_strlen($text) <= $length) {
return $text;
}
return mb_substr($text, 0, $length) . $ellipsis;
}
/**
* Limpar string para uso em IDs/classes CSS
*/
public static function cssId(string $value): string
{
return preg_replace('/[^a-zA-Z0-9\-_]/', '-', strtolower($value));
}
/**
* Escapar para uso em atributos data-*
*/
public static function dataAttr(string $value): string
{
return self::html($value);
}
/**
* Verificar se valor é vazio e retornar fallback
*/
public static function fallback(string $value, string $fallback = ''): string
{
return empty(trim($value)) ? $fallback : $value;
}
/**
* Formatar bytes para exibição humana
*/
public static function bytes(int $bytes): string
{
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) {
$bytes /= 1024;
}
return round($bytes, 2) . ' ' . $units[$i];
}
/**
* Formatar percentagem
*/
public static function percentage(float $value, int $decimals = 1): string
{
return number_format($value, $decimals, ',', '.') . '%';
}
/**
* Formatar tempo decorrido
*/
public static function timeAgo(string $datetime): string
{
if (empty($datetime)) {
return '';
}
try {
$date = new \DateTime($datetime);
$now = new \DateTime();
$diff = $now->diff($date);
if ($diff->y > 0) {
return $diff->y . ' ano' . ($diff->y > 1 ? 's' : '');
} elseif ($diff->m > 0) {
return $diff->m . ' mês' . ($diff->m > 1 ? 'es' : '');
} elseif ($diff->d > 0) {
return $diff->d . ' dia' . ($diff->d > 1 ? 's' : '');
} elseif ($diff->h > 0) {
return $diff->h . ' hora' . ($diff->h > 1 ? 's' : '');
} elseif ($diff->i > 0) {
return $diff->i . ' minuto' . ($diff->i > 1 ? 's' : '');
} else {
return 'agora';
}
} catch (\Exception $e) {
return '';
}
}
}