Contact
/* ══════════════════════════════════════════════════════════════════════ SECURITATE FORMULARE — CSRF, Honeypot, Rate Limiting, Sanitizare ══════════════════════════════════════════════════════════════════════ */ /** Pornire sesiune sigură (idempotentă) */ function hga_session_start(): void { if (session_status() === PHP_SESSION_NONE) { ini_set('session.cookie_httponly', '1'); ini_set('session.cookie_samesite', 'Strict'); session_start(); } } /** Generează / returnează token CSRF pentru sesiunea curentă */ function hga_csrf_token(): string { hga_session_start(); if (empty($_SESSION['hga_public_csrf'])) { $_SESSION['hga_public_csrf'] = bin2hex(random_bytes(32)); } return $_SESSION['hga_public_csrf']; } /** Afișează câmpul ascuns CSRF */ function hga_csrf_field(): string { return ''; } /** Verifică token CSRF — returnează false dacă invalid */ function hga_csrf_verify(): bool { hga_session_start(); $token = (string)($_POST['_csrf'] ?? ''); $stored = (string)($_SESSION['hga_public_csrf'] ?? ''); if ($stored === '' || $token === '') return false; return hash_equals($stored, $token); } /** Verifică honeypot — returnează true dacă este bot (câmpul completat) */ function hga_is_bot(): bool { return (string)($_POST['_hp'] ?? '') !== ''; } /** * Rate limiting simplu bazat pe sesiune. * Permite $maxPerWindow cereri în $windowSec secunde per $key. * Returnează true dacă e permis, false dacă e blocat. */ function hga_rate_limit(string $key, int $maxPerWindow = 5, int $windowSec = 300): bool { hga_session_start(); $now = time(); $rlKey = 'hga_rl_' . $key; if (!isset($_SESSION[$rlKey])) $_SESSION[$rlKey] = ['count' => 0, 'window_start' => $now]; $rl = &$_SESSION[$rlKey]; if ($now - $rl['window_start'] > $windowSec) { $rl = ['count' => 0, 'window_start' => $now]; } if ($rl['count'] >= $maxPerWindow) return false; $rl['count']++; return true; } /** Sanitizare text general pentru formulare publice */ function hga_san(mixed $v, int $maxLen = 1000): string { $v = strip_tags((string)$v); $v = str_replace(['', '
Contact