<?php

declare(strict_types=1);

class Sanitizer
{
    public static function html(string $text): string
    {
        return htmlspecialchars($text, ENT_QUOTES | ENT_HTML5, 'UTF-8');
    }

    public static function text(string $text): string
    {
        return trim(strip_tags($text));
    }

    

    public static function decodeEntities(?string $text): ?string
    {
        if ($text === null || $text === '') {
            return $text;
        }
        for ($i = 0; $i < 3; $i++) {
            $decoded = html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8');
            if ($decoded === $text) {
                break;
            }
            $text = $decoded;
        }
        return $text;
    }

    public static function email(string $email): string
    {
        $filtered = filter_var(trim(strtolower($email)), FILTER_SANITIZE_EMAIL);
        return $filtered !== false ? $filtered : '';
    }

    public static function int(mixed $value): int
    {
        return (int) $value;
    }

    public static function url(string $url): string
    {
        $url = trim($url);
        $filtered = filter_var($url, FILTER_SANITIZE_URL);
        if ($filtered === false) {
            return '';
        }
        if ($filtered !== '' && !preg_match('#^https?://#i', $filtered)) {
            return '';
        }
        return $filtered;
    }

    public static function embedCode(string $code): string
    {
        $code = trim($code);
        if ($code === '') {
            return '';
        }

        if (preg_match('/<iframe\s[^>]*src\s*=\s*["\']([^"\']+)["\'][^>]*><\/iframe>/i', $code, $matches)) {
            $src = trim($matches[1]);
             
            if (!preg_match('#^https?://#i', $src)) {
                return '';
            }
            $src = htmlspecialchars($src, ENT_QUOTES | ENT_HTML5, 'UTF-8');
            $allowfullscreen = preg_match('/allowfullscreen/i', $code) ? ' allowfullscreen' : '';
            return '<iframe src="' . $src . '" width="100%" height="100%" frameborder="0"' . $allowfullscreen . '></iframe>';
        }

        return '';
    }

    public static function richHtml(string $html): string
    {
        $allowed = '<p><br><strong><b><em><i><u><s><h1><h2><h3><h4><h5><h6>'
            . '<ul><ol><li><a><img><table><thead><tbody><tr><th><td>'
            . '<blockquote><pre><code><div><span><hr><figure><figcaption>'
            . '<sub><sup><small>';
        $html = strip_tags($html, $allowed);

         
        $html = preg_replace('/\s+on\w+\s*=\s*"[^"]*"/i', '', $html);
        $html = preg_replace('/\s+on\w+\s*=\s*\'[^\']*\'/i', '', $html);
        $html = preg_replace('/\s+on\w+\s*=\s*[^\s>]*/i', '', $html);

         
        $html = preg_replace('/href\s*=\s*["\']?\s*(javascript|data|vbscript)\s*:[^"\'>\s]*/i', 'href="#"', $html);
        $html = preg_replace('/src\s*=\s*["\']?\s*(javascript|data|vbscript)\s*:[^"\'>\s]*/i', 'src=""', $html);

         
        $html = preg_replace('/\s+style\s*=\s*"[^"]*expression\s*\([^"]*"/i', '', $html);
        $html = preg_replace('/\s+style\s*=\s*\'[^\']*expression\s*\([^\']*\'/i', '', $html);

        return $html;
    }
}
